-
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
1 changed file
with
26 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,29 +2,48 @@ | |
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.mockito.ArgumentCaptor; | ||
import org.otherband.email.VerificationLinkEmailRequest; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
|
||
import static org.mockito.Mockito.*; | ||
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_EMAIL = "[email protected]"; | ||
public static final String TOKEN_ID = "TOKEN_ID"; | ||
public static final String SECRET = "SECRET"; | ||
private SMTPEmailService service; | ||
private String mailUserName; | ||
private final static String SENDER_EMAIL = "[email protected]"; | ||
private JavaMailSender mailSender; | ||
|
||
@BeforeEach | ||
void setup() { | ||
mailUserName = "test"; | ||
mailSender = mock(JavaMailSender.class); | ||
service = new SMTPEmailService(mailUserName, mailSender); | ||
service = new SMTPEmailService(SENDER_EMAIL, mailSender); | ||
} | ||
|
||
@Test | ||
void testSendVerificationEmail() { | ||
VerificationLinkEmailRequest verificationLinkEmailRequest = new VerificationLinkEmailRequest(); | ||
VerificationLinkEmailRequest verificationLinkEmailRequest = buildRequest(); | ||
service.sendLetterVerificationLink(verificationLinkEmailRequest); | ||
verify(mailSender).send(Mockito.any(SimpleMailMessage.class)); | ||
ArgumentCaptor<SimpleMailMessage> argumentCaptor = ArgumentCaptor.forClass(SimpleMailMessage.class); | ||
verify(mailSender).send(argumentCaptor.capture()); | ||
SimpleMailMessage sentMessage = argumentCaptor.getValue(); | ||
assertArrayEquals(new String[]{EXPECTED_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_EMAIL); | ||
return request; | ||
} | ||
} |