Search in sources :

Example 31 with User

use of com.sanctionco.thunder.models.User 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)

Example 32 with User

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

the class VerificationResourceTest method email_isSuccessful.

@Test
void email_isSuccessful() {
    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 asyncResponse = mock(AsyncResponse.class);
    var captor = ArgumentCaptor.forClass(Response.class);
    resource.sendEmail(uriInfo, asyncResponse, key, "test@test.com", "password");
    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) Test(org.junit.jupiter.api.Test)

Example 33 with User

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

the class VerificationResourceTest method verify_isSuccessful.

@Test
void verify_isSuccessful() {
    when(usersDao.findByEmail("test@test.com")).thenReturn(CompletableFuture.completedFuture(unverifiedMockUser));
    when(usersDao.update(unverifiedMockUser.getEmail().getAddress(), verifiedMockUser)).thenReturn(CompletableFuture.completedFuture(verifiedMockUser));
    var asyncResponse = mock(AsyncResponse.class);
    var captor = ArgumentCaptor.forClass(Response.class);
    resource.verifyEmail(asyncResponse, "test@test.com", "verificationToken", ResponseType.JSON);
    verify(asyncResponse, timeout(100).times(1)).resume(captor.capture());
    User result = (User) captor.getValue().getEntity();
    assertAll("Assert successful verify email with JSON response", () -> assertEquals(captor.getValue().getStatusInfo(), Response.Status.OK), () -> assertEquals(verifiedMockUser, result));
}
Also used : User(com.sanctionco.thunder.models.User) Test(org.junit.jupiter.api.Test)

Example 34 with User

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

the class VerificationResourceTest method reset_isSuccessful.

@Test
void reset_isSuccessful() {
    // 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(), existingUser.getPassword());
    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) Test(org.junit.jupiter.api.Test)

Example 35 with User

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

the class RequestValidatorTest method testValidateUserInvalidEmailAddress.

@Test
void testValidateUserInvalidEmailAddress() {
    Email email = new Email("notARealEmail", 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

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