Package org.olat.basesecurity

Source Code of org.olat.basesecurity.SecurityManagerTest

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.basesecurity;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;
import org.olat.core.commons.persistence.DB;
import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.id.Identity;
import org.olat.core.id.OLATResourceable;
import org.olat.core.id.UserConstants;
import org.olat.core.test.OlatTestCase;
import org.olat.core.util.resource.OresHelper;
import org.olat.login.OLATAuthenticationController;
import org.olat.resource.OLATResource;
import org.olat.test.JunitTestHelper;

/**
* SecurityTestSuite is a container of all Tests in this package.
*
* @author Andreas Ch. Kapp
*/
public class SecurityManagerTest extends OlatTestCase {

  private static Logger log = Logger.getLogger(SecurityManagerTest.class.getName());
  private Identity s1,s2,s3,testAdmin;
  private static String testLogin = "test-login";
  private OLATResourceable olatres, olatres2;
  private static boolean isInitialized = false;
  private Manager sm;

  /**
   * @param name
   */
  public SecurityManagerTest(String name) {
    super(name);
  }

  /**
   * @return Test
   */
  public static Test suite() {
    return new TestSuite(SecurityManagerTest.class);
  }

  // Already tested in BusinessGroupTest :
  //  - getGroupsWithPermissionOnOlatResourceable
  //  - getIdentitiesWithPermissionOnOlatResourceable
  /**
   *
   */
  public void testGetIdentitiesByPowerSearch() {
    // test using visibility search
    List userList = sm.getVisibleIdentitiesByPowerSearch(testLogin, null, true, null, null, null, null, null);
    assertEquals(1,userList.size());
    Identity identity = (Identity) userList.get(0);
    assertEquals(testLogin,identity.getName());
    // test using powser search
    userList = sm.getIdentitiesByPowerSearch(testLogin, null, true, null, null, null, null, null, null);
    assertEquals(1,userList.size());
    identity = (Identity) userList.get(0);
    assertEquals(testLogin,identity.getName());
  }
 
  public void testGetIdentitiesByPowerSearchWithuserProperties() {
    Map<String, String> userProperties = new HashMap<String, String>();
    userProperties.put(UserConstants.FIRSTNAME, "first"+ testLogin);
    userProperties.put(UserConstants.LASTNAME, "last"+ testLogin);
    // test using visibility search
    List userList = sm.getVisibleIdentitiesByPowerSearch(testLogin, userProperties, true, null, null, null, null, null);
    assertEquals(1,userList.size());
    Identity identity = (Identity) userList.get(0);
    assertEquals("first" + testLogin,identity.getUser().getProperty(UserConstants.FIRSTNAME, null));
    // test using powser search
    userList = sm.getIdentitiesByPowerSearch(testLogin, userProperties, true, null, null, null, null, null, null);
    assertEquals(1,userList.size());
    identity = (Identity) userList.get(0);
    assertEquals("first" + testLogin,identity.getUser().getProperty(UserConstants.FIRSTNAME, null));
  }

  public void testGetIdentitiesByPowerSearchWithConjunctionFlag() {
    // 1) two fields that match to two different users
    Map<String, String> userProperties = new HashMap<String, String>();
    userProperties.put(UserConstants.FIRSTNAME, s1.getUser().getProperty(UserConstants.FIRSTNAME, null));
    userProperties.put(UserConstants.LASTNAME, s2.getUser().getProperty(UserConstants.LASTNAME, null));
    // with AND search (conjunction) no identity is found
    List userList = sm.getIdentitiesByPowerSearch(null, userProperties, true, null, null, null, null, null, null);
    assertEquals(0, userList.size());
    // with OR search both identities are found
    userList = sm.getIdentitiesByPowerSearch(null, userProperties, false, null, null, null, null, null, null);
    assertEquals(2, userList.size());

    // 2) two fields wheras only one matches to one single user
    userProperties = new HashMap<String, String>();
    userProperties.put(UserConstants.FIRSTNAME, s1.getUser().getProperty(UserConstants.FIRSTNAME, null));
    userProperties.put(UserConstants.LASTNAME, "some nonexisting value");
    // with AND search (conjunction) no identity is found
    userList = sm.getIdentitiesByPowerSearch(null, userProperties, true, null, null, null, null, null, null);
    assertEquals(0, userList.size());
    // with OR search first identity ist found
    userList = sm.getIdentitiesByPowerSearch(null, userProperties, false, null, null, null, null, null, null);
    assertEquals(1, userList.size());
  }
 
