Search in sources :

Example 1 with ManagedUserVM

use of de.tum.in.www1.artemis.web.rest.vm.ManagedUserVM in project ArTEMiS by ls1intum.

the class AccountResourceIntTest method testRegisterAdminIsIgnored.

@Test
@Transactional
public void testRegisterAdminIsIgnored() throws Exception {
    ManagedUserVM validUser = new ManagedUserVM(// id
    null, // login
    "badguy", // password
    "password", // firstName
    "Bad", // lastName
    "Guy", // email
    "badguy@example.com", // activated
    true, // imageUrl
    "http://placehold.it/50x50", // langKey
    Constants.DEFAULT_LANGUAGE, // createdBy
    null, // createdDate
    null, // lastModifiedBy
    null, // lastModifiedDate
    null, new HashSet<>(Collections.singletonList(AuthoritiesConstants.ADMIN)), new ArrayList<String>());
    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(de.tum.in.www1.artemis.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) ManagedUserVM(de.tum.in.www1.artemis.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 de.tum.in.www1.artemis.web.rest.vm.ManagedUserVM in project ArTEMiS by ls1intum.

the class AccountResourceIntTest method testRegisterValid.

@Test
@Transactional
public void testRegisterValid() throws Exception {
    ManagedUserVM validUser = new ManagedUserVM(// id
    null, // login
    "joe", // password
    "password", // firstName
    "Joe", // lastName
    "Shmoe", // email
    "joe@example.com", // activated
    true, // imageUrl
    "http://placehold.it/50x50", // langKey
    Constants.DEFAULT_LANGUAGE, // createdBy
    null, // createdDate
    null, // lastModifiedBy
    null, // lastModifiedDate
    null, new HashSet<>(Collections.singletonList(AuthoritiesConstants.USER)), new ArrayList<String>());
    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(de.tum.in.www1.artemis.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) ManagedUserVM(de.tum.in.www1.artemis.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 de.tum.in.www1.artemis.web.rest.vm.ManagedUserVM in project ArTEMiS by ls1intum.

the class AccountResourceIntTest method testRegisterNullPassword.

@Test
@Transactional
public void testRegisterNullPassword() throws Exception {
    ManagedUserVM invalidUser = new ManagedUserVM(// id
    null, // login
    "bob", // invalid null password
    null, // firstName
    "Bob", // lastName
    "Green", // email
    "bob@example.com", // activated
    true, // imageUrl
    "http://placehold.it/50x50", // langKey
    Constants.DEFAULT_LANGUAGE, // createdBy
    null, // createdDate
    null, // lastModifiedBy
    null, // lastModifiedDate
    null, new HashSet<>(Collections.singletonList(AuthoritiesConstants.USER)), new ArrayList<String>());
    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(de.tum.in.www1.artemis.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) ManagedUserVM(de.tum.in.www1.artemis.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 de.tum.in.www1.artemis.web.rest.vm.ManagedUserVM in project ArTEMiS by ls1intum.

the class UserResourceIntTest method updateUserExistingEmail.

@Test
@Transactional
public void updateUserExistingEmail() throws Exception {
    // Initialize the database with 2 users
    userRepository.saveAndFlush(user);
    User anotherUser = new User();
    anotherUser.setLogin("jhipster");
    anotherUser.setPassword(RandomStringUtils.random(60));
    anotherUser.setActivated(true);
    anotherUser.setEmail("jhipster@localhost");
    anotherUser.setFirstName("java");
    anotherUser.setLastName("hipster");
    anotherUser.setImageUrl("");
    anotherUser.setLangKey("en");
    userRepository.saveAndFlush(anotherUser);
    // Update the user
    User updatedUser = userRepository.findOne(user.getId());
    Set<String> authorities = new HashSet<>();
    authorities.add(AuthoritiesConstants.USER);
    ManagedUserVM managedUserVM = new ManagedUserVM(updatedUser.getId(), updatedUser.getLogin(), updatedUser.getPassword(), updatedUser.getFirstName(), updatedUser.getLastName(), // this email should already be used by anotherUser
    "jhipster@localhost", updatedUser.getActivated(), updatedUser.getImageUrl(), updatedUser.getLangKey(), updatedUser.getCreatedBy(), updatedUser.getCreatedDate(), updatedUser.getLastModifiedBy(), updatedUser.getLastModifiedDate(), authorities, new ArrayList<String>());
    restUserMockMvc.perform(put("/api/users").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(managedUserVM))).andExpect(status().isBadRequest());
}
Also used : User(de.tum.in.www1.artemis.domain.User) ManagedUserVM(de.tum.in.www1.artemis.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 de.tum.in.www1.artemis.web.rest.vm.ManagedUserVM in project ArTEMiS by ls1intum.

the class UserResourceIntTest method createUserWithExistingEmail.

@Test
@Transactional
public void createUserWithExistingEmail() throws Exception {
    // Initialize the database
    userRepository.saveAndFlush(user);
    int databaseSizeBeforeCreate = userRepository.findAll().size();
    Set<String> authorities = new HashSet<>();
    authorities.add(AuthoritiesConstants.USER);
    ManagedUserVM managedUserVM = new ManagedUserVM(null, "anotherlogin", DEFAULT_PASSWORD, DEFAULT_FIRSTNAME, DEFAULT_LASTNAME, // this email should already be used
    DEFAULT_EMAIL, true, DEFAULT_IMAGEURL, DEFAULT_LANGKEY, null, null, null, null, authorities, new ArrayList<String>());
    // 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(de.tum.in.www1.artemis.domain.User) ManagedUserVM(de.tum.in.www1.artemis.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 (de.tum.in.www1.artemis.domain.User)19 ManagedUserVM (de.tum.in.www1.artemis.web.rest.vm.ManagedUserVM)16 Test (org.junit.Test)16 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)16 Transactional (org.springframework.transaction.annotation.Transactional)16 HashSet (java.util.HashSet)9 WithMockUser (org.springframework.security.test.context.support.WithMockUser)8 Timed (com.codahale.metrics.annotation.Timed)2 Authority (de.tum.in.www1.artemis.domain.Authority)1 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)1 EmailAlreadyUsedException (de.tum.in.www1.artemis.web.rest.errors.EmailAlreadyUsedException)1 LoginAlreadyUsedException (de.tum.in.www1.artemis.web.rest.errors.LoginAlreadyUsedException)1 URI (java.net.URI)1 Secured (org.springframework.security.access.annotation.Secured)1