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());
}
}
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());
}
}
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());
}
}
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);
}
}
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");
}
}
Aggregations