Search in sources :

Example 26 with User

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

the class AccountResourceIntegrationTest method testRegisterValid.

@Test
@Transactional
public void testRegisterValid() throws Exception {
    ManagedUserVM validUser = new ManagedUserVM(// id
    null, // login
    "joe", // password
    "password", // firstName
    "Joe", // lastName
    "Shmoe", // e-mail
    "joe@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)));
    restMvc.perform(post("/api/register").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(validUser))).andExpect(status().isCreated());
    Optional<User> user = userRepository.findOneByLogin("joe");
    assertThat(user.isPresent()).isTrue();
}
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 27 with User

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

the class AccountResourceIntegrationTest method testRegisterInvalidPassword.

@Test
@Transactional
public void testRegisterInvalidPassword() throws Exception {
    ManagedUserVM invalidUser = new ManagedUserVM(// id
    null, // login
    "bob", // password with only 3 digits
    "123", // firstName
    "Bob", // lastName
    "Green", // e-mail
    "bob@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/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.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 28 with User

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

the class AccountResourceIntegrationTest method testRegisterDuplicateEmail.

@Test
@Transactional
public void testRegisterDuplicateEmail() throws Exception {
    // Good
    ManagedUserVM validUser = new ManagedUserVM(// id
    null, // login
    "john", // password
    "password", // firstName
    "John", // lastName
    "Doe", // e-mail
    "john@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 e-mail, different login
    ManagedUserVM duplicatedUser = new ManagedUserVM(validUser.getId(), "johnjr", validUser.getPassword(), validUser.getLogin(), validUser.getLastName(), validUser.getEmail(), 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 e-mail
    restMvc.perform(post("/api/register").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(duplicatedUser))).andExpect(status().is4xxClientError());
    Optional<User> userDup = userRepository.findOneByLogin("johnjr");
    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 29 with User

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

the class AccountResource method registerAccount.

/**
 * POST  /register : register the user.
 *
 * @param managedUserVM the managed user View Model
 * @return the ResponseEntity with status 201 (Created) if the user is registered or 400 (Bad Request) if the login or e-mail is already in use
 */
@PostMapping(path = "/register", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_PLAIN_VALUE })
@Timed
public ResponseEntity registerAccount(@Valid @RequestBody ManagedUserVM managedUserVM) {
    HttpHeaders textPlainHeaders = new HttpHeaders();
    textPlainHeaders.setContentType(MediaType.TEXT_PLAIN);
    return userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase()).map(user -> new ResponseEntity<>("login already in use", textPlainHeaders, HttpStatus.BAD_REQUEST)).orElseGet(() -> userRepository.findOneByEmail(managedUserVM.getEmail()).map(user -> new ResponseEntity<>("e-mail address already in use", textPlainHeaders, HttpStatus.BAD_REQUEST)).orElseGet(() -> {
        User user = userService.createUser(managedUserVM.getLogin(), managedUserVM.getPassword(), managedUserVM.getFirstName(), managedUserVM.getLastName(), managedUserVM.getEmail().toLowerCase(), managedUserVM.getImageUrl(), managedUserVM.getLangKey());
        mailService.sendActivationEmail(user);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }));
}
Also used : UserDTO(com.baeldung.service.dto.UserDTO) java.util(java.util) Logger(org.slf4j.Logger) MailService(com.baeldung.service.MailService) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) User(com.baeldung.domain.User) LoggerFactory(org.slf4j.LoggerFactory) HeaderUtil(com.baeldung.web.rest.util.HeaderUtil) ManagedUserVM(com.baeldung.web.rest.vm.ManagedUserVM) StringUtils(org.apache.commons.lang3.StringUtils) Valid(javax.validation.Valid) Timed(com.codahale.metrics.annotation.Timed) HttpStatus(org.springframework.http.HttpStatus) HttpServletRequest(javax.servlet.http.HttpServletRequest) UserService(com.baeldung.service.UserService) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) UserRepository(com.baeldung.repository.UserRepository) KeyAndPasswordVM(com.baeldung.web.rest.vm.KeyAndPasswordVM) ResponseEntity(org.springframework.http.ResponseEntity) SecurityUtils(com.baeldung.security.SecurityUtils) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) User(com.baeldung.domain.User) Timed(com.codahale.metrics.annotation.Timed)

Example 30 with User

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

the class UserService method createUser.

public User createUser(UserDTO userDTO) {
    User user = new User();
    user.setLogin(userDTO.getLogin());
    user.setFirstName(userDTO.getFirstName());
    user.setLastName(userDTO.getLastName());
    user.setEmail(userDTO.getEmail());
    user.setImageUrl(userDTO.getImageUrl());
    if (userDTO.getLangKey() == null) {
        // default language
        user.setLangKey("en");
    } else {
        user.setLangKey(userDTO.getLangKey());
    }
    if (userDTO.getAuthorities() != null) {
        Set<Authority> authorities = new HashSet<>();
        userDTO.getAuthorities().forEach(authority -> authorities.add(authorityRepository.findOne(authority)));
        user.setAuthorities(authorities);
    }
    String encryptedPassword = passwordEncoder.encode(RandomUtil.generatePassword());
    user.setPassword(encryptedPassword);
    user.setResetKey(RandomUtil.generateResetKey());
    user.setResetDate(ZonedDateTime.now());
    user.setActivated(true);
    userRepository.save(user);
    log.debug("Created Information for User: {}", user);
    return user;
}
Also used : User(com.baeldung.domain.User) Authority(com.baeldung.domain.Authority)

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