diff --git a/src/main/java/org/dependencytrack/resources/v1/NotificationPublisherResource.java b/src/main/java/org/dependencytrack/resources/v1/NotificationPublisherResource.java index 105c660c96..65217dc099 100644 --- a/src/main/java/org/dependencytrack/resources/v1/NotificationPublisherResource.java +++ b/src/main/java/org/dependencytrack/resources/v1/NotificationPublisherResource.java @@ -340,7 +340,7 @@ public Response testSmtpPublisherConfig(@FormParam("destination") String destina @ApiResponse(responseCode = "401", description = "Unauthorized") }) @PermissionRequired(Permissions.Constants.SYSTEM_CONFIGURATION) - public Response testSlackPublisherConfig( + public Response testRuleConfiguration ( @Parameter(description = "The UUID of the rule to test", schema = @Schema(type = "string", format = "uuid"), required = true) @PathParam("uuid") @ValidUuid String ruleUuid) throws Exception { try(QueryManager qm = new QueryManager()){ @@ -353,7 +353,9 @@ public Response testSlackPublisherConfig( JsonObject configObject = jsonReader.readObject(); jsonReader.close(); final JsonObject config = Json.createObjectBuilder() - .add(Publisher.CONFIG_DESTINATION, configObject.getString("destination")) + .add(Publisher.CONFIG_DESTINATION, configObject.containsKey("destination") && + !configObject.isNull("destination") ? configObject.getString("destination") : "") + .add(Publisher.CONFIG_TEMPLATE_KEY, rule.getPublisher().getTemplate()) .add(Publisher.CONFIG_TEMPLATE_MIME_TYPE_KEY, rule.getPublisher().getTemplateMimeType()) .build(); @@ -366,7 +368,12 @@ public Response testSlackPublisherConfig( .content("Rule configuration test") .level(rule.getNotificationLevel()) .subject(NotificationUtil.generateSubject(group.toString())); - publisher.inform(PublishContext.from(notification), notification, config); + if(SendMailPublisher.class.isAssignableFrom(publisherClass) && rule.getTeams() != null && rule.getTeams().size() > 0){ + SendMailPublisher sendMailPublisher = (SendMailPublisher) publisherClass.getDeclaredConstructor().newInstance(); + sendMailPublisher.inform(PublishContext.from(notification), notification, config, rule.getTeams()); + }else { + publisher.inform(PublishContext.from(notification), notification, config); + } } return Response.ok().build(); } catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) { diff --git a/src/test/java/org/dependencytrack/resources/v1/NotificationPublisherResourceTest.java b/src/test/java/org/dependencytrack/resources/v1/NotificationPublisherResourceTest.java index 677303f372..991d9f4081 100644 --- a/src/test/java/org/dependencytrack/resources/v1/NotificationPublisherResourceTest.java +++ b/src/test/java/org/dependencytrack/resources/v1/NotificationPublisherResourceTest.java @@ -18,7 +18,9 @@ */ package org.dependencytrack.resources.v1; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.UUID; @@ -41,6 +43,8 @@ import org.junit.Test; import alpine.common.util.UuidUtil; +import alpine.model.ManagedUser; +import alpine.model.Team; import alpine.notification.NotificationLevel; import alpine.server.filters.ApiFilter; import alpine.server.filters.AuthenticationFilter; @@ -368,6 +372,40 @@ public void testNotificationRuleTest() { Assert.assertEquals(200, sendMailResponse.getStatus()); } + @Test + public void testEmailNotificationRuleTest() { + NotificationPublisher publisher = qm.createNotificationPublisher( + "Example Publisher", "Publisher description", + SendMailPublisher.class, "template", "text/html", + false); + + NotificationRule rule = qm.createNotificationRule("Example Rule 1", NotificationScope.PORTFOLIO, NotificationLevel.INFORMATIONAL, publisher); + + List teams = new ArrayList<>(); + Team team = new Team(); + List managedUsers = new ArrayList<>(); + ManagedUser managedUser = new ManagedUser(); + managedUser.setEmail("test@test.com"); + managedUsers.add(managedUser); + team.setManagedUsers(managedUsers); + teams.add(team); + rule.setTeams(teams); + + Set groups = new HashSet<>(Set.of(NotificationGroup.BOM_CONSUMED, NotificationGroup.BOM_PROCESSED, NotificationGroup.BOM_PROCESSING_FAILED, + NotificationGroup.BOM_VALIDATION_FAILED, NotificationGroup.NEW_VULNERABILITY, NotificationGroup.NEW_VULNERABLE_DEPENDENCY, + NotificationGroup.POLICY_VIOLATION, NotificationGroup.PROJECT_CREATED, NotificationGroup.PROJECT_AUDIT_CHANGE, + NotificationGroup.VEX_CONSUMED, NotificationGroup.VEX_PROCESSED)); + rule.setNotifyOn(groups); + + rule.setPublisherConfig("{}"); + + Response sendMailResponse = jersey.target(V1_NOTIFICATION_PUBLISHER + "/test/" + rule.getUuid()).request() + .header(X_API_KEY, apiKey) + .post(Entity.entity("", MediaType.APPLICATION_FORM_URLENCODED_TYPE)); + + Assert.assertEquals(200, sendMailResponse.getStatus()); + } + @Test public void restoreDefaultTemplatesTest() { NotificationPublisher slackPublisher = qm.getDefaultNotificationPublisher(DefaultNotificationPublishers.SLACK.getPublisherClass());