Merge pull request #80 from catalyst/fix-ci

Fix ci
This commit is contained in:
Dmitrii Metelkin 2022-08-19 10:48:37 +10:00 committed by GitHub
commit 46f9c4b8d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 142 additions and 112 deletions

View file

@ -107,7 +107,7 @@ class auth_plugin_userkey extends auth_plugin_base {
/** /**
* Redirects the user to provided URL. * Redirects the user to provided URL.
* *
* @param $url URL to redirect to. * @param string $url URL to redirect to.
* *
* @throws \moodle_exception If gets running via CLI or AJAX call. * @throws \moodle_exception If gets running via CLI or AJAX call.
*/ */
@ -134,8 +134,6 @@ class auth_plugin_userkey extends auth_plugin_base {
/** /**
* Logs a user in using userkey and redirects after. * Logs a user in using userkey and redirects after.
* *
* @TODO: refactor this method to make it easy to read.
*
* @throws \moodle_exception If something went wrong. * @throws \moodle_exception If something went wrong.
*/ */
public function user_login_userkey() { public function user_login_userkey() {
@ -157,7 +155,7 @@ class auth_plugin_userkey extends auth_plugin_base {
if (isloggedin()) { if (isloggedin()) {
require_logout(); require_logout();
} }
print_error($exception->errorcode); throw $exception;
} }
if (isloggedin()) { if (isloggedin()) {
@ -653,7 +651,7 @@ class auth_plugin_userkey extends auth_plugin_base {
$this->redirect($redirect); $this->redirect($redirect);
} else { } else {
// If logged in with different auth type, then display an error. // If logged in with different auth type, then display an error.
print_error('incorrectlogout', 'auth_userkey', $CFG->wwwroot); throw new moodle_exception('incorrectlogout', 'auth_userkey', $CFG->wwwroot);
} }
} }
} }

View file

