Search in sources :

Example 6 with Email

use of com.sanctionco.thunder.models.Email in project thunder by RohanNagar.

the class ThunderClientFake method sendVerificationEmail.

@Override
public CompletableFuture<User> sendVerificationEmail(String email, String password) {
    // This does not actually send an email, but it does update the verification token of the user.
    var user = inMemoryStore.get(email);
    if (user == null) {
        return fail(404);
    }
    if (requirePasswordHeader && !user.getPassword().equals(password)) {
        return fail(401);
    }
    var updated = new User(new Email(user.getEmail().getAddress(), user.getEmail().isVerified(), UUID.randomUUID().toString()), user.getPassword(), user.getProperties());
    inMemoryStore.put(email, updated);
    return CompletableFuture.completedFuture(updated);
}
Also used : User(com.sanctionco.thunder.models.User) Email(com.sanctionco.thunder.models.Email)

Example 7 with Email

use of com.sanctionco.thunder.models.Email in project thunder by RohanNagar.

the class ThunderClientFakeTest method ensureVerifyWorksWithResponseType.

@Test
void ensureVerifyWorksWithResponseType() {
    var client = ThunderClient.fake();
    var user = new User(Email.unverified(ADDRESS), PASSWORD, Collections.emptyMap());
    client.postUser(user).join();
    var token = client.sendVerificationEmail(ADDRESS, PASSWORD).join().getEmail().getVerificationToken();
    assertEquals("Verified", client.verifyUser(ADDRESS, token, ResponseType.HTML).join());
    var expectedUserString = new User(new Email(ADDRESS, true, token), PASSWORD, Collections.emptyMap()).toString();
    assertEquals(expectedUserString, client.verifyUser(ADDRESS, token, ResponseType.JSON).join());
}
Also used : User(com.sanctionco.thunder.models.User) Email(com.sanctionco.thunder.models.Email) Test(org.junit.jupiter.api.Test)

Example 8 with Email

use of com.sanctionco.thunder.models.Email in project thunder by RohanNagar.

the class VerificationResourceTest method email_disabledPasswordHeaderCheckWithNullPasswordSucceeds.

@Test
void email_disabledPasswordHeaderCheckWithNullPasswordSucceeds() {
    when(usersDao.findByEmail(anyString())).thenReturn(CompletableFuture.completedFuture(unverifiedMockUser));
    when(usersDao.update(anyString(), any(User.class))).thenReturn(CompletableFuture.completedFuture(unverifiedMockUser));
    when(emailService.sendVerificationEmail(any(Email.class), anyString())).thenReturn(CompletableFuture.completedFuture(true));
    var requestValidator = new RequestValidator(EMAIL_VALIDATOR, propertyValidator, hashService, false);
    var resource = new VerificationResource(usersDao, OPTIONS, requestValidator, emailService, METRICS);
    var asyncResponse = mock(AsyncResponse.class);
    var captor = ArgumentCaptor.forClass(Response.class);
    resource.sendEmail(uriInfo, asyncResponse, key, "test@test.com", null);
    verify(asyncResponse, timeout(100).times(1)).resume(captor.capture());
    User result = (User) captor.getValue().getEntity();
    assertAll("Assert successful send email", () -> assertEquals(captor.getValue().getStatusInfo(), Response.Status.OK), () -> assertEquals(unverifiedMockUser, result));
}
Also used : User(com.sanctionco.thunder.models.User) Email(com.sanctionco.thunder.models.Email) RequestValidator(com.sanctionco.thunder.validation.RequestValidator) Test(org.junit.jupiter.api.Test)

Example 9 with Email

use of com.sanctionco.thunder.models.Email in project thunder by RohanNagar.

the class RequestValidatorTest method testValidateNullPassword.

@Test
void testValidateNullPassword() {
    var propertyValidator = mock(PropertyValidator.class);
    var validator = new RequestValidator(EMAIL_VALIDATOR, propertyValidator, HASH_SERVICE, true);
    when(propertyValidator.isValidPropertiesMap(anyMap())).thenReturn(true);
    Email email = new Email("test@test.com", false, "token");
    User user = new User(email, "password");
    RequestValidationException e = assertThrows(RequestValidationException.class, () -> validator.validate(null, "test@test.com", user));
    assertEquals("Credentials are required to access this resource.", e.getMessage());
    assertEquals(RequestValidationException.Error.INVALID_PARAMETERS, e.getError());
}
Also used : Email(com.sanctionco.thunder.models.Email) User(com.sanctionco.thunder.models.User) Test(org.junit.jupiter.api.Test)

Example 10 with Email

use of com.sanctionco.thunder.models.Email in project thunder by RohanNagar.

the class RequestValidatorTest method testValidateSuccess.

@Test
void testValidateSuccess() {
    var propertyValidator = mock(PropertyValidator.class);
    var validator = new RequestValidator(EMAIL_VALIDATOR, propertyValidator, HASH_SERVICE, true);
    when(propertyValidator.isValidPropertiesMap(anyMap())).thenReturn(true);
    Email email = new Email("test@test.com", false, "token");
    User user = new User(email, "password");
    assertDoesNotThrow(() -> validator.validate("password", "test@test.com", user));
}
Also used : Email(com.sanctionco.thunder.models.Email) User(com.sanctionco.thunder.models.User) Test(org.junit.jupiter.api.Test)

Aggregations

Email (com.sanctionco.thunder.models.Email)27 User (com.sanctionco.thunder.models.User)26 Test (org.junit.jupiter.api.Test)20 RequestValidator (com.sanctionco.thunder.validation.RequestValidator)9 MetricRegistry (com.codahale.metrics.MetricRegistry)5 Metered (com.codahale.metrics.annotation.Metered)5 RequestValidationException (com.sanctionco.thunder.validation.RequestValidationException)5 Objects (java.util.Objects)5 Inject (javax.inject.Inject)5 POST (javax.ws.rs.POST)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Counter (com.codahale.metrics.Counter)4 ThunderException (com.sanctionco.thunder.ThunderException)4 UsersDao (com.sanctionco.thunder.dao.UsersDao)4 EmailService (com.sanctionco.thunder.email.EmailService)4 SwaggerAnnotations (com.sanctionco.thunder.openapi.SwaggerAnnotations)4 MetricNameUtil (com.sanctionco.thunder.util.MetricNameUtil)4 Auth (io.dropwizard.auth.Auth)4