Search in sources :

Example 11 with WebApplicationException

use of org.rembx.jeeshop.rest.WebApplicationException in project jeeshop by remibantos.

the class Orders method find.

@GET
@Path("/{orderId}")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ ADMIN, ADMIN_READONLY, USER })
public Order find(@PathParam("orderId") @NotNull Long orderId, @QueryParam("enhanced") Boolean enhanced) {
    Order order = entityManager.find(Order.class, orderId);
    if (sessionContext.isCallerInRole(USER) && !sessionContext.isCallerInRole(ADMIN)) {
        User authenticatedUser = userFinder.findByLogin(sessionContext.getCallerPrincipal().getName());
        if (!order.getUser().equals(authenticatedUser)) {
            throw new WebApplicationException(Response.Status.UNAUTHORIZED);
        }
    }
    if (enhanced != null && enhanced) {
        orderFinder.enhanceOrder(order);
    }
    checkNotNull(order);
    return order;
}
Also used : Order(org.rembx.jeeshop.order.model.Order) User(org.rembx.jeeshop.user.model.User) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) RolesAllowed(javax.annotation.security.RolesAllowed)

Example 12 with WebApplicationException

use of org.rembx.jeeshop.rest.WebApplicationException in project jeeshop by remibantos.

the class OrdersCT method modifyUnknownCatalog_ShouldThrowNotFoundException.

@Test
public void modifyUnknownCatalog_ShouldThrowNotFoundException() {
    Order detachedOrder = new Order();
    detachedOrder.setId(9999L);
    try {
        service.modify(detachedOrder);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.NOT_FOUND);
    }
}
Also used : Order(org.rembx.jeeshop.order.model.Order) TestOrder(org.rembx.jeeshop.order.test.TestOrder) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) Test(org.junit.Test)

Example 13 with WebApplicationException

use of org.rembx.jeeshop.rest.WebApplicationException in project jeeshop by remibantos.

the class MailTemplates method modify.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed(ADMIN)
public MailTemplate modify(MailTemplate mailTemplate) {
    MailTemplate existingMailTemplate = entityManager.find(MailTemplate.class, mailTemplate.getId());
    checkNotNull(existingMailTemplate);
    MailTemplate existingTplWithSameLocaleAndName = mailTemplateFinder.findByNameAndLocale(mailTemplate.getName(), mailTemplate.getLocale());
    if (existingTplWithSameLocaleAndName != null && !existingTplWithSameLocaleAndName.getId().equals(mailTemplate.getId())) {
        throw new WebApplicationException(Response.Status.CONFLICT);
    }
    return entityManager.merge(mailTemplate);
}
Also used : WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) MailTemplate(org.rembx.jeeshop.user.model.MailTemplate) RolesAllowed(javax.annotation.security.RolesAllowed)

Example 14 with WebApplicationException

use of org.rembx.jeeshop.rest.WebApplicationException in project jeeshop by remibantos.

the class DiscountsCT method modifyUnknownDiscount_ShouldThrowNotFoundException.

@Test
public void modifyUnknownDiscount_ShouldThrowNotFoundException() {
    Discount detachedDiscountToModify = new Discount(9999L);
    try {
        service.modify(detachedDiscountToModify);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.NOT_FOUND);
    }
}
Also used : Discount(org.rembx.jeeshop.catalog.model.Discount) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) Test(org.junit.Test)

Example 15 with WebApplicationException

use of org.rembx.jeeshop.rest.WebApplicationException in project jeeshop by remibantos.

the class OrdersCT method find_whenClientHasUserRoleAndOrderBelongsToAnotherUser_ShouldThrowException.

@Test
public void find_whenClientHasUserRoleAndOrderBelongsToAnotherUser_ShouldThrowException() throws Exception {
    entityManager.getTransaction().begin();
    User user = new User("777@test.com", "test", "M.", "John", "Doe", "+33616161616", null, null, "fr_FR", null);
    entityManager.persist(user);
    entityManager.getTransaction().commit();
    when(sessionContextMock.isCallerInRole(JeeshopRoles.USER)).thenReturn(true);
    when(sessionContextMock.isCallerInRole(JeeshopRoles.ADMIN)).thenReturn(false);
    when(sessionContextMock.getCallerPrincipal()).thenReturn(new PrincipalImpl("777@test.com"));
    try {
        service.find(1L, null);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.UNAUTHORIZED);
    } finally {
        entityManager.getTransaction().begin();
        entityManager.remove(user);
        entityManager.persist(user);
    }
}
Also used : User(org.rembx.jeeshop.user.model.User) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) PrincipalImpl(sun.security.acl.PrincipalImpl) Test(org.junit.Test)

Aggregations

WebApplicationException (org.rembx.jeeshop.rest.WebApplicationException)24 Test (org.junit.Test)19 User (org.rembx.jeeshop.user.model.User)8 TestUser (org.rembx.jeeshop.user.test.TestUser)6 MailTemplate (org.rembx.jeeshop.user.model.MailTemplate)5 RolesAllowed (javax.annotation.security.RolesAllowed)4 Order (org.rembx.jeeshop.order.model.Order)3 Address (org.rembx.jeeshop.user.model.Address)3 TestMailTemplate (org.rembx.jeeshop.user.test.TestMailTemplate)3 PrincipalImpl (sun.security.acl.PrincipalImpl)3 Date (java.util.Date)2 Catalog (org.rembx.jeeshop.catalog.model.Catalog)2 TestOrder (org.rembx.jeeshop.order.test.TestOrder)2 IOException (java.io.IOException)1 PermitAll (javax.annotation.security.PermitAll)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 Category (org.rembx.jeeshop.catalog.model.Category)1