Search in sources :

Example 6 with RequestValidator

use of com.sanctionco.thunder.validation.RequestValidator in project thunder by RohanNagar.

the class UserResourceTest method testUpdateUserServerSideHashNoPasswordChange.

@Test
void testUpdateUserServerSideHashNoPasswordChange() {
    var hashService = HashAlgorithm.SHA256.newHashService(true, false);
    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 the same password
    var updatedUser = new User(new Email(existingEmail.getAddress(), true, "token"), // hashes to the above
    "password", Collections.singletonMap("ID", 80));
    // Expect that the password stays the same
    var expectedResponse = new User(new Email(updatedUser.getEmail().getAddress(), true, "token"), "saltysaltysalt226cb4d24e21a9955515d52d6dc86449202f55f5b1463a800d2803cdda90298530", 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("password", result.getPassword()), () -> assertEquals(expectedResponse, userCaptor.getValue()), () -> 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 7 with RequestValidator

use of com.sanctionco.thunder.validation.RequestValidator in project thunder by RohanNagar.

the class UserResourceTest method put_whenPasswordHeaderCheckIsDisabledThenMissingPasswordSucceeds.

@Test
void put_whenPasswordHeaderCheckIsDisabledThenMissingPasswordSucceeds() {
    var validator = new RequestValidator(EMAIL_VALIDATOR, propertyValidator, HASH_SERVICE, false);
    var resource = new UserResource(usersDao, OPTIONS, validator, HASH_SERVICE, 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, "password", Collections.emptyMap());
    // Define the updated user with changed verification info
    var updatedUser = new User(new Email(existingEmail.getAddress(), false, "changedToken"), "password", Collections.singletonMap("Key", "Value"));
    // 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));
    // Update with a missing password header
    resource.updateUser(asyncResponse, key, null, 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) RequestValidator(com.sanctionco.thunder.validation.RequestValidator) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

User (com.sanctionco.thunder.models.User)7 RequestValidator (com.sanctionco.thunder.validation.RequestValidator)7 Test (org.junit.jupiter.api.Test)7 Email (com.sanctionco.thunder.models.Email)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5