Search in sources :

Example 51 with AlmSettingDto

use of org.sonar.db.alm.setting.AlmSettingDto in project sonarqube by SonarSource.

the class ListGithubOrganizationsActionTest method fail_when_unable_to_create_personal_access_token.

@Test
public void fail_when_unable_to_create_personal_access_token() {
    AlmSettingDto githubAlmSetting = setupAlm();
    when(appClient.createUserAccessToken(githubAlmSetting.getUrl(), githubAlmSetting.getClientId(), githubAlmSetting.getDecryptedClientSecret(encryption), "abc")).thenThrow(IllegalStateException.class);
    TestRequest request = ws.newRequest().setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey()).setParam(PARAM_TOKEN, "abc");
    assertThatThrownBy(request::execute).isInstanceOf(IllegalStateException.class).hasMessage(null);
}
Also used : AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test)

Example 52 with AlmSettingDto

use of org.sonar.db.alm.setting.AlmSettingDto in project sonarqube by SonarSource.

the class ListGithubOrganizationsActionTest method fail_create_personal_access_token_because_of_invalid_settings.

@Test
public void fail_create_personal_access_token_because_of_invalid_settings() {
    AlmSettingDto githubAlmSetting = setupAlm();
    when(appClient.createUserAccessToken(githubAlmSetting.getUrl(), githubAlmSetting.getClientId(), githubAlmSetting.getDecryptedClientSecret(encryption), "abc")).thenThrow(IllegalArgumentException.class);
    TestRequest request = ws.newRequest().setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey()).setParam(PARAM_TOKEN, "abc");
    assertThatThrownBy(request::execute).isInstanceOf(BadRequestException.class).hasMessage("Unable to authenticate with GitHub. Check the GitHub App client ID and client secret configured in the Global Settings and try again.");
}
Also used : BadRequestException(org.sonar.server.exceptions.BadRequestException) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test)

Example 53 with AlmSettingDto

use of org.sonar.db.alm.setting.AlmSettingDto 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)

Example 54 with AlmSettingDto

use of org.sonar.db.alm.setting.AlmSettingDto in project sonarqube by SonarSource.

the class ListGithubOrganizationsActionTest method return_organizations_overriding_existing_personal_access_token.

@Test
public void return_organizations_overriding_existing_personal_access_token() {
    AlmSettingDto githubAlmSettings = setupAlm();
    // old pat
    AlmPatDto pat = db.almPats().insert(p -> p.setAlmSettingUuid(githubAlmSettings.getUuid()).setUserUuid(userSession.getUuid()));
    // new pat
    UserAccessToken accessToken = new UserAccessToken("token_for_abc");
    when(appClient.createUserAccessToken(githubAlmSettings.getUrl(), githubAlmSettings.getClientId(), githubAlmSettings.getDecryptedClientSecret(encryption), "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(), githubAlmSettings.getDecryptedClientSecret(encryption), "abc");
    verify(appClient).listOrganizations(eq(githubAlmSettings.getUrl()), argThat(token -> token.getValue().equals(accessToken.getValue())), eq(1), eq(100));
    Mockito.verifyNoMoreInteractions(appClient);
    assertThat(db.getDbClient().almPatDao().selectByUserAndAlmSetting(db.getSession(), userSession.getUuid(), githubAlmSettings).get().getPersonalAccessToken()).isEqualTo(accessToken.getValue());
}
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) UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) 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)

Example 55 with AlmSettingDto

use of org.sonar.db.alm.setting.AlmSettingDto in project sonarqube by SonarSource.

the class ImportGitLabProjectActionTest method import_project_with_specific_different_default_branch.

@Test
public void import_project_with_specific_different_default_branch() {
    UserDto user = db.users().insertUser();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
    AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
    db.almPats().insert(dto -> {
        dto.setAlmSettingUuid(almSetting.getUuid());
        dto.setUserUuid(user.getUuid());
        dto.setPersonalAccessToken("PAT");
    });
    Project project = getGitlabProject();
    when(gitlabHttpClient.getProject(any(), any(), any())).thenReturn(project);
    when(gitlabHttpClient.getBranches(any(), any(), any())).thenReturn(singletonList(new GitLabBranch("main", true)));
    when(uuidFactory.create()).thenReturn("uuid");
    Projects.CreateWsResponse response = ws.newRequest().setParam("almSetting", almSetting.getKey()).setParam("gitlabProjectId", "12345").executeProtobuf(Projects.CreateWsResponse.class);
    verify(gitlabHttpClient).getProject(almSetting.getUrl(), "PAT", 12345L);
    verify(gitlabHttpClient).getBranches(almSetting.getUrl(), "PAT", 12345L);
    Projects.CreateWsResponse.Project result = response.getProject();
    assertThat(result.getKey()).isEqualTo(project.getPathWithNamespace() + "_uuid");
    assertThat(result.getName()).isEqualTo(project.getName());
    Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
    assertThat(projectDto).isPresent();
    assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
    Assertions.assertThat(db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get())).extracting(BranchDto::getKey, BranchDto::isMain).containsExactlyInAnyOrder(tuple("main", true));
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) Project(org.sonar.alm.client.gitlab.Project) UserDto(org.sonar.db.user.UserDto) Projects(org.sonarqube.ws.Projects) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) GitLabBranch(org.sonar.alm.client.gitlab.GitLabBranch) Test(org.junit.Test)

Aggregations

AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)217 Test (org.junit.Test)175 UserDto (org.sonar.db.user.UserDto)113 TestRequest (org.sonar.server.ws.TestRequest)48 ProjectDto (org.sonar.db.project.ProjectDto)40 NotFoundException (org.sonar.server.exceptions.NotFoundException)37 DbSession (org.sonar.db.DbSession)36 WebService (org.sonar.api.server.ws.WebService)31 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)28 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)28 Rule (org.junit.Rule)28 Mockito.mock (org.mockito.Mockito.mock)28 DbTester (org.sonar.db.DbTester)28 UserSessionRule (org.sonar.server.tester.UserSessionRule)28 WsActionTester (org.sonar.server.ws.WsActionTester)28 Encryption (org.sonar.api.config.internal.Encryption)27 ForbiddenException (org.sonar.server.exceptions.ForbiddenException)27 ComponentFinder (org.sonar.server.component.ComponentFinder)26 Tuple.tuple (org.assertj.core.groups.Tuple.tuple)25 ProjectAlmSettingDto (org.sonar.db.alm.setting.ProjectAlmSettingDto)25