-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VKT(Backend): More backend tests for auth link email
- Loading branch information
Showing
2 changed files
with
93 additions
and
3 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
93 changes: 93 additions & 0 deletions
93
backend/vkt/src/test/java/fi/oph/vkt/service/ExaminerEnrollmentEmailServiceTest.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,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<Long> 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()); | ||
} | ||
} |