Search in sources :

Example 46 with SimpleMailMessage

use of org.springframework.mail.SimpleMailMessage in project molgenis by molgenis.

the class AccountServiceImplTest method createUser.

@Test
public void createUser() throws URISyntaxException, UsernameAlreadyExistsException, EmailAlreadyExistsException {
    when(idGenerator.generateId(SECURE_RANDOM)).thenReturn("3541db68-435b-416b-8c2c-cf2edf6ba435");
    accountService.createUser(user, "http://molgenis.org/activate");
    ArgumentCaptor<User> argument = ArgumentCaptor.forClass(User.class);
    verify(dataService).add(eq(USER), argument.capture());
    verify(argument.getValue()).setActive(false);
    SimpleMailMessage expected = new SimpleMailMessage();
    expected.setTo("jan.jansen@activation.nl");
    expected.setSubject("User registration for Molgenis title");
    expected.setText("User registration for Molgenis title\n" + "User name: jansenj Full name: Jan Jansen\n" + "In order to activate the user visit the following URL:\n" + "http://molgenis.org/activate/3541db68-435b-416b-8c2c-cf2edf6ba435\n\n");
    verify(mailSender).send(expected);
}
Also used : User(org.molgenis.data.security.auth.User) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) Test(org.testng.annotations.Test)

Example 47 with SimpleMailMessage

use of org.springframework.mail.SimpleMailMessage in project molgenis by molgenis.

the class FeedbackControllerTest method submitErrorWhileSendingMail.

@Test
public void submitErrorWhileSendingMail() throws Exception {
    List<String> adminEmails = Collections.singletonList("molgenis@molgenis.org");
    when(userService.getSuEmailAddresses()).thenReturn(adminEmails);
    SimpleMailMessage expected = new SimpleMailMessage();
    expected.setTo("molgenis@molgenis.org");
    expected.setCc("user@domain.com");
    expected.setReplyTo("user@domain.com");
    expected.setSubject("[feedback-app123] Feedback form");
    expected.setText("Feedback from First Last (user@domain.com):\n\n" + "Feedback.\nLine two.");
    doThrow(new MailSendException("ERRORRR!")).when(mailSender).send(expected);
    mockMvcFeedback.perform(MockMvcRequestBuilders.post(FeedbackController.URI).param("name", "First Last").param("subject", "Feedback form").param("email", "user@domain.com").param("feedback", "Feedback.\nLine two.").param("captcha", "validCaptcha")).andExpect(status().isOk()).andExpect(view().name("view-feedback")).andExpect(model().attribute("feedbackForm", hasProperty("submitted", equalTo(false)))).andExpect(model().attribute("feedbackForm", hasProperty("errorMessage", equalTo("Unfortunately, we were unable to send the mail containing " + "your feedback. Please contact the administrator."))));
    verify(captchaService, times(1)).validateCaptcha("validCaptcha");
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailSendException(org.springframework.mail.MailSendException) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 48 with SimpleMailMessage

use of org.springframework.mail.SimpleMailMessage in project molgenis by molgenis.

the class AccountServiceImpl method activateUser.

@Override
@RunAsSystem
public void activateUser(String activationCode) {
    User user = dataService.query(USER, User.class).eq(ACTIVE, false).and().eq(ACTIVATIONCODE, activationCode).findOne();
    if (user != null) {
        user.setActive(true);
        dataService.update(USER, user);
        // send activated email to user
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(user.getEmail());
        mailMessage.setSubject("Your registration request for " + appSettings.getTitle());
        mailMessage.setText(createActivatedEmailText(user, appSettings.getTitle()));
        mailSender.send(mailMessage);
    } else {
        throw new MolgenisUserException("Invalid activation code or account already activated.");
    }
}
Also used : User(org.molgenis.data.security.auth.User) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MolgenisUserException(org.molgenis.security.user.MolgenisUserException) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Example 49 with SimpleMailMessage

use of org.springframework.mail.SimpleMailMessage in project molgenis by molgenis.

the class AccountServiceImpl method resetPassword.

@Override
@RunAsSystem
public void resetPassword(String userEmail) {
    User user = dataService.query(USER, User.class).eq(EMAIL, userEmail).findOne();
    if (user != null) {
        String newPassword = idGenerator.generateId(SHORT_SECURE_RANDOM);
        user.setPassword(newPassword);
        user.setChangePassword(true);
        dataService.update(USER, user);
        // send password reseted email to user
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(user.getEmail());
        mailMessage.setSubject("Your new password request");
        mailMessage.setText(createPasswordResettedEmailText(newPassword));
        mailSender.send(mailMessage);
    } else {
        throw new MolgenisUserException("Invalid email address.");
    }
}
Also used : User(org.molgenis.data.security.auth.User) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MolgenisUserException(org.molgenis.security.user.MolgenisUserException) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Example 50 with SimpleMailMessage

use of org.springframework.mail.SimpleMailMessage in project molgenis by molgenis.

the class ProgressImplTest method testMailSuccess.

@Test
public void testMailSuccess() {
    jobExecution.setSuccessEmail("a@b.c,d@e.f");
    progress.start();
    progress.status("Working....");
    progress.success();
    System.out.println(jobExecution.getLog());
    assertTrue(jobExecution.getLog().contains("- Execution started." + System.lineSeparator()));
    assertTrue(jobExecution.getLog().contains("- Working...." + System.lineSeparator()));
    assertTrue(jobExecution.getLog().contains("- Execution successful. Time spent: "));
    SimpleMailMessage mail = new SimpleMailMessage();
    mail.setTo(new String[] { "a@b.c", "d@e.f" });
    mail.setSubject("Annotator job succeeded.");
    mail.setText(jobExecution.getLog());
    Mockito.verify(mailSender).send(mail);
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Aggregations

SimpleMailMessage (org.springframework.mail.SimpleMailMessage)106 MailException (org.springframework.mail.MailException)17 Test (org.junit.Test)13 Test (org.testng.annotations.Test)7 Test (org.junit.jupiter.api.Test)6 User (org.molgenis.data.security.auth.User)5 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)5 IOException (java.io.IOException)4 MessagingException (javax.mail.MessagingException)4 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 MailSendException (org.springframework.mail.MailSendException)4 InternetAddress (jakarta.mail.internet.InternetAddress)3 MimeMessage (jakarta.mail.internet.MimeMessage)3 Date (java.util.Date)3 RunAsSystem (org.molgenis.security.core.runas.RunAsSystem)3 MolgenisUserException (org.molgenis.security.user.MolgenisUserException)3 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 User (fr.codechill.spring.model.User)2