001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.auth.common;
007
008import static org.junit.Assert.assertEquals;
009
010import java.util.HashSet;
011import java.util.Set;
012
013import org.apache.http.auth.BasicUserPrincipal;
014import org.apache.shiro.authc.AuthenticationInfo;
015import org.fcrepo.auth.common.ContainerRolesPrincipalProvider.ContainerRolesPrincipal;
016import org.junit.Test;
017
018/**
019 * @author peichman
020 */
021public class ServletContainerAuthenticatingRealmTest {
022
023    @Test
024    public void testDoGetAuthenticationInfo() {
025        final String username = "foo";
026        final String roleName = "fedoraUser";
027
028        final ServletContainerAuthenticatingRealm realm = new ServletContainerAuthenticatingRealm();
029        final Set<String> roles = new HashSet<>();
030        roles.add(roleName);
031        final ContainerAuthToken token = new ContainerAuthToken(username, roles);
032        final AuthenticationInfo info = realm.doGetAuthenticationInfo(token);
033        // should have 2 principals (user and one role)
034        assertEquals(2, info.getPrincipals().asSet().size());
035        assertEquals(1, info.getPrincipals().byType(BasicUserPrincipal.class).size());
036        assertEquals(1, info.getPrincipals().byType(ContainerRolesPrincipal.class).size());
037
038        assertEquals(username, info.getPrincipals().byType(BasicUserPrincipal.class).toArray(
039                new BasicUserPrincipal[1])[0].getName());
040        assertEquals(roleName, info.getPrincipals().byType(ContainerRolesPrincipal.class).toArray(
041                new ContainerRolesPrincipal[1])[0].getName());
042    }
043
044    @Test
045    public void testDoGetAuthenticationInfoWithNoRoles() {
046        final String username = "foo";
047
048        final ServletContainerAuthenticatingRealm realm = new ServletContainerAuthenticatingRealm();
049        final ContainerAuthToken token = new ContainerAuthToken(username, new HashSet<>());
050        // make sure this doesn't blow up on an empty set of roles
051        final AuthenticationInfo info = realm.doGetAuthenticationInfo(token);
052        // should have 1 principal (user)
053        assertEquals(1, info.getPrincipals().asSet().size());
054        assertEquals(1, info.getPrincipals().byType(BasicUserPrincipal.class).size());
055        assertEquals(0, info.getPrincipals().byType(ContainerRolesPrincipal.class).size());
056
057        assertEquals(username, info.getPrincipals().byType(BasicUserPrincipal.class).toArray(
058                new BasicUserPrincipal[1])[0].getName());
059    }
060
061}