|
| 1 | +/** |
| 2 | + * Copyright (C) 2024 Tolgee s.r.o. and contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.tolgee.email |
| 18 | + |
| 19 | +import io.tolgee.configuration.tolgee.SmtpProperties |
| 20 | +import io.tolgee.testing.assert |
| 21 | +import jakarta.mail.internet.MimeMessage |
| 22 | +import jakarta.mail.internet.MimeMultipart |
| 23 | +import org.junit.jupiter.api.BeforeEach |
| 24 | +import org.junit.jupiter.api.Test |
| 25 | +import org.junit.jupiter.api.extension.ExtendWith |
| 26 | +import org.mockito.ArgumentCaptor |
| 27 | +import org.mockito.Captor |
| 28 | +import org.mockito.kotlin.verify |
| 29 | +import org.mockito.kotlin.whenever |
| 30 | +import org.springframework.beans.factory.annotation.Autowired |
| 31 | +import org.springframework.boot.test.mock.mockito.MockBean |
| 32 | +import org.springframework.context.annotation.Import |
| 33 | +import org.springframework.mail.javamail.JavaMailSender |
| 34 | +import org.springframework.mail.javamail.JavaMailSenderImpl |
| 35 | +import org.springframework.stereotype.Component |
| 36 | +import org.springframework.test.context.junit.jupiter.SpringExtension |
| 37 | +import java.util.* |
| 38 | + |
| 39 | +@Component |
| 40 | +@ExtendWith(SpringExtension::class) |
| 41 | +@Import(EmailService::class, EmailTemplateConfig::class) |
| 42 | +class EmailServiceTest { |
| 43 | + @MockBean |
| 44 | + private lateinit var smtpProperties: SmtpProperties |
| 45 | + |
| 46 | + @MockBean |
| 47 | + private lateinit var mailSender: JavaMailSender |
| 48 | + |
| 49 | + @Autowired |
| 50 | + private lateinit var emailService: EmailService |
| 51 | + |
| 52 | + @Captor |
| 53 | + private lateinit var emailCaptor: ArgumentCaptor<MimeMessage> |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + fun beforeEach() { |
| 57 | + val sender = JavaMailSenderImpl() |
| 58 | + whenever(smtpProperties.from).thenReturn( "Tolgee Test <[email protected]>") |
| 59 | + whenever(mailSender.createMimeMessage()).let { |
| 60 | + val msg = sender.createMimeMessage() |
| 61 | + it.thenReturn(msg) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `it sends a rendered email with variables and ICU strings processed`() { |
| 67 | + emailService.sendEmailTemplate( "[email protected]", "zz-test-email", Locale. ENGLISH, TEST_PROPERTIES) |
| 68 | + verify(mailSender).send(emailCaptor.capture()) |
| 69 | + |
| 70 | + val email = emailCaptor.value |
| 71 | + email.subject.assert.isEqualTo("Test email (written with React Email)") |
| 72 | + email.allRecipients.asList(). assert.singleElement().asString().isEqualTo( "[email protected]") |
| 73 | + |
| 74 | + email.content |
| 75 | + .let { it as MimeMultipart } |
| 76 | + .let { it.getBodyPart(0).content as MimeMultipart } |
| 77 | + .let { it.getBodyPart(0).content as String } |
| 78 | + .assert |
| 79 | + .contains("Value of `testVar` : <span>test!!</span>") |
| 80 | + .contains("<span>Was `testVar` equal to "meow" : </span><span>no</span>") |
| 81 | + .contains("Powered by") |
| 82 | + .doesNotContain(" th:") |
| 83 | + .doesNotContain(" data-th") |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun `it correctly processes conditional blocks`() { |
| 88 | + // FWIW this is very close to just testing Thymeleaf itself, but it serves as a sanity check for the template itself |
| 89 | + emailService.sendEmailTemplate( "[email protected]", "zz-test-email", Locale. ENGLISH, TEST_PROPERTIES_MEOW) |
| 90 | + verify(mailSender).send(emailCaptor.capture()) |
| 91 | + |
| 92 | + val email = emailCaptor.value |
| 93 | + email.content |
| 94 | + .let { it as MimeMultipart } |
| 95 | + .let { it.getBodyPart(0).content as MimeMultipart } |
| 96 | + .let { it.getBodyPart(0).content as String } |
| 97 | + .assert |
| 98 | + .contains("Value of `testVar` : <span>meow</span>") |
| 99 | + .contains("<span>Was `testVar` equal to "meow" : </span><span>yes</span>") |
| 100 | + } |
| 101 | + |
| 102 | + companion object { |
| 103 | + private val TEST_PROPERTIES = mapOf( |
| 104 | + "testVar" to "test!!", |
| 105 | + "testList" to listOf( |
| 106 | + mapOf("name" to "Name #1"), |
| 107 | + mapOf("name" to "Name #2"), |
| 108 | + mapOf("name" to "Name #3"), |
| 109 | + ) |
| 110 | + ) |
| 111 | + |
| 112 | + private val TEST_PROPERTIES_MEOW = mapOf( |
| 113 | + "testVar" to "meow", |
| 114 | + "testList" to listOf( |
| 115 | + mapOf("name" to "Name #1"), |
| 116 | + mapOf("name" to "Name #2"), |
| 117 | + mapOf("name" to "Name #3"), |
| 118 | + ) |
| 119 | + ) |
| 120 | + } |
| 121 | +} |
0 commit comments