    001package org.apache.turbine.services.security;
    002/*
    003 * Licensed to the Apache Software Foundation (ASF) under one
    004 * or more contributor license agreements.  See the NOTICE file
    005 * distributed with this work for additional information
    006 * regarding copyright ownership.  The ASF licenses this file
    007 * to you under the Apache License, Version 2.0 (the
    008 * "License"); you may not use this file except in compliance
    009 * with the License.  You may obtain a copy of the License at
    010 *
    011 *   http://www.apache.org/licenses/LICENSE-2.0
    012 *
    013 * Unless required by applicable law or agreed to in writing,
    014 * software distributed under the License is distributed on an
    015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    016 * KIND, either express or implied.  See the License for the
    017 * specific language governing permissions and limitations
    018 * under the License.
    019 */
    020import static org.junit.Assert.assertFalse;
    021import static org.junit.Assert.assertTrue;
    022
    023import org.apache.fulcrum.security.SecurityService;
    024import org.apache.fulcrum.security.entity.Group;
    025import org.apache.fulcrum.security.entity.Role;
    026import org.apache.fulcrum.security.model.turbine.TurbineModelManager;
    027import org.apache.fulcrum.security.model.turbine.entity.TurbineUser;
    028import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole;
    029import org.apache.fulcrum.security.model.turbine.entity.impl.TurbineUserImpl;
    030import org.apache.fulcrum.security.model.turbine.test.AbstractTurbineModelManagerTest;
    031import org.apache.fulcrum.security.util.DataBackendException;
    032import org.apache.fulcrum.security.util.EntityExistsException;
    033import org.apache.fulcrum.security.util.UnknownEntityException;
    034import org.apache.fulcrum.testcontainer.BaseUnit4Test;
    035import org.apache.turbine.om.security.User;
    036import org.apache.turbine.services.ServiceManager;
    037import org.apache.turbine.services.TurbineServices;
    038import org.apache.turbine.util.TurbineConfig;
    039import org.junit.AfterClass;
    040import org.junit.Before;
    041import org.junit.BeforeClass;
    042import org.junit.Test;
    043
    044/**
    045 * Test that the SecurityService works properly by comparing behaviour of Turbine and Fulcrum security services using memory user manager.
    046 *
    047 * Code adapted from SecurityServiceAdapter in Fulcrum Security Adapter
    048 *
    049 * @author gkallidis
    050 * @version $Id$
    051 */
    052
    053public class SecurityServiceTest extends BaseUnit4Test
    054{
    055
    056    SecurityService fulcrumSecurityService;
    057    org.apache.turbine.services.security.SecurityService securityService;
    058    static TurbineConfig tc;
    059
    060
    061    @BeforeClass
    062    public static void init() throws Exception
    063    {
    064        tc = new TurbineConfig(".", "/conf/test/CompleteTurbineResources.properties");
    065        tc.initialize();
    066
    067    }
    068
    069    @Before
    070    public void setUpBefore() throws Exception
    071    {
    072
    073        ServiceManager serviceManager = TurbineServices.getInstance();
    074        //
    075        fulcrumSecurityService = (SecurityService) serviceManager.getService(SecurityService.ROLE);
    076
    077        securityService = (org.apache.turbine.services.security.SecurityService)
    078                        TurbineServices.getInstance().getService(org.apache.turbine.services.security.SecurityService.SERVICE_NAME);
    079    }
    080
    081    @Test
    082    public void testAccountExists() throws Exception
    083    {
    084
    085        User user = new org.apache.turbine.om.security.DefaultUserImpl(new TurbineUserImpl());
    086        user.setAccessCounter(5);
    087
    088        assertFalse(securityService.accountExists(user));
    089        assertFalse(fulcrumSecurityService.getUserManager().checkExists(user));
    090
    091    }
    092    @Test
    093        public void testCreateUser() throws Exception
    094        {
    095
    096                User user = new org.apache.turbine.om.security.DefaultUserImpl(new TurbineUserImpl());
    097                user.setAccessCounter(5);
    098                user.setName("ringo");
    099                securityService.addUser(user,"fakepasswrod");
    100                assertTrue(securityService.accountExists(user));
    101                assertTrue(fulcrumSecurityService.getUserManager().checkExists(user));
    102
    103        }
    104
    105    /**
    106     * Tests Turbine and Fulcrum.
    107     *
    108     * Fulcrum part is similar/duplicated from {@link AbstractTurbineModelManagerTest#testGrantUserGroupRole()}
    109     *
    110     *
    111     * @throws Exception
    112     */
    113    @Test
    114    public void testGrantUserGroupRole() throws Exception
    115    {
    116        Group group = fulcrumSecurityService.getGroupManager().getGroupInstance();
    117        group.setName("TEST_GROUP");
    118        fulcrumSecurityService.getGroupManager().addGroup(group);
    119        Role role = fulcrumSecurityService.getRoleManager().getRoleInstance();
    120        role.setName("TEST_Role");
    121        fulcrumSecurityService.getRoleManager().addRole(role);
    122
    123        //  Turbine security service returns a wrapped instance: org.apache.turbine.om.security.DefaultUserImpl
    124        // which implements org.apache.turbine.om.security.User and contains
    125        User user = securityService.getUserInstance("Clint");
    126                // memory
    127        securityService.addUser(user, "clint");
    128        securityService.grant(user, group, role);
    129
    130                addUserAndCheck(group, role, user.getUserDelegate());
    131
    132        // Fulcrum security service returns a raw org.apache.fulcrum.security.model.turbine.entity.impl.TurbineUserImpl,
    133                org.apache.fulcrum.security.UserManager  userManager = fulcrumSecurityService.getUserManager();
    134                TurbineUser fulcrumUser = userManager.getUserInstance("Clint2");
    135        userManager.addUser(fulcrumUser, "clint2");         // memory
    136        ((TurbineModelManager)fulcrumSecurityService.getModelManager()).grant(fulcrumUser, group, role);
    137
    138        addUserAndCheck(group, role, fulcrumUser);
    139
    140    }
    141
    142    /**
    143     * Fulcrum contract check
    144     *
    145     * @param group Fulcrum interface
    146     * @param role Fulcrum interface
    147     * @param user Fulcrum interface
    148     * @throws EntityExistsException
    149     * @throws DataBackendException
    150     * @throws UnknownEntityException
    151     */
    152        private void addUserAndCheck(Group group, Role role, TurbineUser user)
    153                        throws EntityExistsException, DataBackendException,
    154                        UnknownEntityException
    155        {
    156
    157        boolean ugrFound = false;
    158        TurbineUserGroupRole ugrTest = null;
    159        for (TurbineUserGroupRole ugr : ((TurbineUser) user).getUserGroupRoleSet())
    160        {
    161            if (ugr.getUser().equals(user) && ugr.getGroup().equals(group) && ugr.getRole().equals(role))
    162            {
    163                ugrFound = true;
    164                ugrTest = ugr;
    165                break;
    166            }
    167        }
    168        assertTrue(ugrFound);
    169        assertTrue(ugrTest.getGroup().equals(group));
    170        assertTrue(ugrTest.getUser().equals(user));
    171        }
    172
    173
    174        @AfterClass
    175        public static void setupAfter()
    176    {
    177        ServiceManager serviceManager = TurbineServices.getInstance();
    178        serviceManager.shutdownService(org.apache.turbine.services.security.SecurityService.SERVICE_NAME);
    179        serviceManager.shutdownServices();
    180    }
    181
    182
    183}