Skip to content

Commit c366a6e

Browse files
committed
Naming, formatting and moved one test method
1 parent c617bc2 commit c366a6e

File tree

3 files changed

+64
-56
lines changed

3 files changed

+64
-56
lines changed

jaspic/basic-authentication/src/test/java/org/javaee7/jaspic/basicauthentication/BasicAuthenticationStatelessTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public void testPublicAccessIsStateless() throws IOException, SAXException {
168168
* authenticated identity are remembered
169169
*/
170170
@Test
171-
public void testUserIdentityIsStateless() throws IOException, SAXException {
171+
public void testProtectedThenPublicAccessIsStateless() throws IOException, SAXException {
172172

173173
// -------------------- Request 1 ---------------------------
174174

jaspic/custom-principal/src/test/java/org/javaee7/jaspictest/customprincipal/CustomPrincipalPublicTest.java

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -39,53 +39,13 @@ public void testPublicPageLoggedin() throws IOException, SAXException {
3939
"Username is not the expected one 'test'",
4040
response.contains("web username: test")
4141
);
42-
assertTrue(
43-
"Username is correct, but the expected role 'architect' is not present.",
44-
response.contains("web user has role \"architect\": true"));
45-
46-
assertTrue(
47-
"Username and roles are correct, but principal type is not the expected custom type.",
48-
response.contains("isCustomPrincipal: true")
49-
);
50-
}
51-
52-
@Test
53-
public void testPublicPageNotRememberLogin() throws IOException, SAXException {
54-
55-
// -------------------- Request 1 ---------------------------
56-
57-
String response = getFromServerPath("public/servlet");
58-
59-
// Not logged-in
60-
assertTrue(response.contains("web username: null"));
61-
assertTrue(response.contains("web user has role \"architect\": false"));
62-
63-
// -------------------- Request 2 ---------------------------
64-
65-
response = getFromServerPath("public/servlet?doLogin");
66-
67-
// Now has to be logged-in
68-
assertTrue(
69-
"Username is not the expected one 'test'",
70-
response.contains("web username: test")
71-
);
7242
assertTrue(
7343
"Username is correct, but the expected role 'architect' is not present.",
7444
response.contains("web user has role \"architect\": true")
7545
);
76-
77-
// -------------------- Request 3 ---------------------------
78-
79-
response = getFromServerPath("public/servlet");
80-
81-
// Not logged-in
82-
assertTrue(
83-
"Should not be authenticated, but username was not null. Did the container remember it from previous request?",
84-
response.contains("web username: null")
85-
);
8646
assertTrue(
87-
"Request was not authenticated (username correctly null), but unauthenticated user incorrectly has role 'architect'",
88-
response.contains("web user has role \"architect\": false")
47+
"Username and roles are correct, but principal type is not the expected custom type.",
48+
response.contains("isCustomPrincipal: true")
8949
);
9050
}
9151

jaspic/custom-principal/src/test/java/org/javaee7/jaspictest/customprincipal/CustomPrincipalStatelessTest.java

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.xml.sax.SAXException;
1515

1616
/**
17-
* Idential test as in basic-authentication, but not performed against a SAM which sets a custom principal.
17+
* Idential test as in basic-authentication, but now performed against a SAM which sets a custom principal.
1818
* Therefore tests that for this kind of usage of the PrincipalCallback JASPIC is stateless just as well.
1919
*
2020
* @author Arjan Tijms
@@ -96,17 +96,59 @@ public void testProtectedAccessIsStateless2() throws IOException, SAXException {
9696
response = getFromServerPath("protected/servlet");
9797

9898
// Not logged-in thus should not be accessible.
99-
assertFalse("Could access protected page, but should not be able to. "
100-
+ "Did the container remember the authenticated identity that was set in previous request?",
101-
response.contains("This is a protected servlet"));
99+
assertFalse(
100+
"Could access protected page, but should not be able to. " +
101+
"Did the container remember the authenticated identity that was set in previous request?",
102+
response.contains("This is a protected servlet")
103+
);
104+
}
105+
106+
@Test
107+
public void testPublicAccessIsStateless() throws IOException, SAXException {
108+
109+
// -------------------- Request 1 ---------------------------
110+
111+
String response = getFromServerPath("public/servlet");
112+
113+
// Not logged-in
114+
assertTrue(response.contains("web username: null"));
115+
assertTrue(response.contains("web user has role \"architect\": false"));
116+
117+
// -------------------- Request 2 ---------------------------
118+
119+
response = getFromServerPath("public/servlet?doLogin");
120+
121+
// Now has to be logged-in
122+
assertTrue(
123+
"Username is not the expected one 'test'",
124+
response.contains("web username: test")
125+
);
126+
assertTrue(
127+
"Username is correct, but the expected role 'architect' is not present.",
128+
response.contains("web user has role \"architect\": true")
129+
);
130+
131+
// -------------------- Request 3 ---------------------------
132+
133+
response = getFromServerPath("public/servlet");
134+
135+
// Not logged-in
136+
assertTrue(
137+
"Should not be authenticated, but username was not null. Did the container remember it from previous request?",
138+
response.contains("web username: null")
139+
);
140+
assertTrue(
141+
"Request was not authenticated (username correctly null), but unauthenticated user incorrectly has role 'architect'",
142+
response.contains("web user has role \"architect\": false")
143+
);
102144
}
103145

104146
/**
105147
* Tests independently from being able to access a protected resource if any details of a previously established
106148
* authenticated identity are remembered
107149
*/
108150
@Test
109-
public void testUserIdentityIsStateless() throws IOException, SAXException {
151+
public void testProtectedThenPublicAccessIsStateless() throws IOException, SAXException {
110152

111153
// -------------------- Request 1 ---------------------------
112154

@@ -119,14 +161,20 @@ public void testUserIdentityIsStateless() throws IOException, SAXException {
119161
response = getFromServerPath("public/servlet");
120162

121163
// No details should linger around
122-
assertFalse("User principal was 'test', but it should be null here. "
123-
+ "The container seemed to have remembered it from the previous request.",
124-
response.contains("web username: test"));
125-
assertTrue("User principal was not null, but it should be null here. ",
126-
response.contains("web username: null"));
127-
assertTrue("The unauthenticated user has the role 'architect', which should not be the case. "
128-
+ "The container seemed to have remembered it from the previous request.",
129-
response.contains("web user has role \"architect\": false"));
164+
assertFalse(
165+
"User principal was 'test', but it should be null here. " +
166+
"The container seemed to have remembered it from the previous request.",
167+
response.contains("web username: test")
168+
);
169+
assertTrue(
170+
"User principal was not null, but it should be null here. ",
171+
response.contains("web username: null")
172+
);
173+
assertTrue(
174+
"The unauthenticated user has the role 'architect', which should not be the case. " +
175+
"The container seemed to have remembered it from the previous request.",
176+
response.contains("web user has role \"architect\": false")
177+
);
130178
}
131179

132180
}

0 commit comments

Comments
 (0)