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