You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by ad...@apache.org on 2010/10/16 17:59:54 UTC

svn commit: r1023320 - in /shiro/sandbox/crowd: ./ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/shiro/ src/test/java/org/apache/shiro/realm/ src/test/java/org/apache/shiro/realm/crowd/

Author: adc
Date: Sat Oct 16 15:59:54 2010
New Revision: 1023320

URL: http://svn.apache.org/viewvc?rev=1023320&view=rev
Log:
SHIRO-18 unit tests

Added:
    shiro/sandbox/crowd/src/test/java/org/
    shiro/sandbox/crowd/src/test/java/org/apache/
    shiro/sandbox/crowd/src/test/java/org/apache/shiro/
    shiro/sandbox/crowd/src/test/java/org/apache/shiro/realm/
    shiro/sandbox/crowd/src/test/java/org/apache/shiro/realm/crowd/
    shiro/sandbox/crowd/src/test/java/org/apache/shiro/realm/crowd/CrowdRealmTest.java
Modified:
    shiro/sandbox/crowd/pom.xml

Modified: shiro/sandbox/crowd/pom.xml
URL: http://svn.apache.org/viewvc/shiro/sandbox/crowd/pom.xml?rev=1023320&r1=1023319&r2=1023320&view=diff
==============================================================================
--- shiro/sandbox/crowd/pom.xml (original)
+++ shiro/sandbox/crowd/pom.xml Sat Oct 16 15:59:54 2010
@@ -71,10 +71,38 @@
         </dependency>
 
         <dependency>
+            <groupId>org.easymock</groupId>
+            <artifactId>easymock</artifactId>
+            <version>2.4</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
-            <version>1.5.6</version>
+            <version>1.5.8</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-jdk14</artifactId>
+            <version>1.5.8</version>
+            <scope>test</scope>
         </dependency>
     </dependencies>
 
+    <repositories>
+        <repository>
+            <id>central</id>
+            <url>https://m2proxy.atlassian.com/repository/public</url>
+            <snapshots>
+                <enabled>true</enabled>
+                <updatePolicy>always</updatePolicy>
+            </snapshots>
+            <releases>
+                <enabled>true</enabled>
+            </releases>
+        </repository>
+    </repositories>
+
 </project>

