@Test public void return_user_info_even_when_user_is_already_member_of_organization() { OrganizationDto organization = db.organizations().insert(); db.users().insertDefaultGroup(organization, "default"); UserDto user = db.users().insertUser(u -> u.setEmail("john@smith.com")); IntStream.range(0, 3) .mapToObj(i -> db.users().insertGroup(organization)) .forEach(g -> db.users().insertMembers(g, user)); db.organizations().addMember(organization, user); AddMemberWsResponse result = call(organization.getKey(), user.getLogin()); assertThat(result.getUser().getLogin()).isEqualTo(user.getLogin()); assertThat(result.getUser().getName()).isEqualTo(user.getName()); assertThat(result.getUser().getAvatar()).isEqualTo("b0d8c6e5ea589e6fc3d3e08afb1873bb"); assertThat(result.getUser().getGroupCount()).isEqualTo(3); }
@Test public void anonymous_should_be_authorized() { ComponentDto project1 = db.components().insertPublicProject(organization); ComponentDto project2 = db.components().insertPublicProject(organization); UserDto user1 = db.users().insertUser("u1"); GroupDto group = db.users().insertGroup(organization); db.users().insertMembers(group, user1); assertThat(underTest.keepAuthorizedProjectIds(dbSession, newHashSet(project1.getId(), project2.getId()), null, UserRole.USER)) .containsOnly(project1.getId(), project2.getId()); // group does not have the role "admin" assertThat(underTest.keepAuthorizedProjectIds(dbSession, newHashSet(project1.getId()), null, "admin")) .isEmpty(); }
@Test public void remove_from_organization_groups() { OrganizationDto anotherOrganization = db.organizations().insert(); UserDto anotherUser = db.users().insertUser(); GroupDto group = db.users().insertGroup(organization); GroupDto anotherGroup = db.users().insertGroup(anotherOrganization); db.users().insertMembers(group, user, anotherUser); db.users().insertMembers(anotherGroup, user, anotherUser); call(organization.getKey(), user.getLogin()); assertThat(dbClient.groupMembershipDao().selectGroupIdsByUserId(dbSession, user.getId())) .containsOnly(anotherGroup.getId()); assertThat(dbClient.groupMembershipDao().selectGroupIdsByUserId(dbSession, anotherUser.getId())) .containsOnly(group.getId(), anotherGroup.getId()); }
@Test public void delete_organization_member() { OrganizationDto organization = dbTester.organizations().insert(); OrganizationDto anotherOrganization = dbTester.organizations().insert(); UserDto user = dbTester.users().insertUser(); UserDto anotherUser = dbTester.users().insertUser(); GroupDto group = dbTester.users().insertGroup(organization); GroupDto anotherGroup = dbTester.users().insertGroup(anotherOrganization); dbTester.users().insertMembers(group, user, anotherUser); dbTester.users().insertMembers(anotherGroup, user, anotherUser); underTest.deleteByOrganizationAndUser(dbSession, organization.getUuid(), user.getId()); assertThat(dbClient.groupMembershipDao().selectGroupIdsByUserId(dbSession, user.getId())) .containsOnly(anotherGroup.getId()); assertThat(dbClient.groupMembershipDao().selectGroupIdsByUserId(dbSession, anotherUser.getId())) .containsOnly(group.getId(), anotherGroup.getId()); }
@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(); }
@Test public void group_should_be_authorized() { ComponentDto project1 = db.components().insertPrivateProject(organization); ComponentDto project2 = db.components().insertPrivateProject(organization); ComponentDto project3 = db.components().insertPrivateProject(organization); UserDto user1 = db.users().insertUser("u1"); GroupDto group = db.users().insertGroup(organization); db.users().insertMembers(group, user1); db.users().insertProjectPermissionOnUser(user1, UserRole.USER, project1); db.users().insertProjectPermissionOnGroup(group, UserRole.USER, project2); db.users().insertProjectPermissionOnGroup(group, UserRole.USER, project3); assertThat(underTest.keepAuthorizedProjectIds(dbSession, newHashSet(project2.getId(), project3.getId()), user1.getId(), UserRole.USER)) .containsOnly(project2.getId(), project3.getId()); // group does not have the role "admin" assertThat(underTest.keepAuthorizedProjectIds(dbSession, newHashSet(project2.getId(), project3.getId()), user1.getId(), UserRole.ADMIN)) .isEmpty(); }
@Test public void return_group_counts_if_org_admin() { OrganizationDto organization = db.organizations().insert(); UserDto currentUser = db.users().insertUser(); userSession.logIn(currentUser) .addPermission(OrganizationPermission.ADMINISTER, organization) .addMembership(organization); UserDto user = db.users().insertUser(); db.organizations().addMember(organization, user); UserDto anotherUser = db.users().insertUser(); db.organizations().addMember(organization, anotherUser); IntStream.range(0, 10) .mapToObj(i -> db.users().insertGroup(organization)) .forEach(g -> db.users().insertMembers(g, user)); OrganizationDto anotherOrganization = db.organizations().insert(); GroupDto anotherGroup = db.users().insertGroup(anotherOrganization); db.users().insertMember(anotherGroup, user); indexAllUsers(); SearchMembersWsResponse result = ws.newRequest() .setParam("organization", organization.getKey()) .executeProtobuf(SearchMembersWsResponse.class); assertThat(result.getUsersList()).extracting(User::getLogin, User::getGroupCount, User::hasGroupCount) .containsExactlyInAnyOrder( tuple(user.getLogin(), 10, true), tuple(anotherUser.getLogin(), 0, true)); }