MDL-72464 webservice: fix detection of non-expiring external tokens.

This commit is contained in:
Paul Holden
2021-10-10 20:40:47 +01:00
parent 7cef80d284
commit 2163c70d1c
3 changed files with 76 additions and 3 deletions
+35 -2
View File
@@ -1,8 +1,8 @@
@core @core_user
Feature: Enable/disable password field based on authentication selected.
Feature: Edit a users password
In order edit a user password properly
As an admin
I need to be able to notice if the change in password is allowed by athuentication plugin or not
I need to be able to edit their profile and change their password
@javascript
Scenario: Verify the password field is enabled/disabled based on authentication selected, in user edit advanced page.
@@ -15,3 +15,36 @@ Feature: Enable/disable password field based on authentication selected.
And the "New password" "field" should be enabled
# We need to cancel/submit a form that has been modified.
And I press "Create user"
Scenario: Sign out everywhere field is not present if user doesn't have active token
Given the following "users" exist:
| username | firstname | lastname | email |
| user01 | User | One | user01@example.com |
And I log in as "admin"
When I navigate to "Users > Accounts > Browse list of users" in site administration
And I click on "User One" "link" in the "users" "table"
And I click on "Edit profile" "link"
Then "Sign out everywhere" "field" should not exist
@javascript
Scenario Outline: Sign out everywhere field is present based on expiry of active token
Given the following "users" exist:
| username | firstname | lastname | email |
| user01 | User | One | user01@example.com |
And I log in as "admin"
When I navigate to "Plugins > Web services > Manage tokens" in site administration
And I click on "Add" "link"
And I set the following fields to these values:
| User | User One |
| Service | Moodle mobile web service |
| validuntil[enabled] | 1 |
| Valid until | <validuntil> |
And I press "Save changes"
And I navigate to "Users > Accounts > Browse list of users" in site administration
And I click on "User One" "link" in the "users" "table"
And I click on "Edit profile" "link"
Then "Sign out everywhere" "field" <shouldornot> exist
Examples:
| validuntil | shouldornot |
| ## -1 month ## | should not |
| ## +1 month ## | should |
+1 -1
View File
@@ -803,7 +803,7 @@ class webservice {
$sql = 'SELECT t.*, s.name as servicename FROM {external_tokens} t JOIN
{external_services} s ON t.externalserviceid = s.id WHERE
t.userid = :userid AND (t.validuntil IS NULL OR t.validuntil > :now)';
t.userid = :userid AND (COALESCE(t.validuntil, 0) = 0 OR t.validuntil > :now)';
$params = array('userid' => $userid, 'now' => time());
return $DB->get_records_sql($sql, $params);
}
+40
View File
@@ -193,6 +193,46 @@ class webservice_test extends advanced_testcase {
$this->assertEquals($before + 60, $token->lastaccess);
}
/**
* Data provider for {@see test_get_active_tokens}
*
* @return array
*/
public function get_active_tokens_provider(): array {
return [
'No expiration' => [0, true],
'Active' => [time() + DAYSECS, true],
'Expired' => [time() - DAYSECS, false],
];
}
/**
* Test getting active tokens for a user
*
* @param int $validuntil
* @param bool $expectedactive
*
* @dataProvider get_active_tokens_provider
*/
public function test_get_active_tokens(int $validuntil, bool $expectedactive): void {
global $DB;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$serviceid = $DB->get_field('external_services', 'id', ['shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE], MUST_EXIST);
external_generate_token(EXTERNAL_TOKEN_PERMANENT, $serviceid, $user->id, context_system::instance(), $validuntil, '');
$tokens = webservice::get_active_tokens($user->id);
if ($expectedactive) {
$this->assertCount(1, $tokens);
$this->assertEquals($serviceid, reset($tokens)->externalserviceid);
} else {
$this->assertEmpty($tokens);
}
}
/**
* Utility method that tests the parameter type of a method info's input/output parameter.
*