@Before public void setUp() { IntegratedGoogleLoginService mockLogin = Mockito.mock(IntegratedGoogleLoginService.class); registerService(IntegratedGoogleLoginService.class, mockLogin); registerService(PluginInfoService.class, mockInfoService); LinkedHashMap<String, CredentialedUser> allUsers = new LinkedHashMap<String, CredentialedUser>(); CredentialedUser user = Mockito.mock(CredentialedUser.class); allUsers.put("foo@example.com", user); when(mockLogin.getAllUsers()).thenReturn(allUsers); Credential credential = Mockito.mock(Credential.class); when(user.getCredential()).thenReturn(credential); GoogleLoginState loginState = Mockito.mock(GoogleLoginState.class); when(user.getGoogleLoginState()).thenReturn(loginState); }
private void mockCredentials() throws Exception { IntegratedGoogleLoginService mockLoginService = TestUtils.installMockService(IntegratedGoogleLoginService.class); GoogleLoginState googleLoginState = mock(GoogleLoginState.class); Credential credential = mock(Credential.class); this.user = mock(CredentialedUser.class); LinkedHashMap<String, CredentialedUser> allusers = new LinkedHashMap<String, CredentialedUser>(); when(this.user.getCredential()).thenReturn(credential); when(this.user.getEmail()).thenReturn(USER); when(this.user.getGoogleLoginState()).thenReturn(googleLoginState); when(googleLoginState.fetchAccessToken()).thenReturn(PASSWORD); when(mockLoginService.getAllUsers()).thenReturn(allusers); allusers.put(USER, this.user); }
/** * Updates the text of the panel as follows: if no project is selected, no message is displayed, * if the project represents a valid project, the project details are displayed, if the project * represents an invalid project, an error message is displayed. */ private void refreshApplicationInfoPanel(CloudProject selectedProject) { if (selectedProject == null) { applicationInfoPanel.clearMessage(); return; } Optional<CredentialedUser> user = Services.getLoginService().getLoggedInUser(selectedProject.googleUsername()); if (user.isPresent()) { applicationInfoPanel.refresh(selectedProject.projectId(), user.get().getCredential()); } else { applicationInfoPanel.setMessage( AppEngineMessageBundle.getString("appengine.infopanel.no.region"), true /* isError*/); } }
/** * Fetches the list of {@link Role} for the supplied {@link CloudProject} by querying the Iam API. */ static List<Role> getServiceAccountRoles(CloudProject cloudProject) { Optional<CredentialedUser> user = Services.getLoginService().getLoggedInUser(cloudProject.googleUsername()); if (!user.isPresent()) { LOG.error("Cannot fetch service account roles: logged in user not found."); return ImmutableList.of(); } Iam iam = GoogleApiClientFactory.getInstance().getIamClient(user.get().getCredential()); try { return iam.roles().list().execute().getRoles(); } catch (IOException e) { LOG.warn("Exception occurred attempting to fetch service account roles"); return ImmutableList.of(); } }
@Test public void projectSelector_validProjectSelected_infoPanelShowsProjectDetails() { String projectId = "projectId"; String googleUsername = "some-user-id"; CloudProject project = CloudProject.create(projectId, projectId, googleUsername); when(credentialedUser.getCredential()).thenReturn(credential); when(mockGoogleLoginService.getLoggedInUser(googleUsername)) .thenReturn(Optional.of(credentialedUser)); when(projectSelector.getSelectedProject()).thenReturn(project); deploymentConfiguration.setCloudProjectName(projectId); deploymentConfiguration.setGoogleUsername(googleUsername); configurationPanel.resetEditorFrom(deploymentConfiguration); verify(infoPanel).refresh(projectId, credential); } }
private List<Project> loadUserProjects(CredentialedUser user) throws IOException { CloudResourceManager cloudResourceManagerClient = GoogleApiClientFactory.getInstance().getCloudResourceManagerClient(user.getCredential()); final List<Project> result = new ArrayList<>();
/** * Using the supplied {@link ServiceAccount}, this creates and returns a new {@link * ServiceAccountKey}. */ private static ServiceAccountKey createServiceAccountKey( CredentialedUser user, ServiceAccount serviceAccount) throws IOException { Iam iam = GoogleApiClientFactory.getInstance().getIamClient(user.getCredential()); CreateServiceAccountKeyRequest keyRequest = new CreateServiceAccountKeyRequest(); return iam.projects() .serviceAccounts() .keys() .create(serviceAccount.getName(), keyRequest) .execute(); }
try { final CredentialedUser user = Services.getLoginService().getAllUsers().get(userEmail); final Credential credential = (user != null ? user.getCredential() : null); if (credential != null) { user.getGoogleLoginState()
throws IOException { CloudResourceManager resourceManager = GoogleApiClientFactory.getInstance().getCloudResourceManagerClient(user.getCredential());
/** Creates a new {@link ServiceAccount} for the given {@link CloudProject} using the IAM API. */ private static ServiceAccount createServiceAccount( CredentialedUser user, String name, CloudProject cloudProject) throws IOException { CreateServiceAccountRequest request = new CreateServiceAccountRequest(); ServiceAccount serviceAccount = new ServiceAccount(); serviceAccount.setDisplayName(name); request.setServiceAccount(serviceAccount); request.setAccountId(createServiceAccountId(name)); Iam iam = GoogleApiClientFactory.getInstance().getIamClient(user.getCredential()); return iam.projects() .serviceAccounts() .create( String.format(SERVICE_ACCOUNT_CREATE_REQUEST_PROJECT_FORMAT, cloudProject.projectId()), request) .execute(); }
private static void enableApi( CloudLibrary library, CloudProject cloudProject, CredentialedUser user) throws IOException { UsageTrackerService.getInstance() .trackEvent(GctTracking.CLIENT_LIBRARY_ENABLE_API) .addMetadata(GctTracking.METADATA_LABEL_KEY, library.getName()) .ping(); ServiceManagement serviceManagement = GoogleApiClientFactory.getInstance().getServiceManagementClient(user.getCredential()); serviceManagement .services() .enable( library.getServiceName(), new EnableServiceRequest() .setConsumerId( String.format(SERVICE_REQUEST_PROJECT_FORMAT, cloudProject.projectId()))) .execute(); }
@NotNull public ListReposResponse list(CredentialedUser user, String cloudProject) throws CloudRepositoryServiceException { try { Credential credential = user.getCredential(); HttpRequestInitializer initializer = httpRequest -> { HttpHeaders headers = new HttpHeaders(); httpRequest.setConnectTimeout(LIST_TIMEOUT_MS); httpRequest.setReadTimeout(LIST_TIMEOUT_MS); httpRequest.setHeaders(headers); credential.initialize(httpRequest); }; HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); String userAgent = ServiceManager.getService(PluginInfoService.class).getUserAgent(); Source source = new Source.Builder(httpTransport, JacksonFactory.getDefaultInstance(), initializer) .setRootUrl(CLOUD_SOURCE_API_ROOT_URL) .setServicePath("") // this ends up prefixed to user agent .setApplicationName(userAgent) .build(); return new CustomUrlSourceRequest(source, cloudProject).execute(); } catch (IOException | GeneralSecurityException ex) { throw new CloudRepositoryServiceException(); } }