use of com.sanctionco.thunder.models.User 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));
}
use of com.sanctionco.thunder.models.User 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));
}
use of com.sanctionco.thunder.models.User 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));
}
use of com.sanctionco.thunder.models.User in project thunder by RohanNagar.
the class UserResourceTest method get_isSuccessful.
@Test
void get_isSuccessful() {
when(usersDao.findByEmail(EMAIL.getAddress())).thenReturn(CompletableFuture.completedFuture(USER));
var asyncResponse = mock(AsyncResponse.class);
var captor = ArgumentCaptor.forClass(Response.class);
resource.getUser(asyncResponse, key, "password", 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));
}
use of com.sanctionco.thunder.models.User in project thunder by RohanNagar.
the class UserResourceTest method create_shouldSucceed.
@Test
void create_shouldSucceed() {
when(usersDao.insert(any(User.class))).thenReturn(CompletableFuture.completedFuture(UPDATED_USER));
var asyncResponse = mock(AsyncResponse.class);
var captor = ArgumentCaptor.forClass(Response.class);
resource.postUser(asyncResponse, key, USER);
verify(asyncResponse, timeout(100).times(1)).resume(captor.capture());
User result = (User) captor.getValue().getEntity();
assertAll("Response is correct for a successful creation", () -> assertEquals(Response.Status.CREATED, captor.getValue().getStatusInfo()), () -> assertEquals(UPDATED_USER, result));
}
Aggregations