View source
<?php
require_once drupal_get_path('module', 'ldap_authorization') . '/tests/LdapAuthorizationTestCase.class.php';
class LdapAuthorizationDeriveFromAttr extends LdapAuthorizationTestCase {
public static function getInfo() {
return array(
'group' => 'LDAP Authorization',
'name' => 'LDAP Authorization: Derive from User Attributes',
'description' => 'e.g. memberOf attribute in Active Directory. Tests are in absence of logons to isolate mapping logic.',
);
}
function testDeriveFromAttr() {
return;
$this->ldapTestId = 'DeriveFromAttr';
$this->serversData = 'DeriveFromAttr/ldap_servers.inc';
$this->authorizationData = 'DeriveFromAttr/ldap_authorization.inc';
$this->authenticationData = 'DeriveFromAttr/ldap_authentication.inc';
$this->consumerType = 'drupal_role';
$this
->prepTestData();
$user = $this
->drupalCreateUser(array());
$unkool = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'unkool',
'mail' => 'unkool@nowhere.myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($unkool, 'query', $this->consumerType);
$this
->assertTrue(count($new_authorizations[$this->consumerType]) == 0, 'user account unkool tested for granting no drupal roles ', $this->ldapTestId . '.nomatch');
$user = $this
->drupalCreateUser(array());
$jkool = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'jkool',
'mail' => 'jkool@guests.myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($jkool, 'query', $this->consumerType);
$correct_roles = (bool) (isset($new_authorizations[$this->consumerType]) && in_array('sysadmins', $new_authorizations['drupal_role']));
$this
->assertTrue($correct_roles, 'user account jkool tested for granting drupal_role "sysadmins"', $this->ldapTestId . '.onematch');
$correct_roles = (bool) (isset($new_authorizations['drupal_role']) && in_array('mailgroup17', $new_authorizations['drupal_role']));
$this
->assertTrue($correct_roles, 'user account jkool tested for granting drupal_role "mailgroup17" from numeric ldap value', $this->ldapTestId . '.numeric_attr_value');
user_delete($user->uid);
$user = $this
->drupalCreateUser(array());
$wilmaf = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'wilmaf',
'mail' => 'wilmaf@guests.myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($wilmaf, 'query', $this->consumerType);
$correct_roles = (bool) (isset($new_authorizations[$this->consumerType]) && in_array('sysadmins', $new_authorizations['drupal_role']));
$this
->assertTrue($correct_roles, 'user account wilma tested for granting drupal_role "sysadmins"', $this->ldapTestId . '.escaped');
user_delete($user->uid);
$user = $this
->drupalCreateUser(array());
$verykool = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'verykool',
'mail' => 'verykool@myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($verykool, 'query', $this->consumerType);
$correct_roles = (bool) (isset($new_authorizations[$this->consumerType]) && in_array('netadmins', $new_authorizations[$this->consumerType]) && in_array('sysadmins', $new_authorizations[$this->consumerType]));
$this
->assertTrue($correct_roles, 'user account verykool tested for granting "netadmins" and "sysadmins" drupal roles ', $this->ldapTestId . '.manymatch');
$this
->assertTrue($correct_roles, 'user account verykool tested for case insensitivity ', $this->ldapTestId . '.caseinsensitive');
user_delete($user->uid);
$consumer_conf_admin = ldap_authorization_get_consumer_admin_object($this->consumerType);
$consumer_conf_admin->deriveFromAttrUseFirstAttr = 1;
$consumer_conf_admin
->save();
$user = $this
->drupalCreateUser(array());
$verykool = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'verykool',
'mail' => 'verykool@guests.myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($verykool, 'query', $this->consumerType);
$correct_roles = (bool) (isset($new_authorizations[$this->consumerType]) && in_array('netadmins2', $new_authorizations['drupal_role']));
if (!$correct_roles) {
debug('new authorizations');
debug($new_authorizations);
}
$this
->assertTrue($correct_roles, 'user account verykool tested for granting drupal_role "netadmins2"', $this->ldapTestId . '.deriveFromAttrUseFirstAttr');
$consumer_conf_admin->deriveFromAttrUseFirstAttr = 0;
$consumer_conf_admin
->save();
user_delete($user->uid);
$consumer_conf_admin->deriveFromAttrUseFirstAttr = 1;
$consumer_conf_admin
->save();
$user = $this
->drupalCreateUser(array());
$wilmaf = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'wilmaf',
'mail' => 'wilmaf@guests.myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($wilmaf, 'query', $this->consumerType);
$correct_roles = (bool) (isset($new_authorizations[$this->consumerType]) && in_array('comma freaks', $new_authorizations['drupal_role']));
$this
->assertTrue($correct_roles, 'user account wilmaf tested for granting drupal_role "comma freaks"', $this->ldapTestId . '.deriveFromAttrUseFirstAttr.escaped');
$consumer_conf_admin->deriveFromAttrUseFirstAttr = 0;
$consumer_conf_admin
->save();
user_delete($user->uid);
$consumer_conf_admin->deriveFromAttrUseFirstAttr = 1;
$consumer_conf_admin
->save();
$user = $this
->drupalCreateUser(array());
$barneyr = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'barneyr',
'mail' => 'barneyr@guests.myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($barneyr, 'query', $this->consumerType);
$correct_roles = (bool) (isset($new_authorizations[$this->consumerType]) && in_array('comma freaks', $new_authorizations['drupal_role']));
$this
->assertTrue($correct_roles, 'user account barneyr tested for granting drupal_role "comma freaks"', $this->ldapTestId . '.deriveFromAttrUseFirstAttr.quoted');
$consumer_conf_admin->deriveFromAttrUseFirstAttr = 0;
$consumer_conf_admin
->save();
user_delete($user->uid);
module_enable(array(
'php',
));
$php = " \$parts = explode(\"@\", \$name); \n if (count(\$parts) == 2) {\n print \$parts[0];\n }; \n ";
$this->testFunctions
->setFakeServerProperty('ldapauthor1', 'ldapToDrupalUserPhp', $php);
$user = $this
->drupalCreateUser(array());
$verykool = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'verykool@gmail.com',
'mail' => 'verykool@gmail.com',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($verykool, 'query', $this->consumerType);
$correct_roles = (bool) (isset($new_authorizations[$this->consumerType]) && in_array('netadmins', $new_authorizations['drupal_role']) && in_array('sysadmins', $new_authorizations['drupal_role']));
$this
->assertTrue($correct_roles, 'php transform drupal username verykool@gmail.com to ldap username attribute verykool"', $this->ldapTestId . '.ldapToDrupalUserPhp');
$this->testFunctions
->setFakeServerProperty('ldap_test_server__ldapauthor1', 'ldapToDrupalUserPhp', NULL);
user_delete($user->uid);
module_disable(array(
'php',
));
}
function testDeriveFromAttrNested() {
$this->ldapTestId = 'DeriveFromAttr.nested';
$this->serversData = 'DeriveFromAttr/ldap_servers.nested.inc';
$this->authorizationData = 'DeriveFromAttr/ldap_authorization.nested.inc';
$this->authenticationData = 'DeriveFromAttr/ldap_authentication.inc';
$this->consumerType = 'drupal_role';
$this
->prepTestData();
$user = $this
->drupalCreateUser(array());
$unkool = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'unkool',
'mail' => 'unkool@nowhere.myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($unkool, 'query', $this->consumerType);
$this
->assertTrue(!isset($new_authorizations[$this->consumerType]) || count($new_authorizations[$this->consumerType]) == 0, 'user account unkool tested for granting no drupal roles ', $this->ldapTestId . '.nomatch');
$user = $this
->drupalCreateUser(array());
$justin = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'justin',
'mail' => 'justin@myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($justin, 'query', $this->consumerType);
$this
->assertTrue(count($new_authorizations[$this->consumerType]) == 1, 'user account justin tested for granting 1 drupal roles ', $this->ldapTestId . '.no_parent_groups');
$correct_roles = (bool) (isset($new_authorizations[$this->consumerType]) && in_array('cn=people,dc=ad,dc=myuniversity,dc=edu', $new_authorizations[$this->consumerType]));
$this
->assertTrue($correct_roles, 'user account justin tested for granting drupal_role "cn=people,ou=it,dc=ad,dc=myuniversity,dc=edu"', $this->ldapTestId . '.no_parent_groups');
$user = $this
->drupalCreateUser(array());
$newkool = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'newkool',
'mail' => 'newkool@myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($newkool, 'query', $this->consumerType);
$this
->assertTrue(count($new_authorizations[$this->consumerType]) == 4, 'user account newkool tested for granting 4 drupal roles ', $this->ldapTestId . '.nomatch');
$correct_roles = (bool) (isset($new_authorizations[$this->consumerType]) && in_array('cn=sysadmins,ou=it,dc=ad,dc=myuniversity,dc=edu', $new_authorizations[$this->consumerType]) && in_array('cn=it,ou=it,dc=ad,dc=myuniversity,dc=edu', $new_authorizations[$this->consumerType]) && in_array('cn=staff,ou=people,dc=ad,dc=myuniversity,dc=edu', $new_authorizations[$this->consumerType]) && in_array('cn=people,dc=ad,dc=myuniversity,dc=edu', $new_authorizations[$this->consumerType]));
$this
->assertTrue($correct_roles, 'user account newkool tested for granting correct drupal roles', $this->ldapTestId . '.parents1');
$user = $this
->drupalCreateUser(array());
$joeprogrammer = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'joeprogrammer',
'mail' => 'joeprogrammer@myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($joeprogrammer, 'query', $this->consumerType);
$this
->assertTrue(count($new_authorizations[$this->consumerType]) == 4, 'user account joeprogrammer tested for granting 4 drupal roles ', $this->ldapTestId . '.nomatch');
$correct_roles = (bool) (isset($new_authorizations[$this->consumerType]) && in_array('cn=developers,ou=it,dc=ad,dc=myuniversity,dc=edu', $new_authorizations[$this->consumerType]) && in_array('cn=it,ou=it,dc=ad,dc=myuniversity,dc=edu', $new_authorizations[$this->consumerType]) && in_array('cn=staff,ou=people,dc=ad,dc=myuniversity,dc=edu', $new_authorizations[$this->consumerType]) && in_array('cn=people,dc=ad,dc=myuniversity,dc=edu', $new_authorizations[$this->consumerType]));
$this
->assertTrue($correct_roles, 'user joeprogrammer tested for granting correct drupal roles', $this->ldapTestId . '.parents2');
$user = $this
->drupalCreateUser(array());
$memento = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'memento',
'mail' => 'memento@myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($memento, 'query', $this->consumerType);
$this
->assertTrue(count($new_authorizations[$this->consumerType]) == 2, 'user account memento tested for granting 2 roles ', $this->ldapTestId . '.recursion');
$correct_roles = (bool) (isset($new_authorizations[$this->consumerType]) && in_array('cn=lessrecursive,dc=ad,dc=myuniversity,dc=edu', $new_authorizations[$this->consumerType]) && in_array('cn=recursive,dc=ad,dc=myuniversity,dc=edu', $new_authorizations[$this->consumerType]));
$this
->assertTrue($correct_roles, 'user memento tested for granting correct drupal roles', $this->ldapTestId . '.recursion');
}
}