123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Backend\Controller\Adminhtml;
- use Magento\Framework\Message\MessageInterface;
- /**
- * Test class for \Magento\Backend\Controller\Adminhtml\Auth
- * @magentoAppArea adminhtml
- * @magentoDbIsolation enabled
- */
- class AuthTest extends \Magento\TestFramework\TestCase\AbstractController
- {
- /**
- * @var \Magento\Backend\Model\Auth\Session
- */
- protected $_session;
- /**
- * @var \Magento\Backend\Model\Auth
- */
- protected $_auth;
- protected function tearDown()
- {
- $this->_session = null;
- $this->_auth = null;
- parent::tearDown();
- }
- /**
- * Performs user login
- */
- protected function _login()
- {
- \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
- \Magento\Backend\Model\UrlInterface::class
- )->turnOffSecretKey();
- $this->_auth = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
- \Magento\Backend\Model\Auth::class
- );
- $this->_auth->login(
- \Magento\TestFramework\Bootstrap::ADMIN_NAME,
- \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
- );
- $this->_session = $this->_auth->getAuthStorage();
- }
- /**
- * Performs user logout
- */
- protected function _logout()
- {
- $this->_auth->logout();
- \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
- \Magento\Backend\Model\UrlInterface::class
- )->turnOnSecretKey();
- }
- /**
- * Check not logged state
- * @covers \Magento\Backend\Controller\Adminhtml\Auth\Login::execute
- */
- public function testNotLoggedLoginAction()
- {
- $this->dispatch('backend/admin/auth/login');
- /** @var $backendUrlModel \Magento\Backend\Model\UrlInterface */
- $backendUrlModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
- \Magento\Backend\Model\UrlInterface::class
- );
- $backendUrlModel->turnOffSecretKey();
- $url = $backendUrlModel->getUrl('admin');
- $this->assertRedirect($this->stringStartsWith($url));
- }
- /**
- * Check logged state
- * @covers \Magento\Backend\Controller\Adminhtml\Auth\Login::execute
- * @magentoDbIsolation enabled
- */
- public function testLoggedLoginAction()
- {
- $this->_login();
- $this->dispatch('backend/admin/auth/login');
- /** @var $backendUrlModel \Magento\Backend\Model\UrlInterface */
- $backendUrlModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
- \Magento\Backend\Model\UrlInterface::class
- );
- $url = $backendUrlModel->getStartupPageUrl();
- $expected = $backendUrlModel->getUrl($url);
- $this->assertRedirect($this->stringStartsWith($expected));
- $this->_logout();
- }
- /**
- * @magentoAppIsolation enabled
- */
- public function testNotLoggedLoginActionWithRedirect()
- {
- /** @var \Magento\Framework\Data\Form\FormKey $formKey */
- $formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
- $this->getRequest()->setPostValue(
- [
- 'login' => [
- 'username' => \Magento\TestFramework\Bootstrap::ADMIN_NAME,
- 'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
- ],
- 'form_key' => $formKey->getFormKey(),
- ]
- );
- $this->dispatch('backend/admin/index/index');
- $response = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
- ->get(\Magento\Framework\App\ResponseInterface::class);
- $code = $response->getHttpResponseCode();
- $this->assertTrue($code >= 300 && $code < 400, 'Incorrect response code');
- $this->assertTrue(
- \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
- \Magento\Backend\Model\Auth::class
- )->isLoggedIn()
- );
- }
- /**
- * @covers \Magento\Backend\Controller\Adminhtml\Auth\Logout::execute
- * @magentoDbIsolation enabled
- */
- public function testLogoutAction()
- {
- $this->_login();
- $this->dispatch('backend/admin/auth/logout');
- $this->assertRedirect(
- $this->equalTo(
- \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
- \Magento\Backend\Helper\Data::class
- )->getHomePageUrl()
- )
- );
- $this->assertFalse($this->_session->isLoggedIn(), 'User is not logged out.');
- }
- /**
- * @covers \Magento\Backend\Controller\Adminhtml\Auth\DeniedJson::execute
- * @covers \Magento\Backend\Controller\Adminhtml\Auth\DeniedJson::_getDeniedJson
- * @magentoDbIsolation enabled
- */
- public function testDeniedJsonAction()
- {
- $this->_login();
- $this->dispatch('backend/admin/auth/deniedJson');
- $data = [
- 'ajaxExpired' => 1,
- 'ajaxRedirect' => \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
- \Magento\Backend\Helper\Data::class
- )->getHomePageUrl(),
- ];
- $expected = json_encode($data);
- $this->assertEquals($expected, $this->getResponse()->getBody());
- $this->_logout();
- }
- /**
- * @covers \Magento\Backend\Controller\Adminhtml\Auth\DeniedIframe::execute
- * @covers \Magento\Backend\Controller\Adminhtml\Auth\DeniedIframe::_getDeniedIframe
- * @magentoDbIsolation enabled
- */
- public function testDeniedIframeAction()
- {
- $this->_login();
- $this->dispatch('backend/admin/auth/deniedIframe');
- $homeUrl = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
- \Magento\Backend\Helper\Data::class
- )->getHomePageUrl();
- $expected = '<script>parent.window.location =';
- $this->assertStringStartsWith($expected, $this->getResponse()->getBody());
- $this->assertContains($homeUrl, $this->getResponse()->getBody());
- $this->_logout();
- }
- /**
- * Test user logging process when user not assigned to any role
- * @dataProvider incorrectLoginDataProvider
- * @magentoDbIsolation enabled
- *
- * @param $params
- */
- public function testIncorrectLogin($params)
- {
- /** @var \Magento\Framework\Data\Form\FormKey $formKey */
- $formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
- $params['form_key'] = $formKey->getFormKey();
- $this->getRequest()->setPostValue($params);
- $this->dispatch('backend/admin/auth/login');
- $this->assertSessionMessages(
- $this->equalTo(
- [
- 'The account sign-in was incorrect or your account is disabled temporarily. '
- . 'Please wait and try again later.'
- ]
- ),
- MessageInterface::TYPE_ERROR
- );
- $backendUrlModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
- \Magento\Backend\Model\UrlInterface::class
- );
- $backendUrlModel->turnOffSecretKey();
- $url = $backendUrlModel->getUrl('admin');
- $this->assertRedirect($this->stringStartsWith($url));
- }
- public function incorrectLoginDataProvider()
- {
- return [
- 'login dummy user' => [
- [
- 'login' => [
- 'username' => 'test1',
- 'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
- ],
- ],
- ],
- 'login without role' => [
- [
- 'login' => [
- 'username' => 'test2',
- 'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
- ],
- ],
- ],
- 'login not active user' => [
- [
- 'login' => [
- 'username' => 'test3',
- 'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
- ],
- ],
- ]
- ];
- }
- }
|