Search in sources :

Example 1 with SCAN

use of org.sonar.db.permission.GlobalPermission.SCAN in project sonarqube by SonarSource.

the class UserPermissionDaoTest method deleteByUserId.

@Test
public void deleteByUserId() {
    UserDto user1 = insertUser();
    UserDto user2 = insertUser();
    ComponentDto project = db.components().insertPrivateProject();
    db.users().insertPermissionOnUser(user1, SCAN);
    db.users().insertPermissionOnUser(user1, ADMINISTER);
    db.users().insertProjectPermissionOnUser(user1, ADMINISTER_QUALITY_GATES.getKey(), project);
    db.users().insertPermissionOnUser(user2, SCAN);
    db.users().insertProjectPermissionOnUser(user2, ADMINISTER_QUALITY_GATES.getKey(), project);
    underTest.deleteByUserUuid(dbSession, user1);
    dbSession.commit();
    assertThat(db.select("select user_uuid as \"userUuid\", component_uuid as \"projectUuid\", role as \"permission\" from user_roles")).extracting((row) -> row.get("userUuid"), (row) -> row.get("projectUuid"), (row) -> row.get("permission")).containsOnly(tuple(user2.getUuid(), null, SCAN.getKey()), tuple(user2.getUuid(), project.uuid(), ADMINISTER_QUALITY_GATES.getKey()));
}
Also used : IntStream(java.util.stream.IntStream) ISSUE_ADMIN(org.sonar.api.web.UserRole.ISSUE_ADMIN) QUALITY_PROFILE_ADMIN(org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN) Arrays(java.util.Arrays) UserDto(org.sonar.db.user.UserDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PROVISION_PROJECTS(org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS) Random(java.util.Random) ADMINISTER(org.sonar.db.permission.GlobalPermission.ADMINISTER) DbSession(org.sonar.db.DbSession) DEFAULT_PAGE_SIZE(org.sonar.db.permission.PermissionQuery.DEFAULT_PAGE_SIZE) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) ADMIN(org.sonar.api.web.UserRole.ADMIN) Uuids(org.sonar.core.util.Uuids) SCAN(org.sonar.db.permission.GlobalPermission.SCAN) Arrays.asList(java.util.Arrays.asList) SYSTEM_ADMIN(org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN) PROVISIONING(org.sonar.core.permission.GlobalPermissions.PROVISIONING) ADMINISTER_QUALITY_GATES(org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES) DbTester(org.sonar.db.DbTester) Tuple(org.assertj.core.groups.Tuple) System2(org.sonar.api.utils.System2) Assertions.tuple(org.assertj.core.api.Assertions.tuple) USER(org.sonar.api.web.UserRole.USER) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Test(org.junit.Test) NoOpAuditPersister(org.sonar.db.audit.NoOpAuditPersister) Consumer(java.util.function.Consumer) ComponentTesting.newPrivateProjectDto(org.sonar.db.component.ComponentTesting.newPrivateProjectDto) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) Arrays.stream(java.util.Arrays.stream) UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 2 with SCAN

use of org.sonar.db.permission.GlobalPermission.SCAN in project sonarqube by SonarSource.

the class BranchReportSubmitterTest method submit_report_on_missing_branch_of_missing_project_provisions_project_when_PROVISION_PROJECT_perm.

