Search in sources :

Example 26 with WebApplicationException

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

the class MailTemplatesCT method modifyUnknown_ShouldThrowNotFoundException.

@Test
public void modifyUnknown_ShouldThrowNotFoundException() {
    MailTemplate detachedMailTemplate = new MailTemplate();
    detachedMailTemplate.setId(9999L);
    try {
        service.modify(detachedMailTemplate);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.NOT_FOUND);
    }
}
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.jupiter.api.Test)

Example 27 with WebApplicationException

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

the class UsersCT method modify_ShouldThrowUnauthorizedError_WhenAuthenticatedUserDoesNotMatchLogin.

@Test
public void modify_ShouldThrowUnauthorizedError_WhenAuthenticatedUserDoesNotMatchLogin() throws Exception {
    User detachedUserToModify = new User("test2@test.com", "test", "John", "Doe", "+33616161616", null, new Date(), "fr_FR", null);
    try {
        when(sessionContextMock.isUserInRole(JeeshopRoles.USER)).thenReturn(true);
        when(sessionContextMock.getUserPrincipal()).thenReturn(new BasicUserPrincipal(testUser.firstUser().getLogin()));
        service.modify(sessionContextMock, detachedUserToModify);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.UNAUTHORIZED);
    }
}
Also used : TestUser(org.rembx.jeeshop.user.test.TestUser) User(org.rembx.jeeshop.user.model.User) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 28 with WebApplicationException

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

the class UsersCT method create_shouldThrowConflictExWhenUserWithGivenLoginAlreadyExists.

@Test
public void create_shouldThrowConflictExWhenUserWithGivenLoginAlreadyExists() throws Exception {
    User user = new User();
    user.setLogin("test@test.com");
    try {
        service.create(sessionContextMock, user);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.CONFLICT);
    }
}
Also used : TestUser(org.rembx.jeeshop.user.test.TestUser) User(org.rembx.jeeshop.user.model.User) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) Test(org.junit.jupiter.api.Test)

Example 29 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 30 with WebApplicationException

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

the class Catalogs method delete.

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

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