AuthTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Model;
  7. use Magento\Framework\Data\Form\FormKey;
  8. use Magento\Framework\Exception\AuthenticationException;
  9. /**
  10. * Test class for \Magento\Backend\Model\Auth.
  11. *
  12. * @magentoAppArea adminhtml
  13. * @magentoAppIsolation enabled
  14. * @magentoDbIsolation enabled
  15. */
  16. class AuthTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var \Magento\Backend\Model\Auth
  20. */
  21. protected $_model;
  22. protected function setUp()
  23. {
  24. parent::setUp();
  25. \Magento\TestFramework\Helper\Bootstrap::getInstance()
  26. ->loadArea(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE);
  27. $this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  28. ->create(\Magento\Backend\Model\Auth::class);
  29. }
  30. /**
  31. * @dataProvider getLoginDataProvider
  32. * @param string $userName
  33. * @param string $password
  34. * @expectedException \Magento\Framework\Exception\AuthenticationException
  35. */
  36. public function testLoginFailed($userName, $password)
  37. {
  38. $this->_model->login($userName, $password);
  39. }
  40. public function getLoginDataProvider()
  41. {
  42. return [
  43. 'Invalid credentials' => ['not_exists', 'not_exists'],
  44. 'Empty credentials' => ['', 'not_exists']
  45. ];
  46. }
  47. public function testSetGetAuthStorage()
  48. {
  49. // by default \Magento\Backend\Model\Auth\Session class will instantiate as a Authentication Storage
  50. $this->assertInstanceOf(\Magento\Backend\Model\Auth\Session::class, $this->_model->getAuthStorage());
  51. $mockStorage = $this->createMock(\Magento\Backend\Model\Auth\StorageInterface::class);
  52. $this->_model->setAuthStorage($mockStorage);
  53. $this->assertInstanceOf(\Magento\Backend\Model\Auth\StorageInterface::class, $this->_model->getAuthStorage());
  54. $incorrectStorage = new \StdClass();
  55. try {
  56. $this->_model->setAuthStorage($incorrectStorage);
  57. $this->fail('Incorrect authentication storage setted.');
  58. } catch (AuthenticationException $e) {
  59. // in case of exception - Auth works correct
  60. $this->assertNotEmpty($e->getMessage());
  61. }
  62. }
  63. public function testGetCredentialStorageList()
  64. {
  65. $storage = $this->_model->getCredentialStorage();
  66. $this->assertInstanceOf(\Magento\Backend\Model\Auth\Credential\StorageInterface::class, $storage);
  67. }
  68. public function testLoginSuccessful()
  69. {
  70. $this->_model->login(
  71. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  72. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  73. );
  74. $this->assertInstanceOf(
  75. \Magento\Backend\Model\Auth\Credential\StorageInterface::class,
  76. $this->_model->getUser()
  77. );
  78. $this->assertGreaterThan(time() - 10, $this->_model->getAuthStorage()->getUpdatedAt());
  79. }
  80. public function testLoginFlushesFormKey()
  81. {
  82. /** @var FormKey $dataFormKey */
  83. $dataFormKey = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(FormKey::class);
  84. $beforeKey = $dataFormKey->getFormKey();
  85. $this->_model->login(
  86. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  87. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  88. );
  89. $afterKey = $dataFormKey->getFormKey();
  90. $this->assertNotEquals($beforeKey, $afterKey);
  91. }
  92. /**
  93. * @magentoAppIsolation enabled
  94. */
  95. public function testLogout()
  96. {
  97. $this->_model->login(
  98. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  99. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  100. );
  101. $this->assertNotEmpty($this->_model->getAuthStorage()->getData());
  102. $this->_model->logout();
  103. $this->assertEmpty($this->_model->getAuthStorage()->getData());
  104. }
  105. /**
  106. * Disabled form security in order to prevent exit from the app
  107. * @magentoAdminConfigFixture admin/security/session_lifetime 100
  108. */
  109. public function testIsLoggedIn()
  110. {
  111. $this->_model->login(
  112. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  113. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  114. );
  115. $this->assertTrue($this->_model->isLoggedIn());
  116. }
  117. public function testGetUser()
  118. {
  119. $this->_model->login(
  120. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  121. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  122. );
  123. $this->assertNotNull($this->_model->getUser());
  124. $this->assertGreaterThan(0, $this->_model->getUser()->getId());
  125. $this->assertInstanceOf(
  126. \Magento\Backend\Model\Auth\Credential\StorageInterface::class,
  127. $this->_model->getUser()
  128. );
  129. }
  130. }