Search in sources :

Example 21 with PasswordChangeDTO

use of io.github.jhipster.sample.service.dto.PasswordChangeDTO in project jhipster-sample-app-elasticsearch by jhipster.

the class AccountResourceIntTest method testChangePasswordTooSmall.

@Test
@Transactional
@WithMockUser("change-password-too-small")
public void testChangePasswordTooSmall() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password-too-small");
    user.setEmail("change-password-too-small@example.com");
    userRepository.saveAndFlush(user);
    restMvc.perform(post("/api/account/change-password").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, "new")))).andExpect(status().isBadRequest());
    User updatedUser = userRepository.findOneByLogin("change-password-too-small").orElse(null);
    assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
}
Also used : User(io.github.jhipster.sample.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) PasswordChangeDTO(io.github.jhipster.sample.service.dto.PasswordChangeDTO) 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 22 with PasswordChangeDTO

use of io.github.jhipster.sample.service.dto.PasswordChangeDTO in project jhipster-sample-app-dto by jhipster.

the class AccountResourceIntTest method testChangePasswordWrongExistingPassword.

@Test
@Transactional
@WithMockUser("change-password-wrong-existing-password")
public void testChangePasswordWrongExistingPassword() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password-wrong-existing-password");
    user.setEmail("change-password-wrong-existing-password@example.com");
    userRepository.saveAndFlush(user);
    restMvc.perform(post("/api/account/change-password").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO("1" + currentPassword, "new password")))).andExpect(status().isBadRequest());
    User updatedUser = userRepository.findOneByLogin("change-password-wrong-existing-password").orElse(null);
    assertThat(passwordEncoder.matches("new password", updatedUser.getPassword())).isFalse();
    assertThat(passwordEncoder.matches(currentPassword, updatedUser.getPassword())).isTrue();
}
Also used : User(io.github.jhipster.sample.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) PasswordChangeDTO(io.github.jhipster.sample.service.dto.PasswordChangeDTO) 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 23 with PasswordChangeDTO

use of io.github.jhipster.sample.service.dto.PasswordChangeDTO in project jhipster-sample-app-dto by jhipster.

the class AccountResourceIntTest method testChangePasswordTooLong.

@Test
@Transactional
@WithMockUser("change-password-too-long")
public void testChangePasswordTooLong() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password-too-long");
    user.setEmail("change-password-too-long@example.com");
    userRepository.saveAndFlush(user);
    restMvc.perform(post("/api/account/change-password").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, RandomStringUtils.random(101))))).andExpect(status().isBadRequest());
    User updatedUser = userRepository.findOneByLogin("change-password-too-long").orElse(null);
    assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
}
Also used : User(io.github.jhipster.sample.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) PasswordChangeDTO(io.github.jhipster.sample.service.dto.PasswordChangeDTO) 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 24 with PasswordChangeDTO

use of io.github.jhipster.sample.service.dto.PasswordChangeDTO in project jhipster-sample-app-dto by jhipster.

the class AccountResourceIntTest method testChangePassword.

@Test
@Transactional
@WithMockUser("change-password")
public void testChangePassword() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password");
    user.setEmail("change-password@example.com");
    userRepository.saveAndFlush(user);
    restMvc.perform(post("/api/account/change-password").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, "new password")))).andExpect(status().isOk());
    User updatedUser = userRepository.findOneByLogin("change-password").orElse(null);
    assertThat(passwordEncoder.matches("new password", updatedUser.getPassword())).isTrue();
}
Also used : User(io.github.jhipster.sample.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) PasswordChangeDTO(io.github.jhipster.sample.service.dto.PasswordChangeDTO) 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)

Aggregations

User (io.github.jhipster.sample.domain.User)24 PasswordChangeDTO (io.github.jhipster.sample.service.dto.PasswordChangeDTO)24 Test (org.junit.Test)24 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)24 WithMockUser (org.springframework.security.test.context.support.WithMockUser)24 Transactional (org.springframework.transaction.annotation.Transactional)16 AbstractCassandraTest (io.github.jhipster.sample.AbstractCassandraTest)4