Search in sources :

Example 21 with User

use of com.baeldung.domain.User in project tutorials by eugenp.

the class UserServiceIntegrationTest method assertThatOnlyActivatedUserCanRequestPasswordReset.

@Test
public void assertThatOnlyActivatedUserCanRequestPasswordReset() {
    User user = userService.createUser("johndoe", "johndoe", "John", "Doe", "john.doe@localhost", "http://placehold.it/50x50", "en-US");
    Optional<User> maybeUser = userService.requestPasswordReset("john.doe@localhost");
    assertThat(maybeUser.isPresent()).isFalse();
    userRepository.delete(user);
}
Also used : User(com.baeldung.domain.User) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 22 with User

use of com.baeldung.domain.User in project tutorials by eugenp.

the class UserServiceIntegrationTest method assertThatResetKeyMustNotBeOlderThan24Hours.

@Test
public void assertThatResetKeyMustNotBeOlderThan24Hours() {
    User user = userService.createUser("johndoe", "johndoe", "John", "Doe", "john.doe@localhost", "http://placehold.it/50x50", "en-US");
    ZonedDateTime daysAgo = ZonedDateTime.now().minusHours(25);
    String resetKey = RandomUtil.generateResetKey();
    user.setActivated(true);
    user.setResetDate(daysAgo);
    user.setResetKey(resetKey);
    userRepository.save(user);
    Optional<User> maybeUser = userService.completePasswordReset("johndoe2", user.getResetKey());
    assertThat(maybeUser.isPresent()).isFalse();
    userRepository.delete(user);
}
Also used : User(com.baeldung.domain.User) ZonedDateTime(java.time.ZonedDateTime) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 23 with User

use of com.baeldung.domain.User in project tutorials by eugenp.

the class AccountResourceIntegrationTest method testGetExistingAccount.

@Test
public void testGetExistingAccount() throws Exception {
    Set<Authority> authorities = new HashSet<>();
    Authority authority = new Authority();
    authority.setName(AuthoritiesConstants.ADMIN);
    authorities.add(authority);
    User user = new User();
    user.setLogin("test");
    user.setFirstName("john");
    user.setLastName("doe");
    user.setEmail("john.doe@jhipster.com");
    user.setImageUrl("http://placehold.it/50x50");
    user.setAuthorities(authorities);
    when(mockUserService.getUserWithAuthorities()).thenReturn(user);
    restUserMockMvc.perform(get("/api/account").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)).andExpect(jsonPath("$.login").value("test")).andExpect(jsonPath("$.firstName").value("john")).andExpect(jsonPath("$.lastName").value("doe")).andExpect(jsonPath("$.email").value("john.doe@jhipster.com")).andExpect(jsonPath("$.imageUrl").value("http://placehold.it/50x50")).andExpect(jsonPath("$.authorities").value(AuthoritiesConstants.ADMIN));
}
Also used : User(com.baeldung.domain.User) Authority(com.baeldung.domain.Authority) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 24 with User

use of com.baeldung.domain.User in project tutorials by eugenp.

the class AccountResourceIntegrationTest method testRegisterDuplicateLogin.

@Test
@Transactional
public void testRegisterDuplicateLogin() throws Exception {
    // Good
    ManagedUserVM validUser = new ManagedUserVM(// id
    null, // login
    "alice", // password
    "password", // firstName
    "Alice", // lastName
    "Something", // e-mail
    "alice@example.com", // activated
    true, // imageUrl
    "http://placehold.it/50x50", // langKey
    "en", // createdBy
    null, // createdDate
    null, // lastModifiedBy
    null, // lastModifiedDate
    null, new HashSet<>(Arrays.asList(AuthoritiesConstants.USER)));
    // Duplicate login, different e-mail
    ManagedUserVM duplicatedUser = new ManagedUserVM(validUser.getId(), validUser.getLogin(), validUser.getPassword(), validUser.getLogin(), validUser.getLastName(), "alicejr@example.com", true, validUser.getImageUrl(), validUser.getLangKey(), validUser.getCreatedBy(), validUser.getCreatedDate(), validUser.getLastModifiedBy(), validUser.getLastModifiedDate(), validUser.getAuthorities());
    // Good user
    restMvc.perform(post("/api/register").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(validUser))).andExpect(status().isCreated());
    // Duplicate login
    restMvc.perform(post("/api/register").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(duplicatedUser))).andExpect(status().is4xxClientError());
    Optional<User> userDup = userRepository.findOneByEmail("alicejr@example.com");
    assertThat(userDup.isPresent()).isFalse();
}
Also used : User(com.baeldung.domain.User) ManagedUserVM(com.baeldung.web.rest.vm.ManagedUserVM) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 25 with User

use of com.baeldung.domain.User in project tutorials by eugenp.

the class AccountResourceIntegrationTest method testSaveInvalidLogin.

@Test
@Transactional
public void testSaveInvalidLogin() throws Exception {
    UserDTO invalidUser = new UserDTO(// id
    null, // login <-- invalid
    "funky-log!n", // firstName
    "Funky", // lastName
    "One", // e-mail
    "funky@example.com", // activated
    true, // imageUrl
    "http://placehold.it/50x50", // langKey
    "en", // createdBy
    null, // createdDate
    null, // lastModifiedBy
    null, // lastModifiedDate
    null, new HashSet<>(Arrays.asList(AuthoritiesConstants.USER)));
    restUserMockMvc.perform(post("/api/account").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(invalidUser))).andExpect(status().isBadRequest());
    Optional<User> user = userRepository.findOneByEmail("funky@example.com");
    assertThat(user.isPresent()).isFalse();
}
Also used : User(com.baeldung.domain.User) UserDTO(com.baeldung.service.dto.UserDTO) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

User (com.baeldung.domain.User)32 Test (org.junit.Test)23 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)23 Transactional (org.springframework.transaction.annotation.Transactional)18 ManagedUserVM (com.baeldung.web.rest.vm.ManagedUserVM)16 HashSet (java.util.HashSet)8 ZonedDateTime (java.time.ZonedDateTime)5 Authority (com.baeldung.domain.Authority)3 UserRepository (com.baeldung.repository.UserRepository)2 UserDTO (com.baeldung.service.dto.UserDTO)2 Timed (com.codahale.metrics.annotation.Timed)2 java.util (java.util)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Post (com.baeldung.domain.Post)1 SecurityUtils (com.baeldung.security.SecurityUtils)1 MailService (com.baeldung.service.MailService)1 UserService (com.baeldung.service.UserService)1 HeaderUtil (com.baeldung.web.rest.util.HeaderUtil)1 KeyAndPasswordVM (com.baeldung.web.rest.vm.KeyAndPasswordVM)1