forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-67748 admin: Add Behat tests for tokens filtering features
Credit goes to Andrew for Behat data generators for webservices. Co-authored-by: Andrew Nicols <[email protected]>
- Loading branch information
1 parent
b0fd376
commit 4a9f864
Showing
3 changed files
with
260 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,76 @@ | ||
@core @core_admin | ||
Feature: Manage tokens | ||
In order to manage webservice usage | ||
Feature: Manage external services tokens | ||
In order to manage external service usage | ||
As an admin | ||
I need to be able to create and delete tokens | ||
I need to be able to create, filter and delete tokens | ||
|
||
Background: | ||
Given the following "users" exist: | ||
| username | password | firstname | lastname | | ||
| testuser | testuser | Joe | Bloggs | | ||
| testuser2 | testuser2 | TestFirstname | TestLastname | | ||
| username | password | firstname | lastname | | ||
| user1 | user1 | Firstname1 | Lastname1 | | ||
| user2 | user2 | Firstname2 | Lastname2 | | ||
| user3 | user3 | Firstname3 | Lastname3 | | ||
| user4 | user4 | Firstname4 | Lastname4 | | ||
And I change window size to "small" | ||
And I log in as "admin" | ||
And I am on site homepage | ||
|
||
@javascript | ||
Scenario: Add & delete a token | ||
Given I navigate to "Server > Web services > Manage tokens" in site administration | ||
Scenario: Add a token to user identified by name and then delete that token | ||
Given I log in as "admin" | ||
And I am on site homepage | ||
And I navigate to "Server > Web services > Manage tokens" in site administration | ||
And I press "Create token" | ||
And I set the field "User" to "Joe Bloggs" | ||
And I set the field "User" to "Firstname1 Lastname1" | ||
And I set the field "Service" to "Moodle mobile web service" | ||
And I set the field "IP restriction" to "127.0.0.1" | ||
When I press "Save changes" | ||
Then I should see "Joe Bloggs" | ||
And I should see "127.0.0.1" | ||
And I follow "Delete" | ||
Then I should see "Moodle mobile web service" in the "Firstname1 Lastname1" "table_row" | ||
And I should see "127.0.0.1" in the "Firstname1 Lastname1" "table_row" | ||
And I click on "Delete" "link" in the "Firstname1 Lastname1" "table_row" | ||
And I should see "Do you really want to delete this web service token for Firstname1 Lastname1 on the service Moodle mobile web service?" | ||
And I press "Delete" | ||
And I should not see "Joe Bloggs" | ||
And "Firstname1 Lastname1" "table_row" should not exist | ||
|
||
@javascript | ||
Scenario: Tokens can be filtered by user and by service | ||
Given the following "core_webservice > Service" exists: | ||
| name | Site information | | ||
| shortname | siteinfo | | ||
| enabled | 1 | | ||
And the following "core_webservice > Service function" exists: | ||
| service | siteinfo | | ||
| functions | core_webservice_get_site_info | | ||
And the following "core_webservice > Tokens" exist: | ||
| user | service | | ||
| user2 | siteinfo | | ||
| user3 | moodle_mobile_app | | ||
| user4 | siteinfo | | ||
When I log in as "admin" | ||
And I navigate to "Server > Web services > Manage tokens" in site administration | ||
|
||
# All created tokens are shown by default. | ||
And "Firstname1 Lastname1" "table_row" should not exist | ||
And I should see "Site information" in the "Firstname2 Lastname2" "table_row" | ||
And I should see "Moodle mobile web service" in the "Firstname3 Lastname3" "table_row" | ||
And I should see "Site information" in the "Firstname4 Lastname4" "table_row" | ||
|
||
# Filter tokens by user (note we can select the user by the identity field here). | ||
When I click on "Tokens filter" "link" | ||
And I set the field "User" to "[email protected]" | ||
And I press "Show only matching tokens" | ||
Then "Firstname3 Lastname3" "table_row" should not exist | ||
And "Firstname4 Lastname4" "table_row" should not exist | ||
And I should see "Site information" in the "Firstname2 Lastname2" "table_row" | ||
|
||
# Reset the filter. | ||
And I press "Show all tokens" | ||
And I should see "Site information" in the "Firstname2 Lastname2" "table_row" | ||
And I should see "Moodle mobile web service" in the "Firstname3 Lastname3" "table_row" | ||
And I should see "Site information" in the "Firstname4 Lastname4" "table_row" | ||
|
||
# Filter tokens by service. | ||
And I click on "Tokens filter" "link" | ||
And I set the field "Service" to "Site information" | ||
And I press "Show only matching tokens" | ||
And I should see "Site information" in the "Firstname2 Lastname2" "table_row" | ||
And I should see "Site information" in the "Firstname4 Lastname4" "table_row" | ||
And "Firstname3 Lastname3" "table_row" should not exist |
57 changes: 57 additions & 0 deletions
57
webservice/tests/generator/behat_core_webservice_generator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
// This file is part of Moodle - https://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Behat data generator for core_webservice. | ||
* | ||
* @package core_webservice | ||
* @category test | ||
* @copyright 2021 Andrew Nicols <[email protected]> | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class behat_core_webservice_generator extends behat_generator_base { | ||
|
||
/** | ||
* Get the list of creatable entities for a web service. | ||
* | ||
* @return array | ||
*/ | ||
protected function get_creatable_entities(): array { | ||
|
||
return [ | ||
'Services' => [ | ||
'singular' => 'Service', | ||
'datagenerator' => 'service', | ||
'required' => ['name'], | ||
], | ||
|
||
'Service functions' => [ | ||
'singular' => 'Service function', | ||
'datagenerator' => 'service_functions', | ||
'required' => ['service', 'functions'], | ||
], | ||
|
||
'Tokens' => [ | ||
'singular' => 'Token', | ||
'datagenerator' => 'token', | ||
'required' => ['user'], | ||
'switchids' => [ | ||
'user' => 'userid', | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
global $CFG; | ||
require_once(__DIR__ . '/../../lib.php'); | ||
|
||
/** | ||
* Data generator for core_webservice plugin. | ||
* | ||
* @package core_webservice | ||
* @category test | ||
* @copyright 2021 Andrew Nicols <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class core_webservice_generator extends component_generator_base { | ||
/** | ||
* Create a new webservice service. | ||
* | ||
* @param array $data | ||
* @return stdClass | ||
*/ | ||
public function create_service(array $data): \stdClass { | ||
$webservicemanager = new webservice(); | ||
|
||
$requiredfields = [ | ||
'name', | ||
'shortname', | ||
]; | ||
|
||
foreach ($requiredfields as $fieldname) { | ||
if (!array_key_exists($fieldname, $data)) { | ||
throw new \coding_exception("Field '{$fieldname}' missing when creating new service"); | ||
} | ||
} | ||
|
||
$optionalfields = [ | ||
'requiredcapability' => '', | ||
'restrictedusers' => 0, | ||
'component' => '', | ||
'timemodified' => time(), | ||
]; | ||
|
||
foreach ($optionalfields as $fieldname => $value) { | ||
if (!array_key_exists($fieldname, $data)) { | ||
$data[$fieldname] = $value; | ||
} | ||
} | ||
|
||
$serviceid = $webservicemanager->add_external_service((object) $data); | ||
|
||
return $webservicemanager->get_external_service_by_id($serviceid); | ||
} | ||
|
||
/** | ||
* Associate a webservice function with service. | ||
* | ||
* @param array $data | ||
*/ | ||
public function create_service_functions(array $data): void { | ||
$webservicemanager = new webservice(); | ||
|
||
$requiredfields = [ | ||
'service', | ||
'functions', | ||
]; | ||
|
||
foreach ($requiredfields as $fieldname) { | ||
if (!array_key_exists($fieldname, $data)) { | ||
throw new \coding_exception("Field '{$fieldname}' missing when creating new service"); | ||
} | ||
} | ||
|
||
$service = $webservicemanager->get_external_service_by_shortname($data['service']); | ||
|
||
$functions = explode(',', $data['functions']); | ||
foreach ($functions as $functionname) { | ||
$functionname = trim($functionname); | ||
$webservicemanager->add_external_function_to_service($functionname, $service->id); | ||
} | ||
} | ||
|
||
/** | ||
* Create a new webservice token. | ||
* | ||
* @param array $data | ||
*/ | ||
public function create_token(array $data): void { | ||
$webservicemanager = new webservice(); | ||
|
||
$requiredfields = [ | ||
'userid', | ||
'service', | ||
]; | ||
|
||
foreach ($requiredfields as $fieldname) { | ||
if (!array_key_exists($fieldname, $data)) { | ||
throw new \coding_exception("Field '{$fieldname}' missing when creating new service"); | ||
} | ||
} | ||
|
||
$optionalfields = [ | ||
'context' => context_system::instance(), | ||
'validuntil' => 0, | ||
'iprestriction' => '', | ||
]; | ||
|
||
foreach ($optionalfields as $fieldname => $value) { | ||
if (!array_key_exists($fieldname, $data)) { | ||
$data[$fieldname] = $value; | ||
} | ||
} | ||
|
||
$service = $webservicemanager->get_external_service_by_shortname($data['service']); | ||
|
||
external_generate_token( | ||
EXTERNAL_TOKEN_PERMANENT, | ||
$service->id, | ||
$data['userid'], | ||
$data['context'], | ||
$data['validuntil'], | ||
$data['iprestriction'] | ||
); | ||
} | ||
} |