  public void testGetIdentitiesByPowerSearchWithGroups() {
    SecurityGroup[] groups = {sm.findSecurityGroupByName(Constants.GROUP_OLATUSERS)};
    List userList = sm.getVisibleIdentitiesByPowerSearch(testLogin, null, true, groups, null, null, null, null);
    assertEquals(1,userList.size());
    Identity identity = (Identity) userList.get(0);
    assertEquals(testLogin,identity.getName());
    SecurityGroup[] authors = {sm.findSecurityGroupByName(Constants.GROUP_AUTHORS)};
    userList = sm.getVisibleIdentitiesByPowerSearch(testLogin, null, true, authors, null, null, null, null);
    assertEquals(0,userList.size());
  }
 
  public void testGetIdentitiesByPowerSearchWithAuthProviders() {
    // 1) only auth providers and login
    String[] authProviders = {OLATAuthenticationController.PROVIDER_OLAT};
    List userList = sm.getVisibleIdentitiesByPowerSearch(testLogin, null, true, null, null, authProviders, null, null);
    assertEquals(1,userList.size());
    Identity identity = (Identity) userList.get(0);
    assertEquals(testLogin,identity.getName());
    String[] nonAuthProviders = {"NonAuthProvider"};
    userList = sm.getVisibleIdentitiesByPowerSearch(testLogin, null, true, null, null, nonAuthProviders, null, null);
    assertEquals(0,userList.size());
   
    // 2) two fields wheras only one matches to one single user
    Map<String, String> userProperties = new HashMap<String, String>();
    userProperties.put(UserConstants.FIRSTNAME, s1.getUser().getProperty(UserConstants.FIRSTNAME, null));
    userProperties.put(UserConstants.LASTNAME, "some nonexisting value");
    // with AND search (conjunction) no identity is found
    userList = sm.getIdentitiesByPowerSearch(null, userProperties, true, null, null, authProviders, null, null, null);
    assertEquals(0, userList.size());
    // with OR search first identity ist found
    userList = sm.getIdentitiesByPowerSearch(null, userProperties, false, null, null, authProviders, null, null, null);
    assertEquals(1, userList.size());

    // 3) two fields wheras only one matches to one single user
    sm.createAndPersistAuthentication(s1, "mytest_p", s1.getName(), "sdf");
    String[] myProviders = new String[] {"mytest_p", "non-existing-provider"};
    userProperties = new HashMap<String, String>();
    userProperties.put(UserConstants.FIRSTNAME, s1.getUser().getProperty(UserConstants.FIRSTNAME, null));
    userProperties.put(UserConstants.LASTNAME, "some nonexisting value");
    // with AND search (conjunction) no identity is found
    userList = sm.getIdentitiesByPowerSearch(null, userProperties, true, null, null, myProviders, null, null, null);
    assertEquals(0, userList.size());
    // with OR search identity is found via auth provider and via first name
    userList = sm.getIdentitiesByPowerSearch(null, userProperties, false, null, null, myProviders, null, null, null);
    assertEquals(1, userList.size());
  }
 
  public void testGetPoliciesOfIdentity() {
    SecurityGroup olatUsersGroup = sm.findSecurityGroupByName(Constants.GROUP_OLATUSERS);
    sm.createAndPersistPolicy(olatUsersGroup, Constants.PERMISSION_ACCESS, olatres);
    List policies = sm.getPoliciesOfIdentity(s1);
    assertEquals(1,policies.size());
    Object[] policy = (Object[])policies.get(0);
    assertEquals(olatUsersGroup.getKey(),((SecurityGroup)policy[0]).getKey());
    assertEquals(Constants.PERMISSION_ACCESS,((Policy)policy[1]).getPermission());
    assertEquals(olatres.getResourceableId(),((OLATResource)policy[2]).getResourceableId());
  }
 
  public void testRemoveIdentityFromSecurityGroup() {
    SecurityGroup olatUsersGroup = sm.findSecurityGroupByName(Constants.GROUP_OLATUSERS);
    assertTrue(sm.isIdentityInSecurityGroup(s1, olatUsersGroup));
    sm.removeIdentityFromSecurityGroup(s1, olatUsersGroup);
    assertFalse(sm.isIdentityInSecurityGroup(s1, olatUsersGroup));
    sm.addIdentityToSecurityGroup(s1, olatUsersGroup);
    assertTrue(sm.isIdentityInSecurityGroup(s1, olatUsersGroup));
  }
 
  public void testGetIdentitiesAndDateOfSecurityGroup() {
    SecurityGroup olatUsersGroup = sm.findSecurityGroupByName(Constants.GROUP_OLATUSERS);
    List identities = sm.getIdentitiesAndDateOfSecurityGroup(olatUsersGroup, false);// not sortedByAddDate
    assertEquals(2,identities.size());
    ((Identity)((Object[])identities.get(1))[0]).setLastLogin(new Date());
    identities = sm.getIdentitiesAndDateOfSecurityGroup(olatUsersGroup, true);// sortedByAddDate
    assertEquals(2,identities.size());
  }
 
