-
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.
- Loading branch information
Showing
6 changed files
with
107 additions
and
17 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
32 changes: 32 additions & 0 deletions
32
src/main/java/org/otherband/email/smtp/SMTPEmailService.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,32 @@ | ||
package org.otherband.email.smtp; | ||
|
||
import org.otherband.Utils; | ||
import org.otherband.email.EmailService; | ||
import org.otherband.email.VerificationLinkEmailRequest; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Profile("production") | ||
@Service | ||
public class SMTPEmailService implements EmailService { | ||
private final String mailUserName; | ||
private final JavaMailSender mailSender; | ||
|
||
public SMTPEmailService(@Value("${spring.mail.username}") String mailUserName, JavaMailSender mailSender) { | ||
this.mailUserName = mailUserName; | ||
this.mailSender = mailSender; | ||
} | ||
|
||
@Override | ||
public void sendLetterVerificationLink(VerificationLinkEmailRequest verificationLinkEmailRequest) { | ||
SimpleMailMessage message = new SimpleMailMessage(); | ||
message.setFrom(mailUserName); | ||
message.setTo(verificationLinkEmailRequest.getReceiverEmail()); | ||
message.setText(Utils.buildLetterVerificationLink(verificationLinkEmailRequest)); | ||
message.setSubject("Recommendation letter verification link"); | ||
mailSender.send(message); | ||
} | ||
} |
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,4 @@ | ||
spring.mail.host=smtp.gmail.com | ||
spring.mail.port=587 | ||
spring.mail.username=<SMTP username> | ||
spring.mail.password=<SMTP password> |
49 changes: 49 additions & 0 deletions
49
src/test/java/org/otherband/email/smtp/SMTPEmailServiceTest.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,49 @@ | ||
package org.otherband.email.smtp; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.otherband.email.VerificationLinkEmailRequest; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
class SMTPEmailServiceTest { | ||
private static final String EXPECTED_ID = "LETTER_ID"; | ||
public static final String EXPECTED_RECEIVER_EMAIL = "[email protected]"; | ||
public static final String TOKEN_ID = "TOKEN_ID"; | ||
public static final String SECRET = "SECRET"; | ||
private SMTPEmailService service; | ||
private final static String SENDER_EMAIL = "[email protected]"; | ||
private JavaMailSender mailSender; | ||
|
||
@BeforeEach | ||
void setup() { | ||
mailSender = mock(JavaMailSender.class); | ||
service = new SMTPEmailService(SENDER_EMAIL, mailSender); | ||
} | ||
|
||
@Test | ||
void testSendVerificationEmail() { | ||
VerificationLinkEmailRequest verificationLinkEmailRequest = buildRequest(); | ||
service.sendLetterVerificationLink(verificationLinkEmailRequest); | ||
ArgumentCaptor<SimpleMailMessage> argumentCaptor = ArgumentCaptor.forClass(SimpleMailMessage.class); | ||
verify(mailSender).send(argumentCaptor.capture()); | ||
SimpleMailMessage sentMessage = argumentCaptor.getValue(); | ||
assertArrayEquals(new String[]{EXPECTED_RECEIVER_EMAIL}, sentMessage.getTo()); | ||
assertEquals("Recommendation letter verification link", sentMessage.getSubject()); | ||
assertTrue(sentMessage.getText().contains("/api/v1/recommendation-letter/verify/LETTER_ID/TOKEN_ID/SECRET")); | ||
} | ||
|
||
private static VerificationLinkEmailRequest buildRequest() { | ||
VerificationLinkEmailRequest request = new VerificationLinkEmailRequest(); | ||
request.setLetterId(EXPECTED_ID); | ||
request.setTokenId(TOKEN_ID); | ||
request.setSecretToken(SECRET); | ||
request.setReceiverEmail(EXPECTED_RECEIVER_EMAIL); | ||
return request; | ||
} | ||
} |