Search in sources :

Example 1 with UserDTO

use of de.tum.in.www1.artemis.service.dto.UserDTO in project ArTEMiS by ls1intum.

the class AccountResourceIntTest method testSaveExistingEmailAndLogin.

@Test
@Transactional
@WithMockUser("save-existing-email-and-login")
public void testSaveExistingEmailAndLogin() throws Exception {
    User user = new User();
    user.setLogin("save-existing-email-and-login");
    user.setEmail("save-existing-email-and-login@example.com");
    user.setPassword(RandomStringUtils.random(60));
    user.setActivated(true);
    userRepository.saveAndFlush(user);
    UserDTO userDTO = new UserDTO(// id
    null, // login
    "not-used", // firstName
    "firstname", // lastName
    "lastname", // email
    "save-existing-email-and-login@example.com", // activated
    false, // 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/account").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(userDTO))).andExpect(status().isOk());
    User updatedUser = userRepository.findOneByLogin("save-existing-email-and-login").orElse(null);
    assertThat(updatedUser.getEmail()).isEqualTo("save-existing-email-and-login@example.com");
}
Also used : User(de.tum.in.www1.artemis.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) UserDTO(de.tum.in.www1.artemis.service.dto.UserDTO) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with UserDTO

use of de.tum.in.www1.artemis.service.dto.UserDTO in project ArTEMiS by ls1intum.

the class AccountResourceIntTest method testSaveInvalidEmail.

@Test
@Transactional
@WithMockUser("save-invalid-email")
public void testSaveInvalidEmail() throws Exception {
    User user = new User();
    user.setLogin("save-invalid-email");
    user.setEmail("save-invalid-email@example.com");
    user.setPassword(RandomStringUtils.random(60));
    user.setActivated(true);
    userRepository.saveAndFlush(user);
    UserDTO userDTO = new UserDTO(// id
    null, // login
    "not-used", // firstName
    "firstname", // lastName
    "lastname", // email
    "invalid email", // activated
    false, // 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/account").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(userDTO))).andExpect(status().isBadRequest());
    assertThat(userRepository.findOneByEmailIgnoreCase("invalid email")).isNotPresent();
}
Also used : User(de.tum.in.www1.artemis.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) UserDTO(de.tum.in.www1.artemis.service.dto.UserDTO) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with UserDTO

use of de.tum.in.www1.artemis.service.dto.UserDTO in project ArTEMiS by ls1intum.

the class AccountResourceIntTest method testSaveAccount.

@Test
@Transactional
@WithMockUser("save-account")
public void testSaveAccount() throws Exception {
    User user = new User();
    user.setLogin("save-account");
    user.setEmail("save-account@example.com");
    user.setPassword(RandomStringUtils.random(60));
    user.setActivated(true);
    userRepository.saveAndFlush(user);
    UserDTO userDTO = new UserDTO(// id
    null, // login
    "not-used", // firstName
    "firstname", // lastName
    "lastname", // email
    "save-account@example.com", // activated
    false, // 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/account").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(userDTO))).andExpect(status().isOk());
    User updatedUser = userRepository.findOneByLogin(user.getLogin()).orElse(null);
    assertThat(updatedUser.getFirstName()).isEqualTo(userDTO.getFirstName());
    assertThat(updatedUser.getLastName()).isEqualTo(userDTO.getLastName());
    assertThat(updatedUser.getEmail()).isEqualTo(userDTO.getEmail());
    assertThat(updatedUser.getLangKey()).isEqualTo(userDTO.getLangKey());
    assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
    assertThat(updatedUser.getImageUrl()).isEqualTo(userDTO.getImageUrl());
    assertThat(updatedUser.getActivated()).isEqualTo(true);
    assertThat(updatedUser.getAuthorities()).isEmpty();
}
Also used : User(de.tum.in.www1.artemis.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) UserDTO(de.tum.in.www1.artemis.service.dto.UserDTO) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with UserDTO

use of de.tum.in.www1.artemis.service.dto.UserDTO in project ArTEMiS by ls1intum.

the class UserResourceIntTest method testUserDTOtoUser.

@Test
public void testUserDTOtoUser() {
    UserDTO userDTO = new UserDTO(DEFAULT_ID, DEFAULT_LOGIN, DEFAULT_FIRSTNAME, DEFAULT_LASTNAME, DEFAULT_EMAIL, true, DEFAULT_IMAGEURL, DEFAULT_LANGKEY, DEFAULT_LOGIN, null, DEFAULT_LOGIN, null, Stream.of(AuthoritiesConstants.USER).collect(Collectors.toSet()), new ArrayList<String>());
    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.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
    assertThat(user.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
    assertThat(user.getCreatedBy()).isNull();
    assertThat(user.getCreatedDate()).isNotNull();
    assertThat(user.getLastModifiedBy()).isNull();
    assertThat(user.getLastModifiedDate()).isNotNull();
    assertThat(user.getAuthorities()).extracting("name").containsExactly(AuthoritiesConstants.USER);
}
Also used : User(de.tum.in.www1.artemis.domain.User) UserDTO(de.tum.in.www1.artemis.service.dto.UserDTO) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with UserDTO

use of de.tum.in.www1.artemis.service.dto.UserDTO in project ArTEMiS by ls1intum.

the class UserService method registerUser.

public User registerUser(ManagedUserVM userDTO) {
    User newUser = new User();
    Authority authority = authorityRepository.findOne(AuthoritiesConstants.USER);
    Set<Authority> authorities = new HashSet<>();
    String encryptedPassword = passwordEncoder().encode(userDTO.getPassword());
    newUser.setLogin(userDTO.getLogin());
    // new user gets initially a generated password
    newUser.setPassword(encryptedPassword);
    newUser.setFirstName(userDTO.getFirstName());
    newUser.setLastName(userDTO.getLastName());
    newUser.setEmail(userDTO.getEmail());
    newUser.setImageUrl(userDTO.getImageUrl());
    newUser.setLangKey(userDTO.getLangKey());
    // new user is not active
    newUser.setActivated(false);
    // new user gets registration key
    newUser.setActivationKey(RandomUtil.generateActivationKey());
    authorities.add(authority);
    newUser.setAuthorities(authorities);
    userRepository.save(newUser);
    log.debug("Created Information for User: {}", newUser);
    return newUser;
}
Also used : User(de.tum.in.www1.artemis.domain.User) Authority(de.tum.in.www1.artemis.domain.Authority) HashSet(java.util.HashSet)

Aggregations

User (de.tum.in.www1.artemis.domain.User)8 UserDTO (de.tum.in.www1.artemis.service.dto.UserDTO)7 Test (org.junit.Test)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)6 Authority (de.tum.in.www1.artemis.domain.Authority)4 WithMockUser (org.springframework.security.test.context.support.WithMockUser)4 Transactional (org.springframework.transaction.annotation.Transactional)4 HashSet (java.util.HashSet)2 Timed (com.codahale.metrics.annotation.Timed)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1