Commit 8a8d771e authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review
Browse files

Merge "Fixing behat for testing webservices"

parents a5f3c897 c740a5cf
......@@ -219,7 +219,7 @@ EOD;
$record['authname'] = 'internal';
}
if (!$auth = get_record('auth_instance', 'institution', $record['institution'], 'authname', $record['authname'], 'active', 1)) {
throw new SystemException("The authentication method authname" . $record['authname'] . " for institution '" . $record['institution'] . "' does not exist.");
throw new SystemException("The authentication method authname " . $record['authname'] . " for institution '" . $record['institution'] . "' does not exist.");
}
$record['authinstance'] = $auth->id;
// Don't exceed max user accounts for the institution
......@@ -377,6 +377,11 @@ EOD;
if (!empty($record['editwindowstart']) && !empty($record['editwindowend']) && ($record['editwindowstart'] >= $record['editwindowend'])) {
throw new SystemException('Invalid group editability setting. ' . get_string('editwindowendbeforestart', 'group'));
}
if (!empty($record['institution'])) {
if (!get_field('institution', 'id', 'name', $record['institution'])) {
throw new SystemException('Invalid institution for group - Institution with short name "' . $record['institution'] . '" does not exist');
}
}
$group_data = array(
'id' => null,
'name' => $record['name'],
......@@ -392,7 +397,7 @@ EOD;
'usersautoadded' => 0,
'viewnotify' => GROUP_ROLES_ALL,
'submittableto' => isset($record['submittableto']) ? $record['submittableto'] : 0,
'allowarchives' => isset($record['allowarchives']) ? $record['allowarchives'] : 0,
'allowarchives' => isset($record['allowarchives']) ? $record['allowarchives'] : 0,
'editroles' => isset($record['editroles']) ? $record['editroles'] : 'all',
'hidden' => 0,
'hidemembers' => 0,
......@@ -409,6 +414,11 @@ EOD;
// Create a new group
db_begin();
$group_data['id'] = group_create($group_data);
// Because group_create expects user to be logged in to check if they can create a group for a particular institution
// we will make it for 'mahara' institution and then adjust it here
if (!empty($record['institution'])) {
set_field('group', 'institution', $record['institution'], 'id', $group_data['id']);
}
db_commit();
$this->groupcount++;
......@@ -497,7 +507,16 @@ EOD;
'authname' => 'internal',
);
insert_record('auth_instance', $authinstance);
if (!empty($record['authname'])) {
$authinstance = (object)array(
'instancename' => $record['authname'],
'priority' => 1,
'active' => 1,
'institution' => $newinstitution->name,
'authname' => $record['authname'],
);
insert_record('auth_instance', $authinstance);
}
// We need to add the default lines to the site_content table for this institution
// We also need to set the institution to be using default static pages to begin with
// so that using custom institution pages is an opt-in situation
......
......@@ -69,6 +69,7 @@ class BehatDataGenerators extends BehatBase {
'members' => 'text',
'staff' => 'text',
'admins' => 'text',
'institution' => 'text',
),
'required' => array('name', 'owner'),
),
......@@ -95,6 +96,7 @@ class BehatDataGenerators extends BehatBase {
'members' => 'text',
'staff' => 'text',
'admins' => 'text',
'authname' => 'string',
),
'required' => array('name', 'displayname')
),
......
......@@ -452,6 +452,34 @@ class BehatForms extends BehatBase {
$field->set_value($value);
}
/**
* Enable a switch in a table row containing the specified text
*
* @When /^I enable the switch in "(?P<fieldlabel>(?:[^"]|\\")*)" row$/
* @param string $fieldlabel the label of the field
* @throws ElementNotFoundException
*/
public function i_enable_switch_in_row($fieldlabel) {
// The table row container.
$textliteral = $this->escaper->escapeLiteral($fieldlabel);
$exception = new ElementNotFoundException($this->getSession(), 'field', null, 'the row containing the text "' . $fieldlabel . '"');
$xpath = "//tr[contains(normalize-space(.), " . $textliteral . ")]";
$rownode = $this->find('xpath', $xpath, $exception);
// Looking for the element DOM node inside the specified row.
list($selector, $locator) = $this->transform_selector('checkbox', $fieldlabel);
$switch_node = $this->find($selector, $locator, false, $rownode);
$this->ensure_node_is_visible($switch_node);
if (!$switch_node->isChecked()) {
// For some reasons, the Mink function click() and check() do not work
// Using jQuery as a workaround
$jscode = "jQuery(\"tr label:contains(" . $this->escapeDoubleQuotes($textliteral) . ")\").closest(\"tr\").find(\"input[type=checkbox]\")[0].click();";
$this->getSession()->executeScript($jscode);
}
}
/**
* Enable a switch
*
......
......@@ -201,6 +201,27 @@ class BehatGeneral extends BehatBase {
$this->getSession()->getDriver()->getWebDriverSession()->postAlert_text($text);
}
/**
* Fill the wstoken text in field. This step finds the relevant token and fill in field.
* @When /^I fill in the wstoken for "([^"]*)" owned by "([^"]*)"$/
*/
public function i_fill_in_wstoken($service, $user) {
$tokens = get_records_sql_array("SELECT token FROM {external_services} es
JOIN {external_tokens} et ON et.externalserviceid = es.id
JOIN {usr} u ON u.id = et.userid
WHERE es.name = ?
AND (u.username = ? OR CONCAT(u.firstname, ' ', u.lastname) = ?)", array($service, $user, $user));
if (!$tokens) {
throw new Exception(sprintf('Invalid token. No wstoken found for service "%s" owned by "%s".', $service, $user));
}
if (count($tokens) > 1) {
throw new Exception(sprintf('Too many tokens. More than one wstoken exists for user "%s" for service "%s".', $user, $service));
}
$token = $tokens[0]->token;
// success
$this->getSession()->getPage()->fillField("wstoken", $token);
}
/**
* Assert the text in popup window. This step does not work in all the browsers, consider it experimental.
* @Then /^I should see "(?P<text>(?:[^"]|\\")*)" in popup$/
......
@javascript @core @core_administration @core_webservices
Feature: To create and test a webservice service group
In order to use webservices
As an admin
So I can benefit from the cross over of Moodle/Mahara
and check correct group fields are returned
Background:
Given the following "institutions" exist:
| name | displayname | authname |
| one | Institution 1 | webservice |
And the following "users" exist:
| username | password | email | firstname | lastname | institution | authname | role |
| serviceadmin | Kupuhipa1 | svad@example.com | Service | Admin | one | webservice | admin |
And the following "groups" exist:
| name | owner | description | grouptype | open | invitefriends | editroles | submittableto | allowarchives | members | staff | institution |
| Group A | admin | This is my group A | standard | ON | OFF | all | OFF | OFF | admin | | one |
Scenario: Turning master switch on
Given I log in as "admin" with password "Kupuhipa1"
And I choose "Configuration" in "Web services" from administration menu
# Turning the master switch on
And I enable the switch "Accept incoming web service requests:"
And I enable the switch "REST:"
# Create a new service group
And I expand the section "Manage service groups"
And I set the field "service" to "Test service"
And I press "Add"
And I scroll to the top
And I click on "Edit" in "Test service" row
And I set the field "Short name" to "testservice"
And I enable the switch "Service"
And I enable the switch "User token access"
And I enable the switch in "mahara_group_get_groups_by_id" row
And I enable the switch in "mahara_group_create_groups" row
And I enable the switch in "mahara_group_update_groups" row
And I press "Save"
# Verify service group was made
And I press "Back"
And I should see "mahara_group_create_groups"
# Create a new token
And I expand the section "Manage service access tokens"
And I fill in select2 input "webservices_token_generate_userid" with "Service" and select "Service Admin (serviceadmin)"
And I press "Generate token"
And I select "Institution 1" from "Institution"
And I select "Test service" from "Service"
And I press "Save"
# Verify the token was made
And I press "Back"
And I should see "Edit" in the "#webservices_token_pseudofieldset" element
# Test the token
And I choose "Test client" in "Web services" from administration menu
And I press "Next"
And I select "Test service (Token)" from "Service"
And I press "Next"
And I select "mahara_group_get_groups_by_id" from "Functions"
And I press "Next"
And I fill in "groupa" for "shortname"
And I select "Institution 1" from "Institution"
And I fill in the wstoken for "Test service" owned by "Service Admin"
# For some reason the submitting this form freezes behat
# And I press "Execute"
# Then I should see "editroles"
@javascript @core @core_administration
@javascript @core @core_administration @core_webservices
Feature: To enable webservices via the webservices configuration form
In order to use webservices
As an admin
......
@javascript @core @core_administration
@javascript @core @core_administration @core_webservices
Feature: Accessing the webservices test client page
In order to be able to access the webservies menus successfully
As an admin
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment