Search in sources :

Example 1 with Email

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

the class UserResourceTest method put_userWithNewEmailSucceeds.

@Test
void put_userWithNewEmailSucceeds() {
    // Set up the user that should already exist in the database
    var existingEmail = new Email("existing@test.com", true, "token");
    var existingUser = new User(existingEmail, "password", Collections.emptyMap());
    // Define the updated user with a new email address
    var updatedUser = new User(new Email("newemail@test.com", true, "token"), "newPassword", Collections.emptyMap());
    // Define the expected user object
    var expectedResponse = new User(new Email(updatedUser.getEmail().getAddress(), false, null), updatedUser.getPassword(), updatedUser.getProperties());
    var userCaptor = ArgumentCaptor.forClass(User.class);
    var asyncResponse = mock(AsyncResponse.class);
    when(usersDao.findByEmail(existingEmail.getAddress())).thenReturn(CompletableFuture.completedFuture(existingUser));
    when(usersDao.update(eq(existingEmail.getAddress()), userCaptor.capture())).thenReturn(CompletableFuture.completedFuture(expectedResponse));
    resource.updateUser(asyncResponse, key, "password", "existing@test.com", updatedUser);
    var responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(asyncResponse, timeout(100).times(1)).resume(responseCaptor.capture());
    var result = (User) responseCaptor.getValue().getEntity();
    assertAll("Assert successful user update with new email", () -> assertEquals(Response.Status.OK, responseCaptor.getValue().getStatusInfo()), () -> assertEquals(expectedResponse, userCaptor.getValue()), () -> assertEquals(expectedResponse, result));
}
Also used : Email(com.sanctionco.thunder.models.Email) User(com.sanctionco.thunder.models.User) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with Email

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

the class UserResourceTest method put_NewPasswordShouldBeHashed.

@Test
void put_NewPasswordShouldBeHashed() {
    var hashService = spy(HashAlgorithm.SHA256.newHashService(true, false));
    when(hashService.hash(anyString())).thenReturn("hashbrowns");
    var validator = new RequestValidator(EMAIL_VALIDATOR, propertyValidator, hashService, true);
    var resource = new UserResource(usersDao, OPTIONS, validator, hashService, METRICS);
    // Set up the user that should already exist in the database
    var existingEmail = new Email("existing@test.com", true, "token");
    var existingUser = new User(existingEmail, "saltysaltysalt226cb4d24e21a9955515d52d6dc86449202f55f5b1463a800d2803cdda90298530", Collections.emptyMap());
    // Define the updated user with changed password
    var updatedUser = new User(new Email(existingEmail.getAddress(), true, "token"), "newPassword", Collections.emptyMap());
    // Expect that the new password is hashed
    var expectedResponse = new User(new Email(updatedUser.getEmail().getAddress(), true, "token"), "hashbrowns", updatedUser.getProperties());
    var userCaptor = ArgumentCaptor.forClass(User.class);
    var asyncResponse = mock(AsyncResponse.class);
    when(usersDao.findByEmail(existingEmail.getAddress())).thenReturn(CompletableFuture.completedFuture(existingUser));
    when(usersDao.update(eq(null), userCaptor.capture())).thenReturn(CompletableFuture.completedFuture(expectedResponse));
    resource.updateUser(asyncResponse, key, "password", null, updatedUser);
    var responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(asyncResponse, timeout(100).times(1)).resume(responseCaptor.capture());
    var result = (User) responseCaptor.getValue().getEntity();
    assertAll("Assert successful user update", () -> assertEquals(Response.Status.OK, responseCaptor.getValue().getStatusInfo()), () -> assertNotEquals("newPassword", result.getPassword()), () -> assertEquals(expectedResponse, userCaptor.getValue()), () -> assertEquals("hashbrowns", userCaptor.getValue().getPassword()), () -> assertEquals(expectedResponse, result));
}
Also used : Email(com.sanctionco.thunder.models.Email) User(com.sanctionco.thunder.models.User) RequestValidator(com.sanctionco.thunder.validation.RequestValidator) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with Email

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

the class UserResourceTest method put_shouldSucceed.

@Test
void put_shouldSucceed() {
    // Set up the user that should already exist in the database
    var existingEmail = new Email("existing@test.com", true, "token");
    var existingUser = new User(existingEmail, "password", Collections.emptyMap());
    // Define the updated user with changed verification info
    var updatedUser = new User(new Email(existingEmail.getAddress(), false, "changedToken"), "newPassword", Collections.emptyMap());
    // Expect that the existing verification information stays the same even though
    // the updated user had different information
    var expectedResponse = new User(new Email(updatedUser.getEmail().getAddress(), true, "token"), updatedUser.getPassword(), updatedUser.getProperties());
    var userCaptor = ArgumentCaptor.forClass(User.class);
    var asyncResponse = mock(AsyncResponse.class);
    when(usersDao.findByEmail(existingEmail.getAddress())).thenReturn(CompletableFuture.completedFuture(existingUser));
    when(usersDao.update(eq(null), userCaptor.capture())).thenReturn(CompletableFuture.completedFuture(expectedResponse));
    resource.updateUser(asyncResponse, key, "password", null, updatedUser);
    var responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(asyncResponse, timeout(100).times(1)).resume(responseCaptor.capture());
    var result = (User) responseCaptor.getValue().getEntity();
    assertAll("Assert successful user update", () -> assertEquals(Response.Status.OK, responseCaptor.getValue().getStatusInfo()), () -> assertEquals(expectedResponse, userCaptor.getValue()), () -> assertEquals(expectedResponse, result));
}
Also used : Email(com.sanctionco.thunder.models.Email) User(com.sanctionco.thunder.models.User) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with Email

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

the class VerificationResourceTest method reset_disabledPasswordHeaderCheckAndNullPasswordSucceeds.

@Test
void reset_disabledPasswordHeaderCheckAndNullPasswordSucceeds() {
    var requestValidator = new RequestValidator(EMAIL_VALIDATOR, propertyValidator, hashService, false);
    var resource = new VerificationResource(usersDao, OPTIONS, requestValidator, emailService, METRICS);
    // Set up the user that should already exist in the database
    Email existingEmail = new Email("existing@test.com", true, "token");
    User existingUser = new User(existingEmail, "password", Collections.emptyMap());
    // Set up expected user object
    Email updatedEmail = new Email("existing@test.com", false, null);
    User updatedUser = new User(updatedEmail, "password", Collections.emptyMap());
    var userCaptor = ArgumentCaptor.forClass(User.class);
    when(usersDao.findByEmail(existingEmail.getAddress())).thenReturn(CompletableFuture.completedFuture(existingUser));
    when(usersDao.update(eq(null), userCaptor.capture())).thenReturn(CompletableFuture.completedFuture(updatedUser));
    var asyncResponse = mock(AsyncResponse.class);
    var responseCaptor = ArgumentCaptor.forClass(Response.class);
    resource.resetVerified(asyncResponse, key, existingEmail.getAddress(), null);
    verify(asyncResponse, timeout(100).times(1)).resume(responseCaptor.capture());
    User result = (User) responseCaptor.getValue().getEntity();
    assertAll("Assert successful verification status reset", () -> assertEquals(responseCaptor.getValue().getStatusInfo(), Response.Status.OK), () -> assertEquals(updatedUser, userCaptor.getValue()), () -> assertEquals(updatedUser, result));
}
Also used : Email(com.sanctionco.thunder.models.Email) User(com.sanctionco.thunder.models.User) RequestValidator(com.sanctionco.thunder.validation.RequestValidator) Test(org.junit.jupiter.api.Test)

Example 5 with Email

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

the class RequestValidatorTest method testValidateUserNullEmailAddress.

@Test
void testValidateUserNullEmailAddress() {
    Email email = new Email(null, false, "token");
    User user = new User(email, "password");
    RequestValidationException e = assertThrows(RequestValidationException.class, () -> validator.validate(user));
    assertEquals("Invalid email address format. Please try again.", 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)

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