Search in sources :

Example 41 with User

use of com.arnaugarcia.uplace.domain.User in project uplace.es by Uplace.

the class UserServiceIntTest method assertThatUserCanResetPassword.

@Test
@Transactional
public void assertThatUserCanResetPassword() {
    String oldPassword = user.getPassword();
    Instant daysAgo = Instant.now().minus(2, ChronoUnit.HOURS);
    String resetKey = RandomUtil.generateResetKey();
    user.setActivated(true);
    user.setResetDate(daysAgo);
    user.setResetKey(resetKey);
    userRepository.saveAndFlush(user);
    Optional<User> maybeUser = userService.completePasswordReset("johndoe2", user.getResetKey());
    assertThat(maybeUser).isPresent();
    assertThat(maybeUser.orElse(null).getResetDate()).isNull();
    assertThat(maybeUser.orElse(null).getResetKey()).isNull();
    assertThat(maybeUser.orElse(null).getPassword()).isNotEqualTo(oldPassword);
    userRepository.delete(user);
}
Also used : User(com.arnaugarcia.uplace.domain.User) Instant(java.time.Instant) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 42 with User

use of com.arnaugarcia.uplace.domain.User in project uplace.es by Uplace.

the class UserServiceIntTest method init.

@Before
public void init() {
    user = new User();
    user.setLogin("johndoe");
    user.setPassword(RandomStringUtils.random(60));
    user.setActivated(true);
    user.setEmail("johndoe@localhost");
    user.setFirstName("john");
    user.setLastName("doe");
    user.setImageUrl("http://placehold.it/50x50");
    user.setLangKey("en");
}
Also used : User(com.arnaugarcia.uplace.domain.User) Before(org.junit.Before)

Example 43 with User

use of com.arnaugarcia.uplace.domain.User in project uplace.es by Uplace.

the class UserServiceIntTest method assertThatResetKeyMustNotBeOlderThan24Hours.

@Test
@Transactional
public void assertThatResetKeyMustNotBeOlderThan24Hours() {
    Instant daysAgo = Instant.now().minus(25, ChronoUnit.HOURS);
    String resetKey = RandomUtil.generateResetKey();
    user.setActivated(true);
    user.setResetDate(daysAgo);
    user.setResetKey(resetKey);
    userRepository.saveAndFlush(user);
    Optional<User> maybeUser = userService.completePasswordReset("johndoe2", user.getResetKey());
    assertThat(maybeUser).isNotPresent();
    userRepository.delete(user);
}
Also used : User(com.arnaugarcia.uplace.domain.User) Instant(java.time.Instant) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 44 with User

use of com.arnaugarcia.uplace.domain.User in project uplace.es by Uplace.

the class AccountResourceIntTest method testRegisterDuplicateEmail.

@Test
@Transactional
public void testRegisterDuplicateEmail() throws Exception {
    // Good
    ManagedUserVM validUser = new ManagedUserVM();
    validUser.setLogin("john");
    validUser.setPassword("password");
    validUser.setFirstName("John");
    validUser.setLastName("Doe");
    validUser.setEmail("john@example.com");
    validUser.setActivated(true);
    validUser.setImageUrl("http://placehold.it/50x50");
    validUser.setLangKey(Constants.DEFAULT_LANGUAGE);
    validUser.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));
    // Duplicate email, different login
    ManagedUserVM duplicatedUser = new ManagedUserVM();
    duplicatedUser.setLogin("johnjr");
    duplicatedUser.setPassword(validUser.getPassword());
    duplicatedUser.setFirstName(validUser.getFirstName());
    duplicatedUser.setLastName(validUser.getLastName());
    duplicatedUser.setEmail(validUser.getEmail());
    duplicatedUser.setActivated(validUser.isActivated());
    duplicatedUser.setImageUrl(validUser.getImageUrl());
    duplicatedUser.setLangKey(validUser.getLangKey());
    duplicatedUser.setCreatedBy(validUser.getCreatedBy());
    duplicatedUser.setCreatedDate(validUser.getCreatedDate());
    duplicatedUser.setLastModifiedBy(validUser.getLastModifiedBy());
    duplicatedUser.setLastModifiedDate(validUser.getLastModifiedDate());
    duplicatedUser.setAuthorities(new HashSet<>(validUser.getAuthorities()));
    // Good user
    restMvc.perform(post("/api/register").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(validUser))).andExpect(status().isCreated());
    // Duplicate email
    restMvc.perform(post("/api/register").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(duplicatedUser))).andExpect(status().is4xxClientError());
    // Duplicate email - with uppercase email address
    ManagedUserVM userWithUpperCaseEmail = new ManagedUserVM();
    userWithUpperCaseEmail.setId(validUser.getId());
    userWithUpperCaseEmail.setLogin("johnjr");
    userWithUpperCaseEmail.setPassword(validUser.getPassword());
    userWithUpperCaseEmail.setFirstName(validUser.getFirstName());
    userWithUpperCaseEmail.setLastName(validUser.getLastName());
    userWithUpperCaseEmail.setEmail(validUser.getEmail().toUpperCase());
    userWithUpperCaseEmail.setActivated(validUser.isActivated());
    userWithUpperCaseEmail.setImageUrl(validUser.getImageUrl());
    userWithUpperCaseEmail.setLangKey(validUser.getLangKey());
    userWithUpperCaseEmail.setCreatedBy(validUser.getCreatedBy());
    userWithUpperCaseEmail.setCreatedDate(validUser.getCreatedDate());
    userWithUpperCaseEmail.setLastModifiedBy(validUser.getLastModifiedBy());
    userWithUpperCaseEmail.setLastModifiedDate(validUser.getLastModifiedDate());
    userWithUpperCaseEmail.setAuthorities(new HashSet<>(validUser.getAuthorities()));
    restMvc.perform(post("/api/register").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(userWithUpperCaseEmail))).andExpect(status().is4xxClientError());
    Optional<User> userDup = userRepository.findOneByLogin("johnjr");
    assertThat(userDup.isPresent()).isFalse();
}
Also used : User(com.arnaugarcia.uplace.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) ManagedUserVM(com.arnaugarcia.uplace.web.rest.vm.ManagedUserVM) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 45 with User

