From 0ef19c07996b30137c925f45f7854003e7db3f16 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 8 May 2020 14:51:39 +1000 Subject: [PATCH 01/12] Add Catalyst to Readme --- README.md | 22 +++++++++++++++++++++- pix/catalyst-logo.png | Bin 0 -> 5294 bytes 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 pix/catalyst-logo.png diff --git a/README.md b/README.md index 5b26a1d..35586bd 100644 --- a/README.md +++ b/README.md @@ -163,4 +163,24 @@ echo getloginurl('barrywhite@googlemail.com', 'barry', 'white', 'barrywhite', 2, TODO: ----- -1. Implement logout webservice to be able to call it from external application. \ No newline at end of file +1. Implement logout webservice to be able to call it from external application. + + +# Crafted by Catalyst IT + +This plugin was developed by Catalyst IT Australia: + +https://www.catalyst-au.net/ + +![Catalyst IT](/pix/catalyst-logo.png?raw=true) + +# Contributing and Support + +Issues, and pull requests using github are welcome and encouraged! + +https://github.com/catalyst/moodle-tool_webanalytics/issues + +If you would like commercial support or would like to sponsor additional improvements +to this plugin please contact us: + +https://www.catalyst-au.net/contact-us diff --git a/pix/catalyst-logo.png b/pix/catalyst-logo.png new file mode 100644 index 0000000000000000000000000000000000000000..c7d1a50f00a9436d32d89efda5b8988f058b3020 GIT binary patch literal 5294 zcmZ9QcRX9~`~UMUZEbHwt)kT!rS_&qjhMB!+IyyAMiZ2l+N&sP7O~YHDM|^UsIlc=LCl_Sn;#9@vqoY|t$QyE^g%iZO|-sffb%*d8gBZ?QEmxioWe zZBoC&BBE}_qI<~oj>j9F(25VOU@ht7%XdGWJ1iaFS=99;ozg-mq>v`MZL)w_r2X8! z)Qb6cWwQK@k$X^oQjeaQ=(0g7g*#(NW9M7Zd#4M#vqY(ff`W!XxRnI5IN+4x3>-AY zfCHEX1iFj;e@yu}1JcTW-_i8R=zZ0oc&N@Ntr$j}UBt98UoJ|ovR)p|E<)NpZlFJn zl14=;ra6f)!xH;;$Iso5h^;^SS6}F%#MiL(TJ_n}{T8}ub$?x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(4*hhZEzaQ@5Ei89LMAsA9q#?aamlvNoZ_P#9tgT&!9K0-~ zyx`p2f>A-@io1@_4*R5)VX_b>7lIr#7`})kD(%fJUFOYFQp6RU>pad)&CloPMqF&8 z7fn4ZeZsayJM%AoNq66CESoj8y@-mAZb?c)<-@JggcCR$n5ypFcrPj|nIG#AI-1Xz z*TW|5Gt{t$bY77T<4nB32DkL7h|dguINE;bw6mC_U+_I!*XwL1y0ny=ZjmFKQlr-) z(!^ zyZzn5Yz@*f5SP<^txqN;A6w1u0=VuTWAFUEb@!Fh5>Ws>w1&^Y-`4r)s`-JwEF{s~ zyr~LyaF+9|-9qto>>D!Q9{hOtD z9c61?`uT#AcH-R#6=VG%cvnvMg0x4p8dw6K+r%F@g^`8&{aY} mz;HnC=E?|+@;_6G?j_~eB)MBZSOjSE05)|!wOSSX$o~TnXEQSZ literal 0 HcmV?d00001 From 37ad867c1a2b476f3fc7757a7bd26f25276943fe Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 8 May 2020 15:14:59 +1000 Subject: [PATCH 02/12] Update Travis --- .travis.yml | 87 ++++++++++++++++++++++++----------------------------- 1 file changed, 40 insertions(+), 47 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1763903..58bffc3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,68 +1,61 @@ language: php -addons: - firefox: "47.0.1" - postgresql: "9.4" +notifications: + email: + recipients: + - dmitriim@catalyst-au.net + +sudo: false cache: directories: - $HOME/.composer/cache - - $HOME/.npm -dist: trusty +addons: + postgresql: "9.6" + +php: + - 7.1 + - 7.2 + - 7.3 + +env: + global: + - DB=pgsql + matrix: + - MOODLE_BRANCH=MOODLE_33_STABLE + - MOODLE_BRANCH=MOODLE_34_STABLE + - MOODLE_BRANCH=MOODLE_35_STABLE + - MOODLE_BRANCH=MOODLE_36_STABLE + - MOODLE_BRANCH=MOODLE_37_STABLE + - MOODLE_BRANCH=MOODLE_38_STABLE + - MOODLE_BRANCH=master matrix: - include: - - php: 7.0 - env: DB=mysqli MOODLE_BRANCH=MOODLE_33_STABLE - - php: 7.0 - env: DB=pgsql MOODLE_BRANCH=MOODLE_33_STABLE - - php: 7.0 - env: DB=mysqli MOODLE_BRANCH=MOODLE_34_STABLE - - php: 7.0 - env: DB=pgsql MOODLE_BRANCH=MOODLE_34_STABLE - - php: 7.1 - env: DB=mysqli MOODLE_BRANCH=MOODLE_33_STABLE - - php: 7.1 - env: DB=pgsql MOODLE_BRANCH=MOODLE_33_STABLE - - php: 7.1 - env: DB=mysqli MOODLE_BRANCH=MOODLE_34_STABLE - - php: 7.1 - env: DB=pgsql MOODLE_BRANCH=MOODLE_34_STABLE - - php: 7.1 - env: DB=mysqli MOODLE_BRANCH=MOODLE_35_STABLE - - php: 7.1 - env: DB=pgsql MOODLE_BRANCH=MOODLE_35_STABLE - - php: 7.1 - env: DB=mysqli MOODLE_BRANCH=MOODLE_36_STABLE - - php: 7.1 - env: DB=pgsql MOODLE_BRANCH=MOODLE_36_STABLE - - php: 7.2 - env: DB=mysqli MOODLE_BRANCH=MOODLE_37_STABLE - - php: 7.2 - env: DB=pgsql MOODLE_BRANCH=MOODLE_37_STABLE - - php: 7.2 - env: DB=mysqli MOODLE_BRANCH=master - - php: 7.2 - env: DB=pgsql MOODLE_BRANCH=master + exclude: + - php: 7.3 + env: MOODLE_BRANCH=MOODLE_33_STABLE + - php: 7.3 + env: MOODLE_BRANCH=MOODLE_34_STABLE + - php: 7.3 + env: MOODLE_BRANCH=MOODLE_35_STABLE before_install: - - phpenv config-rm xdebug.ini - - nvm install 8.9 - - nvm use 8.9 - cd ../.. - - composer create-project -n --no-dev --prefer-dist moodlerooms/moodle-plugin-ci ci ^2 + - composer selfupdate + - composer create-project -n --no-dev moodlerooms/moodle-plugin-ci ci ^1 - export PATH="$(cd ci/bin; pwd):$(cd ci/vendor/bin; pwd):$PATH" install: - - moodle-plugin-ci install + - moodle-plugin-ci install -vvv script: - moodle-plugin-ci phplint + - moodle-plugin-ci phpcpd + - moodle-plugin-ci phpmd - moodle-plugin-ci codechecker - - moodle-plugin-ci validate - - moodle-plugin-ci savepoints - - moodle-plugin-ci mustache - - moodle-plugin-ci grunt + - moodle-plugin-ci csslint + - moodle-plugin-ci shifter + - moodle-plugin-ci jshint - moodle-plugin-ci phpunit - moodle-plugin-ci behat From 14cfe78b53c6320220283831139a17fa383a96b5 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 8 May 2020 15:21:14 +1000 Subject: [PATCH 03/12] Fix issues link in Readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 35586bd..ab516a6 100644 --- a/README.md +++ b/README.md @@ -178,7 +178,7 @@ https://www.catalyst-au.net/ Issues, and pull requests using github are welcome and encouraged! -https://github.com/catalyst/moodle-tool_webanalytics/issues +https://github.com/catalyst/moodle-auth_userkey/issues If you would like commercial support or would like to sponsor additional improvements to this plugin please contact us: From 6ba819fd6ea69d2ccd0eed97919907c1ce0241fd Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 8 May 2020 15:22:02 +1000 Subject: [PATCH 04/12] Add privacy API --- classes/privacy/provider.php | 53 ++++++++++++++++++++++++++++++++++++ lang/en/auth_userkey.php | 1 + 2 files changed, 54 insertions(+) create mode 100644 classes/privacy/provider.php diff --git a/classes/privacy/provider.php b/classes/privacy/provider.php new file mode 100644 index 0000000..d969cfe --- /dev/null +++ b/classes/privacy/provider.php @@ -0,0 +1,53 @@ +. + +/** + * Privacy provider. + * + * @package auth_userkey + * @author Dmitrii Metelkin (dmitriim@catalyst-au.net) + * @copyright 2020 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace auth_userkey\privacy; + +defined('MOODLE_INTERNAL') || die; + +use core_privacy\local\metadata\null_provider; +use core_privacy\local\legacy_polyfill; + +/** + * Privacy provider. + * + * @copyright 2020 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class provider implements null_provider { + + use legacy_polyfill; + + /** + * Get the language string identifier with the component's language + * file to explain why this plugin stores no data. + * + * @return string + */ + public static function _get_reason() { + return 'privacy:metadata'; + } + +} diff --git a/lang/en/auth_userkey.php b/lang/en/auth_userkey.php index d916b6b..27f4ecf 100644 --- a/lang/en/auth_userkey.php +++ b/lang/en/auth_userkey.php @@ -52,3 +52,4 @@ $string['ssourl'] = 'URL of SSO host'; $string['ssourl_desc'] = 'URL of the SSO host to redirect users to. If defined users will be redirected here on login instead of the Moodle Login page'; $string['redirecterrordetected'] = 'Unsupported redirect to {$a} detected, execution terminated.'; $string['noip'] = 'Unable to fetch IP address of client.'; +$string['privacy:metadata'] = 'User key authentication plugin does not store any personal data.'; From 78bf6e94cd5cd7ca1b2653fa2edde36c17cbe14f Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 8 May 2020 15:25:09 +1000 Subject: [PATCH 05/12] Move php unit tests to a folder --- tests/{ => phpunit}/auth_plugin_test.php | 2 +- tests/{ => phpunit}/core_userkey_manager_test.php | 0 tests/{ => phpunit}/externallib_test.php | 0 tests/{ => phpunit}/fake_userkey_manager.php | 0 4 files changed, 1 insertion(+), 1 deletion(-) rename tests/{ => phpunit}/auth_plugin_test.php (99%) rename tests/{ => phpunit}/core_userkey_manager_test.php (100%) rename tests/{ => phpunit}/externallib_test.php (100%) rename tests/{ => phpunit}/fake_userkey_manager.php (100%) diff --git a/tests/auth_plugin_test.php b/tests/phpunit/auth_plugin_test.php similarity index 99% rename from tests/auth_plugin_test.php rename to tests/phpunit/auth_plugin_test.php index 98e9606..7b44476 100644 --- a/tests/auth_plugin_test.php +++ b/tests/phpunit/auth_plugin_test.php @@ -50,7 +50,7 @@ class auth_plugin_userkey_testcase extends advanced_testcase { global $CFG; require_once($CFG->libdir . "/externallib.php"); - require_once($CFG->dirroot . '/auth/userkey/tests/fake_userkey_manager.php'); + require_once($CFG->dirroot . '/auth/userkey/tests/phpunit/fake_userkey_manager.php'); require_once($CFG->dirroot . '/auth/userkey/auth.php'); require_once($CFG->dirroot . '/user/lib.php'); diff --git a/tests/core_userkey_manager_test.php b/tests/phpunit/core_userkey_manager_test.php similarity index 100% rename from tests/core_userkey_manager_test.php rename to tests/phpunit/core_userkey_manager_test.php diff --git a/tests/externallib_test.php b/tests/phpunit/externallib_test.php similarity index 100% rename from tests/externallib_test.php rename to tests/phpunit/externallib_test.php diff --git a/tests/fake_userkey_manager.php b/tests/phpunit/fake_userkey_manager.php similarity index 100% rename from tests/fake_userkey_manager.php rename to tests/phpunit/fake_userkey_manager.php From 9e9df72f523b36d937bfeb7439c2058ea167a21b Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 8 May 2020 17:20:28 +1000 Subject: [PATCH 06/12] Remove code duplication in tests --- tests/phpunit/auth_plugin_test.php | 210 ++++++++++------------------- 1 file changed, 71 insertions(+), 139 deletions(-) diff --git a/tests/phpunit/auth_plugin_test.php b/tests/phpunit/auth_plugin_test.php index 7b44476..e00b097 100644 --- a/tests/phpunit/auth_plugin_test.php +++ b/tests/phpunit/auth_plugin_test.php @@ -56,8 +56,43 @@ class auth_plugin_userkey_testcase extends advanced_testcase { $this->auth = new auth_plugin_userkey(); $this->user = self::getDataGenerator()->create_user(); + } - $this->resetAfterTest(); + /** + * A helper function to create TestKey. + * + * @param array $record Key record. + */ + protected function create_user_private_key(array $record = []) { + global $DB; + + $record = (object)$record; + + if (!isset($record->value)) { + $record->value = 'TestKey'; + } + + if (!isset($record->userid)) { + $record->userid = $this->user->id; + } + + if (!isset($record->userid)) { + $record->instance = $this->user->id; + } + + if (!isset($record->iprestriction)) { + $record->iprestriction = null; + } + if (!isset($record->validuntil)) { + $record->validuntil = time() + 300; + } + if (!isset($record->timecreated)) { + $record->timecreated = time(); + } + + $record->script = 'auth/userkey'; + + $DB->insert_record('user_private_key', $record); } /** @@ -682,19 +717,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase { * @expectedExceptionMessage Expired key */ public function test_expired_key_exception_thrown_if_expired_key() { - global $DB; + $this->create_user_private_key(['validuntil' => time() - 3000]); - $key = new stdClass(); - $key->value = 'ExpiredKey'; - $key->script = 'auth/userkey'; - $key->userid = $this->user->id; - $key->instance = $this->user->id; - $key->iprestriction = null; - $key->validuntil = time() - 3000; - $key->timecreated = time(); - $DB->insert_record('user_private_key', $key); - - $_POST['key'] = 'ExpiredKey'; + $_POST['key'] = 'TestKey'; $this->auth->user_login_userkey(); } @@ -705,19 +730,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase { * @expectedExceptionMessage Client IP address mismatch */ public function test_ipmismatch_exception_thrown_if_ip_is_incorrect() { - global $DB; + $this->create_user_private_key(['iprestriction' => '192.168.1.1']); - $key = new stdClass(); - $key->value = 'IpmismatchKey'; - $key->script = 'auth/userkey'; - $key->userid = $this->user->id; - $key->instance = $this->user->id; - $key->iprestriction = '192.168.1.1'; - $key->validuntil = time() + 300; - $key->timecreated = time(); - $DB->insert_record('user_private_key', $key); - - $_POST['key'] = 'IpmismatchKey'; + $_POST['key'] = 'TestKey'; $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2'; $this->auth->user_login_userkey(); } @@ -729,21 +744,10 @@ class auth_plugin_userkey_testcase extends advanced_testcase { * @expectedExceptionMessage Client IP address mismatch */ public function test_ipmismatch_exception_thrown_if_ip_is_outside_whitelist() { - global $DB; - set_config('ipwhitelist', '10.0.0.0/8;172.16.0.0/12;192.168.0.0/16', 'auth_userkey'); + $this->create_user_private_key(['iprestriction' => '192.161.1.1']); - $key = new stdClass(); - $key->value = 'IpmismatchKey'; - $key->script = 'auth/userkey'; - $key->userid = $this->user->id; - $key->instance = $this->user->id; - $key->iprestriction = '192.161.1.1'; - $key->validuntil = time() + 300; - $key->timecreated = time(); - $DB->insert_record('user_private_key', $key); - - $_POST['key'] = 'IpmismatchKey'; + $_POST['key'] = 'TestKey'; $_SERVER['HTTP_CLIENT_IP'] = '192.161.1.2'; $this->auth->user_login_userkey(); } @@ -755,19 +759,13 @@ class auth_plugin_userkey_testcase extends advanced_testcase { * @expectedExceptionMessageRegExp /Invalid user id/i */ public function test_invalid_user_exception_thrown_if_user_is_invalid() { - global $DB; + $this->create_user_private_key([ + 'userid' => 777, + 'instance' => 777, + 'iprestriction' => '192.168.1.1', + ]); - $key = new stdClass(); - $key->value = 'InvalidUser'; - $key->script = 'auth/userkey'; - $key->userid = 777; - $key->instance = 777; - $key->iprestriction = '192.168.1.1'; - $key->validuntil = time() + 300; - $key->timecreated = time(); - $DB->insert_record('user_private_key', $key); - - $_POST['key'] = 'InvalidUser'; + $_POST['key'] = 'TestKey'; $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1'; $this->auth->user_login_userkey(); } @@ -778,15 +776,10 @@ class auth_plugin_userkey_testcase extends advanced_testcase { public function test_that_key_gets_removed_after_user_logged_in() { global $DB; - $key = new stdClass(); - $key->value = 'RemoveKey'; - $key->script = 'auth/userkey'; - $key->userid = $this->user->id; - $key->instance = $this->user->id; - $key->iprestriction = '192.168.1.1'; - $key->validuntil = time() + 300; - $key->timecreated = time(); - $DB->insert_record('user_private_key', $key); + $this->create_user_private_key([ + 'value' => 'RemoveKey', + 'iprestriction' => '192.168.1.1', + ]); $_POST['key'] = 'RemoveKey'; $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1'; @@ -807,20 +800,11 @@ class auth_plugin_userkey_testcase extends advanced_testcase { * @expectedExceptionMessage Unsupported redirect to http://www.example.com/moodle detected, execution terminated. */ public function test_that_user_logged_in_and_redirected() { - global $DB, $CFG; - - $key = new stdClass(); - $key->value = 'UserLogin'; - $key->script = 'auth/userkey'; - $key->userid = $this->user->id; - $key->instance = $this->user->id; - $key->iprestriction = null; - $key->validuntil = time() + 300; - $key->timecreated = time(); - $DB->insert_record('user_private_key', $key); + global $CFG; + $this->create_user_private_key(); $CFG->wwwroot = 'http://www.example.com/moodle'; - $_POST['key'] = 'UserLogin'; + $_POST['key'] = 'TestKey'; @$this->auth->user_login_userkey(); } @@ -828,19 +812,11 @@ class auth_plugin_userkey_testcase extends advanced_testcase { * Test that a user logs in correctly. */ public function test_that_user_logged_in_correctly() { - global $DB, $USER, $SESSION; + global $USER, $SESSION; - $key = new stdClass(); - $key->value = 'UserLogin'; - $key->script = 'auth/userkey'; - $key->userid = $this->user->id; - $key->instance = $this->user->id; - $key->iprestriction = null; - $key->validuntil = time() + 300; - $key->timecreated = time(); - $DB->insert_record('user_private_key', $key); + $this->create_user_private_key(); - $_POST['key'] = 'UserLogin'; + $_POST['key'] = 'TestKey'; try { // Using @ is the only way to test this. Thanks moodle! @@ -859,19 +835,8 @@ class auth_plugin_userkey_testcase extends advanced_testcase { * @expectedExceptionMessage Unsupported redirect to /course/index.php?id=12&key=134 detected, execution terminated. */ public function test_that_user_gets_redirected_to_internal_wantsurl() { - global $DB; - - $key = new stdClass(); - $key->value = 'WantsUrl'; - $key->script = 'auth/userkey'; - $key->userid = $this->user->id; - $key->instance = $this->user->id; - $key->iprestriction = null; - $key->validuntil = time() + 300; - $key->timecreated = time(); - $DB->insert_record('user_private_key', $key); - - $_POST['key'] = 'WantsUrl'; + $this->create_user_private_key(); + $_POST['key'] = 'TestKey'; $_POST['wantsurl'] = '/course/index.php?id=12&key=134'; // Using @ is the only way to test this. Thanks moodle! @@ -886,19 +851,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase { * execution terminated. */ public function test_that_user_gets_redirected_to_external_wantsurl() { - global $DB; + $this->create_user_private_key(); - $key = new stdClass(); - $key->value = 'WantsUrlExternal'; - $key->script = 'auth/userkey'; - $key->userid = $this->user->id; - $key->instance = $this->user->id; - $key->iprestriction = null; - $key->validuntil = time() + 300; - $key->timecreated = time(); - $DB->insert_record('user_private_key', $key); - - $_POST['key'] = 'WantsUrlExternal'; + $_POST['key'] = 'TestKey'; $_POST['wantsurl'] = 'http://test.com/course/index.php?id=12&key=134'; // Using @ is the only way to test this. Thanks moodle! @@ -993,23 +948,15 @@ class auth_plugin_userkey_testcase extends advanced_testcase { * Test that if one user logged, he will be logged out before a new one is authorised. */ public function test_that_different_authorised_user_is_logged_out_and_new_one_logged_in() { - global $DB, $USER, $SESSION; + global $USER, $SESSION; $user = $this->getDataGenerator()->create_user(); $this->setUser($user); $this->assertEquals($USER->id, $user->id); - $key = new stdClass(); - $key->value = 'UserLogin'; - $key->script = 'auth/userkey'; - $key->userid = $this->user->id; - $key->instance = $this->user->id; - $key->iprestriction = null; - $key->validuntil = time() + 300; - $key->timecreated = time(); - $DB->insert_record('user_private_key', $key); + $this->create_user_private_key(); - $_POST['key'] = 'UserLogin'; + $_POST['key'] = 'TestKey'; try { // Using @ is the only way to test this. Thanks moodle! @@ -1025,21 +972,14 @@ class auth_plugin_userkey_testcase extends advanced_testcase { * Test that authorised user gets logged out when trying to logged in with invalid key. */ public function test_if_invalid_key_authorised_user_gets_logged_out() { - global $DB, $USER, $SESSION; + global $USER, $SESSION; $user = $this->getDataGenerator()->create_user(); $this->setUser($user); $this->assertEquals($USER->id, $user->id); - $key = new stdClass(); - $key->value = 'UserLogin'; - $key->script = 'auth/userkey'; - $key->userid = $this->user->id; - $key->instance = $this->user->id; - $key->iprestriction = null; - $key->validuntil = time() + 300; - $key->timecreated = time(); - $DB->insert_record('user_private_key', $key); + $this->create_user_private_key(); + $_POST['key'] = 'Incorrect Key'; @@ -1062,17 +1002,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase { $this->setUser($this->user); $this->assertEquals($USER->id, $this->user->id); - $key = new stdClass(); - $key->value = 'UserLogin'; - $key->script = 'auth/userkey'; - $key->userid = $this->user->id; - $key->instance = $this->user->id; - $key->iprestriction = null; - $key->validuntil = time() + 300; - $key->timecreated = time(); - $DB->insert_record('user_private_key', $key); + $this->create_user_private_key(); - $_POST['key'] = 'UserLogin'; + $_POST['key'] = 'TestKey'; try { // Using @ is the only way to test this. Thanks moodle! @@ -1081,7 +1013,7 @@ class auth_plugin_userkey_testcase extends advanced_testcase { $this->assertEquals($this->user->id, $USER->id); $this->assertSame(sesskey(), $USER->sesskey); $this->assertObjectNotHasAttribute('userkey', $SESSION); - $keyexists = $DB->record_exists('user_private_key', array('value' => 'UserLogin')); + $keyexists = $DB->record_exists('user_private_key', array('value' => 'TestKey')); $this->assertFalse($keyexists); } } From de5cfdab7e22555f3d6f07a9f1fc36641c0d5ce0 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 8 May 2020 17:22:13 +1000 Subject: [PATCH 07/12] Make sure that we check HTTP_CLIENT_IP first --- tests/phpunit/auth_plugin_test.php | 4 ++++ tests/phpunit/core_userkey_manager_test.php | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/tests/phpunit/auth_plugin_test.php b/tests/phpunit/auth_plugin_test.php index e00b097..9cc786d 100644 --- a/tests/phpunit/auth_plugin_test.php +++ b/tests/phpunit/auth_plugin_test.php @@ -54,6 +54,10 @@ class auth_plugin_userkey_testcase extends advanced_testcase { require_once($CFG->dirroot . '/auth/userkey/auth.php'); require_once($CFG->dirroot . '/user/lib.php'); + parent::setUp(); + + $this->resetAfterTest(); + $CFG->getremoteaddrconf = GETREMOTEADDR_SKIP_HTTP_X_FORWARDED_FOR; $this->auth = new auth_plugin_userkey(); $this->user = self::getDataGenerator()->create_user(); } diff --git a/tests/phpunit/core_userkey_manager_test.php b/tests/phpunit/core_userkey_manager_test.php index a6945d3..abf9ad8 100644 --- a/tests/phpunit/core_userkey_manager_test.php +++ b/tests/phpunit/core_userkey_manager_test.php @@ -52,7 +52,12 @@ class core_userkey_manager_testcase extends advanced_testcase { * Initial set up. */ public function setUp() { + global $CFG; + + parent::setUp(); + $this->resetAfterTest(); + $CFG->getremoteaddrconf = GETREMOTEADDR_SKIP_HTTP_X_FORWARDED_FOR; $this->user = self::getDataGenerator()->create_user(); $this->config = new stdClass(); } From a9fa45b6d4bd35ca49a78717e18f1e5f4d6a00e7 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 8 May 2020 17:43:30 +1000 Subject: [PATCH 08/12] Remove empty line --- tests/phpunit/auth_plugin_test.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/phpunit/auth_plugin_test.php b/tests/phpunit/auth_plugin_test.php index 9cc786d..8e8ef99 100644 --- a/tests/phpunit/auth_plugin_test.php +++ b/tests/phpunit/auth_plugin_test.php @@ -984,7 +984,6 @@ class auth_plugin_userkey_testcase extends advanced_testcase { $this->create_user_private_key(); - $_POST['key'] = 'Incorrect Key'; try { From 4ed8bc9c3cd5423a88a157ec00cb2db6cb8e36f3 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 8 May 2020 17:47:02 +1000 Subject: [PATCH 09/12] Remove phpcpd from travis --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 58bffc3..43d0f82 100644 --- a/.travis.yml +++ b/.travis.yml @@ -51,7 +51,6 @@ install: script: - moodle-plugin-ci phplint - - moodle-plugin-ci phpcpd - moodle-plugin-ci phpmd - moodle-plugin-ci codechecker - moodle-plugin-ci csslint From ac8c1c6cdab971a218d2008cd805fa3cb43a71ca Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 8 May 2020 18:01:11 +1000 Subject: [PATCH 10/12] Modify exclusions in travis --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index 43d0f82..6bf89c8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,6 +33,10 @@ env: matrix: exclude: + - php: 7.1 + env: MOODLE_BRANCH=master + - php: 7.2 + env: MOODLE_BRANCH=MOODLE_33_STABLE - php: 7.3 env: MOODLE_BRANCH=MOODLE_33_STABLE - php: 7.3 From 5a3cd5867faf11360e756b3e98ea2df0a2696511 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 8 May 2020 19:39:22 +1000 Subject: [PATCH 11/12] Fix test names of test functions --- tests/phpunit/externallib_test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/externallib_test.php b/tests/phpunit/externallib_test.php index 4d0342f..f6a269c 100644 --- a/tests/phpunit/externallib_test.php +++ b/tests/phpunit/externallib_test.php @@ -153,7 +153,7 @@ class auth_userkey_externallib_testcase extends advanced_testcase { * @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_seе() { + public function test_exception_thrown_if_required_parameter_email_is_not_set() { global $CFG; $this->setAdminUser(); @@ -172,7 +172,7 @@ class auth_userkey_externallib_testcase extends advanced_testcase { * @expectedException invalid_parameter_exception * @expectedExceptionMessage Invalid parameter value detected (Required parameter "ip" is not set.) */ - public function test_exception_thrown_if_required_parameter_ip_is_not_seе() { + public function test_exception_thrown_if_required_parameter_op_is_not_set() { global $CFG; $this->setAdminUser(); From 0ae1e389b3e863206b743b4ebf25e35821a21878 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 8 May 2020 19:40:04 +1000 Subject: [PATCH 12/12] Version bump --- version.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/version.php b/version.php index 6ab0a6b..b8ba70a 100644 --- a/version.php +++ b/version.php @@ -24,8 +24,8 @@ defined('MOODLE_INTERNAL') || die; -$plugin->version = 2018050200; // The current plugin version (Date: YYYYMMDDXX) -$plugin->release = 2018050200; // Match release exactly to version. +$plugin->version = 2020050800; // The current plugin version (Date: YYYYMMDDXX) +$plugin->release = 2020050800; // Match release exactly to version. $plugin->requires = 2017051500; // Requires Moodle 3.3 version. $plugin->component = 'auth_userkey'; // Full name of the plugin (used for diagnostics). $plugin->maturity = MATURITY_STABLE;