Search in sources :

Example 6 with ListGithubOrganizationsWsResponse

use of org.sonarqube.ws.AlmIntegrations.ListGithubOrganizationsWsResponse in project sonarqube by SonarSource.

the class ListGithubOrganizationsActionTest method return_organizations_and_store_personal_access_token_with_encrypted_client_secret.

@Test
public void return_organizations_and_store_personal_access_token_with_encrypted_client_secret() {
    String decryptedSecret = "decrypted-secret";
    UserAccessToken accessToken = new UserAccessToken("token_for_abc");
    AlmSettingDto githubAlmSettings = setupAlm();
    when(encryption.isEncrypted(any())).thenReturn(true);
    when(encryption.decrypt(any())).thenReturn(decryptedSecret);
    when(appClient.createUserAccessToken(githubAlmSettings.getUrl(), githubAlmSettings.getClientId(), decryptedSecret, "abc")).thenReturn(accessToken);
    setupGhOrganizations(githubAlmSettings, accessToken.getValue());
    ListGithubOrganizationsWsResponse response = ws.newRequest().setParam(PARAM_ALM_SETTING, githubAlmSettings.getKey()).setParam(PARAM_TOKEN, "abc").executeProtobuf(ListGithubOrganizationsWsResponse.class);
    assertThat(response.getPaging()).extracting(Common.Paging::getPageIndex, Common.Paging::getPageSize, Common.Paging::getTotal).containsOnly(1, 100, 2);
    assertThat(response.getOrganizationsList()).extracting(GithubOrganization::getKey, GithubOrganization::getName).containsOnly(tuple("github", "github"), tuple("octacat", "octacat"));
    verify(appClient).createUserAccessToken(githubAlmSettings.getUrl(), githubAlmSettings.getClientId(), decryptedSecret, "abc");
    verify(appClient).listOrganizations(githubAlmSettings.getUrl(), accessToken, 1, 100);
    Mockito.verifyNoMoreInteractions(appClient);
    assertThat(db.getDbClient().almPatDao().selectByUserAndAlmSetting(db.getSession(), userSession.getUuid(), githubAlmSettings).get().getPersonalAccessToken()).isEqualTo(accessToken.getValue());
}
Also used : UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ListGithubOrganizationsWsResponse(org.sonarqube.ws.AlmIntegrations.ListGithubOrganizationsWsResponse) Common(org.sonarqube.ws.Common) Test(org.junit.Test)

Aggregations

ListGithubOrganizationsWsResponse (org.sonarqube.ws.AlmIntegrations.ListGithubOrganizationsWsResponse)6 UserAccessToken (org.sonar.alm.client.github.security.UserAccessToken)5 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)5 Test (org.junit.Test)4 Common (org.sonarqube.ws.Common)4 GithubApplicationClient (org.sonar.alm.client.github.GithubApplicationClient)3 AlmPatDto (org.sonar.db.alm.pat.AlmPatDto)3 NotFoundException (org.sonar.server.exceptions.NotFoundException)3 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)2 Assertions.tuple (org.assertj.core.api.Assertions.tuple)2 BeforeClass (org.junit.BeforeClass)2 Rule (org.junit.Rule)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)2 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)2 Mockito (org.mockito.Mockito)2 Mockito.mock (org.mockito.Mockito.mock)2