Search in sources :

Example 11 with MinijaxRequestContext

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()));
    }
}
Also used : Cookie(javax.ws.rs.core.Cookie) Response(javax.ws.rs.core.Response) MinijaxRequestContext(org.minijax.MinijaxRequestContext) Form(javax.ws.rs.core.Form) MinijaxTest(org.minijax.test.MinijaxTest) Test(org.junit.Test)

Example 12 with MinijaxRequestContext

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());
}
Also used : Response(javax.ws.rs.core.Response) MinijaxRequestContext(org.minijax.MinijaxRequestContext) Form(javax.ws.rs.core.Form) MinijaxTest(org.minijax.test.MinijaxTest) Test(org.junit.Test)

Example 13 with MinijaxRequestContext

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());
}
Also used : Response(javax.ws.rs.core.Response) MinijaxRequestContext(org.minijax.MinijaxRequestContext) Form(javax.ws.rs.core.Form) MinijaxTest(org.minijax.test.MinijaxTest) Test(org.junit.Test)

Example 14 with MinijaxRequestContext

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);
    }
}
Also used : MinijaxRequestContext(org.minijax.MinijaxRequestContext) MinijaxTest(org.minijax.test.MinijaxTest) Test(org.junit.Test)

Example 15 with MinijaxRequestContext

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()));
    }
}
Also used : Cookie(javax.ws.rs.core.Cookie) MinijaxRequestContext(org.minijax.MinijaxRequestContext) MinijaxTest(org.minijax.test.MinijaxTest) Test(org.junit.Test)

Aggregations

MinijaxRequestContext (org.minijax.MinijaxRequestContext)25 Test (org.junit.Test)16 MinijaxTest (org.minijax.test.MinijaxTest)13 Response (javax.ws.rs.core.Response)10 Form (javax.ws.rs.core.Form)8 Cookie (javax.ws.rs.core.Cookie)7 Minijax (org.minijax.Minijax)4 MinijaxApplication (org.minijax.MinijaxApplication)4 MockHttpServletRequest (org.minijax.test.MockHttpServletRequest)3 IOException (java.io.IOException)2 NotFoundException (javax.ws.rs.NotFoundException)2 BeforeClass (org.junit.BeforeClass)2 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 WebSocketDeploymentInfo (io.undertow.websockets.jsr.WebSocketDeploymentInfo)1 AnnotatedEndpoint (io.undertow.websockets.jsr.annotated.AnnotatedEndpoint)1 InputStream (java.io.InputStream)1 InjectionException (javax.enterprise.inject.InjectionException)1 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)1 DefaultValue (javax.ws.rs.DefaultValue)1 MediaType (javax.ws.rs.core.MediaType)1