Search in sources :

Example 1 with ImmutableEmail

use of com.nexblocks.authguard.external.email.ImmutableEmail in project AuthGuard by AuthGuard.

the class VerificationSubscriberTest method onMessage.

@Test
void onMessage() {
    final AccountBO account = AccountBO.builder().id("account-id").email(AccountEmailBO.builder().email("unverified").verified(false).build()).build();
    final VerificationRequestBO verificationRequest = VerificationRequestBO.builder().account(account).emails(Collections.singletonList(account.getEmail())).build();
    final Message<VerificationRequestBO> message = Message.<VerificationRequestBO>builder().eventType(EventType.EMAIL_VERIFICATION).bodyType(VerificationRequestBO.class).messageBody(verificationRequest).build();
    verificationSubscriber.onMessage(message);
    final ArgumentCaptor<AccountTokenDO> accountTokenCaptor = ArgumentCaptor.forClass(AccountTokenDO.class);
    final ArgumentCaptor<ImmutableEmail> emailCaptor = ArgumentCaptor.forClass(ImmutableEmail.class);
    Mockito.verify(accountTokensRepository).save(accountTokenCaptor.capture());
    Mockito.verify(emailProvider, Mockito.times(1)).send(emailCaptor.capture());
    final AccountTokenDO accountToken = accountTokenCaptor.getValue();
    final ImmutableEmail email = emailCaptor.getValue();
    assertThat(accountToken.getAssociatedAccountId()).isEqualTo(account.getId());
    assertThat(accountToken.getAdditionalInformation().get("email")).isEqualTo("unverified");
    assertThat(accountToken.getToken()).isNotNull();
    assertThat(accountToken.getExpiresAt()).isNotNull();
    assertThat(email.getTo()).isEqualTo("unverified");
    assertThat(email.getBody()).isNull();
    assertThat(email.getParameters()).containsOnlyKeys("token");
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) AccountTokenDO(com.nexblocks.authguard.dal.model.AccountTokenDO) VerificationRequestBO(com.nexblocks.authguard.service.model.VerificationRequestBO) ImmutableEmail(com.nexblocks.authguard.external.email.ImmutableEmail) Test(org.junit.jupiter.api.Test)

Example 2 with ImmutableEmail

use of com.nexblocks.authguard.external.email.ImmutableEmail in project AuthGuard by AuthGuard.

the class EmailOtpSubscriber method sendEmail.

private void sendEmail(final AccountBO account, final OneTimePasswordBO otp) {
    if (account.getEmail() != null) {
        final ImmutableMap.Builder<String, String> parameters = ImmutableMap.builder();
        if (account.getFirstName() != null) {
            parameters.put("firstName", account.getFirstName());
        }
        if (account.getLastName() != null) {
            parameters.put("lastName", account.getLastName());
        }
        final ImmutableEmail email = ImmutableEmail.builder().template("otp").parameters(parameters.put("password", otp.getPassword()).build()).to(account.getEmail().getEmail()).build();
        emailProvider.send(email);
    } else {
        LOG.error("An email OTP was generated for an account without an email. Account: {}, password ID: {}", account.getId(), otp.getId());
    }
}
Also used : ImmutableEmail(com.nexblocks.authguard.external.email.ImmutableEmail) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with ImmutableEmail

use of com.nexblocks.authguard.external.email.ImmutableEmail in project AuthGuard by AuthGuard.

the class EmailPasswordlessSubscriber method sendEmail.

private void sendEmail(final AccountBO account, final AccountTokenDO accountToken) {
    if (account.getEmail() != null) {
        final ImmutableMap.Builder<String, String> parameters = ImmutableMap.builder();
        if (account.getFirstName() != null) {
            parameters.put("firstName", account.getFirstName());
        }
        if (account.getLastName() != null) {
            parameters.put("lastName", account.getLastName());
        }
        final ImmutableEmail email = ImmutableEmail.builder().template("passwordless").parameters(parameters.put("token", accountToken.getToken()).build()).to(account.getEmail().getEmail()).build();
        emailProvider.send(email);
    } else {
        LOG.error("A passwordless token was generated for an account without an email. Account: {}, token ID: {}", account.getId(), accountToken.getId());
    }
}
Also used : ImmutableEmail(com.nexblocks.authguard.external.email.ImmutableEmail) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with ImmutableEmail

