Search in sources :

Example 26 with MinijaxRequestContext

use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.

the class AdapterTest method testOnError.

@Test
void testOnError() throws IOException {
    try (final MinijaxRequestContext ctx = createRequestContext()) {
        final MinijaxUndertowWebSocketAdapter adapter = new MinijaxUndertowWebSocketAdapter(ctx, TestSocket4.class);
        adapter.onOpen(emptyMap());
        adapter.onMessage(emptyMap());
        adapter.onClose(emptyMap());
        adapter.onError(emptyMap());
    }
}
Also used : MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) Test(org.junit.jupiter.api.Test) MinijaxTest(org.minijax.rs.test.MinijaxTest)

Example 27 with MinijaxRequestContext

use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.

the class RolesAllowedTest method testLoginUserNotFound.

@Test
void testLoginUserNotFound() throws Exception {
    try (final MinijaxRequestContext ctx = createRequestContext()) {
        final LoginResult result = ctx.getResource(Security.class).login("notfound@example.com", "alicepwd");
        assertEquals(LoginResult.Status.NOT_FOUND, result.getStatus());
    }
}
Also used : MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) MinijaxTest(org.minijax.rs.test.MinijaxTest) Test(org.junit.jupiter.api.Test)

Example 28 with MinijaxRequestContext

use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.

the class RolesAllowedTest method setUpSecurityTest.

@BeforeAll
public static void setUpSecurityTest() throws IOException {
    resetServer();
    getServer().register(PersistenceFeature.class).register(new SecurityFeature(User.class, Dao.class)).register(RolesAllowedTest.class);
    try (final MinijaxRequestContext ctx = createRequestContext()) {
        final Dao dao = ctx.getResource(Dao.class);
        alice = new User();
        alice.setName("Alice");
        alice.setEmail("alice@example.com");
        alice.setHandle("alice");
        alice.setPassword("alicepwd");
        alice.setRoles("user", "admin");
        dao.create(alice);
        bob = new User();
        bob.setName("Bob");
        bob.setEmail("bob@example.com");
        bob.setHandle("bob");
        bob.setPassword("bobpwd");
        bob.setRoles("user");
        dao.create(bob);
        aliceCookie = ctx.getResource(Security.class).loginAs(alice);
        bobCookie = ctx.getResource(Security.class).loginAs(bob);
    }
}
Also used : DefaultBaseDao(org.minijax.dao.DefaultBaseDao) MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 29 with MinijaxRequestContext

use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.

the class ResetPasswordTest method testResetPasswordSuccess.

@Test
void testResetPasswordSuccess() throws IOException {
    final User user = new User();
    user.setName("Example 1");
    user.setEmail("reset-1@example.com");
    user.setRoles("user");
    String code = null;
    try (MinijaxRequestContext ctx = createRequestContext()) {
        ctx.getResource(Dao.class).create(user);
        code = ctx.getResource(Security.class).forgotPassword(user);
    }
    final Form form = new Form();
    form.param("newPassword", "my-new-password");
    form.param("confirmNewPassword", "my-new-password");
    final Response r = target("/resetpassword/" + code).request().post(Entity.form(form));
    assertNotNull(r);
    assertEquals(200, r.getStatus());
    assertFalse(r.getCookies().isEmpty());
    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 : 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 30 with MinijaxRequestContext

use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.

the class ChangePasswordTest method testIncorrectOldPassword.

@Test
void testIncorrectOldPassword() throws IOException {
    final User user = new User();
    user.setName("Example 3");
    user.setEmail("pwd-3@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", "wrong-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)

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