Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification rule test did not account for teams when using email publisher #4316

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()){
Expand All @@ -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();
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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<Team> teams = new ArrayList<>();
Team team = new Team();
List<ManagedUser> managedUsers = new ArrayList<>();
ManagedUser managedUser = new ManagedUser();
managedUser.setEmail("[email protected]");
managedUsers.add(managedUser);
team.setManagedUsers(managedUsers);
teams.add(team);
rule.setTeams(teams);

Set<NotificationGroup> 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());
Expand Down
Loading