Search in sources :

Example 11 with ManagedUserVM

use of com.baeldung.web.rest.vm.ManagedUserVM in project tutorials by eugenp.

the class UserResourceIntegrationTest method createUserWithExistingId.

@Test
@Transactional
public void createUserWithExistingId() throws Exception {
    int databaseSizeBeforeCreate = userRepository.findAll().size();
    Set<String> autorities = new HashSet<>();
    autorities.add("ROLE_USER");
    ManagedUserVM managedUserVM = new ManagedUserVM(1L, DEFAULT_LOGIN, DEFAULT_PASSWORD, DEFAULT_FIRSTNAME, DEFAULT_LASTNAME, DEFAULT_EMAIL, true, DEFAULT_IMAGEURL, DEFAULT_LANGKEY, null, null, null, null, autorities);
    // An entity with an existing ID cannot be created, so this API call must fail
    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(com.baeldung.domain.User) ManagedUserVM(com.baeldung.web.rest.vm.ManagedUserVM) HashSet(java.util.HashSet) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with ManagedUserVM

use of com.baeldung.web.rest.vm.ManagedUserVM 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 13 with ManagedUserVM

use of com.baeldung.web.rest.vm.ManagedUserVM 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 14 with ManagedUserVM

use of com.baeldung.web.rest.vm.ManagedUserVM 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 15 with ManagedUserVM

use of com.baeldung.web.rest.vm.ManagedUserVM 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)

Aggregations

User (com.baeldung.domain.User)16 ManagedUserVM (com.baeldung.web.rest.vm.ManagedUserVM)16 Test (org.junit.Test)15 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)15 Transactional (org.springframework.transaction.annotation.Transactional)15 HashSet (java.util.HashSet)8 UserRepository (com.baeldung.repository.UserRepository)1 SecurityUtils (com.baeldung.security.SecurityUtils)1 MailService (com.baeldung.service.MailService)1 UserService (com.baeldung.service.UserService)1 UserDTO (com.baeldung.service.dto.UserDTO)1 HeaderUtil (com.baeldung.web.rest.util.HeaderUtil)1 KeyAndPasswordVM (com.baeldung.web.rest.vm.KeyAndPasswordVM)1 Timed (com.codahale.metrics.annotation.Timed)1 java.util (java.util)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Valid (javax.validation.Valid)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1