Search in sources :

Example 16 with WebApplicationException

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

the class OrdersCT method create_shouldThrowBadRequestWhenParametersHaveId.

@Test
public void create_shouldThrowBadRequestWhenParametersHaveId() throws Exception {
    Address address = new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA");
    address.setId(777L);
    OrderItem orderItemWithId = new OrderItem();
    orderItemWithId.setId(777L);
    Set<OrderItem> orderItems = Collections.singleton(orderItemWithId);
    try {
        Order order = new Order(null, address, new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA"));
        service.create(order, null);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
    }
    try {
        Order order = new Order(null, new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA"), address);
        service.create(order, null);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
    }
    try {
        Order order = new Order(orderItems, new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA"), address);
        service.create(order, null);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
    }
}
Also used : Order(org.rembx.jeeshop.order.model.Order) TestOrder(org.rembx.jeeshop.order.test.TestOrder) Address(org.rembx.jeeshop.user.model.Address) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) OrderItem(org.rembx.jeeshop.order.model.OrderItem) Test(org.junit.Test)

Example 17 with WebApplicationException

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

the class MailTemplates method create.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed(ADMIN)
public MailTemplate create(MailTemplate mailTemplate) {
    MailTemplate existingTpl = mailTemplateFinder.findByNameAndLocale(mailTemplate.getName(), mailTemplate.getLocale());
    if (existingTpl != null) {
        throw new WebApplicationException(Response.Status.CONFLICT);
    }
    entityManager.persist(mailTemplate);
    return mailTemplate;
}
Also used : WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) MailTemplate(org.rembx.jeeshop.user.model.MailTemplate) RolesAllowed(javax.annotation.security.RolesAllowed)

Example 18 with WebApplicationException

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

the class Users method resetPassword.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{userLogin}/password")
@PermitAll
public void resetPassword(@NotNull @PathParam("userLogin") String userLogin, @QueryParam("token") String token, @NotNull String newPassword) {
    User user;
    if (sessionContext.isCallerInRole(ADMIN)) {
        user = userFinder.findByLogin(userLogin);
    } else if (sessionContext.isCallerInRole(USER)) {
        user = userFinder.findByLogin(sessionContext.getCallerPrincipal().getName());
        if (!userLogin.equals(user.getLogin())) {
            throw new WebApplicationException(Response.Status.UNAUTHORIZED);
        }
    } else {
        user = userFinder.findByLogin(userLogin);
        if (user == null || !user.getActionToken().equals(UUID.fromString(token))) {
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
        user.setActionToken(null);
    }
    user.setPassword(hashSha256Base64(newPassword));
    user.setActivated(true);
    sendMail(user, Mails.userChangePassword);
}
Also used : WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) PermitAll(javax.annotation.security.PermitAll)

Example 19 with WebApplicationException

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

the class MailTemplatesCT method create_shouldThrowConflictException_WhenThereIsAlreadyAMailTemplateWithSameLocaleAndName.

@Test
public void create_shouldThrowConflictException_WhenThereIsAlreadyAMailTemplateWithSameLocaleAndName() {
    MailTemplate mailTemplate = new MailTemplate("Newsletter1", "fr_FR", "test content", "Test Subject");
    try {
        service.create(mailTemplate);
        fail("Should have thrown exception");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.CONFLICT);
    }
}
Also used : WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) TestMailTemplate(org.rembx.jeeshop.user.test.TestMailTemplate) MailTemplate(org.rembx.jeeshop.user.model.MailTemplate) Test(org.junit.Test)

Example 20 with WebApplicationException

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

the class UsersCT method create_shouldThrowBadRequestExceptionExWhenUserAddressHasAnId.

@Test
public void create_shouldThrowBadRequestExceptionExWhenUserAddressHasAnId() throws Exception {
    User user = new User();
    user.setLogin("toto@toto.com");
    Address address = new Address();
    address.setId(1L);
    user.setAddress(address);
    try {
        service.create(user);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
    }
}
Also used : TestUser(org.rembx.jeeshop.user.test.TestUser) User(org.rembx.jeeshop.user.model.User) Address(org.rembx.jeeshop.user.model.Address) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) 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