2016-08-16 07:50:09 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for core_userkey_manager class.
|
|
|
|
*
|
|
|
|
* @package auth_userkey
|
|
|
|
* @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net)
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
|
|
|
use auth_userkey\core_userkey_manager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for core_userkey_manager class.
|
|
|
|
*
|
|
|
|
* @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net)
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
class core_userkey_manager_testcase extends advanced_testcase {
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test user object.
|
|
|
|
* @var
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
protected $user;
|
2016-08-17 02:16:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test config object.
|
|
|
|
* @var
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initial set up.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
$this->resetAfterTest();
|
|
|
|
$this->user = self::getDataGenerator()->create_user();
|
|
|
|
$this->config = new stdClass();
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that core_userkey_manager implements userkey_manager_interface interface.
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
public function test_implements_userkey_manager_interface() {
|
|
|
|
$manager = new core_userkey_manager($this->user->id, $this->config);
|
|
|
|
|
|
|
|
$expected = 'auth_userkey\userkey_manager_interface';
|
|
|
|
$this->assertInstanceOf($expected, $manager);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-19 01:46:22 +00:00
|
|
|
* Test that we can use user ID of not existing user.
|
2016-08-16 07:50:09 +00:00
|
|
|
*/
|
2016-08-19 01:46:22 +00:00
|
|
|
public function test_that_manager_does_not_care_if_user_is_not_exists() {
|
2016-08-16 07:50:09 +00:00
|
|
|
$brokenuserid = 500;
|
|
|
|
$manager = new core_userkey_manager($brokenuserid, $this->config);
|
2016-08-19 01:46:22 +00:00
|
|
|
|
|
|
|
$expected = 'auth_userkey\userkey_manager_interface';
|
|
|
|
$this->assertInstanceOf($expected, $manager);
|
2016-08-16 07:50:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option iprestriction is not set.
|
|
|
|
*/
|
|
|
|
public function test_create_correct_key_if_iprestriction_is_not_set() {
|
2016-08-16 07:50:09 +00:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1';
|
|
|
|
$manager = new core_userkey_manager($this->user->id, $this->config);
|
|
|
|
$value = $manager->create_key();
|
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
|
|
|
$expectedvalue = $value;
|
|
|
|
$expecteduserid = $this->user->id;
|
|
|
|
$expectedscript = 'auth/userkey';
|
|
|
|
$expectedinstance = $this->user->id;
|
|
|
|
$expectediprestriction = null;
|
|
|
|
$expectedvaliduntil = time() + 60;
|
|
|
|
|
|
|
|
$this->assertEquals($expectedvalue, $actualkey->value);
|
|
|
|
$this->assertEquals($expecteduserid, $actualkey->userid);
|
|
|
|
$this->assertEquals($expectedscript, $actualkey->script);
|
|
|
|
$this->assertEquals($expectedinstance, $actualkey->instance);
|
|
|
|
$this->assertEquals($expectediprestriction, $actualkey->iprestriction);
|
|
|
|
$this->assertEquals($expectedvaliduntil, $actualkey->validuntil);
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option iprestriction is set to true.
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
public function test_create_correct_key_if_iprestriction_is_true() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->iprestriction = true;
|
|
|
|
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1';
|
|
|
|
$manager = new core_userkey_manager($this->user->id, $this->config);
|
|
|
|
$value = $manager->create_key();
|
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
|
|
|
$expectedvalue = $value;
|
|
|
|
$expecteduserid = $this->user->id;
|
|
|
|
$expectedscript = 'auth/userkey';
|
|
|
|
$expectedinstance = $this->user->id;
|
|
|
|
$expectediprestriction = '192.168.1.1';
|
|
|
|
$expectedvaliduntil = time() + 60;
|
|
|
|
|
|
|
|
$this->assertEquals($expectedvalue, $actualkey->value);
|
|
|
|
$this->assertEquals($expecteduserid, $actualkey->userid);
|
|
|
|
$this->assertEquals($expectedscript, $actualkey->script);
|
|
|
|
$this->assertEquals($expectedinstance, $actualkey->instance);
|
|
|
|
$this->assertEquals($expectediprestriction, $actualkey->iprestriction);
|
|
|
|
$this->assertEquals($expectedvaliduntil, $actualkey->validuntil);
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option iprestriction is set to false.
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
public function test_create_correct_key_if_iprestriction_is_false() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->iprestriction = false;
|
|
|
|
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1';
|
|
|
|
$manager = new core_userkey_manager($this->user->id, $this->config);
|
|
|
|
$value = $manager->create_key();
|
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
|
|
|
$expectedvalue = $value;
|
|
|
|
$expecteduserid = $this->user->id;
|
|
|
|
$expectedscript = 'auth/userkey';
|
|
|
|
$expectedinstance = $this->user->id;
|
|
|
|
$expectediprestriction = null;
|
|
|
|
$expectedvaliduntil = time() + 60;
|
|
|
|
|
|
|
|
$this->assertEquals($expectedvalue, $actualkey->value);
|
|
|
|
$this->assertEquals($expecteduserid, $actualkey->userid);
|
|
|
|
$this->assertEquals($expectedscript, $actualkey->script);
|
|
|
|
$this->assertEquals($expectedinstance, $actualkey->instance);
|
|
|
|
$this->assertEquals($expectediprestriction, $actualkey->iprestriction);
|
|
|
|
$this->assertEquals($expectedvaliduntil, $actualkey->validuntil);
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option iprestriction is set to a string.
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
public function test_create_correct_key_if_iprestriction_is_string() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->iprestriction = 'string';
|
|
|
|
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1';
|
|
|
|
$manager = new core_userkey_manager($this->user->id, $this->config);
|
|
|
|
$value = $manager->create_key();
|
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
|
|
|
$expectedvalue = $value;
|
|
|
|
$expecteduserid = $this->user->id;
|
|
|
|
$expectedscript = 'auth/userkey';
|
|
|
|
$expectedinstance = $this->user->id;
|
|
|
|
$expectediprestriction = '192.168.1.1';
|
|
|
|
$expectedvaliduntil = time() + 60;
|
|
|
|
|
|
|
|
$this->assertEquals($expectedvalue, $actualkey->value);
|
|
|
|
$this->assertEquals($expecteduserid, $actualkey->userid);
|
|
|
|
$this->assertEquals($expectedscript, $actualkey->script);
|
|
|
|
$this->assertEquals($expectedinstance, $actualkey->instance);
|
|
|
|
$this->assertEquals($expectediprestriction, $actualkey->iprestriction);
|
|
|
|
$this->assertEquals($expectedvaliduntil, $actualkey->validuntil);
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option keylifetime is not set.
|
|
|
|
*/
|
|
|
|
public function test_create_correct_key_if_keylifetime_is_not_set() {
|
2016-08-16 07:50:09 +00:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$manager = new core_userkey_manager($this->user->id, $this->config);
|
|
|
|
$value = $manager->create_key();
|
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
|
|
|
$expectedvalue = $value;
|
|
|
|
$expecteduserid = $this->user->id;
|
|
|
|
$expectedscript = 'auth/userkey';
|
|
|
|
$expectedinstance = $this->user->id;
|
|
|
|
$expectediprestriction = null;
|
|
|
|
$expectedvaliduntil = time() + 60;
|
|
|
|
|
|
|
|
$this->assertEquals($expectedvalue, $actualkey->value);
|
|
|
|
$this->assertEquals($expecteduserid, $actualkey->userid);
|
|
|
|
$this->assertEquals($expectedscript, $actualkey->script);
|
|
|
|
$this->assertEquals($expectedinstance, $actualkey->instance);
|
|
|
|
$this->assertEquals($expectediprestriction, $actualkey->iprestriction);
|
|
|
|
$this->assertEquals($expectedvaliduntil, $actualkey->validuntil);
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option keylifetime is set to integer.
|
|
|
|
*/
|
|
|
|
public function test_create_correct_key_if_keylifetime_is_set_to_integer() {
|
2016-08-16 07:50:09 +00:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->keylifetime = 3000;
|
|
|
|
|
|
|
|
$manager = new core_userkey_manager($this->user->id, $this->config);
|
|
|
|
$value = $manager->create_key();
|
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
|
|
|
$expectedvalue = $value;
|
|
|
|
$expecteduserid = $this->user->id;
|
|
|
|
$expectedscript = 'auth/userkey';
|
|
|
|
$expectedinstance = $this->user->id;
|
|
|
|
$expectediprestriction = null;
|
|
|
|
$expectedvaliduntil = time() + 3000;
|
|
|
|
|
|
|
|
$this->assertEquals($expectedvalue, $actualkey->value);
|
|
|
|
$this->assertEquals($expecteduserid, $actualkey->userid);
|
|
|
|
$this->assertEquals($expectedscript, $actualkey->script);
|
|
|
|
$this->assertEquals($expectedinstance, $actualkey->instance);
|
|
|
|
$this->assertEquals($expectediprestriction, $actualkey->iprestriction);
|
|
|
|
$this->assertEquals($expectedvaliduntil, $actualkey->validuntil);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that key gets created correctly if config option keylifetime is set to a string.
|
|
|
|
*/
|
|
|
|
public function test_create_correct_key_if_keylifetime_is_set_to_string() {
|
2016-08-16 07:50:09 +00:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->config->keylifetime = '3000';
|
|
|
|
|
|
|
|
$manager = new core_userkey_manager($this->user->id, $this->config);
|
|
|
|
$value = $manager->create_key();
|
|
|
|
|
|
|
|
$actualkey = $DB->get_record('user_private_key', array('userid' => $this->user->id));
|
|
|
|
|
|
|
|
$expectedvalue = $value;
|
|
|
|
$expecteduserid = $this->user->id;
|
|
|
|
$expectedscript = 'auth/userkey';
|
|
|
|
$expectedinstance = $this->user->id;
|
|
|
|
$expectediprestriction = null;
|
2016-08-18 04:44:21 +00:00
|
|
|
$expectedvaliduntil = time() + 3000;
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$this->assertEquals($expectedvalue, $actualkey->value);
|
|
|
|
$this->assertEquals($expecteduserid, $actualkey->userid);
|
|
|
|
$this->assertEquals($expectedscript, $actualkey->script);
|
|
|
|
$this->assertEquals($expectedinstance, $actualkey->instance);
|
|
|
|
$this->assertEquals($expectediprestriction, $actualkey->iprestriction);
|
|
|
|
$this->assertEquals($expectedvaliduntil, $actualkey->validuntil);
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that we can delete created key.
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
public function test_can_delete_created_key() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$manager = new core_userkey_manager($this->user->id, $this->config);
|
|
|
|
$manager->create_key();
|
|
|
|
|
|
|
|
$keys = $DB->get_records('user_private_key', array('userid' => $this->user->id));
|
|
|
|
$this->assertEquals(1, count($keys));
|
|
|
|
|
|
|
|
$manager->delete_key();
|
|
|
|
|
|
|
|
$keys = $DB->get_records('user_private_key', array('userid' => $this->user->id));
|
|
|
|
$this->assertEquals(0, count($keys));
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that we can delete all existing keys.
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
public function test_can_delete_all_existing_keys() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$manager = new core_userkey_manager($this->user->id, $this->config);
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
create_user_key('auth/userkey', $this->user->id);
|
|
|
|
create_user_key('auth/userkey', $this->user->id);
|
|
|
|
create_user_key('auth/userkey', $this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$keys = $DB->get_records('user_private_key', array('userid' => $this->user->id));
|
|
|
|
$this->assertEquals(3, count($keys));
|
|
|
|
|
|
|
|
$manager->delete_key();
|
|
|
|
|
|
|
|
$keys = $DB->get_records('user_private_key', array('userid' => $this->user->id));
|
|
|
|
$this->assertEquals(0, count($keys));
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
/**
|
|
|
|
* Test that we create only one key.
|
|
|
|
*/
|
2016-08-16 07:50:09 +00:00
|
|
|
public function test_create_only_one_key() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$manager = new core_userkey_manager($this->user->id, $this->config);
|
|
|
|
|
2016-08-17 02:16:48 +00:00
|
|
|
create_user_key('auth/userkey', $this->user->id);
|
|
|
|
create_user_key('auth/userkey', $this->user->id);
|
|
|
|
create_user_key('auth/userkey', $this->user->id);
|
2016-08-16 07:50:09 +00:00
|
|
|
|
|
|
|
$keys = $DB->get_records('user_private_key', array('userid' => $this->user->id));
|
|
|
|
$this->assertEquals(3, count($keys));
|
|
|
|
|
|
|
|
$manager->create_key();
|
|
|
|
$keys = $DB->get_records('user_private_key', array('userid' => $this->user->id));
|
|
|
|
$this->assertEquals(1, count($keys));
|
|
|
|
}
|
|
|
|
}
|