Search in sources :

Example 6 with PasswordChangeDTO

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

the class AccountResourceIntTest method testChangePassword.

@Test
@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.save(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)

Example 7 with PasswordChangeDTO

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

the class AccountResourceIntTest method testChangePasswordTooLong.

@Test
@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.save(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)

Example 8 with PasswordChangeDTO

use of io.github.jhipster.sample.service.dto.PasswordChangeDTO in project jhipster-sample-app-hazelcast 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 9 with PasswordChangeDTO

use of io.github.jhipster.sample.service.dto.PasswordChangeDTO in project jhipster-sample-app-elasticsearch 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 10 with PasswordChangeDTO

use of io.github.jhipster.sample.service.dto.PasswordChangeDTO in project jhipster-sample-app-elasticsearch 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)

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