Search in sources :

Example 1 with Retriever

use of com.icegreen.greenmail.util.Retriever in project videochat by nkonev.

the class RegistrationControllerTest method testConfirmationSuccess.

@Test
public void testConfirmationSuccess() throws Exception {
    final String email = "newbie@example.com";
    final String username = "newbie";
    final String password = "password";
    EditUserDTO createUserDTO = new EditUserDTO(username, null, null, password, email);
    // register
    MvcResult createAccountRequest = mockMvc.perform(MockMvcRequestBuilders.post(Constants.Urls.API + Constants.Urls.REGISTER).content(objectMapper.writeValueAsString(createUserDTO)).contentType(MediaType.APPLICATION_JSON_UTF8_VALUE).with(csrf())).andExpect(status().isOk()).andReturn();
    String createAccountStr = createAccountRequest.getResponse().getContentAsString();
    LOGGER.info(createAccountStr);
    // login unconfirmed fail
    mockMvc.perform(MockMvcRequestBuilders.post(SecurityConfig.API_LOGIN_URL).contentType(MediaType.APPLICATION_FORM_URLENCODED).param(SecurityConfig.USERNAME_PARAMETER, username).param(SecurityConfig.PASSWORD_PARAMETER, password).with(csrf())).andExpect(status().isUnauthorized());
    // user lost email and reissues token
    {
        long tokenCountBeforeResend = userConfirmationTokenRepository.count();
        mockMvc.perform(post(Constants.Urls.API + Constants.Urls.RESEND_CONFIRMATION_EMAIL + "?email=" + email).with(csrf())).andExpect(status().isOk());
        Assertions.assertEquals(tokenCountBeforeResend + 1, userConfirmationTokenRepository.count());
    }
    // http://www.icegreen.com/greenmail/javadocs/com/icegreen/greenmail/util/Retriever.html
    try (Retriever r = new Retriever(greenMail.getImap())) {
        Message[] messages = r.getMessages(email);
        Assertions.assertEquals(2, messages.length, "backend should sent two email: a) during registration; b) during confirmation token reissue");
        IMAPMessage imapMessage = (IMAPMessage) messages[1];
        String content = (String) imapMessage.getContent();
        String parsedUrl = UrlParser.parseUrlFromMessage(content);
        String tokenUuidString = UriComponentsBuilder.fromUri(new URI(parsedUrl)).build().getQueryParams().get(Constants.Urls.UUID).get(0);
        Assertions.assertTrue(userConfirmationTokenRepository.existsById(tokenUuidString));
        // perform confirm
        mockMvc.perform(get(parsedUrl)).andExpect(status().isOk());
        Assertions.assertFalse(userConfirmationTokenRepository.existsById(tokenUuidString));
    }
    // login confirmed ok
    mockMvc.perform(post(SecurityConfig.API_LOGIN_URL).contentType(MediaType.APPLICATION_FORM_URLENCODED).param(SecurityConfig.USERNAME_PARAMETER, username).param(SecurityConfig.PASSWORD_PARAMETER, password).with(csrf())).andExpect(status().isOk());
    // resend for already confirmed does nothing
    {
        long tokenCountBeforeResend = userConfirmationTokenRepository.count();
        mockMvc.perform(post(Constants.Urls.API + Constants.Urls.RESEND_CONFIRMATION_EMAIL + "?email=" + email).with(csrf())).andExpect(status().isOk());
        Assertions.assertEquals(tokenCountBeforeResend, userConfirmationTokenRepository.count());
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) Message(javax.mail.Message) IMAPMessage(com.sun.mail.imap.IMAPMessage) EditUserDTO(com.github.nkonev.aaa.dto.EditUserDTO) IMAPMessage(com.sun.mail.imap.IMAPMessage) MvcResult(org.springframework.test.web.servlet.MvcResult) URI(java.net.URI) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 2 with Retriever

use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.

the class LargeMessageTest method retrieveAndCheck.

/**
 * Retrieve message from retriever and check the attachment and text content
 *
 * @param server Server to read from
 * @param to     Account to retrieve
 */
private void retrieveAndCheck(AbstractServer server, String to) throws MessagingException, IOException {
    try (Retriever retriever = new Retriever(server)) {
        Message[] messages = retriever.getMessages(to);
        assertThat(messages.length).isEqualTo(1);
        Message message = messages[0];
        assertThat(message.getContentType().startsWith("multipart/mixed")).isTrue();
        MimeMultipart body = (MimeMultipart) message.getContent();
        assertThat(body.getContentType().startsWith("multipart/mixed")).isTrue();
        assertThat(body.getCount()).isEqualTo(2);
        // Message text
        final BodyPart textPart = body.getBodyPart(0);
        String text = (String) textPart.getContent();
        assertThat(text).isEqualTo(createLargeString());
        final BodyPart attachment = body.getBodyPart(1);
        assertThat(attachment.getContentType().equalsIgnoreCase("application/blubb; name=file")).isTrue();
        InputStream attachmentStream = (InputStream) attachment.getContent();
        byte[] bytes = toByteArray(attachmentStream);
        assertThat(bytes).isEqualTo(createLargeByteArray());
    }
}
Also used : BodyPart(jakarta.mail.BodyPart) Retriever(com.icegreen.greenmail.util.Retriever) Message(jakarta.mail.Message) MimeMultipart(jakarta.mail.internet.MimeMultipart) InputStream(java.io.InputStream)

Example 3 with Retriever

use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.

the class LargeMessageTest method retrieveAndCheckBody.

/**
 * Retrieve message from retriever and check the body content
 *
 * @param server Server to read from
 * @param to     Account to retrieve
 */
private void retrieveAndCheckBody(AbstractServer server, String to) throws MessagingException, IOException {
    try (Retriever retriever = new Retriever(server)) {
        Message[] messages = retriever.getMessages(to);
        assertThat(messages.length).isEqualTo(1);
        Message message = messages[0];
        assertThat(message.getContentType().equalsIgnoreCase("application/blubb")).isTrue();
        // Check content
        InputStream contentStream = (InputStream) message.getContent();
        byte[] bytes = toByteArray(contentStream);
        assertThat(bytes).isEqualTo(createLargeByteArray());
        // Dump complete mail message. This leads to a FETCH command without section or "len" specified.
        message.writeTo(new ByteArrayOutputStream());
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) Message(jakarta.mail.Message) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with Retriever

use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.

the class Pop3ServerTest method testRetrieveWithNonDefaultPassword.

@Test
public void testRetrieveWithNonDefaultPassword() throws Exception {
    assertThat(greenMail.getPop3()).isNotNull();
    final String to = "test@localhost";
    final String password = "donotharmanddontrecipricateharm";
    greenMail.setUser(to, password);
    final String subject = GreenMailUtil.random();
    final String body = GreenMailUtil.random();
    GreenMailUtil.sendTextEmailTest(to, "from@localhost", subject, body);
    greenMail.waitForIncomingEmail(5000, 1);
    try (Retriever retriever = new Retriever(greenMail.getPop3())) {
        assertThatThrownBy(() -> retriever.getMessages(to, "wrongpassword")).isInstanceOf(RuntimeException.class).hasCauseInstanceOf(AuthenticationFailedException.class);
        Message[] messages = retriever.getMessages(to, password);
        assertThat(messages.length).isEqualTo(1);
        assertThat(messages[0].getSubject()).isEqualTo(subject);
        assertThat(GreenMailUtil.getBody(messages[0]).trim()).isEqualTo(body);
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) Message(jakarta.mail.Message) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 5 with Retriever

use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.

the class Pop3ServerTest method testRetrieve.

@Test
public void testRetrieve() throws Exception {
    assertThat(greenMail.getPop3()).isNotNull();
    final String subject = GreenMailUtil.random();
    final String body = GreenMailUtil.random() + "\r\n" + GreenMailUtil.random() + "\r\n" + GreenMailUtil.random();
    String to = "test@localhost";
    GreenMailUtil.sendTextEmailTest(to, "from@localhost", subject, body);
    greenMail.waitForIncomingEmail(5000, 1);
    try (Retriever retriever = new Retriever(greenMail.getPop3())) {
        Message[] messages = retriever.getMessages(to);
        assertThat(messages.length).isEqualTo(1);
        assertThat(messages[0].getSubject()).isEqualTo(subject);
        assertThat(GreenMailUtil.getBody(messages[0]).trim()).isEqualTo(body);
        // UID
        POP3Folder f = (POP3Folder) messages[0].getFolder();
        assertThat(f.getUID(messages[0])).isNotEqualTo("UNKNOWN");
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) Message(jakarta.mail.Message) POP3Folder(com.sun.mail.pop3.POP3Folder) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Aggregations

Retriever (com.icegreen.greenmail.util.Retriever)25 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)17 Message (jakarta.mail.Message)17 Test (org.junit.Test)15 MimeMessage (jakarta.mail.internet.MimeMessage)11 MimeMultipart (jakarta.mail.internet.MimeMultipart)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 IMAPMessage (com.sun.mail.imap.IMAPMessage)2 BodyPart (jakarta.mail.BodyPart)2 InputStream (java.io.InputStream)2 URI (java.net.URI)2 Date (java.util.Date)2 Message (javax.mail.Message)2 Test (org.junit.jupiter.api.Test)2 EditUserDTO (com.github.nkonev.aaa.dto.EditUserDTO)1 GreenMailMimeMessage (com.icegreen.greenmail.test.util.GreenMailMimeMessage)1 GreenMail (com.icegreen.greenmail.util.GreenMail)1 POP3Folder (com.sun.mail.pop3.POP3Folder)1 Session (jakarta.mail.Session)1 Properties (java.util.Properties)1