use of com.nexblocks.authguard.external.email.ImmutableEmail in project AuthGuard by AuthGuard.

the class EmailResetTokenSubscriber method sendEmail.

private void sendEmail(final AccountBO account, final AccountTokenDO accountToken) {
    if (account.getEmail() != null) {
        final ImmutableMap.Builder<String, String> parameters = ImmutableMap.builder();
        if (account.getFirstName() != null) {
            parameters.put("firstName", account.getFirstName());
        }
        if (account.getLastName() != null) {
            parameters.put("lastName", account.getLastName());
        }
        final ImmutableEmail email = ImmutableEmail.builder().template("passwordReset").parameters(parameters.put("token", accountToken.getToken()).build()).to(account.getEmail().getEmail()).build();
        emailProvider.send(email);
    } else {
        LOG.error("A password reset token was generated for an account without an email. Account: {}, token ID: {}", account.getId(), accountToken.getId());
    }
}
Also used : ImmutableEmail(com.nexblocks.authguard.external.email.ImmutableEmail) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 5 with ImmutableEmail

use of com.nexblocks.authguard.external.email.ImmutableEmail in project AuthGuard by AuthGuard.

the class EmailOtpSubscriberTest method onValidMessage.

@Test
void onValidMessage() {
    final OneTimePasswordBO otp = OneTimePasswordBO.builder().password("password").build();
    final AccountBO account = AccountBO.builder().email(AccountEmailBO.builder().email("user@test.net").build()).firstName("first").lastName("second").build();
    final OtpMessageBody messageBody = new OtpMessageBody(otp, account, true, false);
    final Message message = Messages.otpGenerated(messageBody);
    final ImmutableEmail expectedEmail = ImmutableEmail.builder().template("otp").to(account.getEmail().getEmail()).parameters(ImmutableMap.of("password", otp.getPassword(), "firstName", account.getFirstName(), "lastName", account.getLastName())).build();
    otpSubscriber.onMessage(message);
    final ArgumentCaptor<ImmutableEmail> sentEmailCaptor = ArgumentCaptor.forClass(ImmutableEmail.class);
    Mockito.verify(emailProvider).send(sentEmailCaptor.capture());
    assertThat(sentEmailCaptor.getValue()).isEqualTo(expectedEmail);
}
Also used : AccountBO(com.nexblocks.authguard.service.model.AccountBO) OtpMessageBody(com.nexblocks.authguard.basic.otp.OtpMessageBody) Message(com.nexblocks.authguard.emb.model.Message) OneTimePasswordBO(com.nexblocks.authguard.service.model.OneTimePasswordBO) ImmutableEmail(com.nexblocks.authguard.external.email.ImmutableEmail) Test(org.junit.jupiter.api.Test)

Aggregations

ImmutableEmail (com.nexblocks.authguard.external.email.ImmutableEmail)8 AccountBO (com.nexblocks.authguard.service.model.AccountBO)5 AccountTokenDO (com.nexblocks.authguard.dal.model.AccountTokenDO)4 Test (org.junit.jupiter.api.Test)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Message (com.nexblocks.authguard.emb.model.Message)3 OtpMessageBody (com.nexblocks.authguard.basic.otp.OtpMessageBody)1 PasswordlessMessageBody (com.nexblocks.authguard.basic.passwordless.PasswordlessMessageBody)1 ResetTokenMessage (com.nexblocks.authguard.service.messaging.ResetTokenMessage)1 OneTimePasswordBO (com.nexblocks.authguard.service.model.OneTimePasswordBO)1 VerificationRequestBO (com.nexblocks.authguard.service.model.VerificationRequestBO)1