Search in sources :

Example 6 with User

use of org.rembx.jeeshop.user.model.User in project jeeshop by remibantos.

the class UsersCT method resetPassword_shouldUpdateUserPasswordForAuthenticatedUser.

@Test
public void resetPassword_shouldUpdateUserPasswordForAuthenticatedUser() throws Exception {
    User user = notActivatedTestUser();
    when(sessionContextMock.isUserInRole(JeeshopRoles.USER)).thenReturn(true);
    when(sessionContextMock.getUserPrincipal()).thenReturn(new BasicUserPrincipal(user.getLogin()));
    service.resetPassword(sessionContextMock, user.getLogin(), null, "newPassword");
    final User updatedUser = entityManager.find(User.class, user.getId());
    assertThat(updatedUser).isNotNull();
    assertThat(updatedUser.getPassword()).isEqualTo(hashSha256Base64("newPassword"));
    verify(mailerMock).sendMail(testMailTemplate.changePasswordMailTpl().getSubject(), user.getLogin(), testMailTemplate.changePasswordMailTpl().getContent());
    removeTestUser(user);
}
Also used : TestUser(org.rembx.jeeshop.user.test.TestUser) User(org.rembx.jeeshop.user.model.User) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) Test(org.junit.jupiter.api.Test)

Example 7 with User

use of org.rembx.jeeshop.user.model.User in project jeeshop by remibantos.

the class UsersCT method create_shouldJustPersistEndUserEvenWhenExceptionDuringMailSending.

@Test
public void create_shouldJustPersistEndUserEvenWhenExceptionDuringMailSending() throws Exception {
    User user = new User("register1@test.com", "test", "John", "Doe", "+33616161616", null, new Date(), "fr_FR", null);
    user.setGender("M.");
    Address address = new Address("7 blue street", "Nowhere", "00001", "John", "Doe", "M.", null, "FRA");
    user.setAddress(address);
    when(sessionContextMock.isUserInRole(JeeshopRoles.ADMIN)).thenReturn(false);
    entityManager.getTransaction().begin();
    service.create(sessionContextMock, user);
    entityManager.getTransaction().commit();
    doThrow(new IllegalStateException("Test Exception")).when(mailerMock).sendMail(testMailTemplate.userRegistrationMailTemplate().getSubject(), user.getLogin(), testMailTemplate.userRegistrationMailTemplate().getContent());
    verify(sessionContextMock).isUserInRole(JeeshopRoles.ADMIN);
    verify(mailerMock).sendMail(testMailTemplate.userRegistrationMailTemplate().getSubject(), user.getLogin(), "<html><body>Welcome M. John Doe</body></html>");
    final User persistedUser = entityManager.find(User.class, user.getId());
    assertThat(persistedUser).isNotNull();
    assertThat(persistedUser.getAddress()).isEqualTo(address);
    entityManager.remove(user);
}
Also used : TestUser(org.rembx.jeeshop.user.test.TestUser) User(org.rembx.jeeshop.user.model.User) Address(org.rembx.jeeshop.user.model.Address) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 8 with User

use of org.rembx.jeeshop.user.model.User 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 9 with User

use of org.rembx.jeeshop.user.model.User 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 10 with User

use of org.rembx.jeeshop.user.model.User in project jeeshop by remibantos.

the class UsersCT method activate_shouldActivateUserAndClearActionToken.

@Test
public void activate_shouldActivateUserAndClearActionToken() throws Exception {
    User user = new User("activate1@test.com", "test", "John", "Doe", "+33616161616", null, new Date(), "fr_FR", null);
    user.setGender("M.");
    final UUID actionToken = UUID.randomUUID();
    user.setActionToken(actionToken);
    user.setActivated(false);
    entityManager.getTransaction().begin();
    entityManager.persist(user);
    entityManager.getTransaction().commit();
    service.activate(user.getLogin(), actionToken.toString());
    final User modifiedUser = entityManager.find(User.class, user.getId());
    assertThat(modifiedUser).isNotNull();
    assertThat(modifiedUser.getActivated()).isTrue();
    assertThat(modifiedUser.getActionToken()).isNull();
}
Also used : TestUser(org.rembx.jeeshop.user.test.TestUser) User(org.rembx.jeeshop.user.model.User) UUID(java.util.UUID) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

User (org.rembx.jeeshop.user.model.User)28 Test (org.junit.jupiter.api.Test)18 TestUser (org.rembx.jeeshop.user.test.TestUser)18 Date (java.util.Date)9 WebApplicationException (org.rembx.jeeshop.rest.WebApplicationException)9 RolesAllowed (javax.annotation.security.RolesAllowed)4 BasicUserPrincipal (org.apache.http.auth.BasicUserPrincipal)3 Address (org.rembx.jeeshop.user.model.Address)3 UUID (java.util.UUID)2 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2 Order (org.rembx.jeeshop.order.model.Order)2 JPAQueryFactory (com.querydsl.jpa.impl.JPAQueryFactory)1 Configuration (freemarker.template.Configuration)1 Template (freemarker.template.Template)1 StringWriter (java.io.StringWriter)1 Discount (org.rembx.jeeshop.catalog.model.Discount)1 Product (org.rembx.jeeshop.catalog.model.Product)1 SKU (org.rembx.jeeshop.catalog.model.SKU)1 MailTemplate (org.rembx.jeeshop.user.model.MailTemplate)1