forked from RedHatInsights/notifications-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RHCLOUD-29047 | feature: specify sender and default recipient (RedHa…
…tInsights#2328) Co-authored-by: Gwenneg Lepage <[email protected]>
- Loading branch information
1 parent
299199b
commit 927c01e
Showing
13 changed files
with
123 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 8 additions & 2 deletions
10
...main/java/com/redhat/cloud/notifications/connector/email/model/bop/SendEmailsRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
package com.redhat.cloud.notifications.connector.email.model.bop; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY; | ||
|
||
@JsonAutoDetect(fieldVisibility = ANY) | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class SendEmailsRequest { | ||
|
||
private final Set<Email> emails = new HashSet<>(); | ||
private final boolean skipUsersResolution = true; | ||
private final String emailSender; | ||
private final String defaultRecipient; | ||
|
||
public void addEmail(Email email) { | ||
emails.add(email); | ||
public SendEmailsRequest(final Set<Email> emails, final String emailSender, final String defaultRecipient) { | ||
this.emails.addAll(emails); | ||
this.emailSender = emailSender; | ||
this.defaultRecipient = defaultRecipient; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
import java.util.UUID; | ||
|
||
import static com.redhat.cloud.notifications.connector.email.constants.ExchangeProperty.EMAIL_RECIPIENTS; | ||
import static com.redhat.cloud.notifications.connector.email.constants.ExchangeProperty.EMAIL_SENDER; | ||
import static com.redhat.cloud.notifications.connector.email.constants.ExchangeProperty.RECIPIENT_SETTINGS; | ||
import static com.redhat.cloud.notifications.connector.email.constants.ExchangeProperty.RENDERED_BODY; | ||
import static com.redhat.cloud.notifications.connector.email.constants.ExchangeProperty.RENDERED_SUBJECT; | ||
|
@@ -73,13 +74,16 @@ void testExtract() { | |
final String emailBody = "fake email body"; | ||
final String emailSubject = "fake email subject"; | ||
|
||
final String emailSender = "\"Red Hat Insights\" [email protected]"; | ||
|
||
// Prepare the JSON object. | ||
final JsonObject payload = new JsonObject(); | ||
payload.put("recipient_settings", recipientSettingsList); | ||
payload.put("subscribers", subscribers); | ||
payload.put("unsubscribers", unsubscribers); | ||
payload.put("email_body", emailBody); | ||
payload.put("email_subject", emailSubject); | ||
payload.put("email_sender", emailSender); | ||
payload.put("subscribed_by_default", true); | ||
|
||
final Exchange exchange = this.createExchangeWithBody(""); | ||
|
@@ -96,6 +100,7 @@ void testExtract() { | |
assertEquals(subscribers, exchange.getProperty(SUBSCRIBERS, Set.class)); | ||
assertEquals(unsubscribers, exchange.getProperty(UNSUBSCRIBERS, Set.class)); | ||
assertEquals(Set.of("[email protected]", "[email protected]", "[email protected]"), exchange.getProperty(EMAIL_RECIPIENTS, Set.class)); | ||
assertEquals(emailSender, exchange.getProperty(EMAIL_SENDER, String.class)); | ||
assertTrue(exchange.getProperty(SUBSCRIBED_BY_DEFAULT, boolean.class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,13 +77,13 @@ void testProcess(boolean skipBopUsersResolution) { | |
if (skipBopUsersResolution) { | ||
assertEquals(5, actualEmail.getJsonArray("bccList").size()); | ||
assertTrue(Set.of("a-email", "b-email", "c-email", "[email protected]", "[email protected]").stream() | ||
.allMatch(bcc -> actualEmail.getJsonArray("bccList").contains(bcc))); | ||
.allMatch(bcc -> actualEmail.getJsonArray("bccList").contains(bcc))); | ||
} else { | ||
assertEquals(3, actualEmail.getJsonArray("bccList").size()); | ||
assertTrue(Set.of("a", "b", "c").stream() | ||
.allMatch(bcc -> actualEmail.getJsonArray("bccList").contains(bcc))); | ||
.allMatch(bcc -> actualEmail.getJsonArray("bccList").contains(bcc))); | ||
assertTrue(Set.of("[email protected]", "[email protected]").stream() | ||
.noneMatch(bcc -> actualEmail.getJsonArray("bccList").contains(bcc))); | ||
.noneMatch(bcc -> actualEmail.getJsonArray("bccList").contains(bcc))); | ||
} | ||
assertEquals("html", actualEmail.getString("bodyType")); | ||
if (skipBopUsersResolution) { | ||
|
25 changes: 25 additions & 0 deletions
25
...ne/src/main/java/com/redhat/cloud/notifications/processors/email/EmailActorsResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.redhat.cloud.notifications.processors.email; | ||
|
||
import com.redhat.cloud.notifications.models.Event; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
@ApplicationScoped | ||
public class EmailActorsResolver { | ||
/** | ||
* Standard "Red Hat Insights" sender that the vast majority of the | ||
* ConsoleDot applications will use. | ||
*/ | ||
public static final String RH_INSIGHTS_SENDER = "\"Red Hat Insights\" [email protected]"; | ||
|
||
/** | ||
* Determines which sender should be set in the email from the given event. | ||
* When sending emails we will use the sender for both the sender itself | ||
* and the default recipient —the one that appears in the "to" field—. | ||
* @param event the event to determine the sender and the default | ||
* recipients from. | ||
* @return the sender that should be used for the given event. | ||
*/ | ||
public String getEmailSender(final Event event) { | ||
return RH_INSIGHTS_SENDER; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...rc/test/java/com/redhat/cloud/notifications/processors/email/EmailActorsResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.redhat.cloud.notifications.processors.email; | ||
|
||
import com.redhat.cloud.notifications.models.Event; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@QuarkusTest | ||
public class EmailActorsResolverTest { | ||
@Inject | ||
EmailActorsResolver emailActorsResolver; | ||
|
||
/** | ||
* Tests that the default "RH Insights" sender is returned for every event. | ||
*/ | ||
@Test | ||
void testGetEmailSender() { | ||
Assertions.assertEquals(EmailActorsResolver.RH_INSIGHTS_SENDER, this.emailActorsResolver.getEmailSender(new Event()), "unexpected email sender returned from the function under test"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,9 @@ public class EmailProcessorTest { | |
@InjectMock | ||
ConnectorSender connectorSender; | ||
|
||
@InjectMock | ||
EmailActorsResolver emailActorsResolver; | ||
|
||
@Inject | ||
EmailProcessor emailProcessor; | ||
|
||
|
@@ -340,6 +343,10 @@ void testSuccess() { | |
endpoint.setId(UUID.randomUUID()); | ||
Mockito.when(this.endpointRepository.getOrCreateDefaultSystemSubscription(event.getAccountId(), event.getOrgId(), EndpointType.EMAIL_SUBSCRIPTION)).thenReturn(endpoint); | ||
|
||
// Mock the sender and the default recipients of the email | ||
final String stubbedSender = "Red Hat Insights [email protected]"; | ||
Mockito.when(this.emailActorsResolver.getEmailSender(Mockito.any())).thenReturn(stubbedSender); | ||
|
||
// Call the processor under test. | ||
this.emailProcessor.process(event, endpoints); | ||
|
||
|
@@ -368,6 +375,7 @@ void testSuccess() { | |
final String resultEmailBody = payload.getString("email_body"); | ||
final String resultEmailSubject = payload.getString("email_subject"); | ||
final String resultOrgId = payload.getString("orgId"); | ||
final String resultEmailSender = payload.getString("email_sender"); | ||
final Set<String> resultSubscribers = payload.getJsonArray("subscribers").stream().map(String.class::cast).collect(toSet()); | ||
final Set<RecipientSettings> resultRecipientSettings = payload.getJsonArray("recipient_settings") | ||
.stream() | ||
|
@@ -386,6 +394,7 @@ void testSuccess() { | |
|
||
Assertions.assertEquals(stubbedRenderedBody, resultEmailBody, "the rendered email's body from the email notification does not match the stubbed email body"); | ||
Assertions.assertEquals(stubbedRenderedSubject, resultEmailSubject, "the rendered email's subject from the email notification does not match the stubbed email subject"); | ||
Assertions.assertEquals(stubbedSender, resultEmailSender, "the rendered email's sender from the email notification does not match the stubbed sender"); | ||
Assertions.assertEquals(event.getOrgId(), resultOrgId, "the organization ID from the email notification does not match the one set in the stubbed event"); | ||
Assertions.assertEquals(Set.copyOf(subscribers), resultSubscribers, "the subscribers set in the email notification do not match the stubbed ones"); | ||
|
||
|