From f9bf6ac8d6f44d22d13148138816cc4f3ccd06fd Mon Sep 17 00:00:00 2001 From: Jarkko Pesonen <435495+jrkkp@users.noreply.github.com> Date: Tue, 14 Jan 2025 09:49:01 +0200 Subject: [PATCH] VKT(Backend): More backend tests for auth link email --- .../service/ExaminerDetailsServiceTest.java | 3 - .../ExaminerEnrollmentEmailServiceTest.java | 93 +++++++++++++++++++ 2 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 backend/vkt/src/test/java/fi/oph/vkt/service/ExaminerEnrollmentEmailServiceTest.java diff --git a/backend/vkt/src/test/java/fi/oph/vkt/service/ExaminerDetailsServiceTest.java b/backend/vkt/src/test/java/fi/oph/vkt/service/ExaminerDetailsServiceTest.java index 006ef0926..fbfa5bf6f 100644 --- a/backend/vkt/src/test/java/fi/oph/vkt/service/ExaminerDetailsServiceTest.java +++ b/backend/vkt/src/test/java/fi/oph/vkt/service/ExaminerDetailsServiceTest.java @@ -58,9 +58,6 @@ public void setup() { final Environment environment = mock(Environment.class); when(environment.getRequiredProperty("app.base-url.api")).thenReturn("http://localhost"); - final UUIDSource uuidSource = mock(UUIDSource.class); - when(uuidSource.getRandomNonce()).thenReturn("269a2da4-58bb-45eb-b125-522b77e9167c"); - final PersonalData personalData = PersonalData .builder() .onrId("1.2.3.4") diff --git a/backend/vkt/src/test/java/fi/oph/vkt/service/ExaminerEnrollmentEmailServiceTest.java b/backend/vkt/src/test/java/fi/oph/vkt/service/ExaminerEnrollmentEmailServiceTest.java new file mode 100644 index 000000000..4f9b0fdb9 --- /dev/null +++ b/backend/vkt/src/test/java/fi/oph/vkt/service/ExaminerEnrollmentEmailServiceTest.java @@ -0,0 +1,93 @@ +package fi.oph.vkt.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import fi.oph.vkt.Factory; +import fi.oph.vkt.model.Email; +import fi.oph.vkt.model.EmailType; +import fi.oph.vkt.model.EnrollmentAppointment; +import fi.oph.vkt.model.Examiner; +import fi.oph.vkt.model.ExaminerExamEvent; +import fi.oph.vkt.model.Municipality; +import fi.oph.vkt.model.Person; +import fi.oph.vkt.repository.*; +import fi.oph.vkt.service.email.EmailService; +import fi.oph.vkt.service.email.sender.EmailSender; +import fi.oph.vkt.util.TemplateRenderer; +import jakarta.annotation.Resource; +import java.io.IOException; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.core.env.Environment; +import org.springframework.data.domain.PageRequest; +import org.springframework.security.test.context.support.WithMockUser; + +@WithMockUser +@DataJpaTest +public class ExaminerEnrollmentEmailServiceTest { + + @Resource + private EmailRepository emailRepository; + + @Resource + private EmailAttachmentRepository emailAttachmentRepository; + + @MockBean + private EmailSender emailSender; + + @MockBean + private TemplateRenderer templateRenderer; + + @Resource + private TestEntityManager entityManager; + + private ExaminerEnrollmentEmailService examinerEnrollmentEmailService; + + @BeforeEach + public void setup() { + final Environment environment = mock(Environment.class); + when(environment.getRequiredProperty("app.base-url.api")).thenReturn("http://localhost"); + + final EmailService emailService = new EmailService(emailRepository, emailAttachmentRepository, emailSender); + + examinerEnrollmentEmailService = new ExaminerEnrollmentEmailService(emailService, environment, templateRenderer); + } + + @Test + public void testSendAuthLink() throws IOException, InterruptedException { + final Examiner examiner = Factory.examiner(); + final Municipality municipality = Factory.municipality(); + final ExaminerExamEvent examEvent = Factory.examinerExamEvent(examiner, municipality); + final Person person = Factory.person(); + final EnrollmentAppointment enrollment = Factory.enrollmentAppointment(examiner, examEvent, person); + + when(templateRenderer.renderEnrollmentAppointmentAuthLink(any())).thenReturn("email body"); + + entityManager.persist(examiner); + entityManager.persist(municipality); + entityManager.persist(examEvent); + entityManager.persist(person); + entityManager.persist(enrollment); + + examinerEnrollmentEmailService.sendEnrollmentAppointmentAuthLink(enrollment); + + final List emails = emailRepository.findEmailsToSend(PageRequest.ofSize(10)); + final Email email = emailRepository.getReferenceById(emails.get(0)); + + assertEquals(1, emails.size()); + assertEquals( + "Ilmoittautuminen Valtionhallinnon kielitutkintoon (VKT) | Ilmoittautuminen Valtionhallinnon kielitutkintoon (VKT)", + email.getSubject() + ); + assertEquals("foo.tester@invalid", email.getRecipientAddress()); + assertEquals("Irma Ilmoittautuja", email.getRecipientName()); + assertEquals(EmailType.ENROLLMENT_APPOINTMENT_AUTH_LINK, email.getEmailType()); + } +}