Search in sources :

Example 66 with UserDto

use of org.sonar.db.user.UserDto in project sonarqube by SonarSource.

the class UserPermissionDaoTest method selectUserUuidsByQuery_is_sorted_by_insensitive_name.

@Test
public void selectUserUuidsByQuery_is_sorted_by_insensitive_name() {
    UserDto user1 = insertUser(u -> u.setName("user1"));
    addGlobalPermission(PROVISIONING, user1);
    UserDto user3 = insertUser(u -> u.setName("user3"));
    addGlobalPermission(SYSTEM_ADMIN, user3);
    UserDto user2 = insertUser(u -> u.setName("User2"));
    addGlobalPermission(PROVISIONING, user2);
    assertThat(underTest.selectUserUuidsByQuery(dbSession, PermissionQuery.builder().build())).containsExactly(user1.getUuid(), user2.getUuid(), user3.getUuid());
}
Also used : UserDto(org.sonar.db.user.UserDto) Test(org.junit.Test)

Example 67 with UserDto

use of org.sonar.db.user.UserDto in project sonarqube by SonarSource.

the class UserPermissionDaoTest method selectUserUuidsByQuery_is_paginated.

@Test
public void selectUserUuidsByQuery_is_paginated() {
    List<String> userUuids = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        String name = "user-" + i;
        UserDto user = insertUser(u -> u.setName(name));
        addGlobalPermission(PROVISIONING, user);
        addGlobalPermission(SYSTEM_ADMIN, user);
        userUuids.add(user.getUuid());
    }
    assertThat(underTest.selectUserUuidsByQuery(dbSession, PermissionQuery.builder().setPageSize(3).setPageIndex(1).build())).containsExactly(userUuids.get(0), userUuids.get(1), userUuids.get(2));
    assertThat(underTest.selectUserUuidsByQuery(dbSession, PermissionQuery.builder().setPageSize(2).setPageIndex(3).build())).containsExactly(userUuids.get(4), userUuids.get(5));
    assertThat(underTest.selectUserUuidsByQuery(dbSession, PermissionQuery.builder().setPageSize(50).setPageIndex(1).build())).hasSize(10);
}
Also used : UserDto(org.sonar.db.user.UserDto) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 68 with UserDto

use of org.sonar.db.user.UserDto in project sonarqube by SonarSource.

the class UserPermissionDaoTest method deleteProjectPermission.

@Test
public void deleteProjectPermission() {
    UserDto user1 = insertUser();
    UserDto user2 = insertUser();
    ComponentDto project1 = db.components().insertPrivateProject();
    ComponentDto project2 = db.components().insertPrivateProject();
    addGlobalPermission("perm", user1);
    addProjectPermission("perm", user1, project1);
    addProjectPermission("perm", user1, project2);
    addProjectPermission("perm", user2, project1);
    // no such provision -> ignore
    underTest.deleteProjectPermission(dbSession, user1, "anotherPerm", project1);
    assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(4);
    underTest.deleteProjectPermission(dbSession, user1, "perm", project1);
    assertThatProjectPermissionDoesNotExist(user1, "perm", project1);
    assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(3);
}
Also used : UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 69 with UserDto

use of org.sonar.db.user.UserDto in project sonarqube by SonarSource.

the class UserPermissionDaoTest method selectUserUuidsByQuery_is_ordering_by_users_having_permissions_first_then_by_name_lowercase_when_high_number_of_users_for_project_permissions.

@Test
public void selectUserUuidsByQuery_is_ordering_by_users_having_permissions_first_then_by_name_lowercase_when_high_number_of_users_for_project_permissions() {
    IntStream.rangeClosed(1, DEFAULT_PAGE_SIZE + 1).forEach(i -> {
        UserDto user = insertUser(u -> u.setLogin("login" + i).setName("" + i));
        // Add global permission to be sure they are excluded
        addGlobalPermission(SYSTEM_ADMIN, user);
    });
    String lastLogin = "login" + (DEFAULT_PAGE_SIZE + 1);
    UserDto lastUser = db.getDbClient().userDao().selectByLogin(dbSession, lastLogin);
    ComponentDto project = db.components().insertPrivateProject();
    db.users().insertProjectPermissionOnUser(lastUser, SCAN.getKey(), project);
    PermissionQuery query = PermissionQuery.builder().setComponent(project).build();
    assertThat(underTest.selectUserUuidsByQueryAndScope(dbSession, query)).hasSize(DEFAULT_PAGE_SIZE).startsWith(lastUser.getUuid());
}
Also used : UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 70 with UserDto