use of com.arnaugarcia.uplace.domain.User in project uplace.es by Uplace.

the class AccountResourceIntTest method testFinishPasswordResetTooSmall.

@Test
@Transactional
public void testFinishPasswordResetTooSmall() throws Exception {
    User user = new User();
    user.setPassword(RandomStringUtils.random(60));
    user.setLogin("finish-password-reset-too-small");
    user.setEmail("finish-password-reset-too-small@example.com");
    user.setResetDate(Instant.now().plusSeconds(60));
    user.setResetKey("reset key too small");
    userRepository.saveAndFlush(user);
    KeyAndPasswordVM keyAndPassword = new KeyAndPasswordVM();
    keyAndPassword.setKey(user.getResetKey());
    keyAndPassword.setNewPassword("foo");
    restMvc.perform(post("/api/account/reset-password/finish").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(keyAndPassword))).andExpect(status().isBadRequest());
    User updatedUser = userRepository.findOneByLogin(user.getLogin()).orElse(null);
    assertThat(passwordEncoder.matches(keyAndPassword.getNewPassword(), updatedUser.getPassword())).isFalse();
}
Also used : KeyAndPasswordVM(com.arnaugarcia.uplace.web.rest.vm.KeyAndPasswordVM) User(com.arnaugarcia.uplace.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

User (com.arnaugarcia.uplace.domain.User)61 Test (org.junit.Test)41 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)41 Transactional (org.springframework.transaction.annotation.Transactional)39 WithMockUser (org.springframework.security.test.context.support.WithMockUser)21 ManagedUserVM (com.arnaugarcia.uplace.web.rest.vm.ManagedUserVM)15 BadRequestAlertException (com.arnaugarcia.uplace.web.rest.errors.BadRequestAlertException)6 UserDTO (com.arnaugarcia.uplace.service.dto.UserDTO)5 Authority (com.arnaugarcia.uplace.domain.Authority)4 Instant (java.time.Instant)4 MimeMessage (javax.mail.internet.MimeMessage)4 Notification (com.arnaugarcia.uplace.domain.Notification)3 KeyAndPasswordVM (com.arnaugarcia.uplace.web.rest.vm.KeyAndPasswordVM)2 LoginVM (com.arnaugarcia.uplace.web.rest.vm.LoginVM)2 Timed (com.codahale.metrics.annotation.Timed)2 Before (org.junit.Before)2 Agent (com.arnaugarcia.uplace.domain.Agent)1 UserRepository (com.arnaugarcia.uplace.repository.UserRepository)1 EmailAlreadyUsedException (com.arnaugarcia.uplace.web.rest.errors.EmailAlreadyUsedException)1 LoginAlreadyUsedException (com.arnaugarcia.uplace.web.rest.errors.LoginAlreadyUsedException)1