1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.chain.commands.servlet;
22
23 import junit.framework.TestCase;
24
25 import org.apache.commons.chain.web.servlet.ServletWebContext;
26 import org.apache.struts.chain.commands.UnauthorizedActionException;
27 import org.apache.struts.chain.contexts.ServletActionContext;
28 import org.apache.struts.config.ActionConfig;
29 import org.apache.struts.mock.MockActionServlet;
30 import org.apache.struts.mock.MockHttpServletRequest;
31 import org.apache.struts.mock.MockHttpServletResponse;
32 import org.apache.struts.mock.MockPrincipal;
33 import org.apache.struts.mock.MockServletConfig;
34 import org.apache.struts.mock.MockServletContext;
35
36
37 public class TestAuthorizeAction extends TestCase {
38 MockHttpServletRequest request = null;
39 MockPrincipal principal = null;
40 ServletWebContext swContext = null;
41 ServletActionContext saContext = null;
42 AuthorizeAction command = null;
43
44 public TestAuthorizeAction(String _name) {
45 super(_name);
46 }
47
48
49 protected void setUp() throws Exception {
50 this.request = new MockHttpServletRequest();
51 this.principal =
52 new MockPrincipal("Mr. Macri", new String[] { "administrator" });
53 this.request.setUserPrincipal(principal);
54
55 MockServletConfig servletConfig = new MockServletConfig();
56 MockServletContext servletContext = new MockServletContext();
57 MockActionServlet servlet =
58 new MockActionServlet(servletContext, servletConfig);
59
60 servlet.initInternal();
61
62 this.saContext =
63 new ServletActionContext(servletContext, request,
64 new MockHttpServletResponse());
65
66 this.saContext.setActionServlet(servlet);
67 this.command = new AuthorizeAction();
68 }
69
70
71 protected void tearDown() {
72 }
73
74 public void testAuthorizeOneRole()
75 throws Exception {
76 ActionConfig config = new ActionConfig();
77
78 config.setPath("/testAuthorizeOneRole");
79 config.setRoles("administrator");
80 this.saContext.setActionConfig(config);
81
82 boolean result = command.execute(saContext);
83
84 assertFalse(result);
85 }
86
87 public void testAuthorizeOneOfManyRoles()
88 throws Exception {
89 ActionConfig config = new ActionConfig();
90
91 config.setPath("/testAuthorizeOneOfManyRoles");
92 config.setRoles("administrator,roustabout,memory");
93 this.saContext.setActionConfig(config);
94
95 boolean result = command.execute(saContext);
96
97 assertFalse(result);
98 }
99
100 public void testAuthorizeNoRoles()
101 throws Exception {
102 ActionConfig config = new ActionConfig();
103
104 config.setPath("/testAuthorizeNoRoles");
105 this.saContext.setActionConfig(config);
106
107 boolean result = command.execute(saContext);
108
109 assertFalse(result);
110 }
111
112 public void testNotAuthorizedOneRole()
113 throws Exception {
114 ActionConfig config = new ActionConfig();
115
116 config.setPath("/testNotAuthorizedOneRole");
117 config.setRoles("roustabout");
118 this.saContext.setActionConfig(config);
119
120 try {
121 boolean result = command.execute(saContext);
122 } catch (UnauthorizedActionException ex) {
123 }
124 }
125
126 public void testNotAuthorizedOneOfManyRoles()
127 throws Exception {
128 ActionConfig config = new ActionConfig();
129
130 config.setPath("/testNotAuthorizedOneOfManyRoles");
131 config.setRoles("roustabout,memory");
132 this.saContext.setActionConfig(config);
133
134 try {
135 boolean result = command.execute(saContext);
136 } catch (UnauthorizedActionException ex) {
137 }
138 }
139
140
141 public static void main(String[] argv) {
142 String[] testCaseList = { TestAuthorizeAction.class.getName() };
143
144 junit.textui.TestRunner.main(testCaseList);
145 }
146 }