use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.
the class ChangePasswordTest method testUserWithoutPassword.
@Test
void testUserWithoutPassword() throws IOException {
final User user = new User();
user.setName("Example 2");
user.setEmail("pwd-2@example.com");
user.setRoles("user");
Cookie cookie = null;
try (MinijaxRequestContext ctx = createRequestContext()) {
ctx.getResource(Dao.class).create(user);
cookie = ctx.getResource(Security.class).loginAs(user);
}
final Form form = new Form();
form.param("csrf", cookie.getValue());
form.param("oldPassword", "my-old-password");
form.param("newPassword", "my-new-password");
form.param("confirmNewPassword", "my-new-password");
final Response r = target("/changepassword").request().cookie(cookie).post(Entity.form(form));
assertNotNull(r);
assertEquals(400, r.getStatus());
}
use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.
the class ChangePasswordTest method testMismatchedPasswords.
@Test
void testMismatchedPasswords() throws IOException {
final User user = new User();
user.setName("Example 4");
user.setEmail("pwd-4@example.com");
user.setRoles("user");
user.setPassword("my-old-password");
Cookie cookie = null;
try (MinijaxRequestContext ctx = createRequestContext()) {
ctx.getResource(Dao.class).create(user);
cookie = ctx.getResource(Security.class).loginAs(user);
}
final Form form = new Form();
form.param("csrf", cookie.getValue());
form.param("oldPassword", "my-old-password");
form.param("newPassword", "my-new-password");
form.param("confirmNewPassword", "different-password");
final Response r = target("/changepassword").request().cookie(cookie).post(Entity.form(form));
assertNotNull(r);
assertEquals(400, r.getStatus());
}
use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.
the class ChangePasswordTest method testChangePasswordSuccess.
@Test
void testChangePasswordSuccess() throws IOException {
final User user = new User();
user.setName("Example 1");
user.setEmail("pwd-1@example.com");
user.setRoles("user");
user.setPassword("my-old-password");
Cookie cookie = null;
try (MinijaxRequestContext ctx = createRequestContext()) {
ctx.getResource(Dao.class).create(user);
cookie = ctx.getResource(Security.class).loginAs(user);
}
final Form form = new Form();
form.param("csrf", cookie.getValue());
form.param("oldPassword", "my-old-password");
form.param("newPassword", "my-new-password");
form.param("confirmNewPassword", "my-new-password");
final Response r = target("/changepassword").request().cookie(cookie).post(Entity.form(form));
assertNotNull(r);
assertEquals(200, r.getStatus());
try (MinijaxRequestContext ctx = createRequestContext()) {
final User check = ctx.getResource(Dao.class).read(User.class, user.getId());
assertFalse(BCrypt.checkpw("my-old-password", check.getPasswordHash()));
assertTrue(BCrypt.checkpw("my-new-password", check.getPasswordHash()));
}
}
use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.
the class ChangePasswordTest method testPasswordTooShort.
@Test
void testPasswordTooShort() throws IOException {
final User user = new User();
user.setName("Example 5");
user.setEmail("pwd-5@example.com");
user.setRoles("user");
user.setPassword("my-old-password");
Cookie cookie = null;
try (MinijaxRequestContext ctx = createRequestContext()) {
ctx.getResource(Dao.class).create(user);
cookie = ctx.getResource(Security.class).loginAs(user);
}
final Form form = new Form();
form.param("csrf", cookie.getValue());
form.param("oldPassword", "my-old-password");
form.param("newPassword", "foo");
form.param("confirmNewPassword", "foo");
final Response r = target("/changepassword").request().cookie(cookie).post(Entity.form(form));
assertNotNull(r);
assertEquals(400, r.getStatus());
}
use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.
the class RolesAllowedTest method testLogin.
@Test
void testLogin() throws Exception {
try (final MinijaxRequestContext ctx = createRequestContext()) {
final LoginResult result = ctx.getResource(Security.class).login("alice@example.com", "alicepwd");
final NewCookie cookie = result.getCookie();
assertNotNull(cookie);
assertNotNull(cookie.getValue());
}
}
Aggregations