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 com.google.common.collect.Sets.newHashSet;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertNotEquals;
023import static org.junit.Assert.assertTrue;
024import static org.mockito.Mockito.mock;
025import static org.mockito.Mockito.when;
026
027import java.security.Principal;
028import java.util.Set;
029
030import javax.servlet.http.HttpServletRequest;
031
032import org.fcrepo.auth.common.ContainerRolesPrincipalProvider.ContainerRolesPrincipal;
033
034import org.junit.Before;
035import org.junit.Test;
036import org.mockito.Mock;
037import org.mockito.MockitoAnnotations;
038
039/**
040 * Tests for {@link ContainerRolesPrincipalProvider}.
041 *
042 * @author Kevin S. Clarke
043 */
044public class ContainerRolesPrincipalProviderTest {
045
046    @Mock
047    private HttpServletRequest request;
048
049    private ContainerRolesPrincipalProvider provider;
050
051    /**
052     * Sets up ContainerRolesPrincipalProviderTest's tests.
053     */
054    @Before
055    public void setUp() {
056        MockitoAnnotations.openMocks(this);
057        provider = new ContainerRolesPrincipalProvider();
058    }
059
060    /**
061     * Test for {@link ContainerRolesPrincipalProvider#setRoleNames(Set)}.
062     */
063    @Test
064    public void testSetRole() {
065        when(request.isUserInRole("a")).thenReturn(true);
066        provider.setRoleNames(newHashSet("a"));
067
068        final Set<Principal> principals = provider.getPrincipals(request);
069
070        assertEquals(1, principals.size());
071        assertTrue("The principals should contain 'a'", principals.contains(new ContainerRolesPrincipal("a")));
072    }
073
074    /**
075     * Test for {@link ContainerRolesPrincipalProvider#setRoleNames(Set)}.
076     */
077    @Test
078    public void testSetRoles() {
079        when(request.isUserInRole("a")).thenReturn(true);
080        when(request.isUserInRole("b")).thenReturn(true);
081        provider.setRoleNames(newHashSet("a", "b"));
082
083        final Set<Principal> principals = provider.getPrincipals(request);
084
085        assertEquals(2, principals.size());
086        assertTrue("The principals should contain 'a'", principals.contains(new ContainerRolesPrincipal("a")));
087        assertTrue("The principals should contain 'b'", principals.contains(new ContainerRolesPrincipal("b")));
088    }
089
090    /**
091     * Test for {@link ContainerRolesPrincipalProvider#setRoleNames(Set)}.
092     */
093    @Test
094    public void testTrimSetRoles() {
095        when(request.isUserInRole("a")).thenReturn(true);
096        when(request.isUserInRole("b")).thenReturn(true);
097        provider.setRoleNames(newHashSet(" a", "b "));
098
099        final Set<Principal> principals = provider.getPrincipals(request);
100
101        assertEquals(2, principals.size());
102        assertTrue("The principals should contain 'a'", principals.contains(new ContainerRolesPrincipal("a")));
103        assertTrue("The principals should contain 'b'", principals.contains(new ContainerRolesPrincipal("b")));
104    }
105
106    /**
107     * Test for {@link ContainerRolesPrincipalProvider#setRoleNames(Set)}.
108     */
109    @Test
110    public void testNoConfigedRoleNames() {
111        final Set<Principal> principals = provider.getPrincipals(request);
112        assertTrue("Empty set expected when no role names configured", principals.isEmpty());
113    }
114
115    /**
116     * Test for {@link ContainerRolesPrincipalProvider#getPrincipals(javax.servlet.http.HttpServletRequest)}.
117     */
118    @Test
119    public void testNoRequest() {
120        provider.setRoleNames(newHashSet("a"));
121
122        final Set<Principal> principals = provider.getPrincipals(null);
123        assertTrue("Empty set expected when no request supplied", principals.isEmpty());
124
125    }
126
127    /**
128     * Test for {@link ContainerRolesPrincipalProvider#getPrincipals(javax.servlet.http.HttpServletRequest)}.
129     */
130    @Test
131    public void testPrincipalEqualsDifferentClass() {
132        when(request.isUserInRole("a")).thenReturn(true);
133        provider.setRoleNames(newHashSet("a"));
134
135        final Set<Principal> principals = provider.getPrincipals(request);
136        final Principal principal = principals.iterator().next();
137
138        assertNotEquals("Principals should not be equal if not the same class", principal, mock(Principal.class));
139    }
140
141}