Skip to content

Commit 2fbe17c

Browse files
author
Eugen
committed
Merge pull request eugenp#186 from Doha2012/master
add api test
2 parents ae0564f + 0923bc3 commit 2fbe17c

File tree

5 files changed

+151
-10
lines changed

5 files changed

+151
-10
lines changed

spring-security-login-and-registration/pom.xml

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@
5454
</dependency>
5555

5656
<dependency>
57-
<groupId>org.springframework</groupId>
58-
<artifactId>spring-test</artifactId>
59-
<scope>test</scope>
60-
</dependency>
57+
<groupId>org.springframework</groupId>
58+
<artifactId>spring-test</artifactId>
59+
<scope>test</scope>
60+
</dependency>
6161

6262
<!-- Password Validation -->
6363
<dependency>
64-
<groupId>org.passay</groupId>
65-
<artifactId>passay</artifactId>
66-
<version>1.0</version>
67-
</dependency>
64+
<groupId>org.passay</groupId>
65+
<artifactId>passay</artifactId>
66+
<version>1.0</version>
67+
</dependency>
6868

6969

7070
<!-- Spring Data JPA dependencies -->
@@ -137,6 +137,19 @@
137137
<scope>test</scope>
138138
</dependency>
139139

140+
<dependency>
141+
<groupId>com.jayway.restassured</groupId>
142+
<artifactId>rest-assured</artifactId>
143+
<version>2.4.0</version>
144+
<scope>test</scope>
145+
<exclusions>
146+
<exclusion>
147+
<artifactId>commons-logging</artifactId>
148+
<groupId>commons-logging</groupId>
149+
</exclusion>
150+
</exclusions>
151+
</dependency>
152+
140153
</dependencies>
141154

142155
<build>
@@ -189,4 +202,4 @@
189202
<guava.version>18.0</guava.version>
190203

191204
</properties>
192-
</project>
205+
</project>

spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/RegistrationController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ public GenericResponse savePassword(final Locale locale, @RequestParam("password
171171
// change user password
172172

173173
@RequestMapping(value = "/user/updatePassword", method = RequestMethod.POST)
174+
@PreAuthorize("hasRole('READ_PRIVILEGE')")
174175
@ResponseBody
175176
public GenericResponse changeUserPassword(final Locale locale, @RequestParam("password") final String password, @RequestParam("oldpassword") final String oldPassword) {
176177
final User user = userService.findUserByEmail(SecurityContextHolder.getContext().getAuthentication().getName());

spring-security-login-and-registration/src/main/webapp/WEB-INF/view/changePassword.jsp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<title><spring:message code="message.changePassword"></spring:message></title>
1414
</head>
1515
<body>
16+
<sec:authorize access="hasRole('READ_PRIVILEGE')">
1617
<nav class="navbar navbar-default">
1718
<div class="container-fluid">
1819
<div class="navbar-header">
@@ -66,7 +67,8 @@ function savePass(){
6667
$("#errormsg").show().html(data.responseJSON.message);
6768
});
6869
}
69-
</script>
70+
</script>
71+
</sec:authorize>
7072
</body>
7173

7274
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.spring;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
6+
import org.springframework.security.crypto.password.PasswordEncoder;
7+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
8+
9+
@Configuration
10+
// @ComponentScan("org.baeldung.test")
11+
public class ConfigTest extends WebMvcConfigurerAdapter {
12+
13+
public ConfigTest() {
14+
super();
15+
}
16+
17+
// API
18+
@Bean
19+
public PasswordEncoder encoder() {
20+
return new BCryptPasswordEncoder(11);
21+
}
22+
23+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)