|
| 1 | +package org.baeldung.test; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.baeldung.persistence.dao.UserRepository; |
| 11 | +import org.baeldung.persistence.model.User; |
| 12 | +import org.baeldung.spring.ConfigTest; |
| 13 | +import org.baeldung.spring.PersistenceJPAConfig; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.runner.RunWith; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 19 | +import org.springframework.test.context.ActiveProfiles; |
| 20 | +import org.springframework.test.context.ContextConfiguration; |
| 21 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 22 | +import org.springframework.test.context.support.AnnotationConfigContextLoader; |
| 23 | + |
| 24 | +import com.jayway.restassured.RestAssured; |
| 25 | +import com.jayway.restassured.authentication.FormAuthConfig; |
| 26 | +import com.jayway.restassured.response.Response; |
| 27 | +import com.jayway.restassured.specification.RequestSpecification; |
| 28 | + |
| 29 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 30 | +@ContextConfiguration(classes = { ConfigTest.class, PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class) |
| 31 | +@ActiveProfiles("test") |
| 32 | +public class RegistrationAPIChangePasswordTest { |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private UserRepository userRepository; |
| 36 | + |
| 37 | + @Autowired |
| 38 | + private PasswordEncoder passwordEncoder; |
| 39 | + |
| 40 | + private final String URL_PREFIX = "http://localhost:8080/spring-security-login-and-registration"; |
| 41 | + |
| 42 | + private final String URL = URL_PREFIX + "/user/updatePassword"; |
| 43 | + |
| 44 | + FormAuthConfig formConfig = new FormAuthConfig(URL_PREFIX + "/j_spring_security_check", "j_username", "j_password"); |
| 45 | + |
| 46 | + @Before |
| 47 | + public void init() { |
| 48 | + User user = userRepository. findByEmail( "[email protected]"); |
| 49 | + if (user == null) { |
| 50 | + user = new User(); |
| 51 | + user.setFirstName("Test"); |
| 52 | + user.setLastName("Test"); |
| 53 | + user.setPassword(passwordEncoder.encode("test")); |
| 54 | + user. setEmail( "[email protected]"); |
| 55 | + user.setEnabled(true); |
| 56 | + userRepository.save(user); |
| 57 | + } else { |
| 58 | + user.setPassword(passwordEncoder.encode("test")); |
| 59 | + userRepository.save(user); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void givenLoggedInUser_whenChangingPassword_thenCorrect() { |
| 65 | + final RequestSpecification request = RestAssured. given(). auth(). form( "[email protected]", "test", formConfig); |
| 66 | + |
| 67 | + final Map<String, String> params = new HashMap<String, String>(); |
| 68 | + params.put("oldpassword", "test"); |
| 69 | + params.put("password", "newtest"); |
| 70 | + |
| 71 | + final Response response = request.with().params(params).post(URL); |
| 72 | + |
| 73 | + assertEquals(200, response.statusCode()); |
| 74 | + assertTrue(response.body().asString().contains("Password updated successfully")); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void givenWrongOldPassword_whenChangingPassword_thenBadRequest() { |
| 79 | + final RequestSpecification request = RestAssured. given(). auth(). form( "[email protected]", "test", formConfig); |
| 80 | + |
| 81 | + final Map<String, String> params = new HashMap<String, String>(); |
| 82 | + params.put("oldpassword", "abc"); |
| 83 | + params.put("password", "newtest"); |
| 84 | + |
| 85 | + final Response response = request.with().params(params).post(URL); |
| 86 | + |
| 87 | + assertEquals(400, response.statusCode()); |
| 88 | + assertTrue(response.body().asString().contains("Invalid Old Password")); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void givenNotAuthenticatedUser_whenChangingPassword_thenRedirect() { |
| 93 | + final Map<String, String> params = new HashMap<String, String>(); |
| 94 | + params.put("oldpassword", "abc"); |
| 95 | + params.put("password", "xyz"); |
| 96 | + |
| 97 | + final Response response = RestAssured.with().params(params).post(URL); |
| 98 | + |
| 99 | + assertEquals(302, response.statusCode()); |
| 100 | + assertFalse(response.body().asString().contains("Password updated successfully")); |
| 101 | + } |
| 102 | +} |
0 commit comments