AuthTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Controller\Adminhtml;
  7. use Magento\Framework\Message\MessageInterface;
  8. /**
  9. * Test class for \Magento\Backend\Controller\Adminhtml\Auth
  10. * @magentoAppArea adminhtml
  11. * @magentoDbIsolation enabled
  12. */
  13. class AuthTest extends \Magento\TestFramework\TestCase\AbstractController
  14. {
  15. /**
  16. * @var \Magento\Backend\Model\Auth\Session
  17. */
  18. protected $_session;
  19. /**
  20. * @var \Magento\Backend\Model\Auth
  21. */
  22. protected $_auth;
  23. protected function tearDown()
  24. {
  25. $this->_session = null;
  26. $this->_auth = null;
  27. parent::tearDown();
  28. }
  29. /**
  30. * Performs user login
  31. */
  32. protected function _login()
  33. {
  34. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  35. \Magento\Backend\Model\UrlInterface::class
  36. )->turnOffSecretKey();
  37. $this->_auth = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  38. \Magento\Backend\Model\Auth::class
  39. );
  40. $this->_auth->login(
  41. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  42. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  43. );
  44. $this->_session = $this->_auth->getAuthStorage();
  45. }
  46. /**
  47. * Performs user logout
  48. */
  49. protected function _logout()
  50. {
  51. $this->_auth->logout();
  52. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  53. \Magento\Backend\Model\UrlInterface::class
  54. )->turnOnSecretKey();
  55. }
  56. /**
  57. * Check not logged state
  58. * @covers \Magento\Backend\Controller\Adminhtml\Auth\Login::execute
  59. */
  60. public function testNotLoggedLoginAction()
  61. {
  62. $this->dispatch('backend/admin/auth/login');
  63. /** @var $backendUrlModel \Magento\Backend\Model\UrlInterface */
  64. $backendUrlModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  65. \Magento\Backend\Model\UrlInterface::class
  66. );
  67. $backendUrlModel->turnOffSecretKey();
  68. $url = $backendUrlModel->getUrl('admin');
  69. $this->assertRedirect($this->stringStartsWith($url));
  70. }
  71. /**
  72. * Check logged state
  73. * @covers \Magento\Backend\Controller\Adminhtml\Auth\Login::execute
  74. * @magentoDbIsolation enabled
  75. */
  76. public function testLoggedLoginAction()
  77. {
  78. $this->_login();
  79. $this->dispatch('backend/admin/auth/login');
  80. /** @var $backendUrlModel \Magento\Backend\Model\UrlInterface */
  81. $backendUrlModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  82. \Magento\Backend\Model\UrlInterface::class
  83. );
  84. $url = $backendUrlModel->getStartupPageUrl();
  85. $expected = $backendUrlModel->getUrl($url);
  86. $this->assertRedirect($this->stringStartsWith($expected));
  87. $this->_logout();
  88. }
  89. /**
  90. * @magentoAppIsolation enabled
  91. */
  92. public function testNotLoggedLoginActionWithRedirect()
  93. {
  94. /** @var \Magento\Framework\Data\Form\FormKey $formKey */
  95. $formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
  96. $this->getRequest()->setPostValue(
  97. [
  98. 'login' => [
  99. 'username' => \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  100. 'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
  101. ],
  102. 'form_key' => $formKey->getFormKey(),
  103. ]
  104. );
  105. $this->dispatch('backend/admin/index/index');
  106. $response = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  107. ->get(\Magento\Framework\App\ResponseInterface::class);
  108. $code = $response->getHttpResponseCode();
  109. $this->assertTrue($code >= 300 && $code < 400, 'Incorrect response code');
  110. $this->assertTrue(
  111. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  112. \Magento\Backend\Model\Auth::class
  113. )->isLoggedIn()
  114. );
  115. }
  116. /**
  117. * @covers \Magento\Backend\Controller\Adminhtml\Auth\Logout::execute
  118. * @magentoDbIsolation enabled
  119. */
  120. public function testLogoutAction()
  121. {
  122. $this->_login();
  123. $this->dispatch('backend/admin/auth/logout');
  124. $this->assertRedirect(
  125. $this->equalTo(
  126. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  127. \Magento\Backend\Helper\Data::class
  128. )->getHomePageUrl()
  129. )
  130. );
  131. $this->assertFalse($this->_session->isLoggedIn(), 'User is not logged out.');
  132. }
  133. /**
  134. * @covers \Magento\Backend\Controller\Adminhtml\Auth\DeniedJson::execute
  135. * @covers \Magento\Backend\Controller\Adminhtml\Auth\DeniedJson::_getDeniedJson
  136. * @magentoDbIsolation enabled
  137. */
  138. public function testDeniedJsonAction()
  139. {
  140. $this->_login();
  141. $this->dispatch('backend/admin/auth/deniedJson');
  142. $data = [
  143. 'ajaxExpired' => 1,
  144. 'ajaxRedirect' => \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  145. \Magento\Backend\Helper\Data::class
  146. )->getHomePageUrl(),
  147. ];
  148. $expected = json_encode($data);
  149. $this->assertEquals($expected, $this->getResponse()->getBody());
  150. $this->_logout();
  151. }
  152. /**
  153. * @covers \Magento\Backend\Controller\Adminhtml\Auth\DeniedIframe::execute
  154. * @covers \Magento\Backend\Controller\Adminhtml\Auth\DeniedIframe::_getDeniedIframe
  155. * @magentoDbIsolation enabled
  156. */
  157. public function testDeniedIframeAction()
  158. {
  159. $this->_login();
  160. $this->dispatch('backend/admin/auth/deniedIframe');
  161. $homeUrl = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  162. \Magento\Backend\Helper\Data::class
  163. )->getHomePageUrl();
  164. $expected = '<script>parent.window.location =';
  165. $this->assertStringStartsWith($expected, $this->getResponse()->getBody());
  166. $this->assertContains($homeUrl, $this->getResponse()->getBody());
  167. $this->_logout();
  168. }
  169. /**
  170. * Test user logging process when user not assigned to any role
  171. * @dataProvider incorrectLoginDataProvider
  172. * @magentoDbIsolation enabled
  173. *
  174. * @param $params
  175. */
  176. public function testIncorrectLogin($params)
  177. {
  178. /** @var \Magento\Framework\Data\Form\FormKey $formKey */
  179. $formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
  180. $params['form_key'] = $formKey->getFormKey();
  181. $this->getRequest()->setPostValue($params);
  182. $this->dispatch('backend/admin/auth/login');
  183. $this->assertSessionMessages(
  184. $this->equalTo(
  185. [
  186. 'The account sign-in was incorrect or your account is disabled temporarily. '
  187. . 'Please wait and try again later.'
  188. ]
  189. ),
  190. MessageInterface::TYPE_ERROR
  191. );
  192. $backendUrlModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  193. \Magento\Backend\Model\UrlInterface::class
  194. );
  195. $backendUrlModel->turnOffSecretKey();
  196. $url = $backendUrlModel->getUrl('admin');
  197. $this->assertRedirect($this->stringStartsWith($url));
  198. }
  199. public function incorrectLoginDataProvider()
  200. {
  201. return [
  202. 'login dummy user' => [
  203. [
  204. 'login' => [
  205. 'username' => 'test1',
  206. 'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
  207. ],
  208. ],
  209. ],
  210. 'login without role' => [
  211. [
  212. 'login' => [
  213. 'username' => 'test2',
  214. 'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
  215. ],
  216. ],
  217. ],
  218. 'login not active user' => [
  219. [
  220. 'login' => [
  221. 'username' => 'test3',
  222. 'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
  223. ],
  224. ],
  225. ]
  226. ];
  227. }
  228. }