Search in sources :

Example 21 with Retriever

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

the class DateTest method retrieveAndCheck.

/**
 * Retrieve message from retriever and check content
 *
 * @param server            Server to read from
 * @param to                Account to retrieve
 * @param sentDate          Desired 'sent' date of message
 * @param checkReceivedDate True if received date should be checked. POP3 does not provide a received date
 */
private void retrieveAndCheck(AbstractServer server, String to, Date sentDate, boolean checkReceivedDate) throws MessagingException {
    try (Retriever retriever = new Retriever(server)) {
        Message[] messages = retriever.getMessages(to);
        assertThat(messages.length).isEqualTo(1);
        Message message = messages[0];
        assertThat(milliSecondDateDiff(message.getSentDate(), sentDate)).isLessThan(3000L);
        if (checkReceivedDate) {
            assertThat(milliSecondDateDiff(message.getReceivedDate(), new Date())).isLessThan(3000L);
        }
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) Message(jakarta.mail.Message) MimeMessage(jakarta.mail.internet.MimeMessage) Date(java.util.Date)

Example 22 with Retriever

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

the class EscapingTest method retrieveAndCheck.

/**
 * Retrieve message from retriever and check content
 *
 * @param server  Server to read from
 * @param to      Account to retrieve
 * @param subject Subject of message
 */
private void retrieveAndCheck(AbstractServer server, String to, String from, String subject) throws MessagingException {
    try (Retriever retriever = new Retriever(server)) {
        Message[] messages = retriever.getMessages(to);
        assertThat(messages.length).isEqualTo(1);
        Message message = messages[0];
        // Message subject
        assertThat(message.getSubject()).isEqualTo(subject);
        assertThat(message.getAllRecipients()[0].toString()).isEqualTo(to);
        assertThat(message.getFrom()[0].toString()).isEqualTo(from);
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) GreenMailMimeMessage(com.icegreen.greenmail.test.util.GreenMailMimeMessage) Message(jakarta.mail.Message)

Example 23 with Retriever

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

the class AuthenticationDisabledTest method testReceiveWithAuthDisabledAndProvisionedUser.

@Test
public void testReceiveWithAuthDisabledAndProvisionedUser() {
    final String to = "to@localhost";
    greenMail.setUser(to, "to", "secret");
    greenMail.waitForIncomingEmail(500, 1);
    try (Retriever retriever = new Retriever(greenMail.getImap())) {
        Message[] messages = retriever.getMessages(to);
        assertThat(messages.length).isEqualTo(0);
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) Message(jakarta.mail.Message) MimeMessage(jakarta.mail.internet.MimeMessage) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 24 with Retriever

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

the class ReplyToTest method retrieveAndCheckReplyTo.

/**
 * Retrieve message from retriever and check the ReplyTo address
 *
 * @param server Server to read from
 * @param login  Account to retrieve
 */
private void retrieveAndCheckReplyTo(AbstractServer server, String login, InternetAddress[] replyToAddrs) throws MessagingException {
    try (Retriever retriever = new Retriever(server)) {
        Message[] messages = retriever.getMessages(login);
        assertThat(messages.length).isEqualTo(1);
        Message message = messages[0];
        assertThat(toInetAddr(message.getReplyTo())).isEqualTo(replyToAddrs);
    }
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) Message(jakarta.mail.Message) MimeMessage(jakarta.mail.internet.MimeMessage)

Example 25 with Retriever

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

the class RegistrationControllerTest method userCanRequestPasswordOnlyOnOwnEmail.

// scheme simplified, suspect that user's email doesn't stolen
@Test
public void userCanRequestPasswordOnlyOnOwnEmail() throws Exception {
    final String user = TestConstants.USER_BOB;
    final String email = user + "@example.com";
    final String newPassword = "new-password";
    // invoke resend, this sends url /password-reset?uuid=<uuid> and confirm code to email
    mockMvc.perform(post(Constants.Urls.API + Constants.Urls.REQUEST_PASSWORD_RESET + "?email=" + email).with(csrf())).andExpect(status().isOk());
    String passwordResetTokenUuidString;
    try (Retriever r = new Retriever(greenMail.getImap())) {
        Message[] messages = r.getMessages(email);
        Assertions.assertEquals(1, messages.length, "backend should sent one email for password reset");
        IMAPMessage imapMessage = (IMAPMessage) messages[0];
        String content = (String) imapMessage.getContent();
        String parsedUrl = UrlParser.parseUrlFromMessage(content);
        passwordResetTokenUuidString = UriComponentsBuilder.fromUri(new URI(parsedUrl)).build().getQueryParams().get(Constants.Urls.UUID).get(0);
    }
    // after open link user see "input new password dialog"
    // user inputs code, code compares with another in ResetPasswordToken
    PasswordResetController.PasswordResetDto passwordResetDto = new PasswordResetController.PasswordResetDto(UUID.fromString(passwordResetTokenUuidString), newPassword);
    // user click "set new password" button in modal
    mockMvc.perform(post(Constants.Urls.API + Constants.Urls.PASSWORD_RESET_SET_NEW).content(objectMapper.writeValueAsString(passwordResetDto)).contentType(MediaType.APPLICATION_JSON_UTF8_VALUE).with(csrf())).andExpect(status().isOk());
    // ... this is changes his password
    // login with new password ok
    mockMvc.perform(post(SecurityConfig.API_LOGIN_URL).contentType(MediaType.APPLICATION_FORM_URLENCODED).param(SecurityConfig.USERNAME_PARAMETER, user).param(SecurityConfig.PASSWORD_PARAMETER, newPassword).with(csrf())).andExpect(status().isOk());
}
Also used : Retriever(com.icegreen.greenmail.util.Retriever) Message(javax.mail.Message) IMAPMessage(com.sun.mail.imap.IMAPMessage) IMAPMessage(com.sun.mail.imap.IMAPMessage) URI(java.net.URI) Test(org.junit.jupiter.api.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