From 8b52b17ad83ffd7d69386b3522516b3dac9a4b03 Mon Sep 17 00:00:00 2001 From: gthomas2 Date: Fri, 16 Jul 2021 12:36:02 +0100 Subject: [PATCH 1/5] MDL-72173 behat: Add alternative login file for behat --- lib/tests/behat/fastlogin.php | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 lib/tests/behat/fastlogin.php diff --git a/lib/tests/behat/fastlogin.php b/lib/tests/behat/fastlogin.php new file mode 100644 index 00000000000..d1e6707efeb --- /dev/null +++ b/lib/tests/behat/fastlogin.php @@ -0,0 +1,59 @@ +. +// phpcs:disable moodle.Files.RequireLogin.Missing +// phpcs:disable moodle.PHP.ForbiddenFunctions.Found + +/** + * Fast login end point for BEHAT TESTS ONLY. + * + * @package theme_cfz + * @author Guy Thomas + * @copyright 2021 Class Technologies Inc. {@link https://www.class.com/} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +require(__DIR__.'/../../../config.php'); + +$behatrunning = defined('BEHAT_SITE_RUNNING') && BEHAT_SITE_RUNNING; +if (!$behatrunning) { + die; +} + +$username = required_param('username', PARAM_ALPHANUMEXT); +// Note - with behat, the password is always the same as the username. +$password = $username; + +$failurereason = null; +$user = authenticate_user_login($username, $password, true, $failurereason, false); +if ($failurereason) { + error_log("Failed to login as behat step for $username with reason: " . $failurereason); + throw new Exception($failurereason); +} +if (!complete_user_login($user)) { + throw new Exception("Failed to login as behat step for $username"); +} + +$redirecturl = optional_param('redirecturl', null, PARAM_URL); +$redirecturl = $redirecturl ?? $CFG->wwwroot; + +if (optional_param('forceeditmode', false, PARAM_INT)) { + $sesskey = sesskey(); + $url = new moodle_url($redirecturl); + $url->param('edit', 1); + $url->param('sesskey', $sesskey); + $redirecturl = $url.''; +} + +redirect($redirecturl); From 441f3132175e91ab9115ac573ae11036a66ce467 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 16 Jul 2021 23:58:36 +0800 Subject: [PATCH 2/5] MDL-72173 behat: Switch to behat login URL --- auth/tests/behat/behat_auth.php | 12 ++--- lib/tests/behat/{fastlogin.php => login.php} | 51 +++++++++++++------- 2 files changed, 38 insertions(+), 25 deletions(-) rename lib/tests/behat/{fastlogin.php => login.php} (57%) diff --git a/auth/tests/behat/behat_auth.php b/auth/tests/behat/behat_auth.php index 6233454f99f..316cc2579fa 100644 --- a/auth/tests/behat/behat_auth.php +++ b/auth/tests/behat/behat_auth.php @@ -42,6 +42,7 @@ class behat_auth extends behat_base { * Logs in the user. There should exist a user with the same value as username and password. * * @Given /^I log in as "(?P(?:[^"]|\\")*)"$/ + * @Given I am logged in as :username * @param string $username the user to log in as. * @param moodle_url|null $wantsurl optional, URL to go to after logging in. */ @@ -52,20 +53,15 @@ class behat_auth extends behat_base { return; } - $loginurl = new moodle_url('/login/index.php'); + $loginurl = new moodle_url('/lib/tests/behat/login.php', [ + 'username' => $username, + ]); if ($wantsurl !== null) { $loginurl->param('wantsurl', $wantsurl->out_as_local_url()); } // Visit login page. $this->execute('behat_general::i_visit', [$loginurl]); - - // Enter username and password. - $this->execute('behat_forms::i_set_the_field_to', array('Username', $this->escape($username))); - $this->execute('behat_forms::i_set_the_field_to', array('Password', $this->escape($username))); - - // Press log in button, no need to check for exceptions as it will checked after this step execution. - $this->execute('behat_forms::press_button', get_string('login')); } /** diff --git a/lib/tests/behat/fastlogin.php b/lib/tests/behat/login.php similarity index 57% rename from lib/tests/behat/fastlogin.php rename to lib/tests/behat/login.php index d1e6707efeb..8103806d8e3 100644 --- a/lib/tests/behat/fastlogin.php +++ b/lib/tests/behat/login.php @@ -17,9 +17,10 @@ // phpcs:disable moodle.PHP.ForbiddenFunctions.Found /** - * Fast login end point for BEHAT TESTS ONLY. + * Login end point for Behat tests only. * - * @package theme_cfz + * @package core_auth + * @category test * @author Guy Thomas * @copyright 2021 Class Technologies Inc. {@link https://www.class.com/} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -28,32 +29,48 @@ require(__DIR__.'/../../../config.php'); $behatrunning = defined('BEHAT_SITE_RUNNING') && BEHAT_SITE_RUNNING; if (!$behatrunning) { - die; + redirect(new moodle_url('/')); } $username = required_param('username', PARAM_ALPHANUMEXT); +$wantsurl = new moodle_url(optional_param('wantsurl', '/', PARAM_URL)); + // Note - with behat, the password is always the same as the username. $password = $username; $failurereason = null; $user = authenticate_user_login($username, $password, true, $failurereason, false); if ($failurereason) { - error_log("Failed to login as behat step for $username with reason: " . $failurereason); - throw new Exception($failurereason); + switch($failurereason) { + case AUTH_LOGIN_NOUSER: + $reason = get_string('invalidlogin'); + break; + case AUTH_LOGIN_SUSPENDED: + $reason = 'User suspended'; + break; + case AUTH_LOGIN_FAILED: + $reason = 'Login failed'; + break; + case AUTH_LOGIN_LOCKOUT: + $reason = 'Account locked'; + break; + case AUTH_LOGIN_UNAUTHORISED: + $reason = get_string('unauthorisedlogin', 'core', $username); + break; + default: + $reason = "Unknown login failure: '{$failureeason}'"; + break; + + } + + // Note: Do not throw an exception here as we sometimes test that login does not work. + // Exceptions are automatic failures in Behat. + \core\notification::add($reason, \core\notification::ERROR); + redirect(new moodle_url('/')); } + if (!complete_user_login($user)) { throw new Exception("Failed to login as behat step for $username"); } -$redirecturl = optional_param('redirecturl', null, PARAM_URL); -$redirecturl = $redirecturl ?? $CFG->wwwroot; - -if (optional_param('forceeditmode', false, PARAM_INT)) { - $sesskey = sesskey(); - $url = new moodle_url($redirecturl); - $url->param('edit', 1); - $url->param('sesskey', $sesskey); - $redirecturl = $url.''; -} - -redirect($redirecturl); +redirect($wantsurl); From 93cb87cddbcf07d66efe56c4da475049d273eec4 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Sun, 18 Jul 2021 21:53:27 +0800 Subject: [PATCH 3/5] MDL-72173 behat: Add and use behat logout URL --- auth/tests/behat/behat_auth.php | 9 ++----- {lib => auth}/tests/behat/login.php | 2 +- auth/tests/behat/logout.php | 41 +++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 8 deletions(-) rename {lib => auth}/tests/behat/login.php (97%) create mode 100644 auth/tests/behat/logout.php diff --git a/auth/tests/behat/behat_auth.php b/auth/tests/behat/behat_auth.php index 316cc2579fa..e6fcadb71f4 100644 --- a/auth/tests/behat/behat_auth.php +++ b/auth/tests/behat/behat_auth.php @@ -53,7 +53,7 @@ class behat_auth extends behat_base { return; } - $loginurl = new moodle_url('/lib/tests/behat/login.php', [ + $loginurl = new moodle_url('/auth/tests/behat/login.php', [ 'username' => $username, ]); if ($wantsurl !== null) { @@ -70,11 +70,6 @@ class behat_auth extends behat_base { * @Given /^I log out$/ */ public function i_log_out() { - - // Wait for page to be loaded. - $this->wait_for_pending_js(); - - // Click on logout link in footer, as it's much faster. - $this->execute('behat_general::i_click_on_in_the', array(get_string('logout'), 'link', '#page-footer', "css_element")); + $this->execute('behat_general::i_visit', [new moodle_url('/auth/tests/behat/logout.php')]); } } diff --git a/lib/tests/behat/login.php b/auth/tests/behat/login.php similarity index 97% rename from lib/tests/behat/login.php rename to auth/tests/behat/login.php index 8103806d8e3..d9d06b47d71 100644 --- a/lib/tests/behat/login.php +++ b/auth/tests/behat/login.php @@ -58,7 +58,7 @@ if ($failurereason) { $reason = get_string('unauthorisedlogin', 'core', $username); break; default: - $reason = "Unknown login failure: '{$failureeason}'"; + $reason = "Unknown login failure: '{$failurereason}'"; break; } diff --git a/auth/tests/behat/logout.php b/auth/tests/behat/logout.php new file mode 100644 index 00000000000..d2d7a69ade0 --- /dev/null +++ b/auth/tests/behat/logout.php @@ -0,0 +1,41 @@ +. +// phpcs:disable moodle.Files.RequireLogin.Missing +// phpcs:disable moodle.PHP.ForbiddenFunctions.Found + +/** + * Login end point for Behat tests only. + * + * @package core_auth + * @category test + * @copyright Andrew Lyons + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +require(__DIR__.'/../../../config.php'); + +$behatrunning = defined('BEHAT_SITE_RUNNING') && BEHAT_SITE_RUNNING; +if (!$behatrunning) { + redirect(new moodle_url('/login/logout.php')); +} + +require_logout(); + +$login = optional_param('loginpage', 0, PARAM_BOOL); +if ($login) { + redirect(get_login_url()); +} else { + redirect(new moodle_url('/')); +} From 41ebf95bbe1381cfb83d723ccb3aa029da962d03 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Mon, 19 Jul 2021 09:43:16 +0800 Subject: [PATCH 4/5] MDL-72173 behat: Automatically log user out on behat login --- auth/tests/behat/behat_auth.php | 1 + auth/tests/behat/login.php | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/auth/tests/behat/behat_auth.php b/auth/tests/behat/behat_auth.php index e6fcadb71f4..08d75997f31 100644 --- a/auth/tests/behat/behat_auth.php +++ b/auth/tests/behat/behat_auth.php @@ -68,6 +68,7 @@ class behat_auth extends behat_base { * Logs out of the system. * * @Given /^I log out$/ + * @Given I am not logged in */ public function i_log_out() { $this->execute('behat_general::i_visit', [new moodle_url('/auth/tests/behat/logout.php')]); diff --git a/auth/tests/behat/login.php b/auth/tests/behat/login.php index d9d06b47d71..2f67eb1c98e 100644 --- a/auth/tests/behat/login.php +++ b/auth/tests/behat/login.php @@ -35,6 +35,15 @@ if (!$behatrunning) { $username = required_param('username', PARAM_ALPHANUMEXT); $wantsurl = new moodle_url(optional_param('wantsurl', '/', PARAM_URL)); +if (isloggedin()) { + // If the user is already logged in, log them out and redirect them back to login again. + require_logout(); + redirect(new moodle_url('/auth/tests/behat/login.php', [ + 'username' => $username, + 'wantsurl' => $wantsurl->out(false), + ])); +} + // Note - with behat, the password is always the same as the username. $password = $username; From ad8283a80f64c44d4bfa756f80e100f39ca8c1cd Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Mon, 19 Jul 2021 10:00:48 +0800 Subject: [PATCH 5/5] MDL-72173 behat: Add tests for login/logout steps --- admin/tool/behat/tests/behat/loginout.feature | 110 ++++++++++++++++++ auth/tests/behat/login.feature | 4 +- 2 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 admin/tool/behat/tests/behat/loginout.feature diff --git a/admin/tool/behat/tests/behat/loginout.feature b/admin/tool/behat/tests/behat/loginout.feature new file mode 100644 index 00000000000..5fe3a8ff021 --- /dev/null +++ b/admin/tool/behat/tests/behat/loginout.feature @@ -0,0 +1,110 @@ +@tool_behat +Feature: Verify that the behat login and logout steps work as expected + In order to use behat login and log out steps + As a test writer + I need to verify that login and logout happen when the steps are used + + Scenario: Log in as a user using the step + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + When I log in as "traverst1" + Then I should see "Thomas Travers" + + @javascript + Scenario: Log in as a user using the step (javascript) + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + When I log in as "traverst1" + Then I should see "Thomas Travers" + + Scenario: Log out using the log out step + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + And I am logged in as traverst1 + When I log out + Then I should not see "Thomas Travers" + And I should see "You are not logged in" + + @javascript + Scenario: Log out using the log out step (javascript) + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + And I am logged in as traverst1 + When I log out + Then I should not see "Thomas Travers" + And I should see "You are not logged in" + + Scenario: Log in step should automatically log user out if already logged in + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + | emeryj | Jane | Emery | + And I am logged in as traverst1 + When I log in as "emeryj" + Then I should not see "Thomas Travers" + And I should see "Jane Emery" + + @javascript + Scenario: Log in step should automatically log user out if already logged in (javascript) + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + | emeryj | Jane | Emery | + And I am logged in as traverst1 + When I log in as "emeryj" + Then I should not see "Thomas Travers" + And I should see "Jane Emery" + + Scenario: I am on page logged in as should redirect to correct page + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + And the following "course" exists: + | fullname | Life, the Universe, and Everything | + | shortname | hhgttg | + When I am on the hhgttg Course page logged in as traverst1 + Then I should see "Thomas Travers" + And I should see "Life, the Universe, and Everything" + + @javascript + Scenario: I am on page logged in as should redirect to correct page (javascript) + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + And the following "course" exists: + | fullname | Life, the Universe, and Everything | + | shortname | hhgttg | + When I am on the hhgttg Course page logged in as traverst1 + Then I should see "Thomas Travers" + And I should see "Life, the Universe, and Everything" + + Scenario: I am on page logged in as should redirect to correct page when automatically logging a user out + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + | emeryj | Jane | Emery | + And the following "course" exists: + | fullname | Life, the Universe, and Everything | + | shortname | hhgttg | + And I am logged in as emeryj + When I am on the hhgttg Course page logged in as traverst1 + Then I should see "Thomas Travers" + And I should see "Life, the Universe, and Everything" + + @javascript + Scenario: I am on page logged in as should redirect to correct page when automatically logging a user out (javacript) + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + | emeryj | Jane | Emery | + And the following "course" exists: + | fullname | Life, the Universe, and Everything | + | shortname | hhgttg | + And I am logged in as emeryj + When I am on the hhgttg Course page logged in as traverst1 + Then I should see "Thomas Travers" + And I should see "Life, the Universe, and Everything" diff --git a/auth/tests/behat/login.feature b/auth/tests/behat/login.feature index cdc5976666e..12cb5da1f6e 100644 --- a/auth/tests/behat/login.feature +++ b/auth/tests/behat/login.feature @@ -35,9 +35,9 @@ Feature: Authentication And I press "Log in" Then I should see "Invalid login, please try again" - Scenario: Log out + Scenario: Log out using the Log out link Given I log in as "admin" - When I log out + When I click on "Logout" "link" in the "#page-footer" "css_element" Then I should see "You are not logged in" in the "page-footer" "region" Scenario Outline: Checking the display of the Remember username checkbox