View Javadoc

    1   package org.apache.turbine.services.security;
    2   /*
    3    * Licensed to the Apache Software Foundation (ASF) under one
    4    * or more contributor license agreements.  See the NOTICE file
    5    * distributed with this work for additional information
    6    * regarding copyright ownership.  The ASF licenses this file
    7    * to you under the Apache License, Version 2.0 (the
    8    * "License"); you may not use this file except in compliance
    9    * with the License.  You may obtain a copy of the License at
    10   *
    11   *   http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing,
    14   * software distributed under the License is distributed on an
    15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    16   * KIND, either express or implied.  See the License for the
    17   * specific language governing permissions and limitations
    18   * under the License.
    19   */
    20  import static org.junit.Assert.assertFalse;
    21  import static org.junit.Assert.assertTrue;
    22
    23  import org.apache.fulcrum.security.SecurityService;
    24  import org.apache.fulcrum.security.entity.Group;
    25  import org.apache.fulcrum.security.entity.Role;
    26  import org.apache.fulcrum.security.model.turbine.TurbineModelManager;
    27  import org.apache.fulcrum.security.model.turbine.entity.TurbineUser;
    28  import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole;
    29  import org.apache.fulcrum.security.model.turbine.entity.impl.TurbineUserImpl;
    30  import org.apache.fulcrum.security.model.turbine.test.AbstractTurbineModelManagerTest;
    31  import org.apache.fulcrum.security.util.DataBackendException;
    32  import org.apache.fulcrum.security.util.EntityExistsException;
    33  import org.apache.fulcrum.security.util.UnknownEntityException;
    34  import org.apache.fulcrum.testcontainer.BaseUnit4Test;
    35  import org.apache.turbine.om.security.User;
    36  import org.apache.turbine.services.ServiceManager;
    37  import org.apache.turbine.services.TurbineServices;
    38  import org.apache.turbine.util.TurbineConfig;
    39  import org.junit.AfterClass;
    40  import org.junit.Before;
    41  import org.junit.BeforeClass;
    42  import org.junit.Test;
    43
    44  /**
    45   * Test that the SecurityService works properly by comparing behaviour of Turbine and Fulcrum security services using memory user manager.
    46   *
    47   * Code adapted from SecurityServiceAdapter in Fulcrum Security Adapter
    48   *
    49   * @author gkallidis
    50   * @version $Id$
    51   */
    52
    53  public class SecurityServiceTest extends BaseUnit4Test
    54  {
    55
    56      SecurityService fulcrumSecurityService;
    57      org.apache.turbine.services.security.SecurityService securityService;
    58      static TurbineConfig tc;
    59
    60
    61      @BeforeClass
    62      public static void init() throws Exception
    63      {
    64      	tc = new TurbineConfig(".", "/conf/test/CompleteTurbineResources.properties");
    65          tc.initialize();
    66
    67      }
    68
    69      @Before
    70      public void setUpBefore() throws Exception
    71      {
    72
    73          ServiceManager serviceManager = TurbineServices.getInstance();
    74          //
    75          fulcrumSecurityService = (SecurityService) serviceManager.getService(SecurityService.ROLE);
    76
    77          securityService = (org.apache.turbine.services.security.SecurityService)
    78          		TurbineServices.getInstance().getService(org.apache.turbine.services.security.SecurityService.SERVICE_NAME);
    79      }
    80
    81      @Test
    82      public void testAccountExists() throws Exception
    83      {
    84
    85          User user = new org.apache.turbine.om.security.DefaultUserImpl(new TurbineUserImpl());
    86          user.setAccessCounter(5);
    87
    88          assertFalse(securityService.accountExists(user));
    89          assertFalse(fulcrumSecurityService.getUserManager().checkExists(user));
    90
    91      }
    92      @Test
    93  	public void testCreateUser() throws Exception
    94  	{
    95
    96  		User user = new org.apache.turbine.om.security.DefaultUserImpl(new TurbineUserImpl());
    97  		user.setAccessCounter(5);
    98  		user.setName("ringo");
    99  		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 }