PutRoleMembershipNotificationTask implements NotificationTask { @Override public List<Notification> getNotifications() { Set<String> recipients = new HashSet<>(); if (role.getAuditEnabled() == Boolean.TRUE) { recipients.add(ZMSUtils.roleResourceName(ZMSConsts.SYS_AUTH_AUDIT_BY_DOMAIN, domain)); recipients.add(ZMSUtils.roleResourceName(ZMSConsts.SYS_AUTH_AUDIT_BY_ORG, org)); } else { final String notifyRoles = role.getNotifyRoles(); if (notifyRoles == null || notifyRoles.isEmpty()) { recipients.add(ZMSUtils.roleResourceName(domain, ZMSConsts.ADMIN_ROLE_NAME)); } else { Iterable<String> roleNames = Splitter.on(',') .omitEmptyStrings() .trimResults() .split(notifyRoles); for (String roleName : roleNames) { if (roleName.indexOf(AuthorityConsts.ROLE_SEP) == -1) { recipients.add(ZMSUtils.roleResourceName(domain, roleName)); } else { recipients.add(roleName); } } } } return Collections.singletonList(notificationCommon.createNotification( recipients, details, putMembershipNotificationToEmailConverter, NOTIFICATION_TYPE)); } PutRoleMembershipNotificationTask(String domain, String org, Role role, Map<String, String> details, DBService dbService, String userDomainPrefix); @Override List<Notification> getNotifications(); @Override String getDescription();  }
@Test public void testGenerateAndSendPostPutMembershipNotification() { DBService dbsvc = Mockito.mock(DBService.class); NotificationService mockNotificationService = Mockito.mock(NotificationService.class); NotificationServiceFactory testfact = () -> mockNotificationService; NotificationManager notificationManager = getNotificationManager(dbsvc, testfact); notificationManager.shutdown(); Map<String, String> details = new HashMap<>(); details.put("domain", "testdomain1"); details.put("role", "role1"); List<RoleMember> roleMembers = new ArrayList<>(); RoleMember rm = new RoleMember().setMemberName("user.domapprover1").setActive(true); roleMembers.add(rm); rm = new RoleMember().setMemberName("user.domapprover2").setActive(true); roleMembers.add(rm); rm = new RoleMember().setMemberName("dom2.testsvc1").setActive(true); roleMembers.add(rm); Role domainRole = new Role().setName("sys.auth.audit.domain:role.testdomain1").setRoleMembers(roleMembers); roleMembers = new ArrayList<>(); rm = new RoleMember().setMemberName("user.orgapprover1").setActive(true); roleMembers.add(rm); rm = new RoleMember().setMemberName("user.orgapprover2").setActive(true); roleMembers.add(rm); rm = new RoleMember().setMemberName("dom2.testsvc1").setActive(true); roleMembers.add(rm); Role orgRole = new Role().setName("sys.auth.audit.org:role.neworg").setRoleMembers(roleMembers); List<Role> roles1 = new ArrayList<>(); roles1.add(orgRole); AthenzDomain athenzDomain1 = new AthenzDomain("sys.auth.audit.org"); athenzDomain1.setRoles(roles1); List<Role> roles2 = new ArrayList<>(); roles2.add(domainRole); AthenzDomain athenzDomain2 = new AthenzDomain("sys.auth.audit.domain"); athenzDomain2.setRoles(roles2); Mockito.when(dbsvc.getRolesByDomain("sys.auth.audit.org")).thenReturn(athenzDomain1.getRoles()); Mockito.when(dbsvc.getRolesByDomain("sys.auth.audit.domain")).thenReturn(athenzDomain2.getRoles()); ArgumentCaptor<Notification> captor = ArgumentCaptor.forClass(Notification.class); Role notifyRole = new Role().setAuditEnabled(true).setSelfServe(false); List<Notification> notifications = new PutRoleMembershipNotificationTask("testdomain1", "neworg", notifyRole, details, dbsvc, USER_DOMAIN_PREFIX).getNotifications(); notificationManager.sendNotifications(notifications); Notification notification = new Notification(); notification.addRecipient("user.domapprover1") .addRecipient("user.domapprover2") .addRecipient("user.orgapprover1") .addRecipient("user.orgapprover2"); notification.addDetails("domain", "testdomain1").addDetails("role", "role1"); PutRoleMembershipNotificationTask.PutMembershipNotificationToEmailConverter converter = new PutRoleMembershipNotificationTask.PutMembershipNotificationToEmailConverter(); notification.setNotificationToEmailConverter(converter); notification.setType("role_membership_approval"); Mockito.verify(mockNotificationService, atLeastOnce()).notify(captor.capture()); Notification actualNotification = captor.getValue(); assertEquals(actualNotification, notification); }
@Test public void testGenerateAndSendPostPutMembershipNotificationNullDomainRole() { DBService dbsvc = Mockito.mock(DBService.class); NotificationService mockNotificationService = Mockito.mock(NotificationService.class); NotificationServiceFactory testfact = () -> mockNotificationService; NotificationManager notificationManager = getNotificationManager(dbsvc, testfact); notificationManager.shutdown(); Map<String, String> details = new HashMap<>(); details.put("domain", "testdomain1"); details.put("role", "role1"); List<RoleMember> roleMembers = new ArrayList<>(); RoleMember rm = new RoleMember().setMemberName("user.orgapprover1").setActive(true); roleMembers.add(rm); rm = new RoleMember().setMemberName("user.orgapprover2").setActive(true); roleMembers.add(rm); rm = new RoleMember().setMemberName("dom2.testsvc1").setActive(true); roleMembers.add(rm); Role orgRole = new Role().setName("sys.auth.audit.org:role.neworg").setRoleMembers(roleMembers); List<Role> roles = new ArrayList<>(); roles.add(orgRole); AthenzDomain athenzDomain = new AthenzDomain("sys.auth.audit.org"); athenzDomain.setRoles(roles); Mockito.when(dbsvc.getRolesByDomain("sys.auth.audit.org")).thenReturn(athenzDomain.getRoles()); ArgumentCaptor<Notification> captor = ArgumentCaptor.forClass(Notification.class); Role notifyRole = new Role().setAuditEnabled(true).setSelfServe(false); List<Notification> notifications = new PutRoleMembershipNotificationTask("testdomain1", "neworg", notifyRole, details, dbsvc, USER_DOMAIN_PREFIX).getNotifications(); notificationManager.sendNotifications(notifications); Notification notification = new Notification(); notification .addRecipient("user.orgapprover1") .addRecipient("user.orgapprover2"); notification.addDetails("domain", "testdomain1").addDetails("role", "role1"); PutRoleMembershipNotificationTask.PutMembershipNotificationToEmailConverter converter = new PutRoleMembershipNotificationTask.PutMembershipNotificationToEmailConverter(); notification.setNotificationToEmailConverter(converter); notification.setType("role_membership_approval"); Mockito.verify(mockNotificationService, atLeastOnce()).notify(captor.capture()); Notification actualNotification = captor.getValue(); assertEquals(actualNotification, notification); }
@Test public void testGenerateAndSendPostPutMembershipNotificationNullOrgRole() { DBService dbsvc = Mockito.mock(DBService.class); NotificationService mockNotificationService = Mockito.mock(NotificationService.class); NotificationServiceFactory testfact = () -> mockNotificationService; NotificationManager notificationManager = getNotificationManager(dbsvc, testfact); notificationManager.shutdown(); Map<String, String> details = new HashMap<>(); details.put("domain", "testdomain1"); details.put("role", "role1"); List<RoleMember> roleMembers = new ArrayList<>(); RoleMember rm = new RoleMember().setMemberName("user.domapprover1").setActive(true); roleMembers.add(rm); rm = new RoleMember().setMemberName("user.domapprover2").setActive(true); roleMembers.add(rm); rm = new RoleMember().setMemberName("dom2.testsvc1").setActive(true); roleMembers.add(rm); Role domainRole = new Role().setName("sys.auth.audit.domain:role.testdomain1").setRoleMembers(roleMembers); List<Role> roles = new ArrayList<>(); roles.add(domainRole); AthenzDomain athenzDomain = new AthenzDomain("sys.auth.audit.domain"); athenzDomain.setRoles(roles); Mockito.when(dbsvc.getRolesByDomain("sys.auth.audit.domain")).thenReturn(athenzDomain.getRoles()); ArgumentCaptor<Notification> captor = ArgumentCaptor.forClass(Notification.class); Role notifyRole = new Role().setAuditEnabled(true).setSelfServe(false); List<Notification> notifications = new PutRoleMembershipNotificationTask("testdomain1", "neworg", notifyRole, details, dbsvc, USER_DOMAIN_PREFIX).getNotifications(); notificationManager.sendNotifications(notifications); Notification notification = new Notification(); notification .addRecipient("user.domapprover1") .addRecipient("user.domapprover2"); notification.addDetails("domain", "testdomain1").addDetails("role", "role1"); PutRoleMembershipNotificationTask.PutMembershipNotificationToEmailConverter converter = new PutRoleMembershipNotificationTask.PutMembershipNotificationToEmailConverter(); notification.setNotificationToEmailConverter(converter); notification.setType("role_membership_approval"); Mockito.verify(mockNotificationService, atLeastOnce()).notify(captor.capture()); Notification actualNotification = captor.getValue(); assertEquals(actualNotification, notification); }
@Test public void testGenerateAndSendPostPutMembershipNotificationSelfserve() { DBService dbsvc = Mockito.mock(DBService.class); NotificationService mockNotificationService = Mockito.mock(NotificationService.class); NotificationServiceFactory testfact = () -> mockNotificationService; NotificationManager notificationManager = getNotificationManager(dbsvc, testfact); notificationManager.shutdown(); Map<String, String> details = new HashMap<>(); details.put("domain", "testdomain1"); details.put("role", "role1"); List<RoleMember> roleMembers = new ArrayList<>(); RoleMember rm = new RoleMember().setMemberName("user.domadmin1").setActive(true); roleMembers.add(rm); rm = new RoleMember().setMemberName("user.domadmin2").setActive(true); roleMembers.add(rm); rm = new RoleMember().setMemberName("dom2.testsvc1").setActive(true); roleMembers.add(rm); Role adminRole = new Role().setName("testdomain1:role.admin").setRoleMembers(roleMembers); List<Role> roles = new ArrayList<>(); roles.add(adminRole); AthenzDomain athenzDomain = new AthenzDomain("testdomain1"); athenzDomain.setRoles(roles); Mockito.when(dbsvc.getRolesByDomain("testdomain1")).thenReturn(athenzDomain.getRoles()); ArgumentCaptor<Notification> captor = ArgumentCaptor.forClass(Notification.class); Role notifyRole = new Role().setAuditEnabled(false).setSelfServe(true); List<Notification> notifications = new PutRoleMembershipNotificationTask("testdomain1", "neworg", notifyRole, details, dbsvc, USER_DOMAIN_PREFIX).getNotifications(); notificationManager.sendNotifications(notifications); Notification notification = new Notification(); notification .addRecipient("user.domadmin1") .addRecipient("user.domadmin2"); notification.addDetails("domain", "testdomain1").addDetails("role", "role1"); PutRoleMembershipNotificationTask.PutMembershipNotificationToEmailConverter converter = new PutRoleMembershipNotificationTask.PutMembershipNotificationToEmailConverter(); notification.setNotificationToEmailConverter(converter); notification.setType("role_membership_approval"); Mockito.verify(mockNotificationService, atLeastOnce()).notify(captor.capture()); Notification actualNotification = captor.getValue(); assertEquals(actualNotification, notification); }
@Test public void testGenerateAndSendPostPutMembershipNotificationNotifyRoles() { DBService dbsvc = Mockito.mock(DBService.class); NotificationService mockNotificationService = Mockito.mock(NotificationService.class); NotificationServiceFactory testfact = () -> mockNotificationService; NotificationManager notificationManager = getNotificationManager(dbsvc, testfact); notificationManager.shutdown(); Map<String, String> details = new HashMap<>(); details.put("domain", "testdomain1"); details.put("role", "role1"); List<RoleMember> roleMembers = new ArrayList<>(); RoleMember rm = new RoleMember().setMemberName("user.domapprover1").setActive(true); roleMembers.add(rm); rm = new RoleMember().setMemberName("user.domapprover2").setActive(true); roleMembers.add(rm); rm = new RoleMember().setMemberName("dom2.testsvc1").setActive(true); roleMembers.add(rm); Role domainRole = new Role().setName("athenz:role.approvers").setRoleMembers(roleMembers); roleMembers = new ArrayList<>(); rm = new RoleMember().setMemberName("user.approver1").setActive(true); roleMembers.add(rm); rm = new RoleMember().setMemberName("user.approver2").setActive(true); roleMembers.add(rm); rm = new RoleMember().setMemberName("dom2.testsvc1").setActive(true); roleMembers.add(rm); Role localRole = new Role().setName("testdomain1:role.notify").setRoleMembers(roleMembers); List<Role> roles1 = new ArrayList<>(); roles1.add(localRole); AthenzDomain athenzDomain1 = new AthenzDomain("coretech"); athenzDomain1.setRoles(roles1); List<Role> roles2 = new ArrayList<>(); roles2.add(domainRole); AthenzDomain athenzDomain2 = new AthenzDomain("athenz"); athenzDomain2.setRoles(roles2); Mockito.when(dbsvc.getRolesByDomain("testdomain1")).thenReturn(athenzDomain1.getRoles()); Mockito.when(dbsvc.getRolesByDomain("athenz")).thenReturn(athenzDomain2.getRoles()); ArgumentCaptor<Notification> captor = ArgumentCaptor.forClass(Notification.class); Role notifyRole = new Role().setAuditEnabled(false).setSelfServe(false).setReviewEnabled(true) .setNotifyRoles("athenz:role.approvers,notify"); List<Notification> notifications = new PutRoleMembershipNotificationTask("testdomain1", "neworg", notifyRole, details, dbsvc, USER_DOMAIN_PREFIX).getNotifications(); notificationManager.sendNotifications(notifications); Notification notification = new Notification(); notification.addRecipient("user.domapprover1") .addRecipient("user.domapprover2") .addRecipient("user.approver1") .addRecipient("user.approver2"); notification.addDetails("domain", "testdomain1").addDetails("role", "role1"); PutRoleMembershipNotificationTask.PutMembershipNotificationToEmailConverter converter = new PutRoleMembershipNotificationTask.PutMembershipNotificationToEmailConverter(); notification.setNotificationToEmailConverter(converter); notification.setType("role_membership_approval"); Mockito.verify(mockNotificationService, atLeastOnce()).notify(captor.capture()); Notification actualNotification = captor.getValue(); assertEquals(actualNotification, notification); assertEquals(actualNotification, notification); }
@Test public void testGenerateAndSendPostPutMembershipNotificationInvalidType() { DBService dbsvc = Mockito.mock(DBService.class); Mockito.when(dbsvc.getPendingMembershipApproverRoles(1)).thenReturn(Collections.emptySet()); NotificationService mockNotificationService = Mockito.mock(NotificationService.class); NotificationServiceFactory testfact = () -> mockNotificationService; NotificationManager notificationManager = getNotificationManager(dbsvc, testfact); notificationManager.shutdown(); Role notifyRole = new Role().setAuditEnabled(false).setSelfServe(false); List<Notification> notifications = new PutRoleMembershipNotificationTask("testdomain1", "neworg", notifyRole, null, dbsvc, USER_DOMAIN_PREFIX).getNotifications(); notificationManager.sendNotifications(notifications); Mockito.verify(mockNotificationService, times(0)).notify(any()); }
@Test public void testGenerateAndSendPostPutMembershipNotificationNullNotificationSvc() { DBService dbsvc = Mockito.mock(DBService.class); Mockito.when(dbsvc.getPendingMembershipApproverRoles(1)).thenReturn(Collections.emptySet()); NotificationServiceFactory testfact = () -> null; NotificationService mockNotificationService = Mockito.mock(NotificationService.class); NotificationManager notificationManager = getNotificationManager(dbsvc, testfact); notificationManager.shutdown(); Role notifyRole = new Role().setAuditEnabled(false).setSelfServe(false); List<Notification> notifications = new PutRoleMembershipNotificationTask("testdomain1", "neworg", notifyRole, null, dbsvc, USER_DOMAIN_PREFIX).getNotifications(); notificationManager.sendNotifications(notifications); verify(mockNotificationService, never()).notify(any(Notification.class)); }