Added: shiro/sandbox/crowd/src/test/java/org/apache/shiro/realm/crowd/CrowdRealmTest.java
URL: http://svn.apache.org/viewvc/shiro/sandbox/crowd/src/test/java/org/apache/shiro/realm/crowd/CrowdRealmTest.java?rev=1023320&view=auto
==============================================================================
--- shiro/sandbox/crowd/src/test/java/org/apache/shiro/realm/crowd/CrowdRealmTest.java (added)
+++ shiro/sandbox/crowd/src/test/java/org/apache/shiro/realm/crowd/CrowdRealmTest.java Sat Oct 16 15:59:54 2010
@@ -0,0 +1,165 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.shiro.realm.crowd;
+
+import java.util.Arrays;
+import java.util.EnumSet;
+
+import com.atlassian.crowd.integration.service.soap.client.SecurityServerClient;
+import org.junit.Test;
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.*;
+
+import org.apache.shiro.authc.AuthenticationInfo;
+import org.apache.shiro.authc.UsernamePasswordToken;
+import org.apache.shiro.authz.AuthorizationInfo;
+import org.apache.shiro.subject.PrincipalCollection;
+
+
+/**
+ * @version $Revision: $ $Date: $
+ */
+public class CrowdRealmTest {
+
+    @Test
+    public void testAuthentication() throws Exception {
+
+        CrowdRealm realm = new CrowdRealm();
+        realm.setName("NutHouse");
+
+        SecurityServerClient client = createStrictMock(SecurityServerClient.class);
+        expect(client.authenticatePrincipalSimple("yoko", "barbie")).andReturn("UNUSED");
+        replay(client);
+
+        realm.setCrowdClient(client);
+
+        AuthenticationInfo authenticationInfo = realm.doGetAuthenticationInfo(new UsernamePasswordToken("yoko", "barbie"));
+
+        verify(client);
+        assertNotNull(authenticationInfo);
+        assertTrue(Arrays.equals("barbie".toCharArray(), (char[]) authenticationInfo.getCredentials()));
+
+        PrincipalCollection collection = authenticationInfo.getPrincipals();
+        assertNotNull(collection);
+        assertTrue(!collection.isEmpty());
+        assertEquals("yoko", collection.getPrimaryPrincipal());
+        assertTrue(!collection.getRealmNames().isEmpty());
+        assertTrue(collection.getRealmNames().contains("NutHouse"));
+        assertTrue(!collection.fromRealm("NutHouse").isEmpty());
+        assertTrue(collection.fromRealm("NutHouse").contains("yoko"));
+    }
+
+    @Test
+    public void testDefaultRoles() throws Exception {
+
+        CrowdRealm realm = new CrowdRealm();
+        realm.setName("NutHouse");
+
+        SecurityServerClient client = createStrictMock(SecurityServerClient.class);
+        expect(client.authenticatePrincipalSimple("yoko", "barbie")).andReturn("UNUSED");
+        expect(client.findRoleMemberships("yoko")).andReturn(new String[]{"big_sister", "table_setter", "dog_walker"});
+        replay(client);
+
+        realm.setCrowdClient(client);
+
+        AuthenticationInfo authenticationInfo = realm.doGetAuthenticationInfo(new UsernamePasswordToken("yoko", "barbie"));
+        AuthorizationInfo authorizationInfo = realm.doGetAuthorizationInfo(authenticationInfo.getPrincipals());
+
+        verify(client);
+        assertTrue(!authorizationInfo.getRoles().isEmpty());
+        assertTrue(authorizationInfo.getRoles().contains("big_sister"));
+        assertTrue(authorizationInfo.getRoles().contains("table_setter"));
+        assertTrue(authorizationInfo.getRoles().contains("dog_walker"));
+    }
+
+    @Test
+    public void testRoleMemberships() throws Exception {
+
+        CrowdRealm realm = new CrowdRealm();
+        realm.setName("NutHouse");
+        realm.setRoleSources(EnumSet.of(RoleSource.ROLES_FROM_CROWD_ROLES));
+
+        SecurityServerClient client = createStrictMock(SecurityServerClient.class);
+        expect(client.authenticatePrincipalSimple("yoko", "barbie")).andReturn("UNUSED");
+        expect(client.findRoleMemberships("yoko")).andReturn(new String[]{"big_sister", "table_setter", "dog_walker"});
+        replay(client);
+
+        realm.setCrowdClient(client);
+
+        AuthenticationInfo authenticationInfo = realm.doGetAuthenticationInfo(new UsernamePasswordToken("yoko", "barbie"));
+        AuthorizationInfo authorizationInfo = realm.doGetAuthorizationInfo(authenticationInfo.getPrincipals());
+
+        verify(client);
+        assertTrue(!authorizationInfo.getRoles().isEmpty());
+        assertTrue(authorizationInfo.getRoles().contains("big_sister"));
+        assertTrue(authorizationInfo.getRoles().contains("table_setter"));
+        assertTrue(authorizationInfo.getRoles().contains("dog_walker"));
+    }
+
+
+    @Test
+    public void testGroupMemberships() throws Exception {
+
+        CrowdRealm realm = new CrowdRealm();
+        realm.setName("NutHouse");
+        realm.setRoleSources(EnumSet.of(RoleSource.ROLES_FROM_CROWD_GROUPS));
+
+        SecurityServerClient client = createStrictMock(SecurityServerClient.class);
+        expect(client.authenticatePrincipalSimple("yoko", "barbie")).andReturn("UNUSED");
+        expect(client.findGroupMemberships("yoko")).andReturn(new String[]{"girls", "naughty"});
+        replay(client);
+
+        realm.setCrowdClient(client);
+
+        AuthenticationInfo authenticationInfo = realm.doGetAuthenticationInfo(new UsernamePasswordToken("yoko", "barbie"));
+        AuthorizationInfo authorizationInfo = realm.doGetAuthorizationInfo(authenticationInfo.getPrincipals());
+
+        verify(client);
+        assertTrue(!authorizationInfo.getRoles().isEmpty());
+        assertTrue(authorizationInfo.getRoles().contains("girls"));
+        assertTrue(authorizationInfo.getRoles().contains("naughty"));
+    }
+
+    @Test
+    public void testAll() throws Exception {
+
+        CrowdRealm realm = new CrowdRealm();
+        realm.setName("NutHouse");
+        realm.setRoleSources(EnumSet.of(RoleSource.ROLES_FROM_CROWD_GROUPS, RoleSource.ROLES_FROM_CROWD_ROLES));
+
+        SecurityServerClient client = createStrictMock(SecurityServerClient.class);
+        expect(client.authenticatePrincipalSimple("yoko", "barbie")).andReturn("UNUSED");
+        expect(client.findRoleMemberships("yoko")).andReturn(new String[]{"big_sister", "table_setter", "dog_walker"});
+        expect(client.findGroupMemberships("yoko")).andReturn(new String[]{"girls", "naughty"});
+        replay(client);
+
+        realm.setCrowdClient(client);
+
+        AuthenticationInfo authenticationInfo = realm.doGetAuthenticationInfo(new UsernamePasswordToken("yoko", "barbie"));
+        AuthorizationInfo authorizationInfo = realm.doGetAuthorizationInfo(authenticationInfo.getPrincipals());
+
+        verify(client);
+        assertTrue(!authorizationInfo.getRoles().isEmpty());
+        assertTrue(authorizationInfo.getRoles().contains("big_sister"));
+        assertTrue(authorizationInfo.getRoles().contains("table_setter"));
+        assertTrue(authorizationInfo.getRoles().contains("dog_walker"));
+        assertTrue(authorizationInfo.getRoles().contains("girls"));
+        assertTrue(authorizationInfo.getRoles().contains("naughty"));
+    }
+}