001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.auth.common;
019
020import static org.junit.Assert.assertEquals;
021
022import java.util.HashSet;
023import java.util.Set;
024
025import org.apache.http.auth.BasicUserPrincipal;
026import org.apache.shiro.authc.AuthenticationInfo;
027import org.fcrepo.auth.common.ContainerRolesPrincipalProvider.ContainerRolesPrincipal;
028import org.junit.Test;
029
030/**
031 * @author peichman
032 */
033public class ServletContainerAuthenticatingRealmTest {
034
035    @Test
036    public void testDoGetAuthenticationInfo() {
037        final String username = "foo";
038        final String roleName = "fedoraUser";
039
040        final ServletContainerAuthenticatingRealm realm = new ServletContainerAuthenticatingRealm();
041        final Set<String> roles = new HashSet<>();
042        roles.add(roleName);
043        final ContainerAuthToken token = new ContainerAuthToken(username, roles);
044        final AuthenticationInfo info = realm.doGetAuthenticationInfo(token);
045        // should have 2 principals (user and one role)
046        assertEquals(2, info.getPrincipals().asSet().size());
047        assertEquals(1, info.getPrincipals().byType(BasicUserPrincipal.class).size());
048        assertEquals(1, info.getPrincipals().byType(ContainerRolesPrincipal.class).size());
049
050        assertEquals(username, info.getPrincipals().byType(BasicUserPrincipal.class).toArray(
051                new BasicUserPrincipal[1])[0].getName());
052        assertEquals(roleName, info.getPrincipals().byType(ContainerRolesPrincipal.class).toArray(
053                new ContainerRolesPrincipal[1])[0].getName());
054    }
055
056    @Test
057    public void testDoGetAuthenticationInfoWithNoRoles() {
058        final String username = "foo";
059
060        final ServletContainerAuthenticatingRealm realm = new ServletContainerAuthenticatingRealm();
061        final ContainerAuthToken token = new ContainerAuthToken(username, new HashSet<>());
062        // make sure this doesn't blow up on an empty set of roles
063        final AuthenticationInfo info = realm.doGetAuthenticationInfo(token);
064        // should have 1 principal (user)
065        assertEquals(1, info.getPrincipals().asSet().size());
066        assertEquals(1, info.getPrincipals().byType(BasicUserPrincipal.class).size());
067        assertEquals(0, info.getPrincipals().byType(ContainerRolesPrincipal.class).size());
068
069        assertEquals(username, info.getPrincipals().byType(BasicUserPrincipal.class).toArray(
070                new BasicUserPrincipal[1])[0].getName());
071    }
072
073}