use of org.minijax.MinijaxRequestContext in project minijax by minijax.
the class ChangePasswordTest method testChangePasswordSuccess.
@Test
public 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.get(Dao.class).create(user);
cookie = ctx.get(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.get(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.MinijaxRequestContext in project minijax by minijax.
the class ResetPasswordTest method testResetPasswordTooShort.
@Test
public void testResetPasswordTooShort() throws IOException {
final User user = new User();
user.setName("Example 3");
user.setEmail("reset-3@example.com");
user.setRoles("user");
String code = null;
try (MinijaxRequestContext ctx = createRequestContext()) {
ctx.get(Dao.class).create(user);
code = ctx.get(Security.class).forgotPassword(user);
}
final Form form = new Form();
form.param("newPassword", "foo");
form.param("confirmNewPassword", "foo");
final Response r = target("/resetpassword/" + code).request().post(Entity.form(form));
assertNotNull(r);
assertEquals(400, r.getStatus());
assertTrue(r.getCookies().isEmpty());
}
use of org.minijax.MinijaxRequestContext in project minijax by minijax.
the class ResetPasswordTest method testResetPasswordMismatch.
@Test
public void testResetPasswordMismatch() throws IOException {
final User user = new User();
user.setName("Example 2");
user.setEmail("reset-2@example.com");
user.setRoles("user");
String code = null;
try (MinijaxRequestContext ctx = createRequestContext()) {
ctx.get(Dao.class).create(user);
code = ctx.get(Security.class).forgotPassword(user);
}
final Form form = new Form();
form.param("newPassword", "my-new-password");
form.param("confirmNewPassword", "different-password");
final Response r = target("/resetpassword/" + code).request().post(Entity.form(form));
assertNotNull(r);
assertEquals(400, r.getStatus());
assertTrue(r.getCookies().isEmpty());
}
use of org.minijax.MinijaxRequestContext in project minijax by minijax.
the class SecurityDaoTest method testApiKeys.
@Test
public void testApiKeys() throws IOException {
try (final MinijaxRequestContext ctx = createRequestContext()) {
final Dao dao = ctx.get(Dao.class);
final User user = new User();
user.setName("Alice");
user.setEmail("apikeytest@example.com");
user.setRoles("user");
dao.create(user);
final ApiKey k1 = new ApiKey();
k1.setName("test1");
k1.setValue("test1test1");
k1.setUser(user);
dao.create(k1);
final ApiKey k2 = new ApiKey();
k2.setName("test2");
k2.setValue("test2test2");
k2.setUser(user);
dao.create(k2);
final List<ApiKey> keys = dao.findApiKeysByUser(user);
assertNotNull(keys);
assertEquals(2, keys.size());
final ApiKey k3 = dao.findApiKeyByValue(k1.getValue());
assertNotNull(k3);
assertEquals(k1, k3);
}
}
use of org.minijax.MinijaxRequestContext in project minijax by minijax.
the class SecurityDaoTest method testDeleteSessionsByUser.
@Test
@SuppressWarnings("unchecked")
public void testDeleteSessionsByUser() throws IOException {
UserSession s1 = null;
UserSession s2 = null;
try (final MinijaxRequestContext ctx = createRequestContext()) {
final Dao dao = ctx.get(Dao.class);
final User user = new User();
user.setName("Alice");
user.setEmail("deletesessions@example.com");
user.setRoles("user");
dao.create(user);
final Security<User> security = ctx.get(Security.class);
final Cookie c1 = security.loginAs(user);
s1 = dao.read(UserSession.class, IdUtils.tryParse(c1.getValue()));
assertNotNull(s1);
assertEquals(user, s1.getUser());
final Cookie c2 = security.loginAs(user);
s2 = dao.read(UserSession.class, IdUtils.tryParse(c2.getValue()));
assertNotNull(s2);
assertEquals(user, s2.getUser());
dao.deleteUserSessionsByUser(user.getId());
}
try (final MinijaxRequestContext ctx = createRequestContext()) {
final Dao dao = ctx.get(Dao.class);
assertNull(dao.read(UserSession.class, s1.getId()));
assertNull(dao.read(UserSession.class, s2.getId()));
}
}
Aggregations