Skip to content

Commit cca87f5

Browse files
committed
Added JASPIC test for isMandatory
Tests for calls to public and protected resources, not yet for request.authenticate
1 parent adb11ac commit cca87f5

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

jaspic/lifecycle/src/main/java/org/javaee7/jaspic/lifecycle/sam/TestLifecycleAuthModule.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject
4545

4646
try {
4747
response.getWriter().write("validateRequest invoked\n");
48+
49+
boolean isMandatory = Boolean.valueOf((String) messageInfo.getMap().get("javax.security.auth.message.MessagePolicy.isMandatory"));
50+
51+
response.getWriter().write("isMandatory: " + isMandatory + "\n");
4852

4953
handler.handle(new Callback[] {
5054
new CallerPrincipalCallback(clientSubject, "test"),
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.javaee7.jaspic.lifecycle.servlet;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.annotation.WebServlet;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
/**
12+
*
13+
* @author Arjan Tijms
14+
*
15+
*/
16+
@WebServlet(urlPatterns = "/public/servlet")
17+
public class PublicServlet extends HttpServlet {
18+
19+
private static final long serialVersionUID = 1L;
20+
21+
@Override
22+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
23+
response.getWriter().write("Public resource invoked\n");
24+
25+
if (request.getParameter("doLogout") != null) {
26+
request.logout();
27+
}
28+
}
29+
30+
}

jaspic/lifecycle/src/test/java/org/javaee7/jaspic/lifecycle/AuthModuleMethodInvocationTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@ public void testBasicSAMMethodsCalled() throws IOException, SAXException {
5656
assertTrue("SAM method secureResponse not called, but should have been.",
5757
response.contains("secureResponse invoked"));
5858

59+
int validateRequestIndex = response.indexOf("validateRequest invoked");
60+
int resourceIndex = response.indexOf("Resource invoked");
61+
int secureResponseIndex = response.indexOf("secureResponse invoked");
62+
5963
// Finally the order should be correct. More than a few implementations call secureResponse before the resource is
6064
// invoked.
6165
assertTrue("SAM methods called in wrong order",
62-
response.contains("validateRequest invoked\nResource invoked\nsecureResponse invoked\n"));
66+
validateRequestIndex < resourceIndex && resourceIndex < secureResponseIndex);
6367
}
6468

6569
/**
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.javaee7.jaspic.lifecycle;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.io.IOException;
6+
7+
import org.javaee7.jaspic.common.ArquillianBase;
8+
import org.jboss.arquillian.container.test.api.Deployment;
9+
import org.jboss.arquillian.junit.Arquillian;
10+
import org.jboss.shrinkwrap.api.Archive;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.xml.sax.SAXException;
14+
15+
/**
16+
* This tests that the "javax.security.auth.message.MessagePolicy.isMandatory" key
17+
* in the message info map is "true" for a protected resource, and not "true" for
18+
* a public resource.
19+
*
20+
* @author Arjan Tijms
21+
*
22+
*/
23+
@RunWith(Arquillian.class)
24+
public class IsMandatoryTest extends ArquillianBase {
25+
26+
@Deployment(testable = false)
27+
public static Archive<?> createDeployment() {
28+
return defaultArchive();
29+
}
30+
31+
@Test
32+
public void testPublicIsNonMandatory() throws IOException, SAXException {
33+
34+
String response = getFromServerPath("public/servlet");
35+
36+
assertTrue("Resource (Servlet) not invoked, but should have been.", response.contains("Public resource invoked"));
37+
38+
assertTrue("isMandatory should be false for public resource, but was not.",
39+
response.contains("isMandatory: false"));
40+
}
41+
42+
@Test
43+
public void testProtectedIsMandatory() throws IOException, SAXException {
44+
45+
String response = getFromServerPath("protected/servlet");
46+
47+
assertTrue("Resource (Servlet) not invoked, but should have been.", response.contains("Resource invoked"));
48+
49+
assertTrue("isMandatory should be true for protected resource, but was not.",
50+
response.contains("isMandatory: true"));
51+
52+
}
53+
54+
55+
}

0 commit comments

Comments
 (0)