Skip to content

Commit

Permalink
MDL-72173 behat: Switch to behat login URL
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Sep 13, 2021
1 parent 6c57830 commit 1349e5d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
12 changes: 4 additions & 8 deletions auth/tests/behat/behat_auth.php
Expand Up @@ -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<username_string>(?:[^"]|\\")*)"$/
* @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.
*/
Expand All @@ -52,20 +53,15 @@ public function i_log_in_as(string $username, moodle_url $wantsurl = null) {
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'));
}

/**
Expand Down
51 changes: 34 additions & 17 deletions lib/tests/behat/fastlogin.php → lib/tests/behat/login.php
Expand Up @@ -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
Expand All @@ -28,32 +29,48 @@

$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);

0 comments on commit 1349e5d

Please sign in to comment.