use of com.yahoo.athenz.common.server.notification.Notification in project athenz by yahoo.
the class AWSZTSHealthNotificationTaskTest method testGetEmailBody.
@Test
public void testGetEmailBody() {
System.setProperty("athenz.notification_workflow_url", "https://athenz.example.com/workflow");
System.setProperty("athenz.notification_support_text", "#Athenz slack channel");
System.setProperty("athenz.notification_support_url", "https://link.to.athenz.channel.com");
Map<String, String> details = new HashMap<>();
details.put(NOTIFICATION_DETAILS_AFFECTED_ZTS, "affected zts");
details.put(NOTIFICATION_DETAILS_AWS_ZTS_HEALTH, "zts.url;domain0;role0;Sun Mar 15 15:08:07 IST 2020;Error message");
Notification notification = new Notification();
notification.setDetails(details);
AWSZTSHealthNotificationTask.AWSZTSHealthNotificationToEmailConverter converter = new AWSZTSHealthNotificationTask.AWSZTSHealthNotificationToEmailConverter(new NotificationToEmailConverterCommon(null));
NotificationEmail notificationAsEmail = converter.getNotificationAsEmail(notification);
String body = notificationAsEmail.getBody();
assertNotNull(body);
assertTrue(body.contains("zts.url"));
assertTrue(body.contains("domain0"));
assertTrue(body.contains("role0"));
assertTrue(body.contains("Sun Mar 15 15:08:07 IST 2020"));
assertTrue(body.contains("Error message"));
// Make sure support text and url do appear
assertTrue(body.contains("slack"));
assertTrue(body.contains("link.to.athenz.channel.com"));
System.clearProperty("athenz.notification_workflow_url");
System.clearProperty("notification_support_text");
System.clearProperty("notification_support_url");
}
use of com.yahoo.athenz.common.server.notification.Notification in project athenz by yahoo.
the class AWSZTSHealthNotificationTaskTest method testGetNotifications.
@Test
public void testGetNotifications() {
System.setProperty(ZTSConsts.ZTS_PROP_NOTIFICATION_AWS_HEALTH_DOMAIN, "testDomain");
ZTSClientNotification clientNotification = new ZTSClientNotification("zts.url", "role", "AWS", 1592346376, false, "testDomain");
RoleMember roleMember1 = new RoleMember();
roleMember1.setMemberName("user.test1");
RoleMember roleMember2 = new RoleMember();
roleMember2.setMemberName("user.test2");
List<RoleMember> roleMembers = new ArrayList<>();
roleMembers.add(roleMember1);
roleMembers.add(roleMember2);
Role adminRole = new Role();
adminRole.setRoleMembers(roleMembers);
adminRole.setName("testDomain:role.admin");
List<Role> roles = new ArrayList<>();
roles.add(adminRole);
when(dataStore.getRolesByDomain(eq("testDomain"))).thenReturn(roles);
AWSZTSHealthNotificationTask awsztsHealthNotificationTask = new AWSZTSHealthNotificationTask(clientNotification, dataStore, userDomainPrefix, serverName, notificationToEmailConverterCommon);
List<Notification> notifications = awsztsHealthNotificationTask.getNotifications();
assertEquals(1, notifications.size());
assertTrue(notifications.get(0).getRecipients().contains("user.test1"));
assertTrue(notifications.get(0).getRecipients().contains("user.test2"));
Timestamp expiration = Timestamp.fromMillis(clientNotification.getExpiration() * 1000);
assertEquals("zts.url;testDomain;role;" + expiration + ";Fail to get token of type AWS. ", notifications.get(0).getDetails().get("awsZtsHealth"));
assertEquals("testServer", notifications.get(0).getDetails().get("affectedZts"));
System.clearProperty(ZTSConsts.ZTS_PROP_NOTIFICATION_AWS_HEALTH_DOMAIN);
}
use of com.yahoo.athenz.common.server.notification.Notification in project athenz by yahoo.
the class MetricNotificationServiceTest method testNotify.
@Test
public void testNotify() {
Metric metric = Mockito.mock(Metric.class);
MetricNotificationService metricNotificationService = new MetricNotificationService(metric);
String[] attributesList1 = new String[] { "key1", "attribute11", "key2", "attribute12", "key3", "attribute13" };
String[] attributesList2 = new String[] { "key1", "attribute21", "key2", "attribute22", "key3", "attribute23" };
List<String[]> attributes = new ArrayList<>();
attributes.add(attributesList1);
attributes.add(attributesList2);
NotificationToMetricConverter notificationToMetricConverter = Mockito.mock(NotificationToMetricConverter.class);
Mockito.when(notificationToMetricConverter.getNotificationAsMetrics(Mockito.any(), Mockito.any())).thenReturn(new NotificationMetric(attributes));
Notification notification = new Notification();
notification.setNotificationToMetricConverter(notificationToMetricConverter);
boolean notify = metricNotificationService.notify(notification);
assertTrue(notify);
ArgumentCaptor<String> captorMetric = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String[]> captorAttributes = ArgumentCaptor.forClass(String[].class);
Mockito.verify(metric, Mockito.times(2)).increment(captorMetric.capture(), captorAttributes.capture());
assertEquals(2, captorMetric.getAllValues().size());
assertEquals("athenz_notification", captorMetric.getAllValues().get(0));
assertEquals("athenz_notification", captorMetric.getAllValues().get(1));
// Mockito captures all varargs arguments in a single array
assertEquals(12, captorAttributes.getAllValues().size());
List<String> expectedAttributes = new ArrayList<String>(Arrays.asList("key1", "attribute11", "key2", "attribute12", "key3", "attribute13", "key1", "attribute21", "key2", "attribute22", "key3", "attribute23"));
assertEquals(expectedAttributes, captorAttributes.getAllValues());
}
use of com.yahoo.athenz.common.server.notification.Notification in project athenz by yahoo.
the class RoleMemberNotificationCommonTest method testExpiryPrincipalGetNotificationDetails.
@Test
public void testExpiryPrincipalGetNotificationDetails() {
DBService dbsvc = Mockito.mock(DBService.class);
RoleMemberNotificationCommon roleMemberNotificationCommon = new RoleMemberNotificationCommon(dbsvc, USER_DOMAIN_PREFIX);
NotificationToEmailConverterCommon notificationToEmailConverterCommon = new NotificationToEmailConverterCommon(null);
// Verify no notification for member without member roles
DomainRoleMember roleMember = new DomainRoleMember();
roleMember.setMemberName("user.joe");
Map<String, DomainRoleMember> members = new HashMap<>();
members.put("user.joe", roleMember);
List<Notification> notification = roleMemberNotificationCommon.getNotificationDetails(members, new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.ExpiryRoleMemberDetailStringer(), new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToMetricConverter(), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToMetricConverter(), memberRole -> DisableNotificationEnum.getEnumSet(0));
assertEquals(0, notification.size());
// Verify the same result when setting the memberRoles to an empty collection
roleMember.setMemberRoles(Collections.emptyList());
notification = roleMemberNotificationCommon.getNotificationDetails(members, new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.ExpiryRoleMemberDetailStringer(), new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToMetricConverter(), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToMetricConverter(), memberRole -> DisableNotificationEnum.getEnumSet(0));
assertEquals(0, notification.size());
final Timestamp expirationTs = Timestamp.fromMillis(100);
final Timestamp reviewTs = Timestamp.fromMillis(50);
List<MemberRole> memberRoles = new ArrayList<>();
memberRoles.add(new MemberRole().setRoleName("role1").setDomainName("athenz1").setMemberName("user.joe").setExpiration(expirationTs).setReviewReminder(reviewTs));
roleMember.setMemberRoles(memberRoles);
notification = roleMemberNotificationCommon.getNotificationDetails(members, new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.ExpiryRoleMemberDetailStringer(), new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToMetricConverter(), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToMetricConverter(), memberRole -> DisableNotificationEnum.getEnumSet(0));
assertEquals(1, notification.size());
assertEquals(2, notification.get(0).getDetails().size());
assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_ROLES_LIST), "athenz1;role1;" + expirationTs);
assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_MEMBER), "user.joe");
memberRoles.add(new MemberRole().setRoleName("role1").setDomainName("athenz2").setMemberName("user.joe").setExpiration(expirationTs).setReviewReminder(reviewTs));
memberRoles.add(new MemberRole().setRoleName("role2").setDomainName("athenz2").setMemberName("user.joe").setExpiration(expirationTs).setReviewReminder(reviewTs));
notification = roleMemberNotificationCommon.getNotificationDetails(members, new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.ExpiryRoleMemberDetailStringer(), new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToMetricConverter(), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToMetricConverter(), memberRole -> DisableNotificationEnum.getEnumSet(0));
assertEquals(1, notification.size());
assertEquals(2, notification.get(0).getDetails().size());
assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_ROLES_LIST), "athenz1;role1;" + expirationTs + "|athenz2;role1;" + expirationTs + "|athenz2;role2;" + expirationTs);
assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_MEMBER), "user.joe");
assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_ROLES_LIST), "athenz1;role1;" + expirationTs + "|athenz2;role1;" + expirationTs + "|athenz2;role2;" + expirationTs);
}
use of com.yahoo.athenz.common.server.notification.Notification in project athenz by yahoo.
the class ZMSNotificationsTest method testGroupExpiryNotification.
@Test
public void testGroupExpiryNotification() {
TopLevelDomain dom1 = zmsTestInitializer.createTopLevelDomainObject("test-domain1", "Test Domain1", "testOrg", zmsTestInitializer.getAdminUser());
zmsTestInitializer.getZms().postTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), zmsTestInitializer.getAuditRef(), dom1);
try {
long currentTimeMillis = System.currentTimeMillis();
Timestamp currentTimestamp = Timestamp.fromMillis(currentTimeMillis);
List<GroupMember> groupMembers = generateGroupMembers(currentTimeMillis);
Group group1 = zmsTestInitializer.createGroupObject("test-domain1", "Group1", groupMembers);
zmsTestInitializer.getZms().putGroup(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", "Group1", zmsTestInitializer.getAuditRef(), group1);
GroupMemberExpiryNotificationTask groupMemberExpiryNotificationTask = new GroupMemberExpiryNotificationTask(zmsTestInitializer.getZms().dbService, zmsTestInitializer.getZms().userDomainPrefix, zmsTestInitializer.getZms().notificationToEmailConverterCommon);
List<Notification> notifications = groupMemberExpiryNotificationTask.getNotifications();
// Email notifications should be sent every 7 days
Set<String> emailNotificationMembers = new HashSet<>(Arrays.asList(new String[] { "user.expireddays0", "user.expireddays1", "user.expireddays7", "user.expireddays14", "user.expireddays21", "user.expireddays28" }));
assertEquals(notifications.size(), 7, "notificationRecipients: " + notificationsToRecipientString(notifications));
for (Notification notification : notifications) {
String recipient = notification.getRecipients().stream().findFirst().get();
if (recipient.equals("user.testadminuser")) {
verifyAdminNotifications(emailNotificationMembers, notification);
} else {
if (emailNotificationMembers.contains(recipient)) {
assertNotNull(notification.getNotificationAsEmail());
} else {
assertNull(notification.getNotificationAsEmail());
}
assertNotNull(notification.getNotificationAsMetrics(currentTimestamp));
}
}
} finally {
zmsTestInitializer.getZms().deleteTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", zmsTestInitializer.getAuditRef());
}
}
Aggregations