From 39c1079f8ce433b4e6036ddd0ac07e66246068c0 Mon Sep 17 00:00:00 2001 From: sergeysviridenko Date: Wed, 16 Aug 2017 14:03:52 +0300 Subject: [PATCH] Added acceptance authorization tests and fixed auth_redirect --- app/library/Github/OAuth.php | 26 ++++++++- tests/acceptance/AuthorizationCest.php | 73 ++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) mode change 100644 => 100755 app/library/Github/OAuth.php create mode 100755 tests/acceptance/AuthorizationCest.php diff --git a/app/library/Github/OAuth.php b/app/library/Github/OAuth.php old mode 100644 new mode 100755 index 9b451a8d..e93d7619 --- a/app/library/Github/OAuth.php +++ b/app/library/Github/OAuth.php @@ -46,10 +46,10 @@ class OAuth extends Injectable */ public function __construct(Config $config) { - $this->redirectUriAuthorize = $config->get('redirectUri'); + $this->logger = $this->getDI()->get('logger', ['auth']); + $this->redirectUriAuthorize = $this->getRedirectGitPath($config->get('redirectUri')); $this->clientId = $config->get('clientId'); $this->clientSecret = $config->get('clientSecret'); - $this->logger = $this->getDI()->get('logger', ['auth']); } public function authorize() @@ -132,4 +132,26 @@ public function send($url, $parameters, $method = 'post') return false; } } + + /** + * @param string $url + * + * @return string + */ + protected function getRedirectGitPath($url) + { + if (empty($url)) { + $this->logger->error("Parameter GITHUB_REDIRECT_URI in .env file doesn't exist"); + } + + $redirectUrl = rtrim($url, '/'); + if (stristr($redirectUrl, '://')) { + $redirectUrl = stristr($redirectUrl, '://'); + $redirectUrl = $_SERVER['REQUEST_SCHEME'] . $redirectUrl . '/'; + } else { + $redirectUrl = $_SERVER['REQUEST_SCHEME'] . '://' . $redirectUrl . '/'; + } + + return $redirectUrl; + } } diff --git a/tests/acceptance/AuthorizationCest.php b/tests/acceptance/AuthorizationCest.php new file mode 100755 index 00000000..a4325aed --- /dev/null +++ b/tests/acceptance/AuthorizationCest.php @@ -0,0 +1,73 @@ + | + +------------------------------------------------------------------------+ +*/ + +class AuthorizationCest +{ + public function shouldCheckAutorizationOnForumWithGit(AcceptanceTester $I) + { + $I->wantTo('check authorization on the forum with github'); + + $I->amOnPage('/discussions/hot'); + $I->click('Log In'); + $I->fillField('login', 'test_application@ukr.net'); + $I->fillField('password', 'test123456'); + $I->click('Sign in'); + + if (in_array('Reauthorization required', $I->grabMultiple('h2'))) { + $I->click('#js-oauth-authorize-btn'); + } + + $I->see('Welcome'); + $I->canSeeInCurrentUrl('/discussions'); + + $I->amOnPage('/discussions/hot'); + $I->click(".//*[@id='forum-navbar-collapse']/ul/li[8]/a"); + $I->see('Goodbye!'); + } + + public function shouldAuthorizationOnGitAndForum(AcceptanceTester $I) + { + $I->wantTo('check authorization on the github after that on the forum'); + + $I->amOnUrl('https://github.com'); + $I->amOnPage('/login'); + $I->fillField('login', 'test_application@ukr.net'); + $I->fillField('password', 'test123456'); + $I->click('commit'); + $I->see('Learn Git and GitHub without any code!'); + + $I->amOnUrl($_SERVER['APP_URL']); + $I->amOnPage('/'); + $I->see('Hot'); + $I->amOnPage('/discussions/hot'); + $I->canSeeInCurrentUrl('/discussions/hot'); + + $I->click('Log In'); + if (in_array('Reauthorization required', $I->grabMultiple('h2'))) { + $I->click('#js-oauth-authorize-btn'); + } + + $I->see('Welcome'); + $I->canSeeInCurrentUrl('/discussions'); + + $I->amOnPage('/discussions/hot'); + $I->click(".//*[@id='forum-navbar-collapse']/ul/li[8]/a"); + $I->see('Goodbye!'); + } +}