use of org.sonar.db.user.UserDto in project sonarqube by SonarSource.

the class AuthorizationDaoTest method keepAuthorizedLoginsOnProject_return_correct_users_on_branch.

@Test
public void keepAuthorizedLoginsOnProject_return_correct_users_on_branch() {
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto branch = db.components().insertProjectBranch(project, c -> c.setBranchType(BranchType.BRANCH));
    GroupDto userGroup = db.users().insertGroup("USERS");
    GroupDto adminGroup = db.users().insertGroup("ADMIN");
    db.users().insertProjectPermissionOnGroup(userGroup, UserRole.USER, project);
    db.users().insertProjectPermissionOnGroup(adminGroup, UserRole.ADMIN, project);
    // admin with "direct" ADMIN role
    UserDto admin1 = db.users().insertUser();
    db.users().insertProjectPermissionOnUser(admin1, UserRole.ADMIN, project);
    // admin2 with ADMIN role through group
    UserDto admin2 = db.users().insertUser();
    db.users().insertMember(adminGroup, admin2);
    // user1 with "direct" USER role
    UserDto user1 = db.users().insertUser();
    db.users().insertProjectPermissionOnUser(user1, UserRole.USER, project);
    // user2 with USER role through group
    UserDto user2 = db.users().insertUser();
    db.users().insertMember(userGroup, user2);
    // user without role
    UserDto userWithNoRole = db.users().insertUser();
    assertThat(underTest.keepAuthorizedLoginsOnProject(dbSession, newHashSet(userWithNoRole.getLogin()), branch.getKey(), UserRole.USER)).isEmpty();
    assertThat(underTest.keepAuthorizedLoginsOnProject(dbSession, newHashSet(user1.getLogin()), branch.getKey(), UserRole.USER)).containsOnly(user1.getLogin());
    Set<String> allLogins = newHashSet(admin1.getLogin(), admin2.getLogin(), user1.getLogin(), user2.getLogin(), userWithNoRole.getLogin());
    // Admin does not have the USER permission set
    assertThat(underTest.keepAuthorizedLoginsOnProject(dbSession, allLogins, branch.getKey(), UserRole.USER)).containsOnly(user1.getLogin(), user2.getLogin());
    assertThat(underTest.keepAuthorizedLoginsOnProject(dbSession, allLogins, branch.getKey(), UserRole.ADMIN)).containsOnly(admin1.getLogin(), admin2.getLogin());
}
Also used : UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) GroupDto(org.sonar.db.user.GroupDto) Test(org.junit.Test)

Aggregations

UserDto (org.sonar.db.user.UserDto)1318 Test (org.junit.Test)1173 ComponentDto (org.sonar.db.component.ComponentDto)332 UserTesting.newUserDto (org.sonar.db.user.UserTesting.newUserDto)216 GroupDto (org.sonar.db.user.GroupDto)152 IssueDto (org.sonar.db.issue.IssueDto)131 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)108 TestRequest (org.sonar.server.ws.TestRequest)102 NotFoundException (org.sonar.server.exceptions.NotFoundException)84 DbSession (org.sonar.db.DbSession)82 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)80 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)64 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)57 Rule (org.junit.Rule)57 DbTester (org.sonar.db.DbTester)54 ForbiddenException (org.sonar.server.exceptions.ForbiddenException)52 WebService (org.sonar.api.server.ws.WebService)44 BadRequestException (org.sonar.server.exceptions.BadRequestException)43 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)41 UserSessionRule (org.sonar.server.tester.UserSessionRule)41