Search in sources :

Example 46 with User

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

the class AccountResourceIntTest method testChangePassword.

@Test
@Transactional
@WithMockUser("change-password")
public void testChangePassword() throws Exception {
    User user = new User();
    user.setPassword(RandomStringUtils.random(60));
    user.setLogin("change-password");
    user.setEmail("change-password@example.com");
    userRepository.saveAndFlush(user);
    restMvc.perform(post("/api/account/change-password").content("new password")).andExpect(status().isOk());
    User updatedUser = userRepository.findOneByLogin("change-password").orElse(null);
    assertThat(passwordEncoder.matches("new password", updatedUser.getPassword())).isTrue();
}
Also used : User(com.arnaugarcia.uplace.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) 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)

Example 47 with User

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

the class AccountResourceIntTest method testRegisterDuplicateLogin.

@Test
@Transactional
public void testRegisterDuplicateLogin() throws Exception {
    // Good
    ManagedUserVM validUser = new ManagedUserVM();
    validUser.setLogin("alice");
    validUser.setPassword("password");
    validUser.setFirstName("Alice");
    validUser.setLastName("Something");
    validUser.setEmail("alice@example.com");
    validUser.setActivated(true);
    validUser.setImageUrl("http://placehold.it/50x50");
    validUser.setLangKey(Constants.DEFAULT_LANGUAGE);
    validUser.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));
    // Duplicate login, different email
    ManagedUserVM duplicatedUser = new ManagedUserVM();
    duplicatedUser.setLogin(validUser.getLogin());
    duplicatedUser.setPassword(validUser.getPassword());
    duplicatedUser.setFirstName(validUser.getFirstName());
    duplicatedUser.setLastName(validUser.getLastName());
    duplicatedUser.setEmail("alicejr@example.com");
    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 login
    restMvc.perform(post("/api/register").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(duplicatedUser))).andExpect(status().is4xxClientError());
    Optional<User> userDup = userRepository.findOneByEmailIgnoreCase("alicejr@example.com");
    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 48 with User

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

the class AccountResourceIntTest 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.setLangKey("en");
    user.setAuthorities(authorities);
    when(mockUserService.getUserWithAuthorities()).thenReturn(Optional.of(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("$.langKey").value("en")).andExpect(jsonPath("$.authorities").value(AuthoritiesConstants.ADMIN));
}
Also used : User(com.arnaugarcia.uplace.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Authority(com.arnaugarcia.uplace.domain.Authority) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 49 with User

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

the class AccountResourceIntTest method testSaveInvalidEmail.

@Test
@Transactional
@WithMockUser("save-invalid-email")
public void testSaveInvalidEmail() throws Exception {
    User user = new User();
    user.setLogin("save-invalid-email");
    user.setEmail("save-invalid-email@example.com");
    user.setPassword(RandomStringUtils.random(60));
    user.setActivated(true);
    userRepository.saveAndFlush(user);
    UserDTO userDTO = new UserDTO();
    userDTO.setLogin("not-used");
    userDTO.setFirstName("firstname");
    userDTO.setLastName("lastname");
    userDTO.setEmail("invalid email");
    userDTO.setActivated(false);
    userDTO.setImageUrl("http://placehold.it/50x50");
    userDTO.setLangKey(Constants.DEFAULT_LANGUAGE);
    userDTO.setAuthorities(Collections.singleton(AuthoritiesConstants.ADMIN));
    restMvc.perform(post("/api/account").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(userDTO))).andExpect(status().isBadRequest());
    assertThat(userRepository.findOneByEmailIgnoreCase("invalid email")).isNotPresent();
}
Also used : User(com.arnaugarcia.uplace.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) UserDTO(com.arnaugarcia.uplace.service.dto.UserDTO) 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)

Example 50 with User

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

the class AccountResourceIntTest method testRegisterNullPassword.

@Test
@Transactional
public void testRegisterNullPassword() throws Exception {
    ManagedUserVM invalidUser = new ManagedUserVM();
    invalidUser.setLogin("bob");
    // invalid null password
    invalidUser.setPassword(null);
    invalidUser.setFirstName("Bob");
    invalidUser.setLastName("Green");
    invalidUser.setEmail("bob@example.com");
    invalidUser.setActivated(true);
    invalidUser.setImageUrl("http://placehold.it/50x50");
    invalidUser.setLangKey(Constants.DEFAULT_LANGUAGE);
    invalidUser.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));
    restUserMockMvc.perform(post("/api/register").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(invalidUser))).andExpect(status().isBadRequest());
    Optional<User> user = userRepository.findOneByLogin("bob");
    assertThat(user.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)

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