From 98695ad6007060ffc6ce21f166870eeaf66973cf Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 24 May 2017 17:17:32 +0200 Subject: [PATCH] SONAR-9287 Refactor unit test --- .../db/permission/UserPermissionDaoTest.java | 467 ++++++++++-------- 1 file changed, 248 insertions(+), 219 deletions(-) diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java index fdffb764833..9b7c64124e3 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java @@ -24,13 +24,12 @@ import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Random; +import java.util.function.Consumer; import javax.annotation.Nullable; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; -import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; @@ -49,175 +48,168 @@ import static org.sonar.db.permission.OrganizationPermission.ADMINISTER; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; import static org.sonar.db.permission.OrganizationPermission.PROVISION_PROJECTS; import static org.sonar.db.permission.OrganizationPermission.SCAN; -import static org.sonar.db.user.UserTesting.newUserDto; public class UserPermissionDaoTest { @Rule - public DbTester dbTester = DbTester.create(System2.INSTANCE); + public DbTester db = DbTester.create(System2.INSTANCE); + private DbSession dbSession = db.getSession(); private UserPermissionDao underTest = new UserPermissionDao(); - private UserDto user1 = newUserDto().setLogin("login1").setName("Marius").setEmail("email1@email.com").setActive(true); - private UserDto user2 = newUserDto().setLogin("login2").setName("Marie").setEmail("email2@email.com").setActive(true); - private UserDto user3 = newUserDto().setLogin("zanother").setName("Zoe").setEmail("zanother3@another.com").setActive(true); - private OrganizationDto organizationDto; - private ComponentDto project1; - private ComponentDto project2; - private DbSession dbSession = dbTester.getSession(); - - @Before - public void setUp() throws Exception { - DbClient dbClient = dbTester.getDbClient(); - dbClient.userDao().insert(dbSession, user1); - dbClient.userDao().insert(dbSession, user2); - dbClient.userDao().insert(dbSession, user3); - organizationDto = dbTester.organizations().insert(); - project1 = dbTester.components().insertPrivateProject(organizationDto); - project2 = dbTester.components().insertPrivateProject(organizationDto); - dbTester.organizations().addMember(organizationDto, user1); - dbTester.organizations().addMember(organizationDto, user2); - dbTester.organizations().addMember(organizationDto, user3); - } @Test public void select_global_permissions() { - OrganizationDto org2 = dbTester.organizations().insert(); - UserPermissionDto global1 = addGlobalPermission(organizationDto, SYSTEM_ADMIN, user1); - UserPermissionDto global2 = addGlobalPermission(organizationDto, SYSTEM_ADMIN, user2); - UserPermissionDto global3 = addGlobalPermission(organizationDto, PROVISIONING, user2); - UserPermissionDto project1Perm = addProjectPermission(organizationDto, USER, user3, project1); + OrganizationDto organization = db.organizations().insert(); + OrganizationDto org2 = db.organizations().insert(); + UserDto user1 = insertUser(u -> u.setLogin("login1").setName("Marius").setEmail("email1@email.com"), organization, org2); + UserDto user2 = insertUser(u -> u.setLogin("login2").setName("Marie").setEmail("email2@email.com"), organization, org2); + UserDto user3 = insertUser(u -> u.setLogin("zanother").setName("Zoe").setEmail("zanother3@another.com"), organization); + ComponentDto project = db.components().insertPrivateProject(organization); + UserPermissionDto global1 = addGlobalPermission(organization, SYSTEM_ADMIN, user1); + UserPermissionDto global2 = addGlobalPermission(organization, SYSTEM_ADMIN, user2); + UserPermissionDto global3 = addGlobalPermission(organization, PROVISIONING, user2); + UserPermissionDto project1Perm = addProjectPermission(organization, USER, user3, project); // permissions on another organization, to be excluded UserPermissionDto org2Global1 = addGlobalPermission(org2, SYSTEM_ADMIN, user1); UserPermissionDto org2Global2 = addGlobalPermission(org2, PROVISIONING, user2); - dbTester.organizations().addMember(org2, user1); - dbTester.organizations().addMember(org2, user2); // global permissions of users who has at least one global permission, ordered by user name then permission - PermissionQuery query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().build(); - expectPermissions(organizationDto, query, null, global2, global3, global1); + PermissionQuery query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().build(); + expectPermissions(organization, query, null, global2, global3, global1); // default query returns all users, whatever their permissions nor organizations // (that's a non-sense, but still this is required for api/permissions/groups // when filtering users by name) - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).build(); - expectPermissions(organizationDto, query, null, global2, global3, org2Global2, global1, org2Global1, project1Perm); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).build(); + expectPermissions(organization, query, null, global2, global3, org2Global2, global1, org2Global1, project1Perm); // return empty list if non-null but empty logins - expectPermissions(organizationDto, query, Collections.emptyList()); + expectPermissions(organization, query, Collections.emptyList()); // global permissions of user1 - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().build(); - expectPermissions(organizationDto, query, asList(user1.getLogin()), global1); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().build(); + expectPermissions(organization, query, asList(user1.getLogin()), global1); // global permissions of user2 - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().build(); - expectPermissions(organizationDto, query, asList(user2.getLogin()), global2, global3); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().build(); + expectPermissions(organization, query, asList(user2.getLogin()), global2, global3); // global permissions of user1, user2 and another one - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().build(); - expectPermissions(organizationDto, query, asList(user1.getLogin(), user2.getLogin(), "missing"), global2, global3, global1); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().build(); + expectPermissions(organization, query, asList(user1.getLogin(), user2.getLogin(), "missing"), global2, global3, global1); // empty global permissions if login does not exist - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().build(); - expectPermissions(organizationDto, query, asList("missing")); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().build(); + expectPermissions(organization, query, asList("missing")); // empty global permissions if user does not have any - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().build(); - expectPermissions(organizationDto, query, asList(user3.getLogin())); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().build(); + expectPermissions(organization, query, asList(user3.getLogin())); // user3 has no global permissions - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().build(); - expectPermissions(organizationDto, query, asList(user3.getLogin())); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().build(); + expectPermissions(organization, query, asList(user3.getLogin())); // global permissions "admin" - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).setPermission(SYSTEM_ADMIN).build(); - expectPermissions(organizationDto, query, null, global2, global1); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).setPermission(SYSTEM_ADMIN).build(); + expectPermissions(organization, query, null, global2, global1); // empty if nobody has the specified global permission - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).setPermission("missing").build(); - expectPermissions(organizationDto, query, null); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).setPermission("missing").build(); + expectPermissions(organization, query, null); // search by user name (matches 2 users) - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().setSearchQuery("mari").build(); - expectPermissions(organizationDto, query, null, global2, global3, global1); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setSearchQuery("mari").build(); + expectPermissions(organization, query, null, global2, global3, global1); // search by user login - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().setSearchQuery("ogin2").build(); - expectPermissions(organizationDto, query, null, global2, global3); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setSearchQuery("ogin2").build(); + expectPermissions(organization, query, null, global2, global3); // search by user email - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().setSearchQuery("mail2").build(); - expectPermissions(organizationDto, query, null, global2, global3); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setSearchQuery("mail2").build(); + expectPermissions(organization, query, null, global2, global3); // search by user name (matches 2 users) and global permission - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).setSearchQuery("Mari").setPermission(PROVISIONING).build(); - expectPermissions(organizationDto, query, null, global3); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).setSearchQuery("Mari").setPermission(PROVISIONING).build(); + expectPermissions(organization, query, null, global3); // search by user name (no match) - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).setSearchQuery("Unknown").build(); - expectPermissions(organizationDto, query, null); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).setSearchQuery("Unknown").build(); + expectPermissions(organization, query, null); } @Test public void select_project_permissions() { - addGlobalPermission(organizationDto, SYSTEM_ADMIN, user1); - UserPermissionDto perm1 = addProjectPermission(organizationDto, USER, user1, project1); - UserPermissionDto perm2 = addProjectPermission(organizationDto, ISSUE_ADMIN, user1, project1); - UserPermissionDto perm3 = addProjectPermission(organizationDto, ISSUE_ADMIN, user2, project1); - addProjectPermission(organizationDto, ISSUE_ADMIN, user3, project2); + OrganizationDto organization = db.organizations().insert(); + UserDto user1 = insertUser(u -> u.setLogin("login1").setName("Marius").setEmail("email1@email.com"), organization); + UserDto user2 = insertUser(u -> u.setLogin("login2").setName("Marie").setEmail("email2@email.com"), organization); + UserDto user3 = insertUser(u -> u.setLogin("zanother").setName("Zoe").setEmail("zanother3@another.com"), organization); + addGlobalPermission(organization, SYSTEM_ADMIN, user1); + ComponentDto project1 = db.components().insertPrivateProject(organization); + ComponentDto project2 = db.components().insertPrivateProject(organization); + UserPermissionDto perm1 = addProjectPermission(organization, USER, user1, project1); + UserPermissionDto perm2 = addProjectPermission(organization, ISSUE_ADMIN, user1, project1); + UserPermissionDto perm3 = addProjectPermission(organization, ISSUE_ADMIN, user2, project1); + addProjectPermission(organization, ISSUE_ADMIN, user3, project2); // project permissions of users who has at least one permission on this project - PermissionQuery query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().setComponentUuid(project1.uuid()).build(); - expectPermissions(organizationDto, query, null, perm3, perm2, perm1); + PermissionQuery query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setComponentUuid(project1.uuid()).build(); + expectPermissions(organization, query, null, perm3, perm2, perm1); // project permissions of user1 - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().setComponentUuid(project1.uuid()).build(); - expectPermissions(organizationDto, query, asList(user1.getLogin()), perm2, perm1); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setComponentUuid(project1.uuid()).build(); + expectPermissions(organization, query, asList(user1.getLogin()), perm2, perm1); // project permissions of user2 - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().setComponentUuid(project1.uuid()).build(); - expectPermissions(organizationDto, query, asList(user2.getLogin()), perm3); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setComponentUuid(project1.uuid()).build(); + expectPermissions(organization, query, asList(user2.getLogin()), perm3); // project permissions of user2 and another one - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().setComponentUuid(project1.uuid()).build(); - expectPermissions(organizationDto, query, asList(user2.getLogin(), "missing"), perm3); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setComponentUuid(project1.uuid()).build(); + expectPermissions(organization, query, asList(user2.getLogin(), "missing"), perm3); // empty project permissions if login does not exist - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().setComponentUuid(project1.uuid()).build(); - expectPermissions(organizationDto, query, asList("missing")); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setComponentUuid(project1.uuid()).build(); + expectPermissions(organization, query, asList("missing")); // empty project permissions if user does not have any - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).withAtLeastOnePermission().setComponentUuid(project1.uuid()).build(); - expectPermissions(organizationDto, query, asList(user3.getLogin())); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setComponentUuid(project1.uuid()).build(); + expectPermissions(organization, query, asList(user3.getLogin())); // empty if nobody has the specified global permission - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).setPermission("missing").setComponentUuid(project1.uuid()).build(); - expectPermissions(organizationDto, query, null); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).setPermission("missing").setComponentUuid(project1.uuid()).build(); + expectPermissions(organization, query, null); // search by user name (matches 2 users), users with at least one permission - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).setSearchQuery("Mari").withAtLeastOnePermission().setComponentUuid(project1.uuid()).build(); - expectPermissions(organizationDto, query, null, perm3, perm2, perm1); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).setSearchQuery("Mari").withAtLeastOnePermission().setComponentUuid(project1.uuid()).build(); + expectPermissions(organization, query, null, perm3, perm2, perm1); // search by user name (matches 2 users) and project permission - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).setSearchQuery("Mari").setPermission(ISSUE_ADMIN).setComponentUuid(project1.uuid()).build(); - expectPermissions(organizationDto, query, null, perm3, perm2); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).setSearchQuery("Mari").setPermission(ISSUE_ADMIN).setComponentUuid(project1.uuid()).build(); + expectPermissions(organization, query, null, perm3, perm2); // search by user name (no match) - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).setSearchQuery("Unknown").setComponentUuid(project1.uuid()).build(); - expectPermissions(organizationDto, query, null); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).setSearchQuery("Unknown").setComponentUuid(project1.uuid()).build(); + expectPermissions(organization, query, null); // permissions of unknown project - query = PermissionQuery.builder().setOrganizationUuid(organizationDto.getUuid()).setComponentUuid("missing").withAtLeastOnePermission().build(); - expectPermissions(organizationDto, query, null); + query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).setComponentUuid("missing").withAtLeastOnePermission().build(); + expectPermissions(organization, query, null); } @Test public void countUsersByProjectPermission() { - addGlobalPermission(organizationDto, SYSTEM_ADMIN, user1); - addProjectPermission(organizationDto, USER, user1, project1); - addProjectPermission(organizationDto, ISSUE_ADMIN, user1, project1); - addProjectPermission(organizationDto, ISSUE_ADMIN, user2, project1); - addProjectPermission(organizationDto, ISSUE_ADMIN, user2, project2); + OrganizationDto organization = db.organizations().insert(); + UserDto user1 = insertUser(organization); + UserDto user2 = insertUser(organization); + ComponentDto project1 = db.components().insertPrivateProject(organization); + ComponentDto project2 = db.components().insertPrivateProject(organization); + addGlobalPermission(organization, SYSTEM_ADMIN, user1); + addProjectPermission(organization, USER, user1, project1); + addProjectPermission(organization, ISSUE_ADMIN, user1, project1); + addProjectPermission(organization, ISSUE_ADMIN, user2, project1); + addProjectPermission(organization, ISSUE_ADMIN, user2, project2); // no projects -> return empty list assertThat(underTest.countUsersByProjectPermission(dbSession, Collections.emptyList())).isEmpty(); @@ -236,22 +228,18 @@ public class UserPermissionDaoTest { @Test public void selectUserIds() { - OrganizationDto org1 = dbTester.organizations().insert(); - OrganizationDto org2 = dbTester.organizations().insert(); - ComponentDto project1 = dbTester.components().insertPrivateProject(org1); - ComponentDto project2 = dbTester.components().insertPrivateProject(org2); - + OrganizationDto org1 = db.organizations().insert(); + OrganizationDto org2 = db.organizations().insert(); + UserDto user1 = insertUser(u -> u.setLogin("login1").setName("Marius").setEmail("email1@email.com"), org1, org2); + UserDto user2 = insertUser(u -> u.setLogin("login2").setName("Marie").setEmail("email2@email.com"), org1, org2); + ComponentDto project1 = db.components().insertPrivateProject(org1); + ComponentDto project2 = db.components().insertPrivateProject(org2); addProjectPermission(org1, USER, user1, project1); addProjectPermission(org1, USER, user2, project1); addProjectPermission(org2, USER, user1, project2); addProjectPermission(org1, ISSUE_ADMIN, user2, project1); addProjectPermission(org2, ISSUE_ADMIN, user2, project2); - addOrganizationMember(org1, user1); - addOrganizationMember(org1, user2); - addOrganizationMember(org2, user1); - addOrganizationMember(org2, user2); - // logins are ordered by user name: user2 ("Marie") then user1 ("Marius") PermissionQuery query = PermissionQuery.builder().setOrganizationUuid(project1.getOrganizationUuid()).setComponentUuid(project1.uuid()).withAtLeastOnePermission().build(); assertThat(underTest.selectUserIds(dbSession, query)).containsExactly(user2.getId(), user1.getId()); @@ -281,86 +269,110 @@ public class UserPermissionDaoTest { @Test public void deleteGlobalPermission() { - addGlobalPermission(organizationDto, "perm1", user1); - addGlobalPermission(organizationDto, "perm2", user1); - addProjectPermission(organizationDto, "perm1", user1, project1); - addProjectPermission(organizationDto, "perm3", user2, project1); - addProjectPermission(organizationDto, "perm4", user2, project2); + OrganizationDto organization = db.organizations().insert(); + UserDto user1 = insertUser(organization); + UserDto user2 = insertUser(organization); + ComponentDto project1 = db.components().insertPrivateProject(organization); + ComponentDto project2 = db.components().insertPrivateProject(organization); + addGlobalPermission(organization, "perm1", user1); + addGlobalPermission(organization, "perm2", user1); + addProjectPermission(organization, "perm1", user1, project1); + addProjectPermission(organization, "perm3", user2, project1); + addProjectPermission(organization, "perm4", user2, project2); // user2 does not have global permissions -> do nothing - underTest.deleteGlobalPermission(dbSession, user2.getId(), "perm1", dbTester.getDefaultOrganization().getUuid()); - assertThat(dbTester.countRowsOfTable(dbSession, "user_roles")).isEqualTo(5); + underTest.deleteGlobalPermission(dbSession, user2.getId(), "perm1", db.getDefaultOrganization().getUuid()); + assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(5); // global permission is not granted -> do nothing - underTest.deleteGlobalPermission(dbSession, user1.getId(), "notGranted", dbTester.getDefaultOrganization().getUuid()); - assertThat(dbTester.countRowsOfTable(dbSession, "user_roles")).isEqualTo(5); + underTest.deleteGlobalPermission(dbSession, user1.getId(), "notGranted", db.getDefaultOrganization().getUuid()); + assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(5); // permission is on project -> do nothing - underTest.deleteGlobalPermission(dbSession, user1.getId(), "perm3", dbTester.getDefaultOrganization().getUuid()); - assertThat(dbTester.countRowsOfTable(dbSession, "user_roles")).isEqualTo(5); + underTest.deleteGlobalPermission(dbSession, user1.getId(), "perm3", db.getDefaultOrganization().getUuid()); + assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(5); // global permission on another organization-> do nothing underTest.deleteGlobalPermission(dbSession, user1.getId(), "notGranted", "anotherOrg"); - assertThat(dbTester.countRowsOfTable(dbSession, "user_roles")).isEqualTo(5); + assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(5); // global permission exists -> delete it, but not the project permission with the same name ! - underTest.deleteGlobalPermission(dbSession, user1.getId(), "perm1", organizationDto.getUuid()); - assertThat(dbTester.countSql(dbSession, "select count(id) from user_roles where role='perm1' and resource_id is null")).isEqualTo(0); - assertThat(dbTester.countRowsOfTable(dbSession, "user_roles")).isEqualTo(4); + underTest.deleteGlobalPermission(dbSession, user1.getId(), "perm1", organization.getUuid()); + assertThat(db.countSql(dbSession, "select count(id) from user_roles where role='perm1' and resource_id is null")).isEqualTo(0); + assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(4); } @Test public void deleteProjectPermission() { - addGlobalPermission(organizationDto, "perm", user1); - addProjectPermission(organizationDto, "perm", user1, project1); - addProjectPermission(organizationDto, "perm", user1, project2); - addProjectPermission(organizationDto, "perm", user2, project1); + OrganizationDto organization = db.organizations().insert(); + UserDto user1 = insertUser(organization); + UserDto user2 = insertUser(organization); + ComponentDto project1 = db.components().insertPrivateProject(organization); + ComponentDto project2 = db.components().insertPrivateProject(organization); + addGlobalPermission(organization, "perm", user1); + addProjectPermission(organization, "perm", user1, project1); + addProjectPermission(organization, "perm", user1, project2); + addProjectPermission(organization, "perm", user2, project1); // no such provision -> ignore underTest.deleteProjectPermission(dbSession, user1.getId(), "anotherPerm", project1.getId()); - assertThat(dbTester.countRowsOfTable(dbSession, "user_roles")).isEqualTo(4); + assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(4); underTest.deleteProjectPermission(dbSession, user1.getId(), "perm", project1.getId()); assertThatProjectPermissionDoesNotExist(user1, "perm", project1); - assertThat(dbTester.countRowsOfTable(dbSession, "user_roles")).isEqualTo(3); + assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(3); } @Test public void deleteProjectPermissions() { - addGlobalPermission(organizationDto, "perm", user1); - addProjectPermission(organizationDto, "perm", user1, project1); - addProjectPermission(organizationDto, "perm", user2, project1); - addProjectPermission(organizationDto, "perm", user1, project2); + OrganizationDto organization = db.organizations().insert(); + UserDto user1 = insertUser(organization); + UserDto user2 = insertUser(organization); + ComponentDto project1 = db.components().insertPrivateProject(organization); + ComponentDto project2 = db.components().insertPrivateProject(organization); + addGlobalPermission(organization, "perm", user1); + addProjectPermission(organization, "perm", user1, project1); + addProjectPermission(organization, "perm", user2, project1); + addProjectPermission(organization, "perm", user1, project2); underTest.deleteProjectPermissions(dbSession, project1.getId()); - assertThat(dbTester.countRowsOfTable(dbSession, "user_roles")).isEqualTo(2); + assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(2); assertThatProjectHasNoPermissions(project1); } @Test public void selectGlobalPermissionsOfUser() { - OrganizationDto org = dbTester.organizations().insert(); - addGlobalPermission(dbTester.getDefaultOrganization(), "perm1", user1); + OrganizationDto organization = db.organizations().insert(); + UserDto user1 = insertUser(organization); + UserDto user2 = insertUser(organization); + UserDto user3 = insertUser(organization); + OrganizationDto org = db.organizations().insert(); + ComponentDto project = db.components().insertPrivateProject(organization); + addGlobalPermission(db.getDefaultOrganization(), "perm1", user1); addGlobalPermission(org, "perm2", user2); addGlobalPermission(org, "perm3", user1); - addProjectPermission(organizationDto, "perm4", user1, project1); - addProjectPermission(organizationDto, "perm5", user1, project1); + addProjectPermission(organization, "perm4", user1, project); + addProjectPermission(organization, "perm5", user1, project); assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), org.getUuid())).containsOnly("perm3"); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), dbTester.getDefaultOrganization().getUuid())).containsOnly("perm1"); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), db.getDefaultOrganization().getUuid())).containsOnly("perm1"); assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), "otherOrg")).isEmpty(); assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user3.getId(), org.getUuid())).isEmpty(); } @Test public void selectProjectPermissionsOfUser() { - OrganizationDto org = dbTester.organizations().insert(); - ComponentDto project3 = dbTester.components().insertPrivateProject(org); - addGlobalPermission(organizationDto, "perm1", user1); - addProjectPermission(organizationDto, "perm2", user1, project1); - addProjectPermission(organizationDto, "perm3", user1, project1); - addProjectPermission(organizationDto, "perm4", user1, project2); - addProjectPermission(organizationDto, "perm5", user2, project1); + OrganizationDto organization = db.organizations().insert(); + UserDto user1 = insertUser(organization); + UserDto user2 = insertUser(organization); + ComponentDto project1 = db.components().insertPrivateProject(organization); + ComponentDto project2 = db.components().insertPrivateProject(organization); + ComponentDto project3 = db.components().insertPrivateProject(organization); + addGlobalPermission(organization, "perm1", user1); + addProjectPermission(organization, "perm2", user1, project1); + addProjectPermission(organization, "perm3", user1, project1); + addProjectPermission(organization, "perm4", user1, project2); + addProjectPermission(organization, "perm5", user2, project1); assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project1.getId())).containsOnly("perm2", "perm3"); assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project2.getId())).containsOnly("perm4"); @@ -369,10 +381,10 @@ public class UserPermissionDaoTest { @Test public void selectGroupIdsWithPermissionOnProjectBut_returns_empty_if_project_does_not_exist() { - OrganizationDto organization = dbTester.organizations().insert(); + OrganizationDto organization = db.organizations().insert(); ComponentDto project = randomPublicOrPrivateProject(organization); - UserDto user = dbTester.users().insertUser(); - dbTester.users().insertProjectPermissionOnUser(user, "foo", project); + UserDto user = insertUser(organization); + db.users().insertProjectPermissionOnUser(user, "foo", project); assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, 1234, UserRole.USER)) .isEmpty(); @@ -380,12 +392,12 @@ public class UserPermissionDaoTest { @Test public void selectGroupIdsWithPermissionOnProjectBut_returns_only_users_of_projects_which_do_not_have_permission() { - OrganizationDto organization = dbTester.organizations().insert(); + OrganizationDto organization = db.organizations().insert(); ComponentDto project = randomPublicOrPrivateProject(organization); - UserDto user1 = dbTester.users().insertUser(); - UserDto user2 = dbTester.users().insertUser(); - dbTester.users().insertProjectPermissionOnUser(user1, "p1", project); - dbTester.users().insertProjectPermissionOnUser(user2, "p2", project); + UserDto user1 = insertUser(organization); + UserDto user2 = insertUser(organization); + db.users().insertProjectPermissionOnUser(user1, "p1", project); + db.users().insertProjectPermissionOnUser(user2, "p2", project); assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, project.getId(), "p2")) .containsOnly(user1.getId()); @@ -397,13 +409,12 @@ public class UserPermissionDaoTest { @Test public void selectGroupIdsWithPermissionOnProjectBut_does_not_return_groups_which_have_no_permission_at_all_on_specified_project() { - OrganizationDto organization = dbTester.organizations().insert(); + OrganizationDto organization = db.organizations().insert(); ComponentDto project = randomPublicOrPrivateProject(organization); - UserDto user1 = dbTester.users().insertUser(); - UserDto user2 = dbTester.users().insertUser(); - UserDto user3 = dbTester.users().insertUser(); - dbTester.users().insertProjectPermissionOnUser(user1, "p1", project); - dbTester.users().insertProjectPermissionOnUser(user2, "p2", project); + UserDto user1 = insertUser(organization); + UserDto user2 = insertUser(organization); + db.users().insertProjectPermissionOnUser(user1, "p1", project); + db.users().insertProjectPermissionOnUser(user2, "p2", project); assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, project.getId(), "p2")) .containsOnly(user1.getId()); @@ -419,7 +430,7 @@ public class UserPermissionDaoTest { @Test public void deleteByOrganization_does_not_fail_if_organization_has_no_user_permission() { - OrganizationDto organization = dbTester.organizations().insert(); + OrganizationDto organization = db.organizations().insert(); underTest.deleteByOrganization(dbSession, organization.getUuid()); dbSession.commit(); @@ -427,21 +438,21 @@ public class UserPermissionDaoTest { @Test public void deleteByOrganization_deletes_all_user_permission_of_specified_organization() { - OrganizationDto organization1 = dbTester.organizations().insert(); - OrganizationDto organization2 = dbTester.organizations().insert(); - OrganizationDto organization3 = dbTester.organizations().insert(); - UserDto user1 = dbTester.users().insertUser(); - UserDto user2 = dbTester.users().insertUser(); - UserDto user3 = dbTester.users().insertUser(); - dbTester.users().insertPermissionOnUser(organization1, user1, "foo"); - dbTester.users().insertPermissionOnUser(organization1, user2, "foo"); - dbTester.users().insertPermissionOnUser(organization1, user2, "bar"); - dbTester.users().insertPermissionOnUser(organization2, user2, "foo"); - dbTester.users().insertPermissionOnUser(organization2, user3, "foo"); - dbTester.users().insertPermissionOnUser(organization2, user3, "bar"); - dbTester.users().insertPermissionOnUser(organization3, user3, "foo"); - dbTester.users().insertPermissionOnUser(organization3, user1, "foo"); - dbTester.users().insertPermissionOnUser(organization3, user1, "bar"); + OrganizationDto organization1 = db.organizations().insert(); + OrganizationDto organization2 = db.organizations().insert(); + OrganizationDto organization3 = db.organizations().insert(); + UserDto user1 = insertUser(organization1, organization2, organization3); + UserDto user2 = insertUser(organization1, organization2, organization3); + UserDto user3 = insertUser(organization1, organization2, organization3); + db.users().insertPermissionOnUser(organization1, user1, "foo"); + db.users().insertPermissionOnUser(organization1, user2, "foo"); + db.users().insertPermissionOnUser(organization1, user2, "bar"); + db.users().insertPermissionOnUser(organization2, user2, "foo"); + db.users().insertPermissionOnUser(organization2, user3, "foo"); + db.users().insertPermissionOnUser(organization2, user3, "bar"); + db.users().insertPermissionOnUser(organization3, user3, "foo"); + db.users().insertPermissionOnUser(organization3, user1, "foo"); + db.users().insertPermissionOnUser(organization3, user1, "bar"); underTest.deleteByOrganization(dbSession, organization3.getUuid()); dbSession.commit(); @@ -458,19 +469,19 @@ public class UserPermissionDaoTest { @Test public void delete_permissions_of_an_organization_member() { - OrganizationDto organization1 = dbTester.organizations().insert(); - OrganizationDto organization2 = dbTester.organizations().insert(); - ComponentDto project = dbTester.components().insertPrivateProject(organization1); - UserDto user1 = dbTester.users().insertUser(); - UserDto user2 = dbTester.users().insertUser(); + OrganizationDto organization1 = db.organizations().insert(); + OrganizationDto organization2 = db.organizations().insert(); + ComponentDto project = db.components().insertPrivateProject(organization1); + UserDto user1 = insertUser(organization1, organization2); + UserDto user2 = insertUser(organization1, organization2); // user 1 permissions - dbTester.users().insertPermissionOnUser(organization1, user1, SCAN); - dbTester.users().insertPermissionOnUser(organization1, user1, ADMINISTER); - dbTester.users().insertProjectPermissionOnUser(user1, UserRole.CODEVIEWER, project); - dbTester.users().insertPermissionOnUser(organization2, user1, SCAN); + db.users().insertPermissionOnUser(organization1, user1, SCAN); + db.users().insertPermissionOnUser(organization1, user1, ADMINISTER); + db.users().insertProjectPermissionOnUser(user1, UserRole.CODEVIEWER, project); + db.users().insertPermissionOnUser(organization2, user1, SCAN); // user 2 permission - dbTester.users().insertPermissionOnUser(organization1, user2, SCAN); - dbTester.users().insertProjectPermissionOnUser(user2, UserRole.CODEVIEWER, project); + db.users().insertPermissionOnUser(organization1, user2, SCAN); + db.users().insertProjectPermissionOnUser(user2, UserRole.CODEVIEWER, project); underTest.deleteOrganizationMemberPermissions(dbSession, organization1.getUuid(), user1.getId()); dbSession.commit(); @@ -486,72 +497,78 @@ public class UserPermissionDaoTest { @Test public void deleteByUserId() { - UserDto user1 = dbTester.users().insertUser(); - UserDto user2 = dbTester.users().insertUser(); - ComponentDto project = dbTester.components().insertPrivateProject(); - dbTester.users().insertPermissionOnUser(user1, SCAN); - dbTester.users().insertPermissionOnUser(user1, ADMINISTER); - dbTester.users().insertProjectPermissionOnUser(user1, ADMINISTER_QUALITY_GATES.getKey(), project); - dbTester.users().insertPermissionOnUser(user2, SCAN); - dbTester.users().insertProjectPermissionOnUser(user2, ADMINISTER_QUALITY_GATES.getKey(), project); + OrganizationDto organization = db.organizations().insert(); + UserDto user1 = insertUser(organization); + UserDto user2 = insertUser(organization); + ComponentDto project = db.components().insertPrivateProject(organization); + 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.deleteByUserId(dbSession, user1.getId()); dbSession.commit(); - assertThat(dbTester.select("select user_id as \"userId\", resource_id as \"projectId\", role as \"permission\" from user_roles")) + assertThat(db.select("select user_id as \"userId\", resource_id as \"projectId\", role as \"permission\" from user_roles")) .extracting((row) -> row.get("userId"), (row) -> row.get("projectId"), (row) -> row.get("permission")) .containsOnly(tuple(user2.getId().longValue(), null, SCAN.getKey()), tuple(user2.getId().longValue(), project.getId(), ADMINISTER_QUALITY_GATES.getKey())); } @Test public void deleteProjectPermissionOfAnyUser_has_no_effect_if_specified_component_does_not_exist() { - OrganizationDto organization = dbTester.organizations().insert(); - dbTester.users().insertPermissionOnUser(organization, user1, SCAN); + OrganizationDto organization = db.organizations().insert(); + UserDto user = insertUser(organization); + db.users().insertPermissionOnUser(organization, user, SCAN); int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, 124L, SCAN.getKey()); assertThat(deletedCount).isEqualTo(0); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), organization.getUuid())).containsOnly(SCAN.getKey()); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getId(), organization.getUuid())).containsOnly(SCAN.getKey()); } @Test public void deleteProjectPermissionOfAnyUser_has_no_effect_if_specified_component_has_no_permission_at_all() { - OrganizationDto organization = dbTester.organizations().insert(); - dbTester.users().insertPermissionOnUser(organization, user1, SCAN); + OrganizationDto organization = db.organizations().insert(); + UserDto user = insertUser(organization); + db.users().insertPermissionOnUser(organization, user, SCAN); ComponentDto project = randomPublicOrPrivateProject(organization); int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project.getId(), SCAN.getKey()); assertThat(deletedCount).isEqualTo(0); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), organization.getUuid())).containsOnly(SCAN.getKey()); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getId(), organization.getUuid())).containsOnly(SCAN.getKey()); } @Test public void deleteProjectPermissionOfAnyUser_has_no_effect_if_specified_component_does_not_have_specified_permission() { - OrganizationDto organization = dbTester.organizations().insert(); - dbTester.users().insertPermissionOnUser(organization, user1, SCAN); + OrganizationDto organization = db.organizations().insert(); + UserDto user = insertUser(organization); + db.users().insertPermissionOnUser(organization, user, SCAN); ComponentDto project = randomPublicOrPrivateProject(organization); - dbTester.users().insertProjectPermissionOnUser(user1, SCAN.getKey(), project); + db.users().insertProjectPermissionOnUser(user, SCAN.getKey(), project); int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project.getId(), "p1"); assertThat(deletedCount).isEqualTo(0); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), organization.getUuid())).containsOnly(SCAN.getKey()); - assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project.getId())).containsOnly(SCAN.getKey()); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getId(), organization.getUuid())).containsOnly(SCAN.getKey()); + assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user.getId(), project.getId())).containsOnly(SCAN.getKey()); } @Test public void deleteProjectPermissionOfAnyUser_deletes_specified_permission_for_any_user_on_the_specified_component() { - OrganizationDto organization = dbTester.organizations().insert(); - dbTester.users().insertPermissionOnUser(organization, user1, SCAN); - dbTester.users().insertPermissionOnUser(organization, user2, SCAN); + OrganizationDto organization = db.organizations().insert(); + UserDto user1 = insertUser(organization); + UserDto user2 = insertUser(organization); + db.users().insertPermissionOnUser(organization, user1, SCAN); + db.users().insertPermissionOnUser(organization, user2, SCAN); ComponentDto project1 = randomPublicOrPrivateProject(organization); ComponentDto project2 = randomPublicOrPrivateProject(organization); - dbTester.users().insertProjectPermissionOnUser(user1, SCAN.getKey(), project1); - dbTester.users().insertProjectPermissionOnUser(user2, SCAN.getKey(), project1); - dbTester.users().insertProjectPermissionOnUser(user1, SCAN.getKey(), project2); - dbTester.users().insertProjectPermissionOnUser(user2, SCAN.getKey(), project2); - dbTester.users().insertProjectPermissionOnUser(user2, PROVISION_PROJECTS.getKey(), project2); + db.users().insertProjectPermissionOnUser(user1, SCAN.getKey(), project1); + db.users().insertProjectPermissionOnUser(user2, SCAN.getKey(), project1); + db.users().insertProjectPermissionOnUser(user1, SCAN.getKey(), project2); + db.users().insertProjectPermissionOnUser(user2, SCAN.getKey(), project2); + db.users().insertProjectPermissionOnUser(user2, PROVISION_PROJECTS.getKey(), project2); int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project1.getId(), SCAN.getKey()); @@ -575,11 +592,23 @@ public class UserPermissionDaoTest { } private ComponentDto randomPublicOrPrivateProject(OrganizationDto organization) { - return new Random().nextBoolean() ? dbTester.components().insertPrivateProject(organization) : dbTester.components().insertPublicProject(organization); + return new Random().nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization); + } + + private UserDto insertUser(Consumer populateUserDto, OrganizationDto... organizations) { + UserDto user = db.users().insertUser(populateUserDto); + Arrays.stream(organizations).forEach(organization -> db.organizations().addMember(organization, user)); + return user; + } + + private UserDto insertUser(OrganizationDto... organizations) { + UserDto user = db.users().insertUser(); + Arrays.stream(organizations).forEach(organization -> db.organizations().addMember(organization, user)); + return user; } private void verifyOrganizationUuidsInTable(String... organizationUuids) { - assertThat(dbTester.select("select organization_uuid as \"organizationUuid\" from user_roles")) + assertThat(db.select("select organization_uuid as \"organizationUuid\" from user_roles")) .extracting((row) -> (String) row.get("organizationUuid")) .containsOnly(organizationUuids); } @@ -618,28 +647,28 @@ public class UserPermissionDaoTest { private UserPermissionDto addGlobalPermission(OrganizationDto org, String permission, UserDto user) { UserPermissionDto dto = new UserPermissionDto(org.getUuid(), permission, user.getId(), null); underTest.insert(dbSession, dto); - dbTester.commit(); + db.commit(); return dto; } private UserPermissionDto addProjectPermission(OrganizationDto org, String permission, UserDto user, ComponentDto project) { UserPermissionDto dto = new UserPermissionDto(org.getUuid(), permission, user.getId(), project.getId()); underTest.insert(dbSession, dto); - dbTester.commit(); + db.commit(); return dto; } private void addOrganizationMember(OrganizationDto org, UserDto user) { - dbTester.organizations().addMember(org, user); + db.organizations().addMember(org, user); } private void assertThatProjectPermissionDoesNotExist(UserDto user, String permission, ComponentDto project) { - assertThat(dbTester.countSql(dbSession, "select count(id) from user_roles where role='" + permission + "' and user_id=" + user.getId() + " and resource_id=" + project.getId())) + assertThat(db.countSql(dbSession, "select count(id) from user_roles where role='" + permission + "' and user_id=" + user.getId() + " and resource_id=" + project.getId())) .isEqualTo(0); } private void assertThatProjectHasNoPermissions(ComponentDto project) { - assertThat(dbTester.countSql(dbSession, "select count(id) from user_roles where resource_id=" + project.getId())).isEqualTo(0); + assertThat(db.countSql(dbSession, "select count(id) from user_roles where resource_id=" + project.getId())).isEqualTo(0); } private void assertOrgPermissionsOfUser(UserDto user, OrganizationDto organization, OrganizationPermission... permissions) { -- 2.39.5