From 722c5d6d567e2e83a9cfe364e82d30604357aea3 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 16 Mar 2021 09:20:20 +0800 Subject: [PATCH 1/1] MDL-67748 webservice: Create behat data generators --- admin/tests/behat/manage_tokens.feature | 53 +++---- webservice/lib.php | 1 + .../behat_core_webservice_generator.php | 59 ++++++++ webservice/tests/generator/lib.php | 139 ++++++++++++++++++ 4 files changed, 220 insertions(+), 32 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 cf7f2a7ff11..8a0fb897c00 100644 --- a/admin/tests/behat/manage_tokens.feature +++ b/admin/tests/behat/manage_tokens.feature @@ -12,12 +12,12 @@ Feature: Manage external services tokens | 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 a token to user identified by name and then delete that token - Given I navigate to "Server > Web services > Manage tokens" in site administration + 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 "Firstname1 Lastname1" And I set the field "Service" to "Moodle mobile web service" @@ -32,40 +32,27 @@ Feature: Manage external services tokens @javascript Scenario: Tokens can be filtered by user and by service - # Create a new custom external service. - Given I navigate to "Server > Web services > External services" in site administration - And I follow "Add" - And I set the following fields to these values: - | Name | Site information | - | Short name | siteinfo | - | Enabled | 1 | - And I click on "Add service" "button" - And I follow "Add functions" - And I set the following fields to these values: - | Name | core_webservice_get_site_info | - And I click on "Add functions" "button" - # Create tokens for users. - Given I navigate to "Server > Web services > Manage tokens" in site administration - And I press "Create token" - And I set the following fields to these values: - | User | Firstname2 Lastname2 | - | Service | Site information | - And I press "Save changes" - And I press "Create token" - And I set the following fields to these values: - | User | Firstname3 Lastname3 | - | Service | Moodle mobile web service | - And I press "Save changes" - And I press "Create token" - And I set the following fields to these values: - | User | Firstname4 Lastname4 | - | Service | Site information | - And I press "Save changes" + 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" @@ -73,11 +60,13 @@ Feature: Manage external services 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" diff --git a/webservice/lib.php b/webservice/lib.php index d8bd2cd34f6..342d2a2ba6d 100644 --- a/webservice/lib.php +++ b/webservice/lib.php @@ -812,6 +812,7 @@ class webservice { public function add_external_service($service) { global $DB; $service->timecreated = time(); + print_r($service); $serviceid = $DB->insert_record('external_services', $service); return $serviceid; } 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 00000000000..1b423d87cbe --- /dev/null +++ b/webservice/tests/generator/behat_core_webservice_generator.php @@ -0,0 +1,59 @@ +. + +/** + * Behat data generator for core_webservice. + * + * @package core_webservice + * @category test + * @copyright 2021 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Behat data generator for core_question. + */ +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 00000000000..9d877abc28b --- /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'] + ); + } +} -- 2.30.0