Search in sources :

Example 16 with MinijaxRequestContext

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

the class ChangePasswordTest method testIncorrectOldPassword.

@Test
public 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.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", "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(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 17 with MinijaxRequestContext

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

the class ExceptionMapperTest method testNotFound.

@Test
public void testNotFound() throws IOException {
    try (final MinijaxRequestContext ctx = createRequestContext()) {
        final MinijaxJsonExceptionMapper mapper = new MinijaxJsonExceptionMapper();
        final Response response = mapper.toResponse(new NotFoundException());
        assertNotNull(response);
        assertEquals(404, response.getStatus());
        assertEquals(404, ((MinijaxJsonExceptionWrapper) response.getEntity()).getCode());
        assertEquals("HTTP 404 Not Found", ((MinijaxJsonExceptionWrapper) response.getEntity()).getMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) MinijaxRequestContext(org.minijax.MinijaxRequestContext) NotFoundException(javax.ws.rs.NotFoundException) MinijaxTest(org.minijax.test.MinijaxTest) Test(org.junit.Test)

Example 18 with MinijaxRequestContext

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

the class ResetPasswordTest method testResetPasswordSuccess.

@Test
public 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.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", "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.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 : 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 19 with MinijaxRequestContext

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

the class EntityProvider method get.

@Override
public T get() {
    final MinijaxRequestContext context = MinijaxRequestContext.getThreadLocal();
    final InputStream entityStream = context.getEntityStream();
    try {
        return getImpl(context, entityStream);
    } catch (final IOException ex) {
        throw new InjectionException(ex.getMessage(), ex);
    }
}
Also used : InjectionException(javax.enterprise.inject.InjectionException) MinijaxRequestContext(org.minijax.MinijaxRequestContext) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 20 with MinijaxRequestContext

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

the class RequestScopedProvider method get.

@Override
public T get() {
    final MinijaxRequestContext context = MinijaxRequestContext.getThreadLocal();
    final ResourceCache resourceCache = context.getResourceCache();
    T instance = resourceCache.get(key);
    if (instance == null) {
        instance = sourceProvider.get();
        resourceCache.put(key, instance);
    }
    return instance;
}
Also used : MinijaxRequestContext(org.minijax.MinijaxRequestContext)

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