use of org.simbasecurity.core.service.communication.mail.template.TemplateWithLinks in project simba-os by cegeka.
the class ResetPasswordService method sendResetPasswordMessageTo.
public void sendResetPasswordMessageTo(User user, ResetPasswordReason reason) {
if (user.getEmail().isEmpty()) {
throw new SimbaException(SimbaMessageKey.EMAIL_ADDRESS_REQUIRED);
}
Token token = tokenManager.generateToken(user, reason);
List<URL> links = linkGenerator.generateResetPasswordLinks(user.getEmail(), token);
String mailBody = templateService.createMailBodyWithLink(new TemplateWithLinks(reason.getTemplate(), links), user.getLanguage());
String subject = templateService.createMailSubject(reason.getSubjectTemplate(), user.getLanguage());
mailService.sendMail(createMail(user, mailBody, subject));
audit.log(auditLogEventFactory.createEventForUserAuthentication(user.getUserName(), reason.getMessage()));
}
use of org.simbasecurity.core.service.communication.mail.template.TemplateWithLinks in project simba-os by cegeka.
the class ResetPasswordServiceTest method sendMessageForNewUser_WillTriggerAuditloggingForNewUser.
@Test
public void sendMessageForNewUser_WillTriggerAuditloggingForNewUser() throws Exception {
EmailAddress email = emailFactory.email("something@mail.com");
User user = aDefaultUser().withUserName("otherTest").withEmail(email).withLanguage(en_US).build();
Token token = Token.generateToken();
when(tokenManagerMock.generateToken(user, newUserReason)).thenReturn(token);
URL link = new URL("http://www.google.com");
List<URL> links = Collections.singletonList(link);
when(linkGeneratorMock.generateResetPasswordLinks(email, token)).thenReturn(links);
when(templateServiceMock.createMailBodyWithLink(new TemplateWithLinks(newUserReason.getTemplate(), links), en_US)).thenReturn("someBody");
when(templateServiceMock.createMailSubject(newUserReason.getSubjectTemplate(), en_US)).thenReturn("New user");
ArgumentCaptor<AuditLogEvent> logCaptor = ArgumentCaptor.forClass(AuditLogEvent.class);
resetPasswordService.sendResetPasswordMessageTo(user, newUserReason);
verify(mailServiceMock).sendMail(mail().from(emailFactory.email("bla@hotmail.com")).to(email).subject("New user").body("someBody"));
verify(auditMock).log(logCaptor.capture());
assertThat(logCaptor.getValue()).extracting(AuditLogEvent::getUsername, AuditLogEvent::getMessage, AuditLogEvent::getCategory).containsExactly("otherTest", "Email has been sent to user for following reason: NewUser", AUTHENTICATION);
}
use of org.simbasecurity.core.service.communication.mail.template.TemplateWithLinks in project simba-os by cegeka.
the class ResetPasswordServiceTest method sendMessage_WillGenerateTokenAndLinkAndSendMail_WillTriggerAuditloggingForForgotPassword.
@Test
public void sendMessage_WillGenerateTokenAndLinkAndSendMail_WillTriggerAuditloggingForForgotPassword() throws Exception {
EmailAddress email = emailFactory.email("something@mail.com");
User user = aDefaultUser().withUserName("test").withEmail(email).withLanguage(en_US).build();
Token token = Token.generateToken();
when(tokenManagerMock.generateToken(user, forgotPasswordReason)).thenReturn(token);
URL link = new URL("http://www.google.com");
List<URL> links = Collections.singletonList(link);
when(linkGeneratorMock.generateResetPasswordLinks(email, token)).thenReturn(links);
when(templateServiceMock.createMailBodyWithLink(new TemplateWithLinks(forgotPasswordReason.getTemplate(), links), en_US)).thenReturn("someBody");
when(templateServiceMock.createMailSubject(forgotPasswordReason.getSubjectTemplate(), en_US)).thenReturn("Reset password");
resetPasswordService.sendResetPasswordMessageTo(user, forgotPasswordReason);
verify(mailServiceMock).sendMail(mail().from(emailFactory.email("bla@hotmail.com")).to(email).subject("Reset password").body("someBody"));
ArgumentCaptor<AuditLogEvent> captor = ArgumentCaptor.forClass(AuditLogEvent.class);
verify(auditMock).log(captor.capture());
assertThat(captor.getValue()).extracting(AuditLogEvent::getUsername, AuditLogEvent::getMessage, AuditLogEvent::getCategory).containsExactly("test", "Email has been sent to user for following reason: ForgotPassword", AUTHENTICATION);
}
Aggregations