Search in sources :

Example 81 with User

use of io.github.jhipster.sample.domain.User in project jhipster-sample-app-cassandra by jhipster.

the class UserResourceIntTest method createUser.

@Test
public void createUser() throws Exception {
    int databaseSizeBeforeCreate = userRepository.findAll().size();
    // Create the User
    ManagedUserVM managedUserVM = new ManagedUserVM();
    managedUserVM.setLogin(DEFAULT_LOGIN);
    managedUserVM.setPassword(DEFAULT_PASSWORD);
    managedUserVM.setFirstName(DEFAULT_FIRSTNAME);
    managedUserVM.setLastName(DEFAULT_LASTNAME);
    managedUserVM.setEmail(DEFAULT_EMAIL);
    managedUserVM.setActivated(true);
    managedUserVM.setLangKey(DEFAULT_LANGKEY);
    managedUserVM.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));
    restUserMockMvc.perform(post("/api/users").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(managedUserVM))).andExpect(status().isCreated());
    // Validate the User in the database
    List<User> userList = userRepository.findAll();
    assertThat(userList).hasSize(databaseSizeBeforeCreate + 1);
    User testUser = userList.get(userList.size() - 1);
    assertThat(testUser.getLogin()).isEqualTo(DEFAULT_LOGIN);
    assertThat(testUser.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
    assertThat(testUser.getLastName()).isEqualTo(DEFAULT_LASTNAME);
    assertThat(testUser.getEmail()).isEqualTo(DEFAULT_EMAIL);
    assertThat(testUser.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
}
Also used : User(io.github.jhipster.sample.domain.User) ManagedUserVM(io.github.jhipster.sample.web.rest.vm.ManagedUserVM) AbstractCassandraTest(io.github.jhipster.sample.AbstractCassandraTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 82 with User

use of io.github.jhipster.sample.domain.User in project jhipster-sample-app-cassandra by jhipster.

the class UserResourceIntTest method createUserWithExistingLogin.

@Test
public void createUserWithExistingLogin() throws Exception {
    // Initialize the database
    userRepository.save(user);
    int databaseSizeBeforeCreate = userRepository.findAll().size();
    ManagedUserVM managedUserVM = new ManagedUserVM();
    // this login should already be used
    managedUserVM.setLogin(DEFAULT_LOGIN);
    managedUserVM.setPassword(DEFAULT_PASSWORD);
    managedUserVM.setFirstName(DEFAULT_FIRSTNAME);
    managedUserVM.setLastName(DEFAULT_LASTNAME);
    managedUserVM.setEmail("anothermail@localhost");
    managedUserVM.setActivated(true);
    managedUserVM.setLangKey(DEFAULT_LANGKEY);
    managedUserVM.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));
    // Create the User
    restUserMockMvc.perform(post("/api/users").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(managedUserVM))).andExpect(status().isBadRequest());
    // Validate the User in the database
    List<User> userList = userRepository.findAll();
    assertThat(userList).hasSize(databaseSizeBeforeCreate);
}
Also used : User(io.github.jhipster.sample.domain.User) ManagedUserVM(io.github.jhipster.sample.web.rest.vm.ManagedUserVM) AbstractCassandraTest(io.github.jhipster.sample.AbstractCassandraTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 83 with User

use of io.github.jhipster.sample.domain.User in project jhipster-sample-app-cassandra by jhipster.

the class UserResourceIntTest method testUserDTOtoUser.

@Test
public void testUserDTOtoUser() {
    UserDTO userDTO = new UserDTO();
    userDTO.setId(DEFAULT_ID);
    userDTO.setLogin(DEFAULT_LOGIN);
    userDTO.setFirstName(DEFAULT_FIRSTNAME);
    userDTO.setLastName(DEFAULT_LASTNAME);
    userDTO.setEmail(DEFAULT_EMAIL);
    userDTO.setActivated(true);
    userDTO.setLangKey(DEFAULT_LANGKEY);
    userDTO.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));
    User user = userMapper.userDTOToUser(userDTO);
    assertThat(user.getId()).isEqualTo(DEFAULT_ID);
    assertThat(user.getLogin()).isEqualTo(DEFAULT_LOGIN);
    assertThat(user.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
    assertThat(user.getLastName()).isEqualTo(DEFAULT_LASTNAME);
    assertThat(user.getEmail()).isEqualTo(DEFAULT_EMAIL);
    assertThat(user.getActivated()).isEqualTo(true);
    assertThat(user.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
    assertThat(user.getAuthorities()).containsExactly(AuthoritiesConstants.USER);
}
Also used : User(io.github.jhipster.sample.domain.User) UserDTO(io.github.jhipster.sample.service.dto.UserDTO) AbstractCassandraTest(io.github.jhipster.sample.AbstractCassandraTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 84 with User

use of io.github.jhipster.sample.domain.User in project jhipster-sample-app-cassandra by jhipster.

the class UserResourceIntTest method updateUserLogin.

@Test
public void updateUserLogin() throws Exception {
    // Initialize the database
    userRepository.save(user);
    int databaseSizeBeforeUpdate = userRepository.findAll().size();
    // Update the user
    User updatedUser = userRepository.findById(user.getId()).get();
    ManagedUserVM managedUserVM = new ManagedUserVM();
    managedUserVM.setId(updatedUser.getId());
    managedUserVM.setLogin(UPDATED_LOGIN);
    managedUserVM.setPassword(UPDATED_PASSWORD);
    managedUserVM.setFirstName(UPDATED_FIRSTNAME);
    managedUserVM.setLastName(UPDATED_LASTNAME);
    managedUserVM.setEmail(UPDATED_EMAIL);
    managedUserVM.setActivated(updatedUser.getActivated());
    managedUserVM.setLangKey(UPDATED_LANGKEY);
    managedUserVM.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));
    restUserMockMvc.perform(put("/api/users").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(managedUserVM))).andExpect(status().isOk());
    // Validate the User in the database
    List<User> userList = userRepository.findAll();
    assertThat(userList).hasSize(databaseSizeBeforeUpdate);
    User testUser = userList.get(userList.size() - 1);
    assertThat(testUser.getLogin()).isEqualTo(UPDATED_LOGIN);
    assertThat(testUser.getFirstName()).isEqualTo(UPDATED_FIRSTNAME);
    assertThat(testUser.getLastName()).isEqualTo(UPDATED_LASTNAME);
    assertThat(testUser.getEmail()).isEqualTo(UPDATED_EMAIL);
    assertThat(testUser.getLangKey()).isEqualTo(UPDATED_LANGKEY);
}
Also used : User(io.github.jhipster.sample.domain.User) ManagedUserVM(io.github.jhipster.sample.web.rest.vm.ManagedUserVM) AbstractCassandraTest(io.github.jhipster.sample.AbstractCassandraTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 85 with User

use of io.github.jhipster.sample.domain.User in project jhipster-sample-app-mongodb by jhipster.

the class UserResource method createUser.

/**
 * POST  /users  : Creates a new user.
 * <p>
 * Creates a new user if the login and email are not already used, and sends an
 * mail with an activation link.
 * The user needs to be activated on creation.
 *
 * @param userDTO the user to create
 * @return the ResponseEntity with status 201 (Created) and with body the new user, or with status 400 (Bad Request) if the login or email is already in use
 * @throws URISyntaxException if the Location URI syntax is incorrect
 * @throws BadRequestAlertException 400 (Bad Request) if the login or email is already in use
 */
@PostMapping("/users")
@Timed
@Secured(AuthoritiesConstants.ADMIN)
public ResponseEntity<User> createUser(@Valid @RequestBody UserDTO userDTO) throws URISyntaxException {
    log.debug("REST request to save User : {}", userDTO);
    if (userDTO.getId() != null) {
        throw new BadRequestAlertException("A new user cannot already have an ID", "userManagement", "idexists");
    // Lowercase the user login before comparing with database
    } else if (userRepository.findOneByLogin(userDTO.getLogin().toLowerCase()).isPresent()) {
        throw new LoginAlreadyUsedException();
    } else if (userRepository.findOneByEmailIgnoreCase(userDTO.getEmail()).isPresent()) {
        throw new EmailAlreadyUsedException();
    } else {
        User newUser = userService.createUser(userDTO);
        mailService.sendCreationEmail(newUser);
        return ResponseEntity.created(new URI("/api/users/" + newUser.getLogin())).headers(HeaderUtil.createAlert("userManagement.created", newUser.getLogin())).body(newUser);
    }
}
Also used : BadRequestAlertException(io.github.jhipster.sample.web.rest.errors.BadRequestAlertException) User(io.github.jhipster.sample.domain.User) LoginAlreadyUsedException(io.github.jhipster.sample.web.rest.errors.LoginAlreadyUsedException) URI(java.net.URI) EmailAlreadyUsedException(io.github.jhipster.sample.web.rest.errors.EmailAlreadyUsedException) Secured(org.springframework.security.access.annotation.Secured) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

User (io.github.jhipster.sample.domain.User)329 Test (org.junit.Test)251 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)251 Transactional (org.springframework.transaction.annotation.Transactional)150 WithMockUser (org.springframework.security.test.context.support.WithMockUser)138 ManagedUserVM (io.github.jhipster.sample.web.rest.vm.ManagedUserVM)90 AbstractCassandraTest (io.github.jhipster.sample.AbstractCassandraTest)38 UserDTO (io.github.jhipster.sample.service.dto.UserDTO)32 Authority (io.github.jhipster.sample.domain.Authority)24 PasswordChangeDTO (io.github.jhipster.sample.service.dto.PasswordChangeDTO)24 MimeMessage (javax.mail.internet.MimeMessage)24 Instant (java.time.Instant)21 Before (org.junit.Before)13 Timed (com.codahale.metrics.annotation.Timed)12 KeyAndPasswordVM (io.github.jhipster.sample.web.rest.vm.KeyAndPasswordVM)12 Scheduled (org.springframework.scheduling.annotation.Scheduled)8 GrantedAuthority (org.springframework.security.core.GrantedAuthority)7 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)7 UserDetails (org.springframework.security.core.userdetails.UserDetails)7 PersistentToken (io.github.jhipster.sample.domain.PersistentToken)6