@Test
public void submit_report_on_missing_branch_of_missing_project_provisions_project_when_PROVISION_PROJECT_perm() {
    ComponentDto nonExistingProject = newPrivateProjectDto();
    UserDto user = db.users().insertUser();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS).addPermission(SCAN);
    Map<String, String> randomCharacteristics = randomNonEmptyMap();
    ComponentDto createdBranch = createButDoNotInsertBranch(nonExistingProject);
    BranchSupport.ComponentKey componentKey = createComponentKeyOfBranch(createdBranch);
    when(branchSupportDelegate.createComponentKey(nonExistingProject.getDbKey(), randomCharacteristics)).thenReturn(componentKey);
    when(componentUpdater.createWithoutCommit(any(), any(), eq(user.getUuid()), eq(user.getLogin()), any())).thenAnswer((Answer<ComponentDto>) invocation -> db.components().insertPrivateProject(nonExistingProject));
    when(branchSupportDelegate.createBranchComponent(any(DbSession.class), same(componentKey), eq(nonExistingProject), any())).thenReturn(createdBranch);
    when(permissionTemplateService.wouldUserHaveScanPermissionWithDefaultTemplate(any(DbSession.class), any(), eq(nonExistingProject.getKey()))).thenReturn(true);
    String taskUuid = mockSuccessfulPrepareSubmitCall();
    InputStream reportInput = IOUtils.toInputStream("{binary}", StandardCharsets.UTF_8);
    underTest.submit(nonExistingProject.getDbKey(), nonExistingProject.name(), randomCharacteristics, reportInput);
    BranchDto exitingProjectMainBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), nonExistingProject.uuid()).get();
    verify(branchSupport).createBranchComponent(any(DbSession.class), same(componentKey), eq(nonExistingProject), eq(exitingProjectMainBranch));
    verify(branchSupportDelegate).createComponentKey(nonExistingProject.getDbKey(), randomCharacteristics);
    verify(branchSupportDelegate).createBranchComponent(any(DbSession.class), same(componentKey), eq(nonExistingProject), eq(exitingProjectMainBranch));
    verifyNoMoreInteractions(branchSupportDelegate);
    verifyQueueSubmit(nonExistingProject, createdBranch, user, randomCharacteristics, taskUuid);
    verify(componentUpdater).commitAndIndex(any(DbSession.class), eq(nonExistingProject));
}
Also used : CeTaskTypes(org.sonar.db.ce.CeTaskTypes) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) DbSession(org.sonar.db.DbSession) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) SCAN(org.sonar.db.permission.GlobalPermission.SCAN) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Map(java.util.Map) Visibility(org.sonar.server.project.Visibility) ComponentTesting(org.sonar.db.component.ComponentTesting) Assert.fail(org.junit.Assert.fail) DbTester(org.sonar.db.DbTester) ImmutableMap(com.google.common.collect.ImmutableMap) System2(org.sonar.api.utils.System2) StandardCharsets(java.nio.charset.StandardCharsets) PermissionTemplateService(org.sonar.server.permission.PermissionTemplateService) IOUtils(org.apache.commons.io.IOUtils) ComponentDto(org.sonar.db.component.ComponentDto) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) BranchDto(org.sonar.db.component.BranchDto) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SCAN_EXECUTION(org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION) ComponentTesting.newBranchDto(org.sonar.db.component.ComponentTesting.newBranchDto) FavoriteUpdater(org.sonar.server.favorite.FavoriteUpdater) UserDto(org.sonar.db.user.UserDto) RunWith(org.junit.runner.RunWith) PROVISION_PROJECTS(org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS) Mockito.spy(org.mockito.Mockito.spy) ProjectDefaultVisibility(org.sonar.server.project.ProjectDefaultVisibility) Answer(org.mockito.stubbing.Answer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Before(org.junit.Before) ComponentUpdater(org.sonar.server.component.ComponentUpdater) UserSessionRule(org.sonar.server.tester.UserSessionRule) Collections.emptyMap(java.util.Collections.emptyMap) BranchType(org.sonar.db.component.BranchType) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) CeQueueImpl(org.sonar.ce.queue.CeQueueImpl) Mockito.verify(org.mockito.Mockito.verify) ComponentTesting.newPrivateProjectDto(org.sonar.db.component.ComponentTesting.newPrivateProjectDto) Rule(org.junit.Rule) CeTaskSubmit(org.sonar.ce.queue.CeTaskSubmit) InputStream(java.io.InputStream) CeQueue(org.sonar.ce.queue.CeQueue) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto) ComponentTesting.newBranchDto(org.sonar.db.component.ComponentTesting.newBranchDto) InputStream(java.io.InputStream) UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Aggregations

Random (java.util.Random)2 IntStream (java.util.stream.IntStream)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Rule (org.junit.Rule)2 Test (org.junit.Test)2 System2 (org.sonar.api.utils.System2)2 DbSession (org.sonar.db.DbSession)2 DbTester (org.sonar.db.DbTester)2 ComponentDto (org.sonar.db.component.ComponentDto)2 ComponentTesting.newPrivateProjectDto (org.sonar.db.component.ComponentTesting.newPrivateProjectDto)2 PROVISION_PROJECTS (org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS)2 SCAN (org.sonar.db.permission.GlobalPermission.SCAN)2 UserDto (org.sonar.db.user.UserDto)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 DataProviderRunner (com.tngtech.java.junit.dataprovider.DataProviderRunner)1 InputStream (java.io.InputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Arrays.asList (java.util.Arrays.asList)1