Search in sources :

Example 6 with MinijaxRequestContext

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

Example 7 with MinijaxRequestContext

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

Example 8 with MinijaxRequestContext

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

Example 9 with MinijaxRequestContext

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

Example 10 with MinijaxRequestContext

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

Aggregations

MinijaxRequestContext (org.minijax.rs.MinijaxRequestContext)33 Test (org.junit.jupiter.api.Test)23 MinijaxTest (org.minijax.rs.test.MinijaxTest)21 Response (jakarta.ws.rs.core.Response)13 Form (jakarta.ws.rs.core.Form)8 MinijaxApplication (org.minijax.rs.MinijaxApplication)7 Cookie (jakarta.ws.rs.core.Cookie)6 MediaType (jakarta.ws.rs.core.MediaType)4 Minijax (org.minijax.Minijax)4 IOException (java.io.IOException)3 List (java.util.List)3 NotFoundException (jakarta.ws.rs.NotFoundException)2 InputStream (java.io.InputStream)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 MinijaxTestRequestContext (org.minijax.rs.test.MinijaxTestRequestContext)2 ByteBuf (io.netty.buffer.ByteBuf)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 AsciiString (io.netty.util.AsciiString)1 HttpString (io.undertow.util.HttpString)1