01: package com.ecyrd.jspwiki.auth.authorize;
02:
03: import java.security.Principal;
04: import java.util.Properties;
05:
06: import junit.framework.Test;
07: import junit.framework.TestCase;
08: import junit.framework.TestSuite;
09:
10: import org.apache.commons.lang.ArrayUtils;
11: import org.jdom.Document;
12:
13: import com.ecyrd.jspwiki.TestEngine;
14: import com.ecyrd.jspwiki.WikiEngine;
15:
16: public class WebContainerAuthorizerTest extends TestCase {
17: WikiEngine m_engine;
18: WebContainerAuthorizer m_authorizer;
19: Document m_webxml;
20:
21: public WebContainerAuthorizerTest(String s) {
22: super (s);
23: }
24:
25: public void setUp() throws Exception {
26: Properties props = new Properties();
27: props.load(TestEngine.findTestProperties());
28: m_engine = new TestEngine(props);
29: m_authorizer = new WebContainerAuthorizer();
30: m_authorizer.initialize(m_engine, props);
31: m_webxml = m_authorizer.getWebXml();
32: if (m_webxml == null) {
33: throw new Exception("Could not load web.xml");
34: }
35: }
36:
37: public void testConstraints() throws Exception {
38: assertTrue(m_authorizer.isConstrained("/Delete.jsp", new Role(
39: "Admin")));
40: assertTrue(m_authorizer.isConstrained("/Login.jsp",
41: Role.AUTHENTICATED));
42: assertFalse(m_authorizer.isConstrained("/UserPreferences.jsp",
43: Role.AUTHENTICATED));
44: }
45:
46: public void testGetRoles() {
47: // We should find 2 roles: AUTHENTICATED plus custom role "Admin"
48: Principal[] roles = m_authorizer.getRoles();
49: assertEquals(2, roles.length);
50: assertTrue(ArrayUtils.contains(roles, Role.AUTHENTICATED));
51: assertTrue(ArrayUtils.contains(roles, new Role("Admin")));
52: }
53:
54: public void testRoles() throws Exception {
55: Role[] roles = m_authorizer.getRoles(m_webxml);
56: boolean found = false;
57: for (int i = 0; i < roles.length; i++) {
58: if (roles[i].equals(Role.AUTHENTICATED)) {
59: found = true;
60: }
61: }
62: assertTrue("Didn't find AUTHENTICATED", found);
63: for (int i = 0; i < roles.length; i++) {
64: if (roles[i].equals(new Role("Admin"))) {
65: found = true;
66: }
67: }
68: assertTrue("Didn't find ADMIN", found);
69: }
70:
71: public void testIsContainerAuthorized() {
72: assertTrue(m_authorizer.isContainerAuthorized());
73: }
74:
75: public static Test suite() {
76: return new TestSuite(WebContainerAuthorizerTest.class);
77: }
78:
79: }
|