Search in sources :

Example 1 with User

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));
}
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 User

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));
}
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 3 with User

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));
}
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 4 with User

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));
}
Also used : User(com.sanctionco.thunder.models.User) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with User

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));
}
Also used : User(com.sanctionco.thunder.models.User) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

User (com.sanctionco.thunder.models.User)51 Test (org.junit.jupiter.api.Test)41 Email (com.sanctionco.thunder.models.Email)26 RequestValidator (com.sanctionco.thunder.validation.RequestValidator)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 UsersDao (com.sanctionco.thunder.dao.UsersDao)8 Objects (java.util.Objects)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8 UUID (java.util.UUID)7 BsonDocument (org.bson.BsonDocument)6 Document (org.bson.Document)6 Metered (com.codahale.metrics.annotation.Metered)5 Counter (com.codahale.metrics.Counter)4 MetricRegistry (com.codahale.metrics.MetricRegistry)4 ThunderException (com.sanctionco.thunder.ThunderException)4 SwaggerAnnotations (com.sanctionco.thunder.openapi.SwaggerAnnotations)4 MetricNameUtil (com.sanctionco.thunder.util.MetricNameUtil)4 RequestValidationException (com.sanctionco.thunder.validation.RequestValidationException)4 POST (javax.ws.rs.POST)4