From 4a9f864969bf9e33dbac1d943bdda77609bbe7ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Mon, 15 Mar 2021 21:19:33 +0100 Subject: [PATCH] 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 --- admin/tests/behat/manage_tokens.feature | 79 ++++++++-- .../behat_core_webservice_generator.php | 57 +++++++ webservice/tests/generator/lib.php | 139 ++++++++++++++++++ 3 files changed, 260 insertions(+), 15 deletions(-) create mode 100644 webservice/tests/generator/behat_core_webservice_generator.php create mode 100644 webservice/tests/generator/lib.php diff --git a/admin/tests/behat/manage_tokens.feature b/admin/tests/behat/manage_tokens.feature index 0327ca4ba8e63..e155dedeee079 100644 --- a/admin/tests/behat/manage_tokens.feature +++ b/admin/tests/behat/manage_tokens.feature @@ -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 "user2@example.com" + 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 diff --git a/webservice/tests/generator/behat_core_webservice_generator.php b/webservice/tests/generator/behat_core_webservice_generator.php new file mode 100644 index 0000000000000..cf6f3744303c6 --- /dev/null +++ b/webservice/tests/generator/behat_core_webservice_generator.php @@ -0,0 +1,57 @@ +. + +/** + * Behat data generator for core_webservice. + * + * @package core_webservice + * @category test + * @copyright 2021 Andrew Nicols + * @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', + ], + ], + ]; + } +} diff --git a/webservice/tests/generator/lib.php b/webservice/tests/generator/lib.php new file mode 100644 index 0000000000000..9d877abc28b15 --- /dev/null +++ b/webservice/tests/generator/lib.php @@ -0,0 +1,139 @@ +. + +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 + * @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'] + ); + } +}