Actualización
This commit is contained in:
218
main/auth/shibboleth/test/shibboleth_test.class.php
Normal file
218
main/auth/shibboleth/test/shibboleth_test.class.php
Normal file
@@ -0,0 +1,218 @@
|
||||
<?php
|
||||
|
||||
namespace Shibboleth;
|
||||
|
||||
/**
|
||||
* Various Unit Tests. Note that those tests create users in the database but
|
||||
* don't delete them.
|
||||
*
|
||||
* @license see /license.txt
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
|
||||
*/
|
||||
class ShibbolethTest
|
||||
{
|
||||
|
||||
static function is_enabled()
|
||||
{
|
||||
return api_get_setting('server_type') == 'test';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ShibbolethTestHelper
|
||||
*/
|
||||
static function helper()
|
||||
{
|
||||
return ShibbolethTestHelper::instance();
|
||||
}
|
||||
|
||||
static function init()
|
||||
{
|
||||
if (!self::is_enabled())
|
||||
{
|
||||
die;
|
||||
}
|
||||
}
|
||||
|
||||
static function test_new_teacher()
|
||||
{
|
||||
self::init();
|
||||
|
||||
self::helper()->setup_new_teacher();
|
||||
$shib_user = Shibboleth::store()->get_user();
|
||||
self::assert(!User::store()->shibboleth_id_exists($shib_user->unique_id));
|
||||
|
||||
Shibboleth::save($shib_user);
|
||||
|
||||
$user = User::store()->get_by_shibboleth_id($shib_user->unique_id);
|
||||
self::assert($user->email == $shib_user->email);
|
||||
self::assert($user->firstname == $shib_user->firstname);
|
||||
self::assert($user->lastname == $shib_user->lastname);
|
||||
self::assert($user->persistent_id == $shib_user->persistent_id);
|
||||
self::assert($user->status == Shibboleth::TEACHER_STATUS);
|
||||
self::assert(!empty($user->password));
|
||||
self::assert(!empty($user->username));
|
||||
}
|
||||
|
||||
static function test_new_student()
|
||||
{
|
||||
self::init();
|
||||
|
||||
self::helper()->setup_new_student();
|
||||
|
||||
$shib_user = Shibboleth::store()->get_user();
|
||||
self::assert(!User::store()->shibboleth_id_exists($shib_user->unique_id));
|
||||
|
||||
Shibboleth::save($shib_user);
|
||||
|
||||
$user = User::store()->get_by_shibboleth_id($shib_user->unique_id);
|
||||
self::assert($user->email == $shib_user->email);
|
||||
self::assert($user->firstname == $shib_user->firstname);
|
||||
self::assert($user->lastname == $shib_user->lastname);
|
||||
self::assert($user->persistent_id == $shib_user->persistent_id);
|
||||
self::assert($user->status == Shibboleth::STUDENT_STATUS);
|
||||
self::assert(!empty($user->password));
|
||||
self::assert(!empty($user->username));
|
||||
}
|
||||
|
||||
static function test_new_staff()
|
||||
{
|
||||
self::init();
|
||||
|
||||
self::helper()->setup_new_staff();
|
||||
|
||||
$shib_user = Shibboleth::store()->get_user();
|
||||
self::assert(!User::store()->shibboleth_id_exists($shib_user->unique_id));
|
||||
|
||||
Shibboleth::save($shib_user);
|
||||
|
||||
$user = User::store()->get_by_shibboleth_id($shib_user->unique_id);
|
||||
self::assert($user->email == $shib_user->email);
|
||||
self::assert($user->firstname == $shib_user->firstname);
|
||||
self::assert($user->lastname == $shib_user->lastname);
|
||||
self::assert($user->persistent_id == $shib_user->persistent_id);
|
||||
self::assert($user->status == Shibboleth::STUDENT_STATUS);
|
||||
self::assert(!empty($user->password));
|
||||
self::assert(!empty($user->username));
|
||||
}
|
||||
|
||||
static function test_new_infer_status_request()
|
||||
{
|
||||
self::init();
|
||||
|
||||
self::helper()->setup_new_staff();
|
||||
$shib_user = Shibboleth::store()->get_user();
|
||||
Shibboleth::save($shib_user);
|
||||
self::assert($shib_user->status_request);
|
||||
|
||||
self::helper()->setup_new_teacher();
|
||||
$shib_user = Shibboleth::store()->get_user();
|
||||
Shibboleth::save($shib_user);
|
||||
|
||||
self::assert(!$shib_user->status_request);
|
||||
|
||||
self::helper()->setup_new_student();
|
||||
$shib_user = Shibboleth::store()->get_user();
|
||||
Shibboleth::save($shib_user);
|
||||
|
||||
self::assert(!$shib_user->status_request);
|
||||
}
|
||||
|
||||
static function test_update_teacher()
|
||||
{
|
||||
self::init();
|
||||
|
||||
$fields = Shibboleth::config()->update_fields;
|
||||
self::assert($fields['email']);
|
||||
self::assert($fields['persistent_id']);
|
||||
self::assert($fields['firstname']);
|
||||
self::assert($fields['lastname']);
|
||||
self::assert(!$fields['status']);
|
||||
|
||||
self::helper()->setup_teacher();
|
||||
$shib_user = Shibboleth::store()->get_user();
|
||||
Shibboleth::save($shib_user);
|
||||
|
||||
$new_shib_user = clone($shib_user);
|
||||
|
||||
$new_shib_user->firstname = 'frs';
|
||||
$new_shib_user->lastname = 'ls';
|
||||
$new_shib_user->email = 'em';
|
||||
$new_shib_user->status = 10;
|
||||
$new_shib_user->persistent_id = 'per';
|
||||
|
||||
Shibboleth::save($new_shib_user);
|
||||
$user = User::store()->get_by_shibboleth_id($shib_user->unique_id);
|
||||
|
||||
self::assert($user->email == $new_shib_user->email);
|
||||
self::assert($value = ($user->shibb_persistent_id == $new_shib_user->persistent_id));
|
||||
|
||||
self::assert($user->firstname == $new_shib_user->firstname);
|
||||
self::assert($user->lastname == $new_shib_user->lastname);
|
||||
self::assert($user->status == $shib_user->status);
|
||||
self::assert(!empty($user->password));
|
||||
self::assert(!empty($user->username));
|
||||
}
|
||||
|
||||
static function test_new_student_multiple_givenname()
|
||||
{
|
||||
self::init();
|
||||
|
||||
self::helper()->setup_new_student_multiple_givenname();
|
||||
|
||||
$shib_user = Shibboleth::store()->get_user();
|
||||
self::assert(!User::store()->shibboleth_id_exists($shib_user->unique_id));
|
||||
|
||||
Shibboleth::save($shib_user);
|
||||
|
||||
$user = User::store()->get_by_shibboleth_id($shib_user->unique_id);
|
||||
|
||||
self::assert($user->email == $shib_user->email);
|
||||
self::assert($user->firstname == 'John');
|
||||
self::assert($user->lastname == $shib_user->lastname);
|
||||
self::assert($user->persistent_id == $shib_user->persistent_id);
|
||||
self::assert($user->status == Shibboleth::STUDENT_STATUS);
|
||||
self::assert(!empty($user->password));
|
||||
self::assert(!empty($user->username));
|
||||
}
|
||||
|
||||
static function test_new_no_affiliation_default()
|
||||
{
|
||||
self::init();
|
||||
|
||||
self::helper()->setup_new_no_affiliation();
|
||||
$shib_user = Shibboleth::store()->get_user();
|
||||
self::assert($config = Shibboleth::config()->default_status == Shibboleth::STUDENT_STATUS);
|
||||
self::assert(!User::store()->shibboleth_id_exists($shib_user->unique_id));
|
||||
self::assert($shib_user->affiliation == '');
|
||||
|
||||
Shibboleth::save($shib_user);
|
||||
|
||||
$user = User::store()->get_by_shibboleth_id($shib_user->unique_id);
|
||||
|
||||
self::assert($user->email == $shib_user->email);
|
||||
self::assert($user->firstname == 'John');
|
||||
self::assert($user->lastname == $shib_user->lastname);
|
||||
self::assert($user->persistent_id == $shib_user->persistent_id);
|
||||
self::assert($user->status == Shibboleth::STUDENT_STATUS);
|
||||
self::assert(!empty($user->password));
|
||||
self::assert(!empty($user->username));
|
||||
}
|
||||
|
||||
static function assert($assertion, $message = '')
|
||||
{
|
||||
if (!$assertion)
|
||||
{
|
||||
$message = "Assert failed $message <br/>";
|
||||
echo $message;
|
||||
// Dump variable for debug
|
||||
error_log(print_r(debug_backtrace(), 1));
|
||||
die;
|
||||
}
|
||||
else
|
||||
{
|
||||
$message = "Assert successful $message <br/>";
|
||||
echo $message;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
133
main/auth/shibboleth/test/shibboleth_test_helper.class.php
Normal file
133
main/auth/shibboleth/test/shibboleth_test_helper.class.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace Shibboleth;
|
||||
|
||||
/**
|
||||
* Helper functions for the tests. Set up various dummy user types: teacher, student, etc.
|
||||
*
|
||||
* @license see /license.txt
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
|
||||
*/
|
||||
class ShibbolethTestHelper
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @return ShibbolethTestHelper
|
||||
*/
|
||||
public static function instance()
|
||||
{
|
||||
static $result = false;
|
||||
if (empty($result))
|
||||
{
|
||||
$result = new self();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function setup_teacher()
|
||||
{
|
||||
$_SERVER['Shib-SwissEP-UniqueID'] = 'usr_1';
|
||||
$_SERVER['Shib-EP-Affiliation'] = 'member;staff;faculty';
|
||||
$_SERVER['Shib-InetOrgPerson-givenName'] = 'John';
|
||||
$_SERVER['Shib-Person-surname'] = 'Doe';
|
||||
$_SERVER['Shib-InetOrgPerson-mail'] = 'john.doe@localhost.org';
|
||||
$_SERVER['persistent-id'] = 'idp!viewer!drea34çcv3d';
|
||||
}
|
||||
|
||||
public function setup_student()
|
||||
{
|
||||
$_SERVER['Shib-SwissEP-UniqueID'] = 'usr_1';
|
||||
$_SERVER['Shib-EP-Affiliation'] = 'member';
|
||||
$_SERVER['Shib-InetOrgPerson-givenName'] = 'John';
|
||||
$_SERVER['Shib-Person-surname'] = 'Doe';
|
||||
$_SERVER['Shib-InetOrgPerson-mail'] = 'john.doe@localhost.org';
|
||||
$_SERVER['persistent-id'] = 'idp!viewer!drea34çcv3d';
|
||||
}
|
||||
|
||||
public function setup_staff()
|
||||
{
|
||||
$id = uniqid();
|
||||
$_SERVER['Shib-SwissEP-UniqueID'] = 'usr_123456';
|
||||
$_SERVER['Shib-EP-Affiliation'] = 'member;staff';
|
||||
$_SERVER['Shib-InetOrgPerson-givenName'] = 'John Staff';
|
||||
$_SERVER['Shib-Person-surname'] = 'Doe';
|
||||
$_SERVER['Shib-InetOrgPerson-mail'] = 'john.staff.doe@localhost.org';
|
||||
$_SERVER['persistent-id'] = 'idp!viewer!usr_123456';
|
||||
}
|
||||
|
||||
public function setup_new_student()
|
||||
{
|
||||
$id = uniqid();
|
||||
$_SERVER['Shib-SwissEP-UniqueID'] = 'usr_' . $id;
|
||||
$_SERVER['Shib-EP-Affiliation'] = 'member';
|
||||
$_SERVER['Shib-InetOrgPerson-givenName'] = 'John';
|
||||
$_SERVER['Shib-Person-surname'] = 'Doe' . $id;
|
||||
$_SERVER['Shib-InetOrgPerson-mail'] = 'john.' . $id . 'Doe@localhost.org';
|
||||
$_SERVER['persistent-id'] = 'idp!viewer!' . md5($id);
|
||||
}
|
||||
|
||||
public function setup_new_student_no_email()
|
||||
{
|
||||
$id = uniqid();
|
||||
$_SERVER['Shib-SwissEP-UniqueID'] = 'usr_' . $id;
|
||||
$_SERVER['Shib-EP-Affiliation'] = 'member';
|
||||
$_SERVER['Shib-InetOrgPerson-givenName'] = 'John';
|
||||
$_SERVER['Shib-Person-surname'] = 'Doe' . $id;
|
||||
$_SERVER['Shib-InetOrgPerson-mail'] = '';
|
||||
$_SERVER['persistent-id'] = 'idp!viewer!' . md5($id);
|
||||
}
|
||||
|
||||
public function setup_new_student_multiple_givenname()
|
||||
{
|
||||
$id = uniqid();
|
||||
$_SERVER['Shib-SwissEP-UniqueID'] = 'usr_' . $id;
|
||||
$_SERVER['Shib-EP-Affiliation'] = 'member';
|
||||
$_SERVER['Shib-InetOrgPerson-givenName'] = 'John;Alex;John Alex';
|
||||
$_SERVER['Shib-Person-surname'] = 'Doe' . $id;
|
||||
$_SERVER['Shib-InetOrgPerson-mail'] = 'john.' . $id . 'Doe@localhost.org';
|
||||
$_SERVER['persistent-id'] = 'idp!viewer!' . md5($id);
|
||||
}
|
||||
|
||||
public function setup_new_teacher()
|
||||
{
|
||||
$id = uniqid();
|
||||
$_SERVER['Shib-SwissEP-UniqueID'] = 'usr_' . $id;
|
||||
$_SERVER['Shib-EP-Affiliation'] = 'member;staff;faculty';
|
||||
$_SERVER['Shib-InetOrgPerson-givenName'] = 'John';
|
||||
$_SERVER['Shib-Person-surname'] = 'Doe' . $id;
|
||||
$_SERVER['Shib-InetOrgPerson-mail'] = 'john.' . $id . 'Doe@localhost.org';
|
||||
$_SERVER['persistent-id'] = 'idp!viewer!' . md5($id);
|
||||
}
|
||||
|
||||
public function setup_new_staff()
|
||||
{
|
||||
$id = uniqid();
|
||||
$_SERVER['Shib-SwissEP-UniqueID'] = 'usr_' . $id;
|
||||
$_SERVER['Shib-EP-Affiliation'] = 'member;staff';
|
||||
$_SERVER['Shib-InetOrgPerson-givenName'] = 'John';
|
||||
$_SERVER['Shib-Person-surname'] = 'Doe' . $id;
|
||||
$_SERVER['Shib-InetOrgPerson-mail'] = 'john.' . $id . 'Doe@localhost.org';
|
||||
$_SERVER['persistent-id'] = 'idp!viewer!' . md5($id);
|
||||
}
|
||||
|
||||
public function setup_new_no_affiliation()
|
||||
{
|
||||
$id = uniqid();
|
||||
$_SERVER['Shib-SwissEP-UniqueID'] = 'usr_' . $id;
|
||||
$_SERVER['Shib-EP-Affiliation'] = '';
|
||||
$_SERVER['Shib-InetOrgPerson-givenName'] = 'John';
|
||||
$_SERVER['Shib-Person-surname'] = 'Doe' . $id;
|
||||
$_SERVER['Shib-InetOrgPerson-mail'] = 'john.' . $id . 'Doe@localhost.org';
|
||||
$_SERVER['persistent-id'] = 'idp!viewer!' . md5($id);
|
||||
}
|
||||
|
||||
public function setup_new_minimal_data()
|
||||
{
|
||||
$id = uniqid();
|
||||
$_SERVER['Shib-SwissEP-UniqueID'] = 'usr_' . $id;
|
||||
$_SERVER['Shib-InetOrgPerson-givenName'] = 'John';
|
||||
$_SERVER['Shib-Person-surname'] = 'Doe' . $id;
|
||||
}
|
||||
|
||||
}
|
||||
32
main/auth/shibboleth/test/test.php
Normal file
32
main/auth/shibboleth/test/test.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Shibboleth;
|
||||
|
||||
/**
|
||||
* Run unit tests. Server needs to be a test server to run those.
|
||||
*
|
||||
* @license see /license.txt
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
|
||||
*/
|
||||
include_once __DIR__.'/../init.php';
|
||||
|
||||
if (!ShibbolethTest::is_enabled())
|
||||
{
|
||||
echo 'This is not a test server';
|
||||
die;
|
||||
}
|
||||
|
||||
echo 'Test started<br/>-------------------<br/>';
|
||||
|
||||
ShibbolethTest::test_new_teacher();
|
||||
ShibbolethTest::test_new_student();
|
||||
ShibbolethTest::test_update_teacher();
|
||||
ShibbolethTest::test_new_student_multiple_givenname();
|
||||
ShibbolethTest::test_new_no_affiliation_default();
|
||||
ShibbolethTest::test_new_staff();
|
||||
ShibbolethTest::test_new_infer_status_request();
|
||||
|
||||
echo '-------------------<br/>Done!';
|
||||
|
||||
|
||||
|
||||
20
main/auth/shibboleth/test/test_no_email.php
Normal file
20
main/auth/shibboleth/test/test_no_email.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Shibboleth;
|
||||
|
||||
/**
|
||||
* @license see /license.txt
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
|
||||
*/
|
||||
include_once __DIR__.'/../init.php';
|
||||
|
||||
if (!ShibbolethTest::is_enabled())
|
||||
{
|
||||
echo 'This is not a test server';
|
||||
die;
|
||||
}
|
||||
|
||||
Shibboleth::session()->logout();
|
||||
ShibbolethTest::helper()->setup_new_student_no_email();
|
||||
|
||||
require_once __DIR__.'/../login.php';
|
||||
Reference in New Issue
Block a user