Search in sources :

Example 21 with UserAccessToken

use of org.sonar.alm.client.github.security.UserAccessToken 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

UserAccessToken (org.sonar.alm.client.github.security.UserAccessToken)21 Test (org.junit.Test)18 AccessToken (org.sonar.alm.client.github.security.AccessToken)11 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)6 AlmPatDto (org.sonar.db.alm.pat.AlmPatDto)4 NotFoundException (org.sonar.server.exceptions.NotFoundException)4 ListGithubOrganizationsWsResponse (org.sonarqube.ws.AlmIntegrations.ListGithubOrganizationsWsResponse)4 IOException (java.io.IOException)3 GithubApplicationClient (org.sonar.alm.client.github.GithubApplicationClient)3 DbSession (org.sonar.db.DbSession)3 Common (org.sonarqube.ws.Common)3 Optional (java.util.Optional)2 Repository (org.sonar.alm.client.github.GithubApplicationClient.Repository)2 ProjectAlmSettingDto (org.sonar.db.alm.setting.ProjectAlmSettingDto)2 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)1 Assertions.tuple (org.assertj.core.api.Assertions.tuple)1