@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace auth_userkey;
/** /**
* Key manager class. * Key manager class.
* *
@ -21,9 +23,6 @@
* @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net) * @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
namespace auth_userkey;
class core_userkey_manager implements userkey_manager_interface { class core_userkey_manager implements userkey_manager_interface {
/** /**
@ -115,17 +114,17 @@ class core_userkey_manager implements userkey_manager_interface {
); );
if (!$key = $DB->get_record('user_private_key', $options)) { if (!$key = $DB->get_record('user_private_key', $options)) {
print_error('invalidkey'); throw new \moodle_exception('invalidkey');
} }
if (!empty($key->validuntil) and $key->validuntil < time()) { if (!empty($key->validuntil) and $key->validuntil < time()) {
print_error('expiredkey'); throw new \moodle_exception('expiredkey');
} }
$this->validate_ip_address($key); $this->validate_ip_address($key);
if (!$user = $DB->get_record('user', array('id' => $key->userid))) { if (!$user = $DB->get_record('user', array('id' => $key->userid))) {
print_error('invaliduserid'); throw new \moodle_exception('invaliduserid');
} }
return $key; return $key;
} }
@ -145,7 +144,7 @@ class core_userkey_manager implements userkey_manager_interface {
$remoteaddr = getremoteaddr(null); $remoteaddr = getremoteaddr(null);
if (empty($remoteaddr)) { if (empty($remoteaddr)) {
print_error('noip', 'auth_userkey'); throw new \moodle_exception('noip', 'auth_userkey');
} }
if (address_in_subnet($remoteaddr, $key->iprestriction)) { if (address_in_subnet($remoteaddr, $key->iprestriction)) {
@ -161,6 +160,6 @@ class core_userkey_manager implements userkey_manager_interface {
} }
} }
print_error('ipmismatch', 'error', '', null, "Remote address: $remoteaddr\nKey IP: $key->iprestriction"); throw new \moodle_exception('ipmismatch', 'error', '', null, "Remote address: $remoteaddr\nKey IP: $key->iprestriction");
} }
} }

View file

@ -22,6 +22,12 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
/**
* Upgrade hook.
*
* @param string $oldversion Old version of the plugin.
* @return bool
*/
function xmldb_auth_userkey_upgrade($oldversion) { function xmldb_auth_userkey_upgrade($oldversion) {
global $DB; global $DB;

View file

@ -28,6 +28,13 @@ require_once($CFG->libdir . "/externallib.php");
require_once($CFG->dirroot . "/webservice/lib.php"); require_once($CFG->dirroot . "/webservice/lib.php");
require_once($CFG->dirroot . "/auth/userkey/auth.php"); require_once($CFG->dirroot . "/auth/userkey/auth.php");
/**
* Webservices for auth_userkey.
*
* @package auth_userkey
* @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class auth_userkey_external extends external_api { class auth_userkey_external extends external_api {
/** /**

View file

@ -25,7 +25,7 @@
require_once(dirname(__FILE__) . '/../../config.php'); require_once(dirname(__FILE__) . '/../../config.php');
if (!is_enabled_auth('userkey')) { if (!is_enabled_auth('userkey')) {
print_error(get_string('pluginisdisabled', 'auth_userkey')); throw new moodle_exception(get_string('pluginisdisabled', 'auth_userkey'));
} }
get_auth_plugin('userkey')->user_login_userkey(); get_auth_plugin('userkey')->user_login_userkey();

View file

@ -25,7 +25,7 @@
require_once(dirname(__FILE__) . '/../../config.php'); require_once(dirname(__FILE__) . '/../../config.php');
if (!is_enabled_auth('userkey')) { if (!is_enabled_auth('userkey')) {
print_error(get_string('pluginisdisabled', 'auth_userkey')); throw new moodle_exception(get_string('pluginisdisabled', 'auth_userkey'));
} }
get_auth_plugin('userkey')->user_logout_userkey(); get_auth_plugin('userkey')->user_logout_userkey();

View file

@ -185,62 +185,58 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
/** /**
* Test that auth plugin throws correct exception if default mapping field is not provided. * Test that auth plugin throws correct exception if default mapping field is not provided.
*
* @expectedException \invalid_parameter_exception
* @expectedExceptionMessage Invalid parameter value detected (Required field "email" is not set or empty.)
*/ */
public function test_throwing_exception_if_default_mapping_field_is_not_provided() { public function test_throwing_exception_if_default_mapping_field_is_not_provided() {
$user = array(); $user = array();
$this->expectException(invalid_parameter_exception::class);
$this->expectExceptionMessage('Invalid parameter value detected (Required field "email" is not set or empty.)');
$actual = $this->auth->get_login_url($user); $actual = $this->auth->get_login_url($user);
} }
/** /**
* Test that auth plugin throws correct exception if username mapping field is not provided, but set in configs. * Test that auth plugin throws correct exception if username mapping field is not provided, but set in configs.
*
* @expectedException \invalid_parameter_exception
* @expectedExceptionMessage Invalid parameter value detected (Required field "username" is not set or empty.)
*/ */
public function test_throwing_exception_if_mapping_field_username_is_not_provided() { public function test_throwing_exception_if_mapping_field_username_is_not_provided() {
$user = array(); $user = array();
set_config('mappingfield', 'username', 'auth_userkey'); set_config('mappingfield', 'username', 'auth_userkey');
$this->auth = new auth_plugin_userkey(); $this->auth = new auth_plugin_userkey();
$this->expectException(invalid_parameter_exception::class);
$this->expectExceptionMessage('Invalid parameter value detected (Required field "username" is not set or empty.)');
$actual = $this->auth->get_login_url($user); $actual = $this->auth->get_login_url($user);
} }
/** /**
* Test that auth plugin throws correct exception if idnumber mapping field is not provided, but set in configs. * Test that auth plugin throws correct exception if idnumber mapping field is not provided, but set in configs.
*
* @expectedException \invalid_parameter_exception
* @expectedExceptionMessage Invalid parameter value detected (Required field "idnumber" is not set or empty.)
*/ */
public function test_throwing_exception_if_mapping_field_idnumber_is_not_provided() { public function test_throwing_exception_if_mapping_field_idnumber_is_not_provided() {
$user = array(); $user = array();
set_config('mappingfield', 'idnumber', 'auth_userkey'); set_config('mappingfield', 'idnumber', 'auth_userkey');
$this->auth = new auth_plugin_userkey(); $this->auth = new auth_plugin_userkey();
$this->expectException(invalid_parameter_exception::class);
$this->expectExceptionMessage('Invalid parameter value detected (Required field "idnumber" is not set or empty.)');
$actual = $this->auth->get_login_url($user); $actual = $this->auth->get_login_url($user);
} }
/** /**
* Test that auth plugin throws correct exception if we trying to request not existing user. * Test that auth plugin throws correct exception if we trying to request not existing user.
*
* @expectedException \invalid_parameter_exception
* @expectedExceptionMessage Invalid parameter value detected (User is not exist)
*/ */
public function test_throwing_exception_if_user_is_not_exist() { public function test_throwing_exception_if_user_is_not_exist() {
$user = array(); $user = array();
$user['email'] = 'notexists@test.com'; $user['email'] = 'notexists@test.com';
$this->expectException(invalid_parameter_exception::class);
$this->expectExceptionMessage('Invalid parameter value detected (User is not exist)');
$actual = $this->auth->get_login_url($user); $actual = $this->auth->get_login_url($user);
} }
/** /**
* Test that auth plugin throws correct exception if we trying to request user, * Test that auth plugin throws correct exception if we trying to request user,
* but ip field is not set and iprestriction is enabled. * but ip field is not set and iprestriction is enabled.
*
* @expectedException \invalid_parameter_exception
* @expectedExceptionMessage Invalid parameter value detected (Required parameter "ip" is not set.)
*/ */
public function test_throwing_exception_if_iprestriction_is_enabled_but_ip_is_missing_in_data() { public function test_throwing_exception_if_iprestriction_is_enabled_but_ip_is_missing_in_data() {
$user = array(); $user = array();
@ -248,6 +244,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
set_config('iprestriction', true, 'auth_userkey'); set_config('iprestriction', true, 'auth_userkey');
$this->auth = new auth_plugin_userkey(); $this->auth = new auth_plugin_userkey();
$this->expectException(invalid_parameter_exception::class);
$this->expectExceptionMessage('Invalid parameter value detected (Required parameter "ip" is not set.)');
$actual = $this->auth->get_login_url($user); $actual = $this->auth->get_login_url($user);
} }
@ -349,11 +348,7 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
/** /**
* Test that we can request a key for a new user. * Test that we can request a key for a new user.
*
* @expectedException \invalid_parameter_exception
* @expectedExceptionMessage Unable to create user, missing value(s): username,firstname,lastname
*/ */
public function test_missing_data_to_create_user() { public function test_missing_data_to_create_user() {
global $CFG, $DB; global $CFG, $DB;
@ -367,13 +362,14 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
$user->email = 'username@test.com'; $user->email = 'username@test.com';
$user->ip = '192.168.1.1'; $user->ip = '192.168.1.1';
$this->expectException(invalid_parameter_exception::class);
$this->expectExceptionMessage('Unable to create user, missing value(s): username,firstname,lastname');
$this->auth->get_login_url($user); $this->auth->get_login_url($user);
} }
/** /**
* Test that when we attempt to create a new user duplicate usernames are caught. * Test that when we attempt to create a new user duplicate usernames are caught.
* @expectedException \invalid_parameter_exception
* @expectedExceptionMessage Username already exists: username
*/ */
public function test_create_refuse_duplicate_username() { public function test_create_refuse_duplicate_username() {
set_config('createuser', true, 'auth_userkey'); set_config('createuser', true, 'auth_userkey');
@ -395,14 +391,14 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
$duplicateuser = clone($originaluser); $duplicateuser = clone($originaluser);
$duplicateuser->email = 'duplicateuser@test.com'; $duplicateuser->email = 'duplicateuser@test.com';
$this->expectException(invalid_parameter_exception::class);
$this->expectExceptionMessage('Username already exists: username');
$this->auth->get_login_url($duplicateuser); $this->auth->get_login_url($duplicateuser);
} }
/** /**
* Test that when we attempt to create a new user duplicate emails are caught. * Test that when we attempt to create a new user duplicate emails are caught.
*
* @expectedException \invalid_parameter_exception
* @expectedExceptionMessage Email address already exists: username@test.com
*/ */
public function test_create_refuse_duplicate_email() { public function test_create_refuse_duplicate_email() {
set_config('createuser', true, 'auth_userkey'); set_config('createuser', true, 'auth_userkey');
@ -425,6 +421,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
$duplicateuser = clone($originaluser); $duplicateuser = clone($originaluser);
$duplicateuser->username = 'duplicateuser'; $duplicateuser->username = 'duplicateuser';
$this->expectException(invalid_parameter_exception::class);
$this->expectExceptionMessage('Email address already exists: username@test.com');
$this->auth->get_login_url($duplicateuser); $this->auth->get_login_url($duplicateuser);
} }
@ -472,9 +471,6 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
/** /**
* Test that when we attempt to update a user duplicate emails are caught. * Test that when we attempt to update a user duplicate emails are caught.
*
* @expectedException \invalid_parameter_exception
* @expectedExceptionMessage Email address already exists: trytoduplicate@test.com
*/ */
public function test_update_refuse_duplicate_email() { public function test_update_refuse_duplicate_email() {
set_config('updateuser', true, 'auth_userkey'); set_config('updateuser', true, 'auth_userkey');
@ -495,14 +491,14 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
$originaluser->city = 'brighton'; $originaluser->city = 'brighton';
$originaluser->ip = '192.168.1.1'; $originaluser->ip = '192.168.1.1';
$this->expectException(invalid_parameter_exception::class);
$this->expectExceptionMessage('Email address already exists: trytoduplicate@test.com');
$this->auth->get_login_url($originaluser); $this->auth->get_login_url($originaluser);
} }
/** /**
* Test that when we attempt to update a user duplicate usernames are caught. * Test that when we attempt to update a user duplicate usernames are caught.
*
* @expectedException \invalid_parameter_exception
* @expectedExceptionMessage Username already exists: trytoduplicate
*/ */
public function test_update_refuse_duplicate_username() { public function test_update_refuse_duplicate_username() {
set_config('updateuser', true, 'auth_userkey'); set_config('updateuser', true, 'auth_userkey');
@ -522,6 +518,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
$originaluser->city = 'brighton'; $originaluser->city = 'brighton';
$originaluser->ip = '192.168.1.1'; $originaluser->ip = '192.168.1.1';
$this->expectException(invalid_parameter_exception::class);
$this->expectExceptionMessage('Username already exists: trytoduplicate');
$this->auth->get_login_url($originaluser); $this->auth->get_login_url($originaluser);
} }
@ -693,57 +692,55 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
/** /**
* Test required parameter exception gets thrown id try to login, but key is not set. * Test required parameter exception gets thrown id try to login, but key is not set.
*
* @expectedException moodle_exception
* @expectedExceptionMessage A required parameter (key) was missing
*/ */
public function test_required_parameter_exception_thrown_if_key_not_set() { public function test_required_parameter_exception_thrown_if_key_not_set() {
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('A required parameter (key) was missing');
$this->auth->user_login_userkey(); $this->auth->user_login_userkey();
} }
/** /**
* Test that incorrect key exception gets thrown if a key is incorrect. * Test that incorrect key exception gets thrown if a key is incorrect.
*
* @expectedException moodle_exception
* @expectedExceptionMessage Incorrect key
*/ */
public function test_invalid_key_exception_thrown_if_invalid_key() { public function test_invalid_key_exception_thrown_if_invalid_key() {
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('Incorrect key');
$_POST['key'] = 'InvalidKey'; $_POST['key'] = 'InvalidKey';
$this->auth->user_login_userkey(); $this->auth->user_login_userkey();
} }
/** /**
* Test that expired key exception gets thrown if a key is expired. * Test that expired key exception gets thrown if a key is expired.
*
* @expectedException moodle_exception
* @expectedExceptionMessage Expired key
*/ */
public function test_expired_key_exception_thrown_if_expired_key() { public function test_expired_key_exception_thrown_if_expired_key() {
$this->create_user_private_key(['validuntil' => time() - 3000]); $this->create_user_private_key(['validuntil' => time() - 3000]);
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('Expired key');
$_POST['key'] = 'TestKey'; $_POST['key'] = 'TestKey';
$this->auth->user_login_userkey(); $this->auth->user_login_userkey();
} }
/** /**
* Test that IP address mismatch exception gets thrown if incorrect IP. * Test that IP address mismatch exception gets thrown if incorrect IP.
*
* @expectedException moodle_exception
* @expectedExceptionMessage Client IP address mismatch
*/ */
public function test_ipmismatch_exception_thrown_if_ip_is_incorrect() { public function test_ipmismatch_exception_thrown_if_ip_is_incorrect() {
$this->create_user_private_key(['iprestriction' => '192.168.1.1']); $this->create_user_private_key(['iprestriction' => '192.168.1.1']);
$_POST['key'] = 'TestKey'; $_POST['key'] = 'TestKey';
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2'; $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('Client IP address mismatch');
$this->auth->user_login_userkey(); $this->auth->user_login_userkey();
} }
/** /**
* Test that IP address mismatch exception gets thrown if incorrect IP and outside whitelist. * Test that IP address mismatch exception gets thrown if incorrect IP and outside whitelist.
*
* @expectedException moodle_exception
* @expectedExceptionMessage Client IP address mismatch
*/ */
public function test_ipmismatch_exception_thrown_if_ip_is_outside_whitelist() { public function test_ipmismatch_exception_thrown_if_ip_is_outside_whitelist() {
set_config('ipwhitelist', '10.0.0.0/8;172.16.0.0/12;192.168.0.0/16', 'auth_userkey'); set_config('ipwhitelist', '10.0.0.0/8;172.16.0.0/12;192.168.0.0/16', 'auth_userkey');
@ -751,14 +748,15 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
$_POST['key'] = 'TestKey'; $_POST['key'] = 'TestKey';
$_SERVER['HTTP_CLIENT_IP'] = '192.161.1.2'; $_SERVER['HTTP_CLIENT_IP'] = '192.161.1.2';
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('Client IP address mismatch');
$this->auth->user_login_userkey(); $this->auth->user_login_userkey();
} }
/** /**
* Test that IP address mismatch exception gets thrown if user id is incorrect. * Test that IP address mismatch exception gets thrown if user id is incorrect.
*
* @expectedException moodle_exception
* @expectedExceptionMessageRegExp /Invalid user id/i
*/ */
public function test_invalid_user_exception_thrown_if_user_is_invalid() { public function test_invalid_user_exception_thrown_if_user_is_invalid() {
$this->create_user_private_key([ $this->create_user_private_key([
@ -769,6 +767,10 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
$_POST['key'] = 'TestKey'; $_POST['key'] = 'TestKey';
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1'; $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1';
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('Invalid user');
$this->auth->user_login_userkey(); $this->auth->user_login_userkey();
} }
@ -797,9 +799,6 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
/** /**
* Test that a user logs in and gets redirected correctly. * Test that a user logs in and gets redirected correctly.
*
* @expectedException moodle_exception
* @expectedExceptionMessage Unsupported redirect to http://www.example.com/moodle detected, execution terminated.
*/ */
public function test_that_user_logged_in_and_redirected() { public function test_that_user_logged_in_and_redirected() {
global $CFG; global $CFG;
@ -807,6 +806,10 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
$this->create_user_private_key(); $this->create_user_private_key();
$CFG->wwwroot = 'http://www.example.com/moodle'; $CFG->wwwroot = 'http://www.example.com/moodle';
$_POST['key'] = 'TestKey'; $_POST['key'] = 'TestKey';
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('Unsupported redirect to http://www.example.com/moodle detected, execution terminated');
@$this->auth->user_login_userkey(); @$this->auth->user_login_userkey();
} }
@ -832,25 +835,21 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
/** /**
* Test that a user gets redirected to internal wantsurl URL successful log in. * Test that a user gets redirected to internal wantsurl URL successful log in.
*
* @expectedException moodle_exception
* @expectedExceptionMessage Unsupported redirect to /course/index.php?id=12&key=134 detected, execution terminated.
*/ */
public function test_that_user_gets_redirected_to_internal_wantsurl() { public function test_that_user_gets_redirected_to_internal_wantsurl() {
$this->create_user_private_key(); $this->create_user_private_key();
$_POST['key'] = 'TestKey'; $_POST['key'] = 'TestKey';
$_POST['wantsurl'] = '/course/index.php?id=12&key=134'; $_POST['wantsurl'] = '/course/index.php?id=12&key=134';
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('Unsupported redirect to /course/index.php?id=12&key=134 detected, execution terminated');
// Using @ is the only way to test this. Thanks moodle! // Using @ is the only way to test this. Thanks moodle!
@$this->auth->user_login_userkey(); @$this->auth->user_login_userkey();
} }
/** /**
* Test that a user gets redirected to external wantsurl URL successful log in. * Test that a user gets redirected to external wantsurl URL successful log in.
*
* @expectedException moodle_exception
* @expectedExceptionMessage Unsupported redirect to http://test.com/course/index.php?id=12&key=134 detected,
* execution terminated.
*/ */
public function test_that_user_gets_redirected_to_external_wantsurl() { public function test_that_user_gets_redirected_to_external_wantsurl() {
$this->create_user_private_key(); $this->create_user_private_key();
@ -858,15 +857,15 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
$_POST['key'] = 'TestKey'; $_POST['key'] = 'TestKey';
$_POST['wantsurl'] = 'http://test.com/course/index.php?id=12&key=134'; $_POST['wantsurl'] = 'http://test.com/course/index.php?id=12&key=134';
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('Unsupported redirect to http://test.com/course/index.php?id=12&key=134 detected, execution terminated');
// Using @ is the only way to test this. Thanks moodle! // Using @ is the only way to test this. Thanks moodle!
@$this->auth->user_login_userkey(); @$this->auth->user_login_userkey();
} }
/** /**
* Test that login hook redirects a user if skipsso not set and ssourl is set. * Test that login hook redirects a user if skipsso not set and ssourl is set.
*
* @expectedException moodle_exception
* @expectedExceptionMessage Unsupported redirect to http://google.com detected, execution terminated.
*/ */
public function test_loginpage_hook_redirects_if_skipsso_not_set_and_ssourl_set() { public function test_loginpage_hook_redirects_if_skipsso_not_set_and_ssourl_set() {
global $SESSION; global $SESSION;
@ -875,6 +874,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
set_config('ssourl', 'http://google.com', 'auth_userkey'); set_config('ssourl', 'http://google.com', 'auth_userkey');
$this->auth = new auth_plugin_userkey(); $this->auth = new auth_plugin_userkey();
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('Unsupported redirect to http://google.com detected, execution terminated.');
$this->auth->loginpage_hook(); $this->auth->loginpage_hook();
} }
@ -906,9 +908,6 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
/** /**
* Test that pre login hook redirects a user if skipsso not set and ssourl is set. * Test that pre login hook redirects a user if skipsso not set and ssourl is set.
*
* @expectedException moodle_exception
* @expectedExceptionMessage Unsupported redirect to http://google.com detected, execution terminated.
*/ */
public function test_pre_loginpage_hook_redirects_if_skipsso_not_set_and_ssourl_set() { public function test_pre_loginpage_hook_redirects_if_skipsso_not_set_and_ssourl_set() {
global $SESSION; global $SESSION;
@ -917,6 +916,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
set_config('ssourl', 'http://google.com', 'auth_userkey'); set_config('ssourl', 'http://google.com', 'auth_userkey');
$this->auth = new auth_plugin_userkey(); $this->auth = new auth_plugin_userkey();
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('Unsupported redirect to http://google.com detected, execution terminated.');
$this->auth->pre_loginpage_hook(); $this->auth->pre_loginpage_hook();
} }
@ -1021,23 +1023,23 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
/** /**
* Test when try to logout, but required return is not set. * Test when try to logout, but required return is not set.
*
* @expectedException moodle_exception
* @expectedExceptionMessage A required parameter (return) was missing
*/ */
public function test_user_logout_userkey_when_required_return_not_set() { public function test_user_logout_userkey_when_required_return_not_set() {
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('A required parameter (return) was missing');
$this->auth->user_logout_userkey(); $this->auth->user_logout_userkey();
} }
/** /**
* Test when try to logout, but user is not logged in. * Test when try to logout, but user is not logged in.
*
* @expectedException moodle_exception
* @expectedExceptionMessage Unsupported redirect to http://google.com detected, execution terminated.
*/ */
public function test_user_logout_userkey_when_user_is_not_logged_in() { public function test_user_logout_userkey_when_user_is_not_logged_in() {
$_POST['return'] = 'http://google.com'; $_POST['return'] = 'http://google.com';
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('Unsupported redirect to http://google.com detected, execution terminated.');
$this->auth->user_logout_userkey(); $this->auth->user_logout_userkey();
} }
@ -1064,12 +1066,13 @@ class auth_plugin_userkey_testcase extends advanced_testcase {
/** /**
* Test when try to logout, but user logged in with different auth type. * Test when try to logout, but user logged in with different auth type.
*
* @expectedException moodle_exception
* @expectedExceptionMessage A required parameter (return) was missing
*/ */
public function test_user_logout_userkey_when_user_logged_in_but_return_not_set() { public function test_user_logout_userkey_when_user_logged_in_but_return_not_set() {
$this->setUser($this->user); $this->setUser($this->user);
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('A required parameter (return) was missing');
$this->auth->user_logout_userkey(); $this->auth->user_logout_userkey();
} }

View file

@ -154,9 +154,6 @@ class core_userkey_manager_testcase extends advanced_testcase {
/** /**
* Test that IP address mismatch exception gets thrown if incorrect IP and outside whitelist. * Test that IP address mismatch exception gets thrown if incorrect IP and outside whitelist.
*
* @expectedException moodle_exception
* @expectedExceptionMessage Client IP address mismatch
*/ */
public function test_exception_if_ip_is_outside_whitelist() { public function test_exception_if_ip_is_outside_whitelist() {
global $DB; global $DB;
@ -169,6 +166,9 @@ class core_userkey_manager_testcase extends advanced_testcase {
$_SERVER['HTTP_CLIENT_IP'] = '193.168.1.2'; $_SERVER['HTTP_CLIENT_IP'] = '193.168.1.2';
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage('Client IP address mismatch');
$manager->validate_key($value); $manager->validate_key($value);
} }

View file

@ -21,7 +21,6 @@
* @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net) * @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class auth_userkey_externallib_testcase extends advanced_testcase { class auth_userkey_externallib_testcase extends advanced_testcase {
/** /**
* User object. * User object.
@ -50,9 +49,6 @@ class auth_userkey_externallib_testcase extends advanced_testcase {
/** /**
* Test call with incorrect required parameter. * Test call with incorrect required parameter.
*
* @expectedException webservice_access_exception
* @expectedExceptionMessage Access control exception (The userkey authentication plugin is disabled.)
*/ */
public function test_throwing_plugin_disabled_exception() { public function test_throwing_plugin_disabled_exception() {
$this->setAdminUser(); $this->setAdminUser();
@ -60,6 +56,10 @@ class auth_userkey_externallib_testcase extends advanced_testcase {
$params = array( $params = array(
'bla' => 'exists@test.com', 'bla' => 'exists@test.com',
); );
$this->expectException(webservice_access_exception::class);
$this->expectExceptionMessage('Access control exception (The userkey authentication plugin is disabled.)');
// Simulate the web service server. // Simulate the web service server.
$result = auth_userkey_external::request_login_url($params); $result = auth_userkey_external::request_login_url($params);
$result = external_api::clean_returnvalue(auth_userkey_external::request_login_url_returns(), $result); $result = external_api::clean_returnvalue(auth_userkey_external::request_login_url_returns(), $result);
@ -146,9 +146,6 @@ class auth_userkey_externallib_testcase extends advanced_testcase {
/** /**
* Test call with missing email required parameter. * Test call with missing email required parameter.
*
* @expectedException invalid_parameter_exception
* @expectedExceptionMessage Invalid parameter value detected (Required field "email" is not set or empty.)
*/ */
public function test_exception_thrown_if_required_parameter_email_is_not_set() { public function test_exception_thrown_if_required_parameter_email_is_not_set() {
global $CFG; global $CFG;
@ -160,14 +157,14 @@ class auth_userkey_externallib_testcase extends advanced_testcase {
'bla' => 'exists@test.com', 'bla' => 'exists@test.com',
); );
$this->expectException(invalid_parameter_exception::class);
$this->expectExceptionMessage('Invalid parameter value detected (Required field "email" is not set or empty.)');
auth_userkey_external::request_login_url($params); auth_userkey_external::request_login_url($params);
} }
/** /**
* Test call with missing ip required parameter. * Test call with missing ip required parameter.
*
* @expectedException invalid_parameter_exception
* @expectedExceptionMessage Invalid parameter value detected (Required parameter "ip" is not set.)
*/ */
public function test_exception_thrown_if_required_parameter_op_is_not_set() { public function test_exception_thrown_if_required_parameter_op_is_not_set() {
global $CFG; global $CFG;
@ -181,14 +178,14 @@ class auth_userkey_externallib_testcase extends advanced_testcase {
'email' => 'exists@test.com', 'email' => 'exists@test.com',
); );
$this->expectException(invalid_parameter_exception::class);
$this->expectExceptionMessage('Invalid parameter value detected (Required parameter "ip" is not set.)');
auth_userkey_external::request_login_url($params); auth_userkey_external::request_login_url($params);
} }
/** /**
* Test request for a user who is not exist. * Test request for a user who is not exist.
*
* @expectedException invalid_parameter_exception
* @expectedExceptionMessage Invalid parameter value detected (User is not exist)
*/ */
public function test_request_not_existing_user() { public function test_request_not_existing_user() {
global $CFG; global $CFG;
@ -200,6 +197,9 @@ class auth_userkey_externallib_testcase extends advanced_testcase {
'email' => 'notexists@test.com', 'email' => 'notexists@test.com',
); );
$this->expectException(invalid_parameter_exception::class);
$this->expectExceptionMessage('Invalid parameter value detected (User is not exist)');
// Simulate the web service server. // Simulate the web service server.
$result = auth_userkey_external::request_login_url($params); $result = auth_userkey_external::request_login_url($params);
$result = external_api::clean_returnvalue(auth_userkey_external::request_login_url_returns(), $result); $result = external_api::clean_returnvalue(auth_userkey_external::request_login_url_returns(), $result);
@ -207,9 +207,6 @@ class auth_userkey_externallib_testcase extends advanced_testcase {
/** /**
* Test that permission exception gets thrown if user doesn't have required permissions. * Test that permission exception gets thrown if user doesn't have required permissions.
*
* @expectedException required_capability_exception
* @expectedExceptionMessage Sorry, but you do not currently have permissions to do that (Generate login user key)
*/ */
public function test_throwing_of_permission_exception() { public function test_throwing_of_permission_exception() {
global $CFG; global $CFG;
@ -221,6 +218,9 @@ class auth_userkey_externallib_testcase extends advanced_testcase {
'email' => 'notexists@test.com', 'email' => 'notexists@test.com',
); );
$this->expectException(required_capability_exception::class);
$this->expectExceptionMessage('Sorry, but you do not currently have permissions to do that (Generate login user key)');
// Simulate the web service server. // Simulate the web service server.
$result = auth_userkey_external::request_login_url($params); $result = auth_userkey_external::request_login_url($params);
$result = external_api::clean_returnvalue(auth_userkey_external::request_login_url_returns(), $result); $result = external_api::clean_returnvalue(auth_userkey_external::request_login_url_returns(), $result);

View file

@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace auth_userkey;
/** /**
* Fake userkey manager for testing. * Fake userkey manager for testing.
* *
@ -21,20 +23,35 @@
* @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net) * @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
namespace auth_userkey;
class fake_userkey_manager implements userkey_manager_interface { class fake_userkey_manager implements userkey_manager_interface {
/**
* Create key.
*
* @param int $userid user ID.
* @param null $allowedips Allowed IPs.
*
* @return string
*/
public function create_key($userid, $allowedips = null) { public function create_key($userid, $allowedips = null) {
return 'FaKeKeyFoRtEsTiNg'; return 'FaKeKeyFoRtEsTiNg';
} }
/**
* Delete keys for a user.
*
* @param int $userid User ID to delete keys for.
*/
public function delete_keys($userid) { public function delete_keys($userid) {
// TODO: Implement delete_keys() method.
} }
/**
* Validate provided key.
*
* @param string $keyvalue Key to validate.
*
* @return object|void
*/
public function validate_key($keyvalue) { public function validate_key($keyvalue) {
// TODO: Implement validate_key() method.
} }
} }