1 |
| package photospace.web.security; |
2 |
| |
3 |
| import javax.servlet.http.*; |
4 |
| import junit.framework.*; |
5 |
| import net.sf.acegisecurity.providers.*; |
6 |
| import net.sf.acegisecurity.*; |
7 |
| import net.sf.acegisecurity.context.*; |
8 |
| import com.mockobjects.helpers.*; |
9 |
| |
10 |
| public class ContributorFilterTest |
11 |
| extends TestCase |
12 |
| { |
13 |
| FilterTestHelper testFilter; |
14 |
| |
15 |
4
| public void setUp()
|
16 |
| { |
17 |
4
| testFilter = new FilterTestHelper(new ContributorFilter());
|
18 |
4
| testFilter.getRequest().setupPathInfo("/username/");
|
19 |
| } |
20 |
4
| public void tearDown()
|
21 |
| { |
22 |
4
| logout();
|
23 |
| } |
24 |
| |
25 |
1
| public void testAnonymous() throws Exception
|
26 |
| { |
27 |
1
| testFilter.getResponse().setExpectedError(HttpServletResponse.SC_FORBIDDEN);
|
28 |
1
| testFilter.testDoFilter();
|
29 |
1
| testFilter.getResponse().verify();
|
30 |
| } |
31 |
| |
32 |
1
| public void testNoRole() throws Exception
|
33 |
| { |
34 |
1
| login(new TestingAuthenticationToken("u", "p", new GrantedAuthority[] {}));
|
35 |
1
| testFilter.getResponse().setExpectedError(HttpServletResponse.SC_FORBIDDEN);
|
36 |
1
| testFilter.testDoFilter();
|
37 |
1
| testFilter.getResponse().verify();
|
38 |
| } |
39 |
| |
40 |
1
| public void testAdmin() throws Exception
|
41 |
| { |
42 |
1
| login(new TestingAuthenticationToken("u", "p", new GrantedAuthority[] { ContributorFilter.CONTRIBUTOR, ContributorFilter.ADMIN}));
|
43 |
1
| testFilter.getResponse().setExpectedErrorNothing();
|
44 |
1
| testFilter.testDoFilter();
|
45 |
1
| testFilter.getResponse().verify();
|
46 |
| } |
47 |
| |
48 |
1
| public void testContributor() throws Exception
|
49 |
| { |
50 |
1
| login(new TestingAuthenticationToken("u", "p", new GrantedAuthority[] { ContributorFilter.CONTRIBUTOR}));
|
51 |
1
| testFilter.getResponse().setExpectedError(HttpServletResponse.SC_FORBIDDEN);
|
52 |
1
| testFilter.testDoFilter();
|
53 |
1
| testFilter.getResponse().verify();
|
54 |
| |
55 |
1
| login(new TestingAuthenticationToken("username", "p", new GrantedAuthority[] { ContributorFilter.CONTRIBUTOR}));
|
56 |
1
| testFilter.getResponse().setExpectedErrorNothing();
|
57 |
1
| testFilter.testDoFilter();
|
58 |
1
| testFilter.getResponse().verify();
|
59 |
| |
60 |
1
| testFilter.getRequest().setupPathInfo("/admin/edit");
|
61 |
1
| testFilter.getRequest().setupAddParameter("path", "/username/photo.jpg");
|
62 |
1
| testFilter.getRequest().setupAddParameter("paths", "/username/photo2.jpg");
|
63 |
1
| testFilter.getResponse().setExpectedErrorNothing();
|
64 |
1
| testFilter.testDoFilter();
|
65 |
1
| testFilter.getResponse().verify();
|
66 |
| |
67 |
1
| testFilter = new FilterTestHelper(new ContributorFilter());
|
68 |
1
| testFilter.getRequest().setupPathInfo("/admin/edit");
|
69 |
1
| testFilter.getRequest().setupAddParameter("path", "/username/photo.jpg");
|
70 |
1
| testFilter.getRequest().setupAddParameter("paths", new String[] { "/username/photo2.jpg", "/foo/photo2.jpg" });
|
71 |
1
| testFilter.getResponse().setExpectedError(HttpServletResponse.SC_FORBIDDEN);
|
72 |
1
| testFilter.testDoFilter();
|
73 |
1
| testFilter.getResponse().verify();
|
74 |
| } |
75 |
| |
76 |
4
| private void login(TestingAuthenticationToken auth)
|
77 |
| { |
78 |
4
| SecureContext context = new SecureContextImpl();
|
79 |
4
| context.setAuthentication(new TestingAuthenticationProvider().authenticate(auth));
|
80 |
4
| ContextHolder.setContext(context);
|
81 |
| } |
82 |
| |
83 |
4
| private void logout()
|
84 |
| { |
85 |
4
| ContextHolder.setContext(null);
|
86 |
| } |
87 |
| } |