Search in sources :

Example 6 with AlmPatDto

use of org.sonar.db.alm.pat.AlmPatDto in project sonarqube by SonarSource.

the class SetPatActionTest method set_new_bitbucketcloud_pat.

@Test
public void set_new_bitbucketcloud_pat() {
    UserDto user = db.users().insertUser();
    AlmSettingDto almSetting = db.almSettings().insertBitbucketCloudAlmSetting();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
    String pat = "12345678987654321";
    String username = "test-user";
    ws.newRequest().setParam("almSetting", almSetting.getKey()).setParam("pat", pat).setParam("username", username).execute();
    Optional<AlmPatDto> actualAlmPat = db.getDbClient().almPatDao().selectByUserAndAlmSetting(db.getSession(), user.getUuid(), almSetting);
    assertThat(actualAlmPat).isPresent();
    assertThat(actualAlmPat.get().getPersonalAccessToken()).isEqualTo(CredentialsEncoderHelper.encodeCredentials(ALM.BITBUCKET_CLOUD, pat, username));
    assertThat(actualAlmPat.get().getUserUuid()).isEqualTo(user.getUuid());
    assertThat(actualAlmPat.get().getAlmSettingUuid()).isEqualTo(almSetting.getUuid());
}
Also used : UserDto(org.sonar.db.user.UserDto) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Test(org.junit.Test)

Example 7 with AlmPatDto

use of org.sonar.db.alm.pat.AlmPatDto in project sonarqube by SonarSource.

the class SetPatActionTest method set_new_azuredevops_pat.

@Test
public void set_new_azuredevops_pat() {
    UserDto user = db.users().insertUser();
    AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
    ws.newRequest().setParam("almSetting", almSetting.getKey()).setParam("pat", "12345678987654321").execute();
    Optional<AlmPatDto> actualAlmPat = db.getDbClient().almPatDao().selectByUserAndAlmSetting(db.getSession(), user.getUuid(), almSetting);
    assertThat(actualAlmPat).isPresent();
    assertThat(actualAlmPat.get().getPersonalAccessToken()).isEqualTo("12345678987654321");
    assertThat(actualAlmPat.get().getUserUuid()).isEqualTo(user.getUuid());
    assertThat(actualAlmPat.get().getAlmSettingUuid()).isEqualTo(almSetting.getUuid());
}
Also used : UserDto(org.sonar.db.user.UserDto) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Test(org.junit.Test)

Example 8 with AlmPatDto

use of org.sonar.db.alm.pat.AlmPatDto in project sonarqube by SonarSource.

the class SetPatActionTest method set_new_gitlab_pat.

@Test
public void set_new_gitlab_pat() {
    UserDto user = db.users().insertUser();
    AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
    ws.newRequest().setParam("almSetting", almSetting.getKey()).setParam("pat", "12345678987654321").execute();
    Optional<AlmPatDto> actualAlmPat = db.getDbClient().almPatDao().selectByUserAndAlmSetting(db.getSession(), user.getUuid(), almSetting);
    assertThat(actualAlmPat).isPresent();
    assertThat(actualAlmPat.get().getPersonalAccessToken()).isEqualTo("12345678987654321");
    assertThat(actualAlmPat.get().getUserUuid()).isEqualTo(user.getUuid());
    assertThat(actualAlmPat.get().getAlmSettingUuid()).isEqualTo(almSetting.getUuid());
}
Also used : UserDto(org.sonar.db.user.UserDto) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Test(org.junit.Test)

Example 9 with AlmPatDto

use of org.sonar.db.alm.pat.AlmPatDto in project sonarqube by SonarSource.

the class CheckPatActionTest method fail_check_pat_alm_setting_not_found.

@Test
public void fail_check_pat_alm_setting_not_found() {
    UserDto user = db.users().insertUser();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
    AlmPatDto almPatDto = newAlmPatDto();
    db.getDbClient().almPatDao().insert(db.getSession(), almPatDto, user.getLogin(), null);
    TestRequest request = ws.newRequest().setParam("almSetting", "testKey");
    assertThatThrownBy(request::execute).isInstanceOf(NotFoundException.class).hasMessage("ALM Setting 'testKey' not found");
}
Also used : UserDto(org.sonar.db.user.UserDto) AlmPatsTesting.newAlmPatDto(org.sonar.db.alm.integration.pat.AlmPatsTesting.newAlmPatDto) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test)

Example 10 with AlmPatDto

use of org.sonar.db.alm.pat.AlmPatDto in project sonarqube by SonarSource.

the class ListGithubOrganizationsActionTest method return_organizations_using_existing_personal_access_token.

@Test
public void return_organizations_using_existing_personal_access_token() {
    AlmSettingDto githubAlmSettings = setupAlm();
    AlmPatDto pat = db.almPats().insert(p -> p.setAlmSettingUuid(githubAlmSettings.getUuid()).setUserUuid(userSession.getUuid()));
    setupGhOrganizations(githubAlmSettings, pat.getPersonalAccessToken());
    ListGithubOrganizationsWsResponse response = ws.newRequest().setParam(PARAM_ALM_SETTING, githubAlmSettings.getKey()).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, never()).createUserAccessToken(any(), any(), any(), any());
    verify(appClient).listOrganizations(eq(githubAlmSettings.getUrl()), argThat(token -> token.getValue().equals(pat.getPersonalAccessToken())), eq(1), eq(100));
    Mockito.verifyNoMoreInteractions(appClient);
    assertThat(db.getDbClient().almPatDao().selectByUserAndAlmSetting(db.getSession(), userSession.getUuid(), githubAlmSettings).get().getPersonalAccessToken()).isEqualTo(pat.getPersonalAccessToken());
}
Also used : AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) GithubOrganization(org.sonarqube.ws.AlmIntegrations.GithubOrganization) Settings(org.sonar.api.config.internal.Settings) BeforeClass(org.junit.BeforeClass) UserDto(org.sonar.db.user.UserDto) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) GlobalPermission(org.sonar.db.permission.GlobalPermission) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GithubApplicationClient(org.sonar.alm.client.github.GithubApplicationClient) GithubApplicationClientImpl(org.sonar.alm.client.github.GithubApplicationClientImpl) PARAM_TOKEN(org.sonar.server.almintegration.ws.github.ListGithubOrganizationsAction.PARAM_TOKEN) UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) BadRequestException(org.sonar.server.exceptions.BadRequestException) PARAM_ALM_SETTING(org.sonar.server.almintegration.ws.github.ListGithubOrganizationsAction.PARAM_ALM_SETTING) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ListGithubOrganizationsWsResponse(org.sonarqube.ws.AlmIntegrations.ListGithubOrganizationsWsResponse) UserSessionRule.standalone(org.sonar.server.tester.UserSessionRule.standalone) Encryption(org.sonar.api.config.internal.Encryption) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) System2(org.sonar.api.utils.System2) Assertions.tuple(org.assertj.core.api.Assertions.tuple) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) WsActionTester(org.sonar.server.ws.WsActionTester) Collectors(java.util.stream.Collectors) NotFoundException(org.sonar.server.exceptions.NotFoundException) Common(org.sonarqube.ws.Common) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) Stream(java.util.stream.Stream) Rule(org.junit.Rule) UnauthorizedException(org.sonar.server.exceptions.UnauthorizedException) Mockito.mock(org.mockito.Mockito.mock) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ListGithubOrganizationsWsResponse(org.sonarqube.ws.AlmIntegrations.ListGithubOrganizationsWsResponse) Common(org.sonarqube.ws.Common) Test(org.junit.Test)

Aggregations

AlmPatDto (org.sonar.db.alm.pat.AlmPatDto)34 NotFoundException (org.sonar.server.exceptions.NotFoundException)24 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)23 Test (org.junit.Test)19 UserDto (org.sonar.db.user.UserDto)19 DbSession (org.sonar.db.DbSession)14 TestRequest (org.sonar.server.ws.TestRequest)11 AlmPatsTesting.newAlmPatDto (org.sonar.db.alm.integration.pat.AlmPatsTesting.newAlmPatDto)10 ProjectAlmSettingDto (org.sonar.db.alm.setting.ProjectAlmSettingDto)7 Collectors (java.util.stream.Collectors)5 GithubApplicationClient (org.sonar.alm.client.github.GithubApplicationClient)5 UserAccessToken (org.sonar.alm.client.github.security.UserAccessToken)5 List (java.util.List)4 Map (java.util.Map)4 Objects.requireNonNull (java.util.Objects.requireNonNull)4 Optional (java.util.Optional)4 Set (java.util.Set)4 BinaryOperator (java.util.function.BinaryOperator)4 Collectors.toList (java.util.stream.Collectors.toList)4 Collectors.toSet (java.util.stream.Collectors.toSet)4