  public void testGetSecurityGroupJoinDateForIdentity(){
    SecurityGroup secGroup = sm.createAndPersistSecurityGroup();
    sm.addIdentityToSecurityGroup(s1, secGroup);
    DBFactory.getInstance().commit();
    Date now = new Date();
    assertTrue(sm.getSecurityGroupJoinDateForIdentity(secGroup, s1).getTime() < now.getTime());
    assertNotNull(sm.getSecurityGroupJoinDateForIdentity(secGroup, s1));
    if (!sm.getSecurityGroupsForIdentity(s2).contains(secGroup)){
      assertNull(sm.getSecurityGroupJoinDateForIdentity(secGroup, s2));
    }
  }
 
  public void testGetAuthentications() {
    List authentications = sm.getAuthentications(s1);
    Authentication authentication = (Authentication)authentications.get(0);
    assertEquals(testLogin,authentication.getAuthusername());
  }

  public void testFindAuthenticationByAuthusername() {
    Authentication authentication = sm.findAuthenticationByAuthusername(testLogin, OLATAuthenticationController.PROVIDER_OLAT);
    assertEquals(testLogin,authentication.getAuthusername());
  }

  public void testCountUniqueUserLoginsSince(){
    Calendar c1 = Calendar.getInstance();
    c1.add(Calendar.DAY_OF_YEAR, -1);
    s1.setLastLogin(c1.getTime());
    c1.add(Calendar.DAY_OF_YEAR, -5);
    s2.setLastLogin(c1.getTime());
    c1.add(Calendar.DAY_OF_YEAR, -15);
    s3.setLastLogin(c1.getTime());
    c1.add(Calendar.DAY_OF_YEAR, -100);
    testAdmin.setLastLogin(c1.getTime());
 
    DB db = DBFactory.getInstance();
    db.updateObject(s1);
    db.updateObject(s2);
    db.updateObject(s3);
    db.updateObject(testAdmin);
    db.closeSession();
   
    Calendar c2 = Calendar.getInstance();   
    //daily:
    assertEquals(0, sm.countUniqueUserLoginsSince(new Date()).intValue());
    c2.add(Calendar.DAY_OF_YEAR, -1);
    assertEquals(1, sm.countUniqueUserLoginsSince(c2.getTime()).intValue() );
    //weekly:
    c2.add(Calendar.DAY_OF_YEAR, -4);
    assertEquals(1, sm.countUniqueUserLoginsSince(c2.getTime()).intValue());
    //monthly:
    c2.add(Calendar.DAY_OF_YEAR, -15);
    assertEquals(2, sm.countUniqueUserLoginsSince(c2.getTime()).intValue());
    c2.add(Calendar.DAY_OF_YEAR, -1);
    assertEquals(3, sm.countUniqueUserLoginsSince(c2.getTime()).intValue());
    //half year:
    c2.add(Calendar.DAY_OF_YEAR, -100);
    assertEquals(4, sm.countUniqueUserLoginsSince(c2.getTime()).intValue());
    //with timestamp < 1.1.1970:
    c2.set(1950, 2, 30);
    assertEquals(4, sm.countUniqueUserLoginsSince(c2.getTime()).intValue() );
  }
 
  /*
   * (non-Javadoc)
   *
   * @see junit.framework.TestCase#setUp()
   */
  protected void setUp() throws Exception {
    super.setUp();
    if (SecurityManagerTest.isInitialized == false) {
      SecurityManagerTest.isInitialized = true;
    }
    DBFactory.getJunitInstance().clearDatabase();
    sm = ManagerFactory.getManager();
    s1 = JunitTestHelper.createAndPersistIdentityAsUser(testLogin);
    s2 = JunitTestHelper.createAndPersistIdentityAsUser("coop");
    s3 = JunitTestHelper.createAndPersistIdentityAsAuthor("diesbach");
    testAdmin = JunitTestHelper.createAndPersistIdentityAsAdmin("testAdmin");

    olatres  = OresHelper.createOLATResourceableInstance("Kürs",new Long("123"));
    olatres2 = OresHelper.createOLATResourceableInstance("Kürs_2",new Long("124"));
  }

  /*
   * (non-Javadoc)
   *
   * @see junit.framework.TestCase#tearDown()
   */
  protected void tearDown() throws Exception {
    try {
      //DB.getInstance().delete("select * from o_bookmark");
      DBFactory.getInstance().closeSession();
    } catch (Exception e) {
      log.error("tearDown failed: ", e);
    }
  }
}
TOP

Related Classes of org.olat.basesecurity.SecurityManagerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.