Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 5bb2d7a

Browse files
FF-104 user registration steps (#30)
* added the steps FF-104 * added repo.save * fixed "user with id is in group with id" step. Co-authored-by: open-schnick <[email protected]>
1 parent 706e746 commit 5bb2d7a

File tree

6 files changed

+139
-81
lines changed

6 files changed

+139
-81
lines changed

src/main/java/de/filefighter/rest/domain/user/data/persistance/UserEntity.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lombok.Builder;
44
import lombok.Getter;
5+
import lombok.Setter;
56
import lombok.ToString;
67
import org.springframework.data.mongodb.core.mapping.Document;
78
import org.springframework.data.mongodb.core.mapping.MongoId;
@@ -10,15 +11,16 @@
1011
@Getter
1112
@ToString
1213
@Builder
14+
@Setter
1315
public class UserEntity {
1416

1517
@MongoId
1618
private final String _id;
17-
private final long userId;
18-
private final String username;
19-
private final String lowercaseUsername; // Redundancy for performance tradeoff.
20-
private final String password;
21-
private final String refreshToken; //TODO: add valid_until for refreshToken
22-
private final long[] groupIds;
19+
private long userId;
20+
private String username;
21+
private String lowercaseUsername; // Redundancy for performance tradeoff.
22+
private String password;
23+
private String refreshToken; //TODO: add valid_until for refreshToken
24+
private long[] groupIds;
2325

2426
}

src/test/java/de/filefighter/rest/RestApplicationIntegrationTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.springframework.web.client.RestTemplate;
1717

1818
import java.io.IOException;
19+
import java.util.Arrays;
1920
import java.util.HashMap;
2021
import java.util.Map;
2122

@@ -108,4 +109,29 @@ public void handleError(@NotNull ClientHttpResponse response) throws IOException
108109
results = new ResponseResults(response);
109110
}
110111
}
111-
}
112+
113+
protected static String serializeUser(String confirmationPassword,int[] groupIds, String password, String username){
114+
StringBuilder jsonString=new StringBuilder("{");
115+
116+
if (confirmationPassword != null){
117+
jsonString.append("\"confirmationPassword\": \"").append(confirmationPassword).append("\",");
118+
}
119+
if (groupIds!=null && groupIds.length>0){
120+
jsonString.append("\"groupIds\": ").append(Arrays.toString(groupIds)).append(",");
121+
}
122+
if (password != null){
123+
jsonString.append("\"password\": \"").append(password).append("\",");
124+
}
125+
if (username != null){
126+
jsonString.append("\"username\": \"").append(username).append("\",");
127+
}
128+
129+
jsonString.append("}");
130+
131+
return jsonString.toString();
132+
}
133+
134+
135+
136+
137+
}

src/test/java/de/filefighter/rest/cucumber/CommonCucumberSteps.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,12 @@ public void responseContainsKeyAndValueOfAtLeast(String key, int value) throws J
166166

167167
assertTrue(actualValue >= value);
168168
}
169+
170+
@And("user with id {long} is in group with id {long}")
171+
public void userWithIdIsInGroupWithId(long userId, long groupId) {
172+
UserEntity userEntity=userRepository.findByUserId(userId);
173+
174+
userEntity.setGroupIds(new long[]{groupId});
175+
userRepository.save(userEntity);
176+
}
169177
}

src/test/java/de/filefighter/rest/cucumber/UserEditInformationSteps.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ public void userRequestsChangeOfUsernameWithValueAndAccessTokenAndId(String newU
2121

2222

2323

24-
String postBody="{" +
25-
" \"groupIds\": [" +
26-
" 0" +
27-
" ]," +
28-
" \"username\": \""+newUsername+"\"" +
29-
"}";
24+
String postBody=serializeUser(null,null,null,newUsername);
25+
3026

3127
executeRestApiCall(HttpMethod.PUT, url, authHeader,postBody);
3228
}
@@ -40,13 +36,8 @@ public void userRequestsChangeOfPasswordWithValueAndAccessTokenAndId(String newP
4036
authHeader.put("Authorization", authHeaderString);
4137

4238

43-
String postBody="{\n" +
44-
" \"confirmationPassword\": \""+newPassword+"\"," +
45-
" \"groupIds\": [" +
46-
" 0" +
47-
" ]," +
48-
" \"password\": \""+newPassword+"\"," +
49-
"}";
39+
String postBody=serializeUser(newPassword,null,newPassword,null);
40+
5041

5142
executeRestApiCall(HttpMethod.GET, url, authHeader,postBody);
5243
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package de.filefighter.rest.cucumber;
2+
3+
import de.filefighter.rest.RestApplicationIntegrationTest;
4+
import io.cucumber.java.en.When;
5+
import org.springframework.http.HttpMethod;
6+
7+
import java.util.HashMap;
8+
9+
import static de.filefighter.rest.configuration.RestConfiguration.*;
10+
11+
public class UserRegistrationSteps extends RestApplicationIntegrationTest {
12+
@When("user requests registration with username {string}, password {string} and password confirmation {string} with accessToken {string}")
13+
public void userRequestsRegistrationWithUsernamePasswordAndPasswordConfirmationWithAccessToken(String username, String password, String passwordConfirmation, String accessToken) {
14+
15+
String authHeaderString = AUTHORIZATION_BEARER_PREFIX + accessToken;
16+
String url = BASE_API_URI + USER_BASE_URI + "register";
17+
18+
19+
HashMap<String, String> authHeader = new HashMap<>();
20+
authHeader.put("Authorization", authHeaderString);
21+
22+
23+
24+
25+
String postBody=serializeUser(password,null,password,username);
26+
27+
executeRestApiCall(HttpMethod.POST, url, authHeader,postBody);
28+
29+
30+
}
31+
}
Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
1-
#Feature: User Registration
2-
# As a user (/admin)
3-
# I want to be able to register (users) with username and password
4-
#
5-
# Background:
6-
# Given database is empty
7-
# And user with id 1234 exists and has username "user", password "secure_password"
8-
# And accessToken with value "accessToken" exists for user 1234
9-
# And user with id 1234 is in group with id 1
10-
#
11-
# Scenario: Successful registration with username, password and password confirmation.
12-
# When user requests registration with username "kangaroo", password "pig-system" and password confirmation "pig-system" with accessToken "accessToken"
13-
# Then response status code is 201
14-
# And response contains key "message" and value "User successfully created."
15-
# And response contains key "status" and value "created"
16-
#
17-
# Scenario: Successful registration with username, password and password confirmation; password matches password of other users.
18-
# When user requests registration with username "kangaroo", password "secure_password" and password confirmation "secure_password" with accessToken "accessToken"
19-
# Then response status code is 201
20-
# And response contains key "message" and value "User successfully created."
21-
# And response contains key "status" and value "created"
22-
#
23-
# Scenario: Failed registration with used username, arbitrary password and password confirmation.
24-
# When user requests registration with username "user", password "pig-system" and password confirmation "pig-system" with accessToken "accessToken"
25-
# Then response status code is 409
26-
# And response contains key "message" and value "User already exists."
27-
# And response contains key "status" and value "conflict"
28-
#
29-
# Scenario: Failed registration with used username (other case), arbitrary password and password confirmation.
30-
# When user requests registration with username "User", password "pig-system" and password confirmation "pig-system" with accessToken "accessToken"
31-
# Then response status code is 409
32-
# And response contains key "message" and value "User already exists."
33-
# And response contains key "status" and value "conflict"
34-
#
35-
# Scenario: Failed registration with username, password and deviating password confirmation.
36-
# When user requests registration with username "kangaroo", password "pig-system" and password confirmation "i-love-capitalism" with accessToken "accessToken"
37-
# Then response status code is 409
38-
# And response contains key "message" and value "Passwords do not match."
39-
# And response contains key "status" and value "conflict"
40-
#
41-
# Scenario: Failed registration with username, password and password confirmation; username is part of password.
42-
# When user requests registration with username "kangaroo", password "kangaroo-system" and password confirmation "kangaroo-system" with accessToken "accessToken"
43-
# Then response status code is 409
44-
# And response contains key "message" and value "Username must not appear in password."
45-
# And response contains key "status" and value "conflict"
46-
#
47-
# Scenario: Failed registration with username, password and password confirmation; password appears in list of top 10k passwords
48-
# When user requests registration with username "kangaroo", password "vietnam" and password confirmation "vietnam" with accessToken "accessToken"
49-
# Then response status code is 409
50-
# And response contains key "message" and value "Password must not appear in the top 10000 most common passwords."
51-
# And response contains key "status" and value "conflict"
52-
# #https://github.com/iryndin/10K-Most-Popular-Passwords/blob/master/passwords.txt
53-
#
54-
# Scenario: Failed registration with username, password and password confirmation; not in group ADMIN
55-
# Given user with id 1236 exists
56-
# And user with id 1236 is in group with id -1
57-
# And accessToken with value "wrongAccessToken" exists for user 1236
58-
# When user requests registration with username "kangaroo", password "pig-system" and password confirmation "pig-system" with accessToken "wrongAccessToken"
59-
# Then response status code is 401
60-
# And response contains key "message" and value "User must not register new users."
61-
# And response contains key "status" and value "unauthorized"
1+
Feature: User Registration
2+
As a user (/admin)
3+
I want to be able to register (users) with username and password
4+
5+
Background:
6+
Given database is empty
7+
And user with id 1234 exists and has username "user", password "secure_password"
8+
And accessToken with value "accessToken" exists for user 1234
9+
And user with id 1234 is in group with id 1
10+
11+
Scenario: Successful registration with username, password and password confirmation.
12+
When user requests registration with username "kangaroo", password "pig-system" and password confirmation "pig-system" with accessToken "accessToken"
13+
Then response status code is 201
14+
And response contains key "message" and value "User successfully created."
15+
And response contains key "status" and value "created"
16+
17+
Scenario: Successful registration with username, password and password confirmation; password matches password of other users.
18+
When user requests registration with username "kangaroo", password "secure_password" and password confirmation "secure_password" with accessToken "accessToken"
19+
Then response status code is 201
20+
And response contains key "message" and value "User successfully created."
21+
And response contains key "status" and value "created"
22+
23+
Scenario: Failed registration with used username, arbitrary password and password confirmation.
24+
When user requests registration with username "user", password "pig-system" and password confirmation "pig-system" with accessToken "accessToken"
25+
Then response status code is 409
26+
And response contains key "message" and value "User already exists."
27+
And response contains key "status" and value "conflict"
28+
29+
Scenario: Failed registration with used username (other case), arbitrary password and password confirmation.
30+
When user requests registration with username "User", password "pig-system" and password confirmation "pig-system" with accessToken "accessToken"
31+
Then response status code is 409
32+
And response contains key "message" and value "User already exists."
33+
And response contains key "status" and value "conflict"
34+
35+
Scenario: Failed registration with username, password and deviating password confirmation.
36+
When user requests registration with username "kangaroo", password "pig-system" and password confirmation "i-love-capitalism" with accessToken "accessToken"
37+
Then response status code is 409
38+
And response contains key "message" and value "Passwords do not match."
39+
And response contains key "status" and value "conflict"
40+
41+
Scenario: Failed registration with username, password and password confirmation; username is part of password.
42+
When user requests registration with username "kangaroo", password "kangaroo-system" and password confirmation "kangaroo-system" with accessToken "accessToken"
43+
Then response status code is 409
44+
And response contains key "message" and value "Username must not appear in password."
45+
And response contains key "status" and value "conflict"
46+
47+
Scenario: Failed registration with username, password and password confirmation; password appears in list of top 10k passwords
48+
When user requests registration with username "kangaroo", password "vietnam" and password confirmation "vietnam" with accessToken "accessToken"
49+
Then response status code is 409
50+
And response contains key "message" and value "Password must not appear in the top 10000 most common passwords."
51+
And response contains key "status" and value "conflict"
52+
#https://github.com/iryndin/10K-Most-Popular-Passwords/blob/master/passwords.txt
53+
54+
Scenario: Failed registration with username, password and password confirmation; not in group ADMIN
55+
Given user 1236 exists
56+
And user with id 1236 is in group with id -1
57+
And accessToken with value "wrongAccessToken" exists for user 1236
58+
When user requests registration with username "kangaroo", password "pig-system" and password confirmation "pig-system" with accessToken "wrongAccessToken"
59+
Then response status code is 401
60+
And response contains key "message" and value "User must not register new users."
61+
And response contains key "status" and value "unauthorized"

0 commit comments

Comments
 (0)