Search in sources :

Example 31 with WebApplicationException

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

the class Stores method modify.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
@RolesAllowed({ ADMIN, STORE_ADMIN })
public Store modify(@Context SecurityContext securityContext, Store store) {
    Store originalCatalog = entityManager.find(Store.class, store.getId());
    checkNotNull(originalCatalog);
    if (!isOwner(securityContext, originalCatalog.getOwner()) && !isAdminUser(securityContext))
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    if (store.getCatalogsIds() != null) {
        List<Catalog> catalogs = new ArrayList<>();
        store.getCatalogsIds().forEach(categoryId -> catalogs.add(entityManager.find(Catalog.class, categoryId)));
        store.setCatalogs(catalogs);
    } else {
        store.setCatalogs(originalCatalog.getCatalogs());
    }
    store.setPresentationByLocale(originalCatalog.getPresentationByLocale());
    return entityManager.merge(store);
}
Also used : WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) ArrayList(java.util.ArrayList) Store(org.rembx.jeeshop.catalog.model.Store) Catalog(org.rembx.jeeshop.catalog.model.Catalog) RolesAllowed(javax.annotation.security.RolesAllowed) Transactional(javax.transaction.Transactional)

Example 32 with WebApplicationException

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

the class Stores method delete.

@DELETE
@Transactional
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ ADMIN, STORE_ADMIN })
@Path("/{storeId}")
public void delete(@Context SecurityContext securityContext, @PathParam("storeId") Long storeId) {
    Store store = entityManager.find(Store.class, storeId);
    checkNotNull(store);
    if (isOwner(securityContext, store.getOwner()) || isAdminUser(securityContext)) {
        entityManager.remove(store);
    } else {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    }
}
Also used : WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) Store(org.rembx.jeeshop.catalog.model.Store) RolesAllowed(javax.annotation.security.RolesAllowed) Transactional(javax.transaction.Transactional)

Example 33 with WebApplicationException

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

the class StoresCT method create_shouldThrowBadRequest_whenOwnerIsNull_for_admin.

@Test
public void create_shouldThrowBadRequest_whenOwnerIsNull_for_admin() {
    tester.setAdminUser();
    Store store = new Store("Superstore");
    try {
        tester.test_create(store);
        fail("should have thrown an exception");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
    }
}
Also used : WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) Store(org.rembx.jeeshop.catalog.model.Store) Test(org.junit.jupiter.api.Test)

Example 34 with WebApplicationException

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

the class CatalogsCT method create_shouldThrowBadRequest_whenOwnerIsNull_for_admin.

@Test
public void create_shouldThrowBadRequest_whenOwnerIsNull_for_admin() {
    tester.setAdminUser();
    Catalog catalog = new Catalog("Catalog");
    try {
        tester.test_create(catalog);
        fail("should have thrown an exception");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
    }
}
Also used : WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) Catalog(org.rembx.jeeshop.catalog.model.Catalog) TestCatalog(org.rembx.jeeshop.catalog.test.TestCatalog) Test(org.junit.jupiter.api.Test)

Example 35 with WebApplicationException

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

the class CatalogsCT method modifyNonManagedCatalog_ShouldThrowForbiddenException.

@Test
public void modifyNonManagedCatalog_ShouldThrowForbiddenException() {
    tester.setSAnotherStoreAdminUser();
    Catalog detachedCatalogToModify = new Catalog(1L, "name");
    try {
        tester.test_modify(detachedCatalogToModify);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.FORBIDDEN);
    }
}
Also used : WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) Catalog(org.rembx.jeeshop.catalog.model.Catalog) TestCatalog(org.rembx.jeeshop.catalog.test.TestCatalog) Test(org.junit.jupiter.api.Test)

Aggregations

WebApplicationException (org.rembx.jeeshop.rest.WebApplicationException)55 Test (org.junit.jupiter.api.Test)34 RolesAllowed (javax.annotation.security.RolesAllowed)19 Transactional (javax.transaction.Transactional)9 Catalog (org.rembx.jeeshop.catalog.model.Catalog)9 User (org.rembx.jeeshop.user.model.User)9 Store (org.rembx.jeeshop.catalog.model.Store)7 TestUser (org.rembx.jeeshop.user.test.TestUser)7 MailTemplate (org.rembx.jeeshop.user.model.MailTemplate)5 Category (org.rembx.jeeshop.catalog.model.Category)4 SKU (org.rembx.jeeshop.catalog.model.SKU)4 TestCatalog (org.rembx.jeeshop.catalog.test.TestCatalog)4 Order (org.rembx.jeeshop.order.model.Order)4 BasicUserPrincipal (org.apache.http.auth.BasicUserPrincipal)3 Product (org.rembx.jeeshop.catalog.model.Product)3 Address (org.rembx.jeeshop.user.model.Address)3 TestMailTemplate (org.rembx.jeeshop.user.test.TestMailTemplate)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 PermitAll (javax.annotation.security.PermitAll)2