1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.aspectj;
20
21 import org.apache.log4j.ConsoleAppender;
22 import org.apache.log4j.Level;
23 import org.apache.log4j.Logger;
24 import org.apache.log4j.SimpleLayout;
25 import org.apache.shiro.SecurityUtils;
26 import org.apache.shiro.authc.UsernamePasswordToken;
27 import org.apache.shiro.authz.UnauthenticatedException;
28 import org.apache.shiro.authz.UnauthorizedException;
29 import org.apache.shiro.config.IniSecurityManagerFactory;
30 import org.apache.shiro.mgt.SecurityManager;
31 import org.apache.shiro.subject.Subject;
32 import org.apache.shiro.util.Factory;
33 import org.junit.*;
34
35
36
37 public class DummyServiceTest {
38
39 private static DummyService SECURED_SERVICE;
40 private static DummyService RESTRICTED_SERVICE;
41
42 @BeforeClass
43 public static void setUpClass() throws Exception {
44 Logger log = Logger.getLogger(AspectjAnnotationsAuthorizingMethodInterceptor.class);
45 log.addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT));
46 log.setLevel(Level.TRACE);
47
48 Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiroDummyServiceTest.ini");
49 SecurityManager securityManager = factory.getInstance();
50 SecurityUtils.setSecurityManager(securityManager);
51
52 SECURED_SERVICE = new SecuredDummyService();
53 RESTRICTED_SERVICE = new RestrictedDummyService();
54 }
55
56 @AfterClass
57 public static void tearDownClass() throws Exception {
58
59 SecurityUtils.setSecurityManager(null);
60 }
61
62 private Subject subject;
63
64 @Before
65 public void setUp() throws Exception {
66 subject = SecurityUtils.getSubject();
67 }
68
69 @After
70 public void tearDown() throws Exception {
71 subject.logout();
72 }
73
74 private void loginAsUser() {
75 subject.login(new UsernamePasswordToken("joe", "bob"));
76 }
77
78 private void loginAsAdmin() {
79 subject.login(new UsernamePasswordToken("root", "secret"));
80 }
81
82
83 @Test
84 public void testAnonymous_asAnonymous() throws Exception {
85 SECURED_SERVICE.anonymous();
86 }
87
88 @Test
89 public void testAnonymous_asUser() throws Exception {
90 loginAsUser();
91 SECURED_SERVICE.anonymous();
92 }
93
94 @Test
95 public void testAnonymous_asAdmin() throws Exception {
96 loginAsAdmin();
97 SECURED_SERVICE.anonymous();
98 }
99
100
101 @Test
102 public void testGuest_asAnonymous() throws Exception {
103 SECURED_SERVICE.guest();
104 }
105
106 @Test(expected = UnauthenticatedException.class)
107 public void testGuest_asUser() throws Exception {
108 loginAsUser();
109 SECURED_SERVICE.guest();
110 }
111
112 @Test(expected = UnauthenticatedException.class)
113 public void testGuest_asAdmin() throws Exception {
114 loginAsAdmin();
115 SECURED_SERVICE.guest();
116 }
117
118
119 @Test(expected = UnauthenticatedException.class)
120 public void testPeek_asAnonymous() throws Exception {
121 SECURED_SERVICE.peek();
122 }
123
124 @Test
125 public void testPeek_asUser() throws Exception {
126 loginAsUser();
127 SECURED_SERVICE.peek();
128 }
129
130 @Test
131 public void testPeek_asAdmin() throws Exception {
132 loginAsAdmin();
133 SECURED_SERVICE.peek();
134 }
135
136
137 @Test(expected = UnauthenticatedException.class)
138
139 public void testRetrieve_asAnonymous() throws Exception {
140 SECURED_SERVICE.retrieve();
141 }
142
143 @Test
144 public void testRetrieve_asUser() throws Exception {
145 loginAsUser();
146 SECURED_SERVICE.retrieve();
147 }
148
149 @Test
150 public void testRetrieve_asAdmin() throws Exception {
151 loginAsAdmin();
152 SECURED_SERVICE.retrieve();
153 }
154
155
156 @Test(expected = UnauthenticatedException.class)
157
158 public void testChange_asAnonymous() throws Exception {
159 SECURED_SERVICE.change();
160 }
161
162 @Test(expected = UnauthorizedException.class)
163 public void testChange_asUser() throws Exception {
164 loginAsUser();
165 SECURED_SERVICE.change();
166 }
167
168 @Test
169 public void testChange_asAdmin() throws Exception {
170 loginAsAdmin();
171 SECURED_SERVICE.change();
172 }
173
174
175 @Test(expected = UnauthenticatedException.class)
176
177 public void testRetrieveRestricted_asAnonymous() throws Exception {
178 RESTRICTED_SERVICE.retrieve();
179 }
180
181 @Test(expected = UnauthorizedException.class)
182 public void testRetrieveRestricted_asUser() throws Exception {
183 loginAsUser();
184 RESTRICTED_SERVICE.retrieve();
185 }
186
187 @Test
188 public void testRetrieveRestricted_asAdmin() throws Exception {
189 loginAsAdmin();
190 RESTRICTED_SERVICE.retrieve();
191 }
192
193 }