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.integration;
019
020import org.apache.http.auth.BasicUserPrincipal;
021
022import org.fcrepo.auth.common.FedoraAuthorizationDelegate;
023import org.fcrepo.auth.common.ServletContainerAuthenticationProvider;
024import org.fcrepo.kernel.modeshape.services.ContainerServiceImpl;
025import org.fcrepo.kernel.api.services.ContainerService;
026
027import org.junit.Assert;
028import org.junit.Test;
029import org.junit.runner.RunWith;
030import org.mockito.Mockito;
031import org.modeshape.jcr.api.ServletCredentials;
032import org.modeshape.jcr.value.Path;
033import org.slf4j.Logger;
034import org.springframework.test.context.ContextConfiguration;
035import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
036
037import javax.inject.Inject;
038import javax.jcr.Repository;
039import javax.jcr.RepositoryException;
040import javax.jcr.Session;
041import javax.jcr.security.Privilege;
042import javax.servlet.http.HttpServletRequest;
043
044import static org.mockito.Matchers.any;
045import static org.mockito.Mockito.atLeastOnce;
046import static org.mockito.Mockito.mock;
047import static org.mockito.Mockito.verify;
048import static org.mockito.Mockito.when;
049import static org.slf4j.LoggerFactory.getLogger;
050
051/**
052 * @author osmandin
053 */
054@RunWith(SpringJUnit4ClassRunner.class)
055@ContextConfiguration(locations = {"/spring-test/mocked-fad-repo-1.xml"})
056public class HttpHeaderPrincipalProviderIT {
057
058    private static Logger logger =
059            getLogger(HttpHeaderPrincipalProviderIT.class);
060
061    @Inject
062    private Repository repo;
063
064    @Inject
065    private FedoraAuthorizationDelegate fad;
066
067    private final HttpServletRequest request = mock(HttpServletRequest.class);
068
069    @Test
070    public void testFactory() {
071        Assert.assertNotNull(
072                "AuthenticationProvider must return a AuthenticationProvider",
073                ServletContainerAuthenticationProvider.getInstance());
074    }
075
076    @Test
077    public void testEmptyPrincipalProvider() throws RepositoryException {
078        when(request.getRemoteUser()).thenReturn("fred");
079        when(request.getUserPrincipal()).thenReturn(
080                new BasicUserPrincipal("fred"));
081        when(
082                request.isUserInRole(Mockito
083                        .eq(ServletContainerAuthenticationProvider.FEDORA_USER_ROLE)))
084                .thenReturn(true);
085        Mockito.reset(fad);
086        when(fad.hasPermission(any(Session.class), any(Path.class), any(String[].class))).thenReturn(true);
087
088        final ServletCredentials credentials =
089                new ServletCredentials(request);
090        final Session session = repo.login(credentials);
091        final Privilege[] rootPrivs =
092                session.getAccessControlManager().getPrivileges("/");
093        for (final Privilege p : rootPrivs) {
094            logger.debug("got priv: " + p.getName());
095        }
096        final ContainerService os = new ContainerServiceImpl();
097        os.findOrCreate(session, "/myobject");
098        verify(fad, atLeastOnce()).hasPermission(any(Session.class), any(Path.class), any(String[].class));
099    }
100
101}