Search in sources :

Example 1 with RequestValidator

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

the class UserResourceTest method get_withDisabledPasswordCheckSucceedsWithIncorrectPassword.

@Test
void get_withDisabledPasswordCheckSucceedsWithIncorrectPassword() {
    var validator = new RequestValidator(EMAIL_VALIDATOR, propertyValidator, HASH_SERVICE, false);
    var resource = new UserResource(usersDao, OPTIONS, validator, HASH_SERVICE, METRICS);
    when(usersDao.findByEmail(EMAIL.getAddress())).thenReturn(CompletableFuture.completedFuture(USER));
    var asyncResponse = mock(AsyncResponse.class);
    var captor = ArgumentCaptor.forClass(Response.class);
    resource.getUser(asyncResponse, key, null, EMAIL.getAddress());
    verify(asyncResponse, timeout(100).times(1)).resume(captor.capture());
    User result = (User) captor.getValue().getEntity();
    assertAll("Assert successful get user", () -> assertEquals(Response.Status.OK, captor.getValue().getStatusInfo()), () -> assertEquals(USER, result));
}
Also used : 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 2 with RequestValidator

use of com.sanctionco.thunder.validation.RequestValidator 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 RequestValidator

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

the class UserResourceTest method delete_nullPasswordWithDisabledHeaderCheckSucceeds.

@Test
void delete_nullPasswordWithDisabledHeaderCheckSucceeds() {
    var validator = new RequestValidator(EMAIL_VALIDATOR, propertyValidator, HASH_SERVICE, false);
    var resource = new UserResource(usersDao, OPTIONS, validator, HASH_SERVICE, METRICS);
    when(usersDao.findByEmail(EMAIL.getAddress())).thenReturn(CompletableFuture.completedFuture(USER));
    when(usersDao.delete(EMAIL.getAddress())).thenReturn(CompletableFuture.completedFuture(USER));
    var asyncResponse = mock(AsyncResponse.class);
    var captor = ArgumentCaptor.forClass(Response.class);
    resource.deleteUser(asyncResponse, key, null, EMAIL.getAddress());
    verify(asyncResponse, timeout(100).times(1)).resume(captor.capture());
    User result = (User) captor.getValue().getEntity();
    assertAll("Assert successful delete user", () -> assertEquals(Response.Status.OK, captor.getValue().getStatusInfo()), () -> assertEquals(USER, result));
}
Also used : 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 4 with RequestValidator

use of com.sanctionco.thunder.validation.RequestValidator 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 RequestValidator

use of com.sanctionco.thunder.validation.RequestValidator 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)

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