Search in sources :

Example 1 with ManagedUserVM

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

the class AccountResourceIntegrationTest method testRegisterAdminIsIgnored.

@Test
@Transactional
public void testRegisterAdminIsIgnored() throws Exception {
    ManagedUserVM validUser = new ManagedUserVM(// id
    null, // login
    "badguy", // password
    "password", // firstName
    "Bad", // lastName
    "Guy", // e-mail
    "badguy@example.com", // activated
    true, // imageUrl
    "http://placehold.it/50x50", // langKey
    "en", // createdBy
    null, // createdDate
    null, // lastModifiedBy
    null, // lastModifiedDate
    null, new HashSet<>(Arrays.asList(AuthoritiesConstants.ADMIN)));
    restMvc.perform(post("/api/register").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(validUser))).andExpect(status().isCreated());
    Optional<User> userDup = userRepository.findOneByLogin("badguy");
    assertThat(userDup.isPresent()).isTrue();
    assertThat(userDup.get().getAuthorities()).hasSize(1).containsExactly(authorityRepository.findOne(AuthoritiesConstants.USER));
}
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 2 with ManagedUserVM

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

the class AccountResourceIntegrationTest method testRegisterInvalidEmail.

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

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

the class AccountResourceIntegrationTest method testRegisterInvalidLogin.

@Test
@Transactional
public void testRegisterInvalidLogin() throws Exception {
    ManagedUserVM invalidUser = new ManagedUserVM(// id
    null, // login <-- invalid
    "funky-log!n", // password
    "password", // 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/register").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) 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 4 with ManagedUserVM

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

the class UserResourceIntegrationTest method updateUser.

@Test
@Transactional
public void updateUser() throws Exception {
    // Initialize the database
    userRepository.saveAndFlush(user);
    int databaseSizeBeforeUpdate = userRepository.findAll().size();
    // Update the user
    User updatedUser = userRepository.findOne(user.getId());
    Set<String> autorities = new HashSet<>();
    autorities.add("ROLE_USER");
    ManagedUserVM managedUserVM = new ManagedUserVM(updatedUser.getId(), updatedUser.getLogin(), UPDATED_PASSWORD, UPDATED_FIRSTNAME, UPDATED_LASTNAME, UPDATED_EMAIL, updatedUser.getActivated(), UPDATED_IMAGEURL, UPDATED_LANGKEY, updatedUser.getCreatedBy(), updatedUser.getCreatedDate(), updatedUser.getLastModifiedBy(), updatedUser.getLastModifiedDate(), autorities);
    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.getFirstName()).isEqualTo(UPDATED_FIRSTNAME);
    assertThat(testUser.getLastName()).isEqualTo(UPDATED_LASTNAME);
    assertThat(testUser.getEmail()).isEqualTo(UPDATED_EMAIL);
    assertThat(testUser.getImageUrl()).isEqualTo(UPDATED_IMAGEURL);
    assertThat(testUser.getLangKey()).isEqualTo(UPDATED_LANGKEY);
}
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 5 with ManagedUserVM

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

the class UserResourceIntegrationTest method createUser.

@Test
@Transactional
public void createUser() throws Exception {
    int databaseSizeBeforeCreate = userRepository.findAll().size();
    // Create the User
    Set<String> autorities = new HashSet<>();
    autorities.add("ROLE_USER");
    ManagedUserVM managedUserVM = new ManagedUserVM(null, DEFAULT_LOGIN, DEFAULT_PASSWORD, DEFAULT_FIRSTNAME, DEFAULT_LASTNAME, DEFAULT_EMAIL, true, DEFAULT_IMAGEURL, DEFAULT_LANGKEY, null, null, null, null, autorities);
    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.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
    assertThat(testUser.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
}
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)

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