@Override public Optional<Path> stageCredentials(String googleUserName) { if (Services.getLoginService().ensureLoggedIn(googleUserName)) { return doStageCredentials(googleUserName); } return Optional.empty(); }
/** Mocks list of currently signed in users returned by login service. First user is active. */ private void mockUserList(List<CredentialedUser> userList) { Map<String, CredentialedUser> emailUserMap = userList .stream() .collect(Collectors.toMap(CredentialedUser::getEmail, Function.identity())); when(googleLoginService.getAllUsers()).thenReturn(emailUserMap); when(googleLoginService.getActiveUser()).thenReturn(userList.get(0)); for (CredentialedUser user : userList) { when(googleLoginService.ensureLoggedIn(user.getEmail())).thenReturn(true); when(googleLoginService.getLoggedInUser(user.getEmail())).thenReturn(Optional.of(user)); } }
@Test public void testStageCredentials_withoutValidCreds() throws Exception { String username = "jones@gmail.com"; when(googleLoginService.ensureLoggedIn(username)).thenReturn(false); assertFalse(helper.stageCredentials(username).isPresent()); }
@Test public void testStageCredentials_withValidCreds() throws Exception { String username = "jones@gmail.com"; String clientId = "clientId"; String clientSecret = "clientSecret"; String refreshToken = "refreshToken"; when(googleLoginService.ensureLoggedIn(username)).thenReturn(true); when(googleLoginService.getLoggedInUser(username)).thenReturn(Optional.of(credentialedUser)); when(credentialedUser.getGoogleLoginState()).thenReturn(loginState); when(loginState.fetchOAuth2ClientId()).thenReturn(clientId); when(loginState.fetchOAuth2ClientSecret()).thenReturn(clientSecret); when(loginState.fetchOAuth2RefreshToken()).thenReturn(refreshToken); helper.stageCredentials(username); Path credentialFile = helper.getCredentialsPath(); Reader credentialFileReader = Files.newBufferedReader(credentialFile, Charset.defaultCharset()); Map jsonMap = new Gson().fromJson(credentialFileReader, Map.class); credentialFileReader.close(); assertEquals(clientId, jsonMap.get("client_id")); assertEquals(clientSecret, jsonMap.get("client_secret")); assertEquals(refreshToken, jsonMap.get("refresh_token")); assertEquals("authorized_user", jsonMap.get("type")); Files.delete(credentialFile); }