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());
}
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));
}
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());
}
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));
}
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));
}
});
}
Aggregations