From b4e3874192a76159f4ffe397d70b6cc4082efb53 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Mon, 12 Apr 2021 11:59:59 +1200 Subject: [PATCH] MNT Add behat to module --- .editorconfig | 2 +- .travis.yml | 2 +- behat.yml | 33 +++++++++++++ composer.json | 3 +- tests/behat/README.md | 1 + tests/behat/_manifest_exclude | 0 tests/behat/features/revoke.feature | 34 +++++++++++++ tests/behat/files/testfile.txt | 1 + tests/behat/src/FeatureContext.php | 9 ++++ tests/behat/src/FixtureContext.php | 74 +++++++++++++++++++++++++++++ 10 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 behat.yml create mode 100644 tests/behat/README.md create mode 100644 tests/behat/_manifest_exclude create mode 100644 tests/behat/features/revoke.feature create mode 100644 tests/behat/files/testfile.txt create mode 100644 tests/behat/src/FeatureContext.php create mode 100644 tests/behat/src/FixtureContext.php diff --git a/.editorconfig b/.editorconfig index 47ae637..601aa50 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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: diff --git a/.travis.yml b/.travis.yml index 1e39bbb..7cda337 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/behat.yml b/behat.yml new file mode 100644 index 0000000..085a932 --- /dev/null +++ b/behat.yml @@ -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 diff --git a/composer.json b/composer.json index da8a9cd..bc070b6 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/tests/behat/README.md b/tests/behat/README.md new file mode 100644 index 0000000..c2fb16c --- /dev/null +++ b/tests/behat/README.md @@ -0,0 +1 @@ +See https://github.com/silverstripe-labs/silverstripe-behat-extension diff --git a/tests/behat/_manifest_exclude b/tests/behat/_manifest_exclude new file mode 100644 index 0000000..e69de29 diff --git a/tests/behat/features/revoke.feature b/tests/behat/features/revoke.feature new file mode 100644 index 0000000..a2c05c0 --- /dev/null +++ b/tests/behat/features/revoke.feature @@ -0,0 +1,34 @@ +@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 + And I wait until I see the ".toast__content" element + Then I should see the text "Successfully logged out of device." in the ".toast__content" element diff --git a/tests/behat/files/testfile.txt b/tests/behat/files/testfile.txt new file mode 100644 index 0000000..8baef1b --- /dev/null +++ b/tests/behat/files/testfile.txt @@ -0,0 +1 @@ +abc diff --git a/tests/behat/src/FeatureContext.php b/tests/behat/src/FeatureContext.php new file mode 100644 index 0000000..250d66c --- /dev/null +++ b/tests/behat/src/FeatureContext.php @@ -0,0 +1,9 @@ +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; + } +}