Skip to content

Commit

Permalink
MNT Add behat to module
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Apr 12, 2021
1 parent 4710b27 commit 6d1d40b
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[{*.yml,package.json}]
[{*.yml,package.json,*.feature}]
indent_size = 2

# The indent size used in the package.json file cannot be changed:
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: ~> 1.0

import:
- silverstripe/silverstripe-travis-shared:config/provision/standard-jobs-range-npm.yml
- silverstripe/silverstripe-travis-shared:config/provision/standard-jobs-range-behat-npm.yml

env:
global:
Expand Down
33 changes: 33 additions & 0 deletions behat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Run session-manager behat tests with this command
# ========================================================================= #
# vendor/bin/selenium-server-standalone -Dwebdriver.firefox.bin="/Applications/Firefox31.app/Contents/MacOS/firefox-bin"
# vendor/bin/serve --bootstrap-file vendor/silverstripe/cms/tests/behat/serve-bootstrap.php
# vendor/bin/behat @session-manager
# ========================================================================= #
default:
suites:
session-manager:
paths:
- "%paths.modules.session-manager%/tests/behat/features"
contexts:
- SilverStripe\SessionManager\Tests\Behat\Context\FeatureContext
- SilverStripe\Framework\Tests\Behaviour\CmsFormsContext
- SilverStripe\Framework\Tests\Behaviour\CmsUiContext
- SilverStripe\BehatExtension\Context\BasicContext
- SilverStripe\BehatExtension\Context\EmailContext
- SilverStripe\BehatExtension\Context\LoginContext
-
SilverStripe\SessionManager\Tests\Behat\Context\FixtureContext:
- "%paths.modules.session-manager%/tests/behat/files/"
extensions:
SilverStripe\BehatExtension\Extension:
bootstrap_file: vendor/silverstripe/cms/tests/behat/serve-bootstrap.php
screenshot_path: "%paths.base%/artifacts/screenshots"
retry_seconds: 4 # default is 2
SilverStripe\BehatExtension\MinkExtension:
default_session: facebook_web_driver
javascript_session: facebook_web_driver
facebook_web_driver:
browser: chrome
wd_host: "http://127.0.0.1:9515" #chromedriver port
browser_name: chrome
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"autoload": {
"psr-4": {
"SilverStripe\\SessionManager\\": "src/",
"SilverStripe\\SessionManager\\Tests\\": "tests/php/"
"SilverStripe\\SessionManager\\Tests\\": "tests/php/",
"SilverStripe\\SessionManager\\Tests\\Behat\\Context\\": "tests/behat/src/"
}
},
"extra": {
Expand Down
1 change: 1 addition & 0 deletions tests/behat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See https://github.com/silverstripe-labs/silverstripe-behat-extension
Empty file added tests/behat/_manifest_exclude
Empty file.
33 changes: 33 additions & 0 deletions tests/behat/features/revoke.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@retry

Feature: See other devices and revoke their access
As a CMS user
I want to see other devices that are currently logged in
So that I can revoke their access

Background:
Given I am logged in with "ADMIN" permissions
# Create a mock login session
And There is a login session for a second device

Scenario: I can see other devices and revoke their access
When I go to "/admin/security"
# Click the ADMIN user
And I click the ".col-FirstName" element
# Ensure XHR loaded from endpoint
And I wait until I see the ".login-session .text-success" element
# Assert text for the two login sessions
Then I should see the text "Current" in the ".login-session .text-success" element
Then I should see the text "Log out" in the ".login-session__logout" element
# Click "Log out" button
When I click the ".login-session__logout" element
# Wait for modal to fade in
And I wait until I see the ".modal-dialog .btn-primary" element
# Click the green button in the modal
When I click the ".modal-dialog .btn-primary" element
# Assert text has changed
Then I should see the text "Logging out..." in the ".login-session__logout" element
# Assert hidden element is applied which fades to not visible via a css transition
Then I see the ".login-session.hidden" element
# Assert toast notification
Then I should see a "Successfully logged out of device." success toast
1 change: 1 addition & 0 deletions tests/behat/files/testfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abc
9 changes: 9 additions & 0 deletions tests/behat/src/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace SilverStripe\SessionManager\Tests\Behat\Context;

use SilverStripe\BehatExtension\Context\SilverStripeContext;

class FeatureContext extends SilverStripeContext
{
}
74 changes: 74 additions & 0 deletions tests/behat/src/FixtureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace SilverStripe\SessionManager\Tests\Behat\Context;

use Behat\Mink\Element\NodeElement;
use SilverStripe\BehatExtension\Context\FixtureContext as BaseFixtureContext;
use SilverStripe\Control\Controller;
use SilverStripe\Security\Member;
use SilverStripe\SessionManager\Model\LoginSession;

/**
* Context used to create fixtures in the SilverStripe ORM.
*/
class FixtureContext extends BaseFixtureContext
{
/**
* @When /^I see the "([^"]+)" element$/
* @param $selector
*/
public function iSeeTheElement($selector): void
{
$page = $this->getMainContext()->getSession()->getPage();
$element = $page->find('css', $selector);
assertNotNull($element, sprintf('Element %s not found', $selector));
}

/**
* @When /^I click the "([^"]+)" element$/
* @param $selector
*/
public function iClickTheElement(string $selector): void
{
$page = $this->getMainContext()->getSession()->getPage();
$element = $page->find('css', $selector);
assertNotNull($element, sprintf('Element %s not found', $selector));
$element->click();
}

/**
* @When /^I should see the text "([^"]+)" in the "([^"]+)" element$/
* @param $selector
*/
public function iShouldSeeTheTextInTheElement(string $text, string $selector): void
{
$page = $this->getMainContext()->getSession()->getPage();
/** @var NodeElement $element */
$element = $page->find('css', $selector);
assertNotNull($element, sprintf('Element %s not found', $selector));
assertSame($text, $element->getText());
}

/**
* @Given /^There is a login session for a second device$/
*/
public function thereIsALoginSessionForASecondDevice(): void
{
$loginSession = $this->createLoginSession();
$loginSession->UserAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)';
$loginSession->write();
}

private function createLoginSession(): LoginSession
{
$request = Controller::curr()->getRequest();
return LoginSession::generate($this->getMember(), false, $request);
}

private function getMember(): Member
{
/** @var Member $member */
$member = Member::get()->find('FirstName', 'ADMIN');
return $member;
}
}

0 comments on commit 6d1d40b

Please sign in to comment.