From 1203369ea6a6bb1a9afb03fd390f1c6342f54aa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Tue, 4 Apr 2017 17:20:19 +0200 Subject: [PATCH] Add acceptance tests related to login MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Calviño Sánchez --- build/acceptance/config/behat.yml | 9 ++ .../features/bootstrap/FeatureContext.php | 7 + .../features/bootstrap/FilesAppContext.php | 39 +++++ .../features/bootstrap/LoginPageContext.php | 137 ++++++++++++++++++ .../bootstrap/NotificationContext.php | 54 +++++++ .../bootstrap/SettingsMenuContext.php | 76 ++++++++++ .../bootstrap/UsersSettingsContext.php | 102 +++++++++++++ build/acceptance/features/login.feature | 47 ++++++ 8 files changed, 471 insertions(+) create mode 100644 build/acceptance/features/bootstrap/FilesAppContext.php create mode 100644 build/acceptance/features/bootstrap/LoginPageContext.php create mode 100644 build/acceptance/features/bootstrap/NotificationContext.php create mode 100644 build/acceptance/features/bootstrap/SettingsMenuContext.php create mode 100644 build/acceptance/features/bootstrap/UsersSettingsContext.php create mode 100644 build/acceptance/features/login.feature diff --git a/build/acceptance/config/behat.yml b/build/acceptance/config/behat.yml index 186a57c2023..6c3d9e4a7b9 100644 --- a/build/acceptance/config/behat.yml +++ b/build/acceptance/config/behat.yml @@ -10,8 +10,17 @@ default: - NextcloudTestServerContext - FeatureContext + - FilesAppContext + - LoginPageContext + - NotificationContext + - SettingsMenuContext + - UsersSettingsContext extensions: Behat\MinkExtension: sessions: default: selenium2: ~ + John: + selenium2: ~ + Jane: + selenium2: ~ diff --git a/build/acceptance/features/bootstrap/FeatureContext.php b/build/acceptance/features/bootstrap/FeatureContext.php index 0598cbae959..a125ea01ccc 100644 --- a/build/acceptance/features/bootstrap/FeatureContext.php +++ b/build/acceptance/features/bootstrap/FeatureContext.php @@ -27,4 +27,11 @@ class FeatureContext implements Context, ActorAwareInterface { use ActorAware; + /** + * @When I visit the Home page + */ + public function iVisitTheHomePage() { + $this->actor->getSession()->visit($this->actor->locatePath("/")); + } + } diff --git a/build/acceptance/features/bootstrap/FilesAppContext.php b/build/acceptance/features/bootstrap/FilesAppContext.php new file mode 100644 index 00000000000..9702e64b552 --- /dev/null +++ b/build/acceptance/features/bootstrap/FilesAppContext.php @@ -0,0 +1,39 @@ +. + * + */ + +use Behat\Behat\Context\Context; + +class FilesAppContext implements Context, ActorAwareInterface { + + use ActorAware; + + /** + * @Then I see that the current page is the Files app + */ + public function iSeeThatTheCurrentPageIsTheFilesApp() { + PHPUnit_Framework_Assert::assertStringStartsWith( + $this->actor->locatePath("/apps/files/"), + $this->actor->getSession()->getCurrentUrl()); + } + +} diff --git a/build/acceptance/features/bootstrap/LoginPageContext.php b/build/acceptance/features/bootstrap/LoginPageContext.php new file mode 100644 index 00000000000..55db8c33bb7 --- /dev/null +++ b/build/acceptance/features/bootstrap/LoginPageContext.php @@ -0,0 +1,137 @@ +. + * + */ + +use Behat\Behat\Context\Context; +use Behat\Behat\Hook\Scope\BeforeScenarioScope; + +class LoginPageContext implements Context, ActorAwareInterface { + + use ActorAware; + + /** + * @var FeatureContext + */ + private $featureContext; + + /** + * @var FilesAppContext + */ + private $filesAppContext; + + /** + * @return Locator + */ + public static function userNameField() { + return Locator::forThe()->field("user")-> + describedAs("User name field in Login page"); + } + + /** + * @return Locator + */ + public static function passwordField() { + return Locator::forThe()->field("password")-> + describedAs("Password field in Login page"); + } + + /** + * @return Locator + */ + public static function loginButton() { + return Locator::forThe()->id("submit")-> + describedAs("Login button in Login page"); + } + + /** + * @return Locator + */ + public static function wrongPasswordMessage() { + return Locator::forThe()->content("Wrong password. Reset it?")-> + describedAs("Wrong password message in Login page"); + } + + /** + * @When I log in with user :user and password :password + */ + public function iLogInWithUserAndPassword($user, $password) { + $this->actor->find(self::userNameField(), 10)->setValue($user); + $this->actor->find(self::passwordField())->setValue($password); + $this->actor->find(self::loginButton())->click(); + } + + /** + * @Then I see that the current page is the Login page + */ + public function iSeeThatTheCurrentPageIsTheLoginPage() { + PHPUnit_Framework_Assert::assertStringStartsWith( + $this->actor->locatePath("/login"), + $this->actor->getSession()->getCurrentUrl()); + } + + /** + * @Then I see that a wrong password message is shown + */ + public function iSeeThatAWrongPasswordMessageIsShown() { + PHPUnit_Framework_Assert::assertTrue( + $this->actor->find(self::wrongPasswordMessage(), 10)->isVisible()); + } + + /** + * @BeforeScenario + */ + public function getOtherRequiredSiblingContexts(BeforeScenarioScope $scope) { + $environment = $scope->getEnvironment(); + + $this->featureContext = $environment->getContext("FeatureContext"); + $this->filesAppContext = $environment->getContext("FilesAppContext"); + } + + /** + * @Given I am logged in + */ + public function iAmLoggedIn() { + $this->featureContext->iVisitTheHomePage(); + $this->iLogInWithUserAndPassword("user0", "123456"); + $this->filesAppContext->iSeeThatTheCurrentPageIsTheFilesApp(); + } + + /** + * @Given I am logged in as the admin + */ + public function iAmLoggedInAsTheAdmin() { + $this->featureContext->iVisitTheHomePage(); + $this->iLogInWithUserAndPassword("admin", "admin"); + $this->filesAppContext->iSeeThatTheCurrentPageIsTheFilesApp(); + } + + /** + * @Given I can not log in with user :user and password :password + */ + public function iCanNotLogInWithUserAndPassword($user, $password) { + $this->featureContext->iVisitTheHomePage(); + $this->iLogInWithUserAndPassword($user, $password); + $this->iSeeThatTheCurrentPageIsTheLoginPage(); + $this->iSeeThatAWrongPasswordMessageIsShown(); + } + +} diff --git a/build/acceptance/features/bootstrap/NotificationContext.php b/build/acceptance/features/bootstrap/NotificationContext.php new file mode 100644 index 00000000000..f8b784e2465 --- /dev/null +++ b/build/acceptance/features/bootstrap/NotificationContext.php @@ -0,0 +1,54 @@ +. + * + */ + +use Behat\Behat\Context\Context; + +class NotificationContext implements Context, ActorAwareInterface { + + use ActorAware; + + /** + * @return Locator + */ + public static function notificationMessage($message) { + return Locator::forThe()->content($message)->descendantOf(self::notificationContainer())-> + describedAs("$message notification"); + } + + /** + * @return Locator + */ + private static function notificationContainer() { + return Locator::forThe()->id("notification-container")-> + describedAs("Notification container"); + } + + /** + * @Then I see that the :message notification is shown + */ + public function iSeeThatTheNotificationIsShown($message) { + PHPUnit_Framework_Assert::assertTrue($this->actor->find( + self::notificationMessage($message), 10)->isVisible()); + } + +} diff --git a/build/acceptance/features/bootstrap/SettingsMenuContext.php b/build/acceptance/features/bootstrap/SettingsMenuContext.php new file mode 100644 index 00000000000..78a29b08a10 --- /dev/null +++ b/build/acceptance/features/bootstrap/SettingsMenuContext.php @@ -0,0 +1,76 @@ +. + * + */ + +use Behat\Behat\Context\Context; + +class SettingsMenuContext implements Context, ActorAwareInterface { + + use ActorAware; + + /** + * @return Locator + */ + public static function settingsMenuButton() { + return Locator::forThe()->xpath("//*[@id = 'header']//*[@id = 'settings']")-> + describedAs("Settings menu button"); + } + + /** + * @return Locator + */ + public static function usersMenuItem() { + return self::menuItemFor("Users"); + } + + /** + * @return Locator + */ + public static function logOutMenuItem() { + return self::menuItemFor("Log out"); + } + + /** + * @return Locator + */ + private static function menuItemFor($itemText) { + return Locator::forThe()->content($itemText)->descendantOf(self::settingsMenuButton())-> + describedAs($itemText . " item in Settings menu"); + } + + /** + * @When I open the User settings + */ + public function iOpenTheUserSettings() { + $this->actor->find(self::settingsMenuButton(), 10)->click(); + $this->actor->find(self::usersMenuItem(), 2)->click(); + } + + /** + * @When I log out + */ + public function iLogOut() { + $this->actor->find(self::settingsMenuButton(), 10)->click(); + $this->actor->find(self::logOutMenuItem(), 2)->click(); + } + +} diff --git a/build/acceptance/features/bootstrap/UsersSettingsContext.php b/build/acceptance/features/bootstrap/UsersSettingsContext.php new file mode 100644 index 00000000000..93ab7246eb6 --- /dev/null +++ b/build/acceptance/features/bootstrap/UsersSettingsContext.php @@ -0,0 +1,102 @@ +. + * + */ + +use Behat\Behat\Context\Context; + +class UsersSettingsContext implements Context, ActorAwareInterface { + + use ActorAware; + + /** + * @return Locator + */ + public static function userNameFieldForNewUser() { + return Locator::forThe()->field("newusername")-> + describedAs("User name field for new user in Users Settings"); + } + + /** + * @return Locator + */ + public static function passwordFieldForNewUser() { + return Locator::forThe()->field("newuserpassword")-> + describedAs("Password field for new user in Users Settings"); + } + + /** + * @return Locator + */ + public static function createNewUserButton() { + return Locator::forThe()->xpath("//form[@id = 'newuser']//input[@type = 'submit']")-> + describedAs("Create user button in Users Settings"); + } + + /** + * @return Locator + */ + public static function rowForUser($user) { + return Locator::forThe()->xpath("//table[@id = 'userlist']//th[normalize-space() = '$user']/..")-> + describedAs("Row for user $user in Users Settings"); + } + + /** + * @return Locator + */ + public static function passwordCellForUser($user) { + return Locator::forThe()->css(".password")->descendantOf(self::rowForUser($user))-> + describedAs("Password cell for user $user in Users Settings"); + } + + /** + * @return Locator + */ + public static function passwordInputForUser($user) { + return Locator::forThe()->css("input")->descendantOf(self::passwordCellForUser($user))-> + describedAs("Password input for user $user in Users Settings"); + } + + /** + * @When I create user :user with password :password + */ + public function iCreateUserWithPassword($user, $password) { + $this->actor->find(self::userNameFieldForNewUser(), 10)->setValue($user); + $this->actor->find(self::passwordFieldForNewUser())->setValue($password); + $this->actor->find(self::createNewUserButton())->click(); + } + + /** + * @When I set the password for :user to :password + */ + public function iSetThePasswordForUserTo($user, $password) { + $this->actor->find(self::passwordCellForUser($user), 10)->click(); + $this->actor->find(self::passwordInputForUser($user), 2)->setValue($password . "\r"); + } + + /** + * @Then I see that the list of users contains the user :user + */ + public function iSeeThatTheListOfUsersContainsTheUser($user) { + PHPUnit_Framework_Assert::assertNotNull($this->actor->find(self::rowForUser($user), 10)); + } + +} diff --git a/build/acceptance/features/login.feature b/build/acceptance/features/login.feature new file mode 100644 index 00000000000..c4cd2add8e6 --- /dev/null +++ b/build/acceptance/features/login.feature @@ -0,0 +1,47 @@ +Feature: login + + Scenario: log in with valid user and password + Given I visit the Home page + When I log in with user user0 and password 123456 + Then I see that the current page is the Files app + + Scenario: try to log in with valid user and invalid password + Given I visit the Home page + When I log in with user user0 and password 654321 + Then I see that the current page is the Login page + And I see that a wrong password message is shown + + Scenario: log in with valid user and invalid password once fixed by admin + Given I act as John + And I can not log in with user user0 and password 654231 + When I act as Jane + And I am logged in as the admin + And I open the User settings + And I set the password for user0 to 654321 + And I see that the "Password successfully changed" notification is shown + And I act as John + And I log in with user user0 and password 654321 + Then I see that the current page is the Files app + + Scenario: try to log in with invalid user + Given I visit the Home page + When I log in with user unknownUser and password 123456 + Then I see that the current page is the Login page + And I see that a wrong password message is shown + + Scenario: log in with invalid user once fixed by admin + Given I act as John + And I can not log in with user unknownUser and password 123456 + When I act as Jane + And I am logged in as the admin + And I open the User settings + And I create user unknownUser with password 123456 + And I see that the list of users contains the user unknownUser + And I act as John + And I log in with user unknownUser and password 123456 + Then I see that the current page is the Files app + + Scenario: log out + Given I am logged in + When I log out + Then I see that the current page is the Login page