private Set<String> keepAuthorizedLogins(DbSession dbSession, String projectKey, Set<SubscriberAndChannel> subscriberAndChannels, @Nullable Boolean global, String permission) { Set<String> logins = subscriberAndChannels.stream() .filter(s -> global == null || s.getSubscriber().isGlobal() == global) .map(s -> s.getSubscriber().getLogin()) .collect(Collectors.toSet()); if (logins.isEmpty()) { return Collections.emptySet(); } return dbClient.authorizationDao().keepAuthorizedLoginsOnProject(dbSession, logins, projectKey, permission); }
@Test public void do_not_call_db_for_project_permission_filtering_if_there_is_no_project_subscriber() { String globalPermission = RandomStringUtils.randomAlphanumeric(4); String projectPermission = RandomStringUtils.randomAlphanumeric(5); String projectUuid = "uuid_45"; when(propertiesDao.findUsersForNotification("NewViolations", "Email", projectUuid)) .thenReturn(newHashSet(new Subscriber("user3", true))); when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", projectUuid)) .thenReturn(newHashSet(new Subscriber("user3", true))); when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, newHashSet("user3"), projectUuid, globalPermission)) .thenReturn(newHashSet("user3")); Multimap<String, NotificationChannel> multiMap = underTest.findSubscribedRecipientsForDispatcher(dispatcher, projectUuid, new SubscriberPermissionsOnProject(globalPermission, projectPermission)); assertThat(multiMap.entries()).hasSize(2); Map<String, Collection<NotificationChannel>> map = multiMap.asMap(); assertThat(map.get("user3")).containsOnly(emailChannel, twitterChannel); verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(globalPermission)); verify(authorizationDao, times(0)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(projectPermission)); }
@Test public void do_not_call_db_for_project_permission_filtering_if_there_is_no_global_subscriber() { String globalPermission = RandomStringUtils.randomAlphanumeric(4); String projectPermission = RandomStringUtils.randomAlphanumeric(5); String projectUuid = "uuid_45"; when(propertiesDao.findUsersForNotification("NewViolations", "Email", projectUuid)) .thenReturn(newHashSet(new Subscriber("user3", false))); when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", projectUuid)) .thenReturn(newHashSet(new Subscriber("user3", false))); when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, newHashSet("user3"), projectUuid, projectPermission)) .thenReturn(newHashSet("user3")); Multimap<String, NotificationChannel> multiMap = underTest.findSubscribedRecipientsForDispatcher(dispatcher, projectUuid, new SubscriberPermissionsOnProject(globalPermission, projectPermission)); assertThat(multiMap.entries()).hasSize(2); Map<String, Collection<NotificationChannel>> map = multiMap.asMap(); assertThat(map.get("user3")).containsOnly(emailChannel, twitterChannel); verify(authorizationDao, times(0)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(globalPermission)); verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(projectPermission)); } }
@Test public void shouldFindSubscribedRecipientForGivenResource() { String projectUuid = "uuid_45"; when(propertiesDao.findUsersForNotification("NewViolations", "Email", projectUuid)) .thenReturn(newHashSet(new Subscriber("user1", false), new Subscriber("user3", false), new Subscriber("user3", true))); when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", "uuid_56")) .thenReturn(newHashSet(new Subscriber("user2", false))); when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", projectUuid)) .thenReturn(newHashSet(new Subscriber("user3", true))); when(propertiesDao.findUsersForNotification("NewAlerts", "Twitter", projectUuid)) .thenReturn(newHashSet(new Subscriber("user4", false))); when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, newHashSet("user1", "user3"), projectUuid, "user")) .thenReturn(newHashSet("user1", "user3")); Multimap<String, NotificationChannel> multiMap = underTest.findSubscribedRecipientsForDispatcher(dispatcher, projectUuid, SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER); assertThat(multiMap.entries()).hasSize(3); Map<String, Collection<NotificationChannel>> map = multiMap.asMap(); assertThat(map.get("user1")).containsOnly(emailChannel); assertThat(map.get("user2")).isNull(); assertThat(map.get("user3")).containsOnly(emailChannel, twitterChannel); assertThat(map.get("user4")).isNull(); // code is optimized to perform only 1 SQL requests for all channels verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), anyString()); }
@Test public void should_apply_distinct_permission_filtering_global_or_project_subscribers() { String globalPermission = RandomStringUtils.randomAlphanumeric(4); String projectPermission = RandomStringUtils.randomAlphanumeric(5); String projectUuid = "uuid_45"; when(propertiesDao.findUsersForNotification("NewViolations", "Email", projectUuid)) .thenReturn(newHashSet(new Subscriber("user1", false), new Subscriber("user3", false), new Subscriber("user3", true))); when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", "uuid_56")) .thenReturn(newHashSet(new Subscriber("user2", false))); when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", projectUuid)) .thenReturn(newHashSet(new Subscriber("user3", true))); when(propertiesDao.findUsersForNotification("NewAlerts", "Twitter", projectUuid)) .thenReturn(newHashSet(new Subscriber("user4", false))); when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, newHashSet("user3", "user4"), projectUuid, globalPermission)) .thenReturn(newHashSet("user3")); when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, newHashSet("user1", "user3"), projectUuid, projectPermission)) .thenReturn(newHashSet("user1", "user3")); Multimap<String, NotificationChannel> multiMap = underTest.findSubscribedRecipientsForDispatcher(dispatcher, projectUuid, new SubscriberPermissionsOnProject(globalPermission, projectPermission)); assertThat(multiMap.entries()).hasSize(3); Map<String, Collection<NotificationChannel>> map = multiMap.asMap(); assertThat(map.get("user1")).containsOnly(emailChannel); assertThat(map.get("user2")).isNull(); assertThat(map.get("user3")).containsOnly(emailChannel, twitterChannel); assertThat(map.get("user4")).isNull(); // code is optimized to perform only 2 SQL requests for all channels verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(globalPermission)); verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(projectPermission)); }
@Test public void keepAuthorizedLoginsOnProject_return_correct_users_on_public_project() { ComponentDto project = db.components().insertPublicProject(organization); UserDto user1 = db.users().insertUser(); // 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(); GroupDto adminGroup = db.users().insertGroup(organization, "ADMIN"); db.users().insertMember(adminGroup, admin2); db.users().insertProjectPermissionOnGroup(adminGroup, UserRole.ADMIN, project); assertThat(underTest.keepAuthorizedLoginsOnProject(dbSession, newHashSet(user1.getLogin()), project.getKey(), UserRole.USER)) .containsOnly(user1.getLogin()); assertThat(underTest.keepAuthorizedLoginsOnProject(dbSession, newHashSet(user1.getLogin(), admin1.getLogin(), admin2.getLogin()), project.getKey(), UserRole.USER)) .containsOnly(user1.getLogin(), admin1.getLogin(), admin2.getLogin()); assertThat(underTest.keepAuthorizedLoginsOnProject(dbSession, newHashSet(user1.getLogin(), admin1.getLogin(), admin2.getLogin()), project.getKey(), UserRole.ADMIN)) .containsOnly(admin1.getLogin(), admin2.getLogin()); }
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()); 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());
assertThat(underTest.keepAuthorizedLoginsOnProject(dbSession, newHashSet(userWithNoRole.getLogin()), project.getKey(), UserRole.USER)) .isEmpty(); assertThat(underTest.keepAuthorizedLoginsOnProject(dbSession, newHashSet(user1.getLogin()), project.getKey(), UserRole.USER)) .containsOnly(user1.getLogin()); assertThat(underTest.keepAuthorizedLoginsOnProject(dbSession, allLogins, project.getKey(), UserRole.USER)) .containsOnly(user1.getLogin(), user2.getLogin()); assertThat(underTest.keepAuthorizedLoginsOnProject(dbSession, allLogins, project.getKey(), UserRole.ADMIN)) .containsOnly(admin1.getLogin(), admin2.getLogin());
private Set<String> keepAuthorizedLogins(DbSession dbSession, String projectKey, Set<SubscriberAndChannel> subscriberAndChannels, @Nullable Boolean global, String permission) { Set<String> logins = subscriberAndChannels.stream() .filter(s -> global == null || s.getSubscriber().isGlobal() == global) .map(s -> s.getSubscriber().getLogin()) .collect(Collectors.toSet()); if (logins.isEmpty()) { return Collections.emptySet(); } return dbClient.authorizationDao().keepAuthorizedLoginsOnProject(dbSession, logins, projectKey, permission); }