/
HttpLoginTest.java
137 lines (105 loc) · 3.93 KB
/
HttpLoginTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package org.rapidoid.http;
/*
* #%L
* rapidoid-integration-tests
* %%
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import org.junit.Test;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.commons.Err;
import org.rapidoid.commons.Rnd;
import org.rapidoid.ctx.Current;
import org.rapidoid.security.Roles;
import org.rapidoid.setup.On;
import org.rapidoid.u.U;
import java.util.List;
@Authors("Nikolche Mihajlovski")
@Since("5.1.0")
public class HttpLoginTest extends HttpTestCommons {
@Test
public void testLogin() {
On.get("/user").json(() -> U.list(Current.username(), Current.roles()));
On.get("/profile").roles(Roles.LOGGED_IN).json(Current::username);
On.post("/mylogin").json((Resp resp, String user, String pass) -> {
boolean success = resp.login(user, pass);
return U.list(success, Current.username(), Current.roles());
});
On.post("/mylogout").json((Resp resp, String user, String pass) -> {
resp.logout();
return U.list(Current.username(), Current.roles());
});
multiThreaded(200, 1000, this::randomUserLogin);
}
private void randomUserLogin() {
switch (Rnd.rnd(4)) {
case 0:
loginFlow("foo", "bar", U.list());
break;
case 1:
loginFlow("abc", "abc", U.list("guest"));
break;
case 2:
loginFlow("chuck", "chuck", U.list("moderator", "restarter"));
break;
case 3:
loginFlow("niko", "easy", U.list("owner", "administrator", "moderator"));
break;
default:
throw Err.notExpected();
}
}
private void loginFlow(String user, String pass, List<String> expectedRoles) {
HttpClient client = HTTP.keepCookies(true).reuseConnections(true).dontClose();
List<Object> notLoggedIn = U.list(false, null, U.list());
List<Object> loggedIn = U.list(true, user, expectedRoles);
eq(client.get(localhost("/user")).parse(), U.list(null, U.list()));
verifyAccessDenied(client);
eq(client.post(localhost("/mylogin?user=a1&pass=b")).parse(), notLoggedIn);
eq(client.post(localhost("/mylogin?user=a2&pass=b")).parse(), notLoggedIn);
verifyAccessDenied(client);
eq(client.post(localhost(U.frmt("/mylogin?user=%s&pass=%s", user, pass))).parse(), loggedIn);
verifyAnonymousAccessDenied();
verifyAccessGranted(user, client);
eq(client.get(localhost("/user")).parse(), U.list(user, expectedRoles));
verifyAnonymousAccessDenied();
verifyAccessGranted(user, client);
eq(client.post(localhost("/mylogin?user=a3&pass=b")).parse(), U.list(false, user, expectedRoles));
verifyAnonymousAccessDenied();
verifyAccessGranted(user, client);
eq(client.get(localhost("/user")).parse(), U.list(user, expectedRoles));
eq(client.post(localhost("/mylogout")).parse(), U.list(null, U.list()));
verifyAnonymousAccessDenied();
verifyLoggedOut(client);
eq(client.get(localhost("/user")).parse(), U.list(null, U.list()));
eq(client.get(localhost("/user")).parse(), U.list(null, U.list()));
verifyLoggedOut(client);
client.close();
}
private void verifyAnonymousAccessDenied() {
onlyGet("/profile");
}
private void verifyAccessGranted(String user, HttpClient client) {
verify("granted-" + user, fetch(client, "get", "/profile"));
}
private void verifyAccessDenied(HttpClient client) {
verify("denied", fetch(client, "get", "/profile"));
}
private void verifyLoggedOut(HttpClient client) {
verify("logout", fetch(client, "get", "/profile"));
}
}