@Test public void keepAuthorizedUsersForRoleAndProject_should_be_able_to_handle_lots_of_users() { List<UserDto> users = IntStream.range(0, 2000).mapToObj(i -> db.users().insertUser()).collect(Collectors.toList()); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, users.stream().map(UserDto::getId).collect(Collectors.toSet()), "user", PROJECT_ID)).isEmpty(); }
@Test public void keepAuthorizedUsersForRoleAndProject_returns_empty_for_any_users_and_any_permission_on_private_project_without_any_permission_in_DB() { ComponentDto project = db.components().insertPrivateProject(organization); PermissionsTestHelper.ALL_PERMISSIONS .forEach(perm -> { assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, randomExistingUserIds, perm, project.getId())) .isEmpty(); }); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, randomExistingUserIds, randomPermission, project.getId())) .isEmpty(); }
@Test public void keepAuthorizedUsersForRoleAndProject_does_not_return_user_if_granted_project_permission_by_AnyOne_on_public_project() { ComponentDto project = db.components().insertPublicProject(organization); ComponentDto otherProject = db.components().insertPublicProject(organization); UserDto otherUser = db.users().insertUser(); db.users().insertProjectPermissionOnAnyone(randomPermission, project); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), randomPermission, project.getId())) .isEmpty(); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), "another perm", project.getId())) .isEmpty(); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), randomPermission, otherProject.getId())) .isEmpty(); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(otherUser.getId()), randomPermission, project.getId())) .isEmpty(); }
@Test public void keepAuthorizedUsersForRoleAndProject_returns_empty_for_any_user_on_private_project_without_any_permission_in_DB_and_permission_CODEVIEWER() { ComponentDto project = db.components().insertPrivateProject(organization); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, randomExistingUserIds, UserRole.CODEVIEWER, project.getId())) .isEmpty(); }
@Test public void keepAuthorizedUsersForRoleAndProject_returns_empty_for_any_user_on_private_project_without_any_permission_in_DB_and_permission_USER() { ComponentDto project = db.components().insertPrivateProject(organization); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, randomExistingUserIds, UserRole.USER, project.getId())) .isEmpty(); }
@Test public void keepAuthorizedUsersForRoleAndProject_returns_any_users_for_public_project_without_any_permission_in_DB_and_permission_USER() { ComponentDto project = db.components().insertPublicProject(organization); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, randomExistingUserIds, UserRole.USER, project.getId())) .containsAll(randomExistingUserIds); }
@Test public void keepAuthorizedUsersForRoleAndProject_returns_any_users_for_public_project_without_any_permission_in_DB_and_permission_CODEVIEWER() { ComponentDto project = db.components().insertPublicProject(organization); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, randomExistingUserIds, UserRole.CODEVIEWER, project.getId())) .containsAll(randomExistingUserIds); }
@Test public void keepAuthorizedUsersForRoleAndProject_returns_empty_for_any_users_on_public_project_without_any_permission_in_DB() { ComponentDto project = db.components().insertPublicProject(organization); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, randomExistingUserIds, randomPermission, project.getId())) .isEmpty(); }
@Test public void keepAuthorizedUsersForRoleAndProject_returns_user_if_granted_project_permission_directly_on_public_project() { ComponentDto project = db.components().insertPublicProject(organization); ComponentDto otherProject = db.components().insertPublicProject(organization); UserDto otherUser = db.users().insertUser(); db.users().insertProjectPermissionOnUser(user, randomPermission, project); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), randomPermission, project.getId())) .containsOnly(user.getId()); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), "another perm", project.getId())) .isEmpty(); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(otherUser.getId()), randomPermission, project.getId())) .isEmpty(); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), randomPermission, otherProject.getId())) .isEmpty(); }
@Test public void keepAuthorizedUsersForRoleAndProject_returns_user_if_granted_project_permission_directly_on_private_project() { ComponentDto project = db.components().insertPrivateProject(organization); ComponentDto otherProject = db.components().insertPublicProject(organization); UserDto otherUser = db.users().insertUser(); db.users().insertProjectPermissionOnUser(user, randomPermission, project); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), randomPermission, project.getId())) .containsOnly(user.getId()); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), "another perm", project.getId())) .isEmpty(); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(otherUser.getId()), randomPermission, project.getId())) .isEmpty(); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), randomPermission, otherProject.getId())) .isEmpty(); }
@Test public void keepAuthorizedUsersForRoleAndProject_returns_user_if_granted_project_permission_by_group_on_private_project() { ComponentDto project = db.components().insertPrivateProject(organization); ComponentDto otherProject = db.components().insertPublicProject(organization); UserDto otherUser = db.users().insertUser(); db.users().insertMember(group1, user); db.users().insertProjectPermissionOnGroup(group1, randomPermission, project); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), randomPermission, project.getId())) .containsOnly(user.getId()); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), "another perm", project.getId())) .isEmpty(); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), randomPermission, otherProject.getId())) .isEmpty(); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(otherUser.getId()), randomPermission, project.getId())) .isEmpty(); }
@Test public void keepAuthorizedUsersForRoleAndProject_returns_empty_if_user_set_is_empty_on_public_project() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPublicProject(organization); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, Collections.emptySet(), UserRole.USER, project.getId())) .isEmpty(); }
@Test public void keepAuthorizedUsersForRoleAndProject_returns_user_if_granted_project_permission_by_group_on_public_project() { ComponentDto project = db.components().insertPublicProject(organization); ComponentDto otherProject = db.components().insertPublicProject(organization); UserDto otherUser = db.users().insertUser(); db.users().insertMember(group1, user); db.users().insertProjectPermissionOnGroup(group1, randomPermission, project); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), randomPermission, project.getId())) .containsOnly(user.getId()); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), "another perm", project.getId())) .isEmpty(); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(user.getId()), randomPermission, otherProject.getId())) .isEmpty(); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, singleton(otherUser.getId()), randomPermission, project.getId())) .isEmpty(); }
@Test public void keep_authorized_users_returns_empty_list_for_role_and_project_for_anonymous() { ComponentDto project1 = db.components().insertPrivateProject(organization); ComponentDto project2 = db.components().insertPrivateProject(organization); ComponentDto project3 = db.components().insertPrivateProject(organization); UserDto user1 = db.users().insertUser("u1"); UserDto user2 = db.users().insertUser("u2"); UserDto user3 = db.users().insertUser("u3"); GroupDto group1 = db.users().insertGroup(organization); GroupDto group2 = db.users().insertGroup(organization); db.users().insertMembers(group1, user1, user2); db.users().insertMembers(group2, user3); db.users().insertProjectPermissionOnUser(user1, UserRole.USER, project1); db.users().insertProjectPermissionOnUser(user2, UserRole.USER, project1); db.users().insertProjectPermissionOnUser(user3, UserRole.USER, project1); db.users().insertProjectPermissionOnGroup(group2, UserRole.USER, project3); assertThat(underTest.keepAuthorizedUsersForRoleAndProject(dbSession, // Only 100 and 101 has 'user' role on project newHashSet(100, 101, 102), "user", PROJECT_ID)).isEmpty(); }