-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33278 from gsmet/mailer-approve-list
Mailer - Add the ability to configure a list of approved recipients
- Loading branch information
Showing
10 changed files
with
262 additions
and
8 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
extensions/mailer/deployment/src/test/java/io/quarkus/mailer/ApproveListNoEmailTest.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,48 @@ | ||
package io.quarkus.mailer; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.logging.Level; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ApproveListNoEmailTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addAsResource(new StringAsset( | ||
"quarkus.mailer.approved-recipients=.*@approved1.com\nquarkus.mailer.log-rejected-recipients=true"), | ||
"application.properties")) | ||
.setLogRecordPredicate(record -> record.getLevel().equals(Level.WARNING)) | ||
.assertLogRecords(lrs -> { | ||
assertTrue(lrs.stream().anyMatch(lr -> lr.getMessage().equals( | ||
"Email 'A subject' was not sent because all recipients were rejected by the configuration: [[email protected], [email protected], [email protected], [email protected], [email protected]]"))); | ||
}); | ||
|
||
@Inject | ||
Mailer mailer; | ||
|
||
@Inject | ||
MockMailbox mockMailbox; | ||
|
||
@Test | ||
public void testApproveList() { | ||
mailer.send(Mail.withText("[email protected]", "A subject", "") | ||
.addCc("[email protected]", "[email protected]") | ||
.addBcc("[email protected]", "[email protected]")); | ||
|
||
assertEquals(0, mockMailbox.getMailMessagesSentTo("[email protected]").size()); | ||
assertEquals(0, mockMailbox.getMailMessagesSentTo("[email protected]").size()); | ||
assertEquals(0, mockMailbox.getMailMessagesSentTo("[email protected]").size()); | ||
assertEquals(0, mockMailbox.getMailMessagesSentTo("[email protected]").size()); | ||
assertEquals(0, mockMailbox.getMailMessagesSentTo("[email protected]").size()); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
extensions/mailer/deployment/src/test/java/io/quarkus/mailer/ApproveListTest.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,52 @@ | ||
package io.quarkus.mailer; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.logging.Level; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ApproveListTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addAsResource(new StringAsset( | ||
"quarkus.mailer.approved-recipients=.*@approved1.com,.*@approved2.com,.*@approved3.com\nquarkus.mailer.log-rejected-recipients=true"), | ||
"application.properties")) | ||
.setLogRecordPredicate(record -> record.getLevel().equals(Level.WARNING)) | ||
.assertLogRecords(lrs -> { | ||
assertTrue(lrs.stream().anyMatch(lr -> lr.getMessage().equals( | ||
"Email 'A subject' was not sent to the following recipients as they were rejected by the configuration: [[email protected], [email protected], [email protected], [email protected], [email protected]]"))); | ||
}); | ||
|
||
@Inject | ||
Mailer mailer; | ||
|
||
@Inject | ||
MockMailbox mockMailbox; | ||
|
||
@Test | ||
public void testApproveList() { | ||
mailer.send(Mail.withText("[email protected]", "A subject", "") | ||
.addTo("[email protected]") | ||
.addCc("[email protected]", "[email protected]", "[email protected]") | ||
.addBcc("[email protected]", "[email protected]", "[email protected]")); | ||
|
||
assertEquals(1, mockMailbox.getMailMessagesSentTo("[email protected]").size()); | ||
assertEquals(1, mockMailbox.getMailMessagesSentTo("[email protected]").size()); | ||
assertEquals(1, mockMailbox.getMailMessagesSentTo("[email protected]").size()); | ||
assertEquals(0, mockMailbox.getMailMessagesSentTo("[email protected]").size()); | ||
assertEquals(0, mockMailbox.getMailMessagesSentTo("[email protected]").size()); | ||
assertEquals(0, mockMailbox.getMailMessagesSentTo("[email protected]").size()); | ||
assertEquals(0, mockMailbox.getMailMessagesSentTo("[email protected]").size()); | ||
assertEquals(0, mockMailbox.getMailMessagesSentTo("[email protected]").size()); | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
...sions/mailer/runtime/src/main/java/io/quarkus/mailer/runtime/TrimmedPatternConverter.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,26 @@ | ||
package io.quarkus.mailer.runtime; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import org.eclipse.microprofile.config.spi.Converter; | ||
|
||
public class TrimmedPatternConverter implements Converter<Pattern> { | ||
|
||
public TrimmedPatternConverter() { | ||
} | ||
|
||
@Override | ||
public Pattern convert(String s) { | ||
if (s == null) { | ||
return null; | ||
} | ||
|
||
String trimmedString = s.trim().toLowerCase(); | ||
|
||
if (trimmedString.isEmpty()) { | ||
return null; | ||
} | ||
|
||
return Pattern.compile(trimmedString, Pattern.CASE_INSENSITIVE); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...untime/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.Converter
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 @@ | ||
io.quarkus.mailer.runtime.TrimmedPatternConverter |
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