Search in sources :

Example 16 with User

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

the class ThunderClientFakeTest method ensureVerifyWorksWithResponseType.

@Test
void ensureVerifyWorksWithResponseType() {
    var client = ThunderClient.fake();
    var user = new User(Email.unverified(ADDRESS), PASSWORD, Collections.emptyMap());
    client.postUser(user).join();
    var token = client.sendVerificationEmail(ADDRESS, PASSWORD).join().getEmail().getVerificationToken();
    assertEquals("Verified", client.verifyUser(ADDRESS, token, ResponseType.HTML).join());
    var expectedUserString = new User(new Email(ADDRESS, true, token), PASSWORD, Collections.emptyMap()).toString();
    assertEquals(expectedUserString, client.verifyUser(ADDRESS, token, ResponseType.JSON).join());
}
Also used : User(com.sanctionco.thunder.models.User) Email(com.sanctionco.thunder.models.Email) Test(org.junit.jupiter.api.Test)

Example 17 with User

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

Example 18 with User

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

the class RequestValidatorTest method testValidateNullPassword.

@Test
void testValidateNullPassword() {
    var propertyValidator = mock(PropertyValidator.class);
    var validator = new RequestValidator(EMAIL_VALIDATOR, propertyValidator, HASH_SERVICE, true);
    when(propertyValidator.isValidPropertiesMap(anyMap())).thenReturn(true);
    Email email = new Email("test@test.com", false, "token");
    User user = new User(email, "password");
    RequestValidationException e = assertThrows(RequestValidationException.class, () -> validator.validate(null, "test@test.com", user));
    assertEquals("Credentials are required to access this resource.", 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)

Example 19 with User

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

the class RequestValidatorTest method testValidateSuccess.

@Test
void testValidateSuccess() {
    var propertyValidator = mock(PropertyValidator.class);
    var validator = new RequestValidator(EMAIL_VALIDATOR, propertyValidator, HASH_SERVICE, true);
    when(propertyValidator.isValidPropertiesMap(anyMap())).thenReturn(true);
    Email email = new Email("test@test.com", false, "token");
    User user = new User(email, "password");
    assertDoesNotThrow(() -> validator.validate("password", "test@test.com", user));
}
Also used : Email(com.sanctionco.thunder.models.Email) User(com.sanctionco.thunder.models.User) Test(org.junit.jupiter.api.Test)

Example 20 with User

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

the class VerificationResource method resetVerified.

/**
 * Resets the verification status of the user with the given email and password.
 *
 * @param response the async response object used to notify that the operation has completed
 * @param auth the auth principal required to access the resource
 * @param email the user's email address
 * @param password the user's password
 */
@POST
@Path("/reset")
@Metered(name = "reset-verification-requests")
@SwaggerAnnotations.Methods.Reset
public void resetVerified(@Suspended AsyncResponse response, @Parameter(hidden = true) @Auth Principal auth, @Parameter(hidden = true) @QueryParam("email") String email, @Parameter(hidden = true) @HeaderParam("password") String password) {
    requestOptions.setTimeout(response, resetTimeoutCounter);
    try {
        requestValidator.validate(password, email, false);
    } catch (RequestValidationException e) {
        response.resume(e.response(email));
        return;
    }
    LOG.info("Attempting to reset verification status for user {}", email);
    usersDao.findByEmail(email).thenApply(user -> {
        // Check that the supplied password is correct for the user's account
        requestValidator.verifyPasswordHeader(password, user.getPassword());
        return new User(new Email(user.getEmail().getAddress(), false, null), user.getPassword(), user.getProperties());
    }).thenCompose(user -> usersDao.update(null, user)).whenComplete((result, throwable) -> {
        if (Objects.isNull(throwable)) {
            LOG.info("Successfully reset verification status for user {}.", email);
            response.resume(Response.ok(result).build());
        } else {
            LOG.error("Error resetting status of {}. Caused by {}", email, throwable.getMessage());
            response.resume(ThunderException.responseFromThrowable(throwable, email));
        }
    });
}
Also used : Email(com.sanctionco.thunder.models.Email) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) Auth(io.dropwizard.auth.Auth) ResponseType(com.sanctionco.thunder.models.ResponseType) SwaggerAnnotations(com.sanctionco.thunder.openapi.SwaggerAnnotations) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) Counter(com.codahale.metrics.Counter) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) RequestValidationException(com.sanctionco.thunder.validation.RequestValidationException) MetricRegistry(com.codahale.metrics.MetricRegistry) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) Logger(org.slf4j.Logger) Metered(com.codahale.metrics.annotation.Metered) AsyncResponse(javax.ws.rs.container.AsyncResponse) UUID(java.util.UUID) Suspended(javax.ws.rs.container.Suspended) EmailService(com.sanctionco.thunder.email.EmailService) Parameter(io.swagger.v3.oas.annotations.Parameter) Objects(java.util.Objects) Principal(java.security.Principal) User(com.sanctionco.thunder.models.User) Response(javax.ws.rs.core.Response) MetricNameUtil(com.sanctionco.thunder.util.MetricNameUtil) ThunderException(com.sanctionco.thunder.ThunderException) UriInfo(javax.ws.rs.core.UriInfo) UsersDao(com.sanctionco.thunder.dao.UsersDao) RequestValidator(com.sanctionco.thunder.validation.RequestValidator) User(com.sanctionco.thunder.models.User) Email(com.sanctionco.thunder.models.Email) RequestValidationException(com.sanctionco.thunder.validation.RequestValidationException) Path(javax.ws.rs.Path) Metered(com.codahale.metrics.annotation.Metered) POST(javax.ws.rs.POST)

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