|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 the original author or authors. |
| 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 | + * https://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 org.springframework.boot.autoconfigure.mail; |
| 18 | + |
| 19 | +import java.net.SocketTimeoutException; |
| 20 | +import java.security.cert.CertPathBuilderException; |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.Arrays; |
| 23 | + |
| 24 | +import javax.net.ssl.SSLException; |
| 25 | + |
| 26 | +import jakarta.mail.Folder; |
| 27 | +import jakarta.mail.Message; |
| 28 | +import jakarta.mail.MessagingException; |
| 29 | +import jakarta.mail.Session; |
| 30 | +import jakarta.mail.Store; |
| 31 | +import org.awaitility.Awaitility; |
| 32 | +import org.junit.jupiter.api.Nested; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.testcontainers.junit.jupiter.Container; |
| 35 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 36 | +import org.testcontainers.utility.MountableFile; |
| 37 | + |
| 38 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 39 | +import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration; |
| 40 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 41 | +import org.springframework.boot.testsupport.container.MailpitContainer; |
| 42 | +import org.springframework.boot.testsupport.container.TestImage; |
| 43 | +import org.springframework.mail.SimpleMailMessage; |
| 44 | +import org.springframework.mail.javamail.JavaMailSenderImpl; |
| 45 | + |
| 46 | +import static org.assertj.core.api.Assertions.assertThat; |
| 47 | +import static org.assertj.core.api.Assertions.assertThatException; |
| 48 | + |
| 49 | +/** |
| 50 | + * Integration tests for {@link MailSenderAutoConfiguration}. |
| 51 | + * |
| 52 | + * @author Rui Figueira |
| 53 | + */ |
| 54 | +@Testcontainers(disabledWithoutDocker = true) |
| 55 | +class MailSenderAutoConfigurationIntegrationTests { |
| 56 | + |
| 57 | + private SimpleMailMessage createMessage(String subject) { |
| 58 | + SimpleMailMessage msg = new SimpleMailMessage(); |
| 59 | + |
| 60 | + |
| 61 | + msg.setSubject(subject); |
| 62 | + msg.setText("Subject: " + subject); |
| 63 | + return msg; |
| 64 | + } |
| 65 | + |
| 66 | + private String getSubject(Message message) { |
| 67 | + try { |
| 68 | + return message.getSubject(); |
| 69 | + } |
| 70 | + catch (MessagingException ex) { |
| 71 | + throw new RuntimeException("Failed to get message subject", ex); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private void assertMessagesContainSubject(Session session, String subject) throws MessagingException { |
| 76 | + try (Store store = session.getStore("pop3")) { |
| 77 | + String host = session.getProperty("mail.pop3.host"); |
| 78 | + int port = Integer.parseInt(session.getProperty("mail.pop3.port")); |
| 79 | + store.connect(host, port, "user", "pass"); |
| 80 | + try (Folder folder = store.getFolder("inbox")) { |
| 81 | + folder.open(Folder.READ_ONLY); |
| 82 | + Awaitility.await() |
| 83 | + .atMost(Duration.ofSeconds(5)) |
| 84 | + .ignoreExceptions() |
| 85 | + .untilAsserted(() -> assertThat(Arrays.stream(folder.getMessages()).map(this::getSubject)) |
| 86 | + .contains(subject)); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Nested |
| 92 | + class ImplicitTlsTests { |
| 93 | + |
| 94 | + @Container |
| 95 | + private static final MailpitContainer mailpit = TestImage.container(MailpitContainer.class) |
| 96 | + .withSmtpRequireTls(true) |
| 97 | + .withSmtpTlsCert(MountableFile |
| 98 | + .forClasspathResource("/org/springframework/boot/autoconfigure/mail/ssl/test-server.crt")) |
| 99 | + .withSmtpTlsKey(MountableFile |
| 100 | + .forClasspathResource("/org/springframework/boot/autoconfigure/mail/ssl/test-server.key")) |
| 101 | + .withPop3Auth("user:pass"); |
| 102 | + |
| 103 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 104 | + .withConfiguration(AutoConfigurations.of(MailSenderAutoConfiguration.class, SslAutoConfiguration.class)); |
| 105 | + |
| 106 | + @Test |
| 107 | + void sendEmailWithSslEnabledAndCert() { |
| 108 | + this.contextRunner.withPropertyValues("spring.mail.host:" + mailpit.getHost(), |
| 109 | + "spring.mail.port:" + mailpit.getSmtpPort(), "spring.mail.ssl.enabled:true", |
| 110 | + "spring.mail.ssl.bundle:test-bundle", |
| 111 | + "spring.ssl.bundle.pem.test-bundle.truststore.certificate=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-ca.crt", |
| 112 | + "spring.ssl.bundle.pem.test-bundle.keystore.certificate=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.crt", |
| 113 | + "spring.ssl.bundle.pem.test-bundle.keystore.private-key=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.key", |
| 114 | + "spring.mail.properties.mail.pop3.host:" + mailpit.getHost(), |
| 115 | + "spring.mail.properties.mail.pop3.port:" + mailpit.getPop3Port()) |
| 116 | + .run((context) -> { |
| 117 | + JavaMailSenderImpl mailSender = context.getBean(JavaMailSenderImpl.class); |
| 118 | + mailSender.send(createMessage("Hello World!")); |
| 119 | + assertMessagesContainSubject(mailSender.getSession(), "Hello World!"); |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void sendEmailWithSslEnabledWithoutCert() { |
| 125 | + this.contextRunner |
| 126 | + .withPropertyValues("spring.mail.host:" + mailpit.getHost(), |
| 127 | + "spring.mail.port:" + mailpit.getSmtpPort(), "spring.mail.ssl.enabled:true") |
| 128 | + .run((context) -> { |
| 129 | + JavaMailSenderImpl mailSender = context.getBean(JavaMailSenderImpl.class); |
| 130 | + assertThatException().isThrownBy(() -> mailSender.send(createMessage("Should fail"))) |
| 131 | + .withRootCauseInstanceOf(CertPathBuilderException.class); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void sendEmailWithoutSslWithCert() { |
| 137 | + this.contextRunner.withPropertyValues("spring.mail.host:" + mailpit.getHost(), |
| 138 | + "spring.mail.port:" + mailpit.getSmtpPort(), "spring.mail.properties.mail.smtp.timeout:1000", |
| 139 | + "spring.mail.ssl.bundle:test-bundle", |
| 140 | + "spring.ssl.bundle.pem.test-bundle.truststore.certificate=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-ca.crt", |
| 141 | + "spring.ssl.bundle.pem.test-bundle.keystore.certificate=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.crt", |
| 142 | + "spring.ssl.bundle.pem.test-bundle.keystore.private-key=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.key") |
| 143 | + .run((context) -> { |
| 144 | + JavaMailSenderImpl mailSender = context.getBean(JavaMailSenderImpl.class); |
| 145 | + assertThatException().isThrownBy(() -> mailSender.send(createMessage("Should fail"))) |
| 146 | + .withRootCauseInstanceOf(SocketTimeoutException.class); |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | + @Nested |
| 153 | + class StarttlsTests { |
| 154 | + |
| 155 | + @Container |
| 156 | + private static final MailpitContainer mailpit = TestImage.container(MailpitContainer.class) |
| 157 | + .withSmtpRequireStarttls(true) |
| 158 | + .withSmtpTlsCert(MountableFile |
| 159 | + .forClasspathResource("/org/springframework/boot/autoconfigure/mail/ssl/test-server.crt")) |
| 160 | + .withSmtpTlsKey(MountableFile |
| 161 | + .forClasspathResource("/org/springframework/boot/autoconfigure/mail/ssl/test-server.key")) |
| 162 | + .withPop3Auth("user:pass"); |
| 163 | + |
| 164 | + final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 165 | + .withConfiguration(AutoConfigurations.of(MailSenderAutoConfiguration.class, SslAutoConfiguration.class)); |
| 166 | + |
| 167 | + @Test |
| 168 | + void sendEmailWithStarttlsAndCertAndSslDisabled() { |
| 169 | + this.contextRunner.withPropertyValues("spring.mail.host:" + mailpit.getHost(), |
| 170 | + "spring.mail.port:" + mailpit.getSmtpPort(), |
| 171 | + "spring.mail.properties.mail.smtp.starttls.enable:true", |
| 172 | + "spring.mail.properties.mail.smtp.starttls.required:true", "spring.mail.ssl.bundle:test-bundle", |
| 173 | + "spring.ssl.bundle.pem.test-bundle.truststore.certificate=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-ca.crt", |
| 174 | + "spring.ssl.bundle.pem.test-bundle.keystore.certificate=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.crt", |
| 175 | + "spring.ssl.bundle.pem.test-bundle.keystore.private-key=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.key", |
| 176 | + "spring.mail.properties.mail.pop3.host:" + mailpit.getHost(), |
| 177 | + "spring.mail.properties.mail.pop3.port:" + mailpit.getPop3Port()) |
| 178 | + .run((context) -> { |
| 179 | + JavaMailSenderImpl mailSender = context.getBean(JavaMailSenderImpl.class); |
| 180 | + mailSender.send(createMessage("Sent with STARTTLS")); |
| 181 | + assertMessagesContainSubject(mailSender.getSession(), "Sent with STARTTLS"); |
| 182 | + }); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + void sendEmailWithStarttlsAndCertAndSslEnabled() { |
| 187 | + this.contextRunner.withPropertyValues("spring.mail.host:" + mailpit.getHost(), |
| 188 | + "spring.mail.port:" + mailpit.getSmtpPort(), "spring.mail.ssl.enabled:true", |
| 189 | + "spring.mail.properties.mail.smtp.starttls.enable:true", |
| 190 | + "spring.mail.properties.mail.smtp.starttls.required:true", "spring.mail.ssl.bundle:test-bundle", |
| 191 | + "spring.ssl.bundle.pem.test-bundle.truststore.certificate=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-ca.crt", |
| 192 | + "spring.ssl.bundle.pem.test-bundle.keystore.certificate=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.crt", |
| 193 | + "spring.ssl.bundle.pem.test-bundle.keystore.private-key=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.key", |
| 194 | + "spring.mail.properties.mail.pop3.host:" + mailpit.getHost(), |
| 195 | + "spring.mail.properties.mail.pop3.port:" + mailpit.getPop3Port()) |
| 196 | + .run((context) -> { |
| 197 | + JavaMailSenderImpl mailSender = context.getBean(JavaMailSenderImpl.class); |
| 198 | + assertThatException().isThrownBy(() -> mailSender.send(createMessage("Should fail"))) |
| 199 | + .withRootCauseInstanceOf(SSLException.class); |
| 200 | + }); |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + void sendEmailWithStarttlsWithoutCert() { |
| 205 | + this.contextRunner |
| 206 | + .withPropertyValues("spring.mail.host:" + mailpit.getHost(), |
| 207 | + "spring.mail.port:" + mailpit.getSmtpPort(), |
| 208 | + "spring.mail.properties.mail.smtp.starttls.enable:true", |
| 209 | + "spring.mail.properties.mail.smtp.starttls.required:true") |
| 210 | + .run((context) -> { |
| 211 | + JavaMailSenderImpl mailSender = context.getBean(JavaMailSenderImpl.class); |
| 212 | + assertThatException().isThrownBy(() -> mailSender.send(createMessage("Should fail"))) |
| 213 | + .withRootCauseInstanceOf(CertPathBuilderException.class); |
| 214 | + }); |
| 215 | + } |
| 216 | + |
| 217 | + } |
| 218 | + |
| 219 | +} |
0 commit comments