From 0470f7bb9e73ec6e69ef853e81096873d0aeb4b2 Mon Sep 17 00:00:00 2001 From: Yazan Amer Date: Sat, 7 Oct 2023 12:54:06 +0300 Subject: [PATCH] feat: test extension --- .../email/smtp/SMTPEmailServiceTest.java | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/otherband/email/smtp/SMTPEmailServiceTest.java b/src/test/java/org/otherband/email/smtp/SMTPEmailServiceTest.java index f5c7cf5..4d2a993 100644 --- a/src/test/java/org/otherband/email/smtp/SMTPEmailServiceTest.java +++ b/src/test/java/org/otherband/email/smtp/SMTPEmailServiceTest.java @@ -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@email.com"; + 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 = "test@email.com"; 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 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; } } \ No newline at end of file