AuthSessionTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Model\Plugin;
  7. /**
  8. * @magentoAppIsolation enabled
  9. */
  10. class AuthSessionTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Backend\Model\Auth
  14. */
  15. protected $auth;
  16. /**
  17. * @var \Magento\Backend\Model\Auth\Session
  18. */
  19. protected $authSession;
  20. /**
  21. * @var \Magento\Security\Model\AdminSessionInfo
  22. */
  23. protected $adminSessionInfo;
  24. /**
  25. * @var \Magento\Security\Model\AdminSessionsManager
  26. */
  27. protected $adminSessionsManager;
  28. /**
  29. * @var \Magento\Framework\ObjectManagerInterface
  30. */
  31. protected $objectManager;
  32. /**
  33. * @var \Magento\Framework\Stdlib\DateTime
  34. */
  35. protected $dateTime;
  36. /**
  37. * @var \Magento\Security\Model\ConfigInterface
  38. */
  39. protected $securityConfig;
  40. /**
  41. * Set up
  42. */
  43. protected function setUp()
  44. {
  45. parent::setUp();
  46. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  47. $this->objectManager->get(\Magento\Framework\Config\ScopeInterface::class)
  48. ->setCurrentScope(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE);
  49. $this->auth = $this->objectManager->create(\Magento\Backend\Model\Auth::class);
  50. $this->authSession = $this->objectManager->create(\Magento\Backend\Model\Auth\Session::class);
  51. $this->adminSessionInfo = $this->objectManager->create(\Magento\Security\Model\AdminSessionInfo::class);
  52. $this->auth->setAuthStorage($this->authSession);
  53. $this->adminSessionsManager = $this->objectManager->get(\Magento\Security\Model\AdminSessionsManager::class);
  54. $this->dateTime = $this->objectManager->create(\Magento\Framework\Stdlib\DateTime::class);
  55. $this->securityConfig = $this->objectManager->create(\Magento\Security\Model\ConfigInterface::class);
  56. }
  57. /**
  58. * Tear down
  59. */
  60. protected function tearDown()
  61. {
  62. $this->auth = null;
  63. $this->authSession = null;
  64. $this->adminSessionInfo = null;
  65. $this->adminSessionsManager = null;
  66. $this->objectManager = null;
  67. parent::tearDown();
  68. }
  69. /**
  70. * Test of prolong user action
  71. * session manager will not trigger new prolong if previous prolong was less than X sec ago
  72. * X - is calculated based on current admin session lifetime
  73. *
  74. * @see \Magento\Security\Model\AdminSessionsManager::lastProlongIsOldEnough
  75. * @magentoDbIsolation enabled
  76. */
  77. public function testConsecutiveProcessProlong()
  78. {
  79. $this->auth->login(
  80. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  81. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  82. );
  83. $sessionId = $this->authSession->getSessionId();
  84. $prolongsDiff = log($this->securityConfig->getAdminSessionLifetime()) - 2; // X from comment above
  85. $dateInPast = $this->dateTime->formatDate($this->authSession->getUpdatedAt() - $prolongsDiff);
  86. $this->adminSessionsManager->getCurrentSession()
  87. ->setData(
  88. 'updated_at',
  89. $dateInPast
  90. )
  91. ->save();
  92. $this->adminSessionInfo->load($sessionId, 'session_id');
  93. $oldUpdatedAt = $this->adminSessionInfo->getUpdatedAt();
  94. $this->authSession->prolong();
  95. $this->adminSessionInfo->load($sessionId, 'session_id');
  96. $updatedAt = $this->adminSessionInfo->getUpdatedAt();
  97. $this->assertSame(strtotime($oldUpdatedAt), strtotime($updatedAt));
  98. }
  99. /**
  100. * Test of prolong user action
  101. * session manager will trigger new prolong if previous prolong was more than X sec ago
  102. * X - is calculated based on current admin session lifetime
  103. *
  104. * @see \Magento\Security\Model\AdminSessionsManager::lastProlongIsOldEnough
  105. * @magentoDbIsolation enabled
  106. */
  107. public function testProcessProlong()
  108. {
  109. $this->auth->login(
  110. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  111. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  112. );
  113. $sessionId = $this->authSession->getSessionId();
  114. $prolongsDiff = 4 * log($this->securityConfig->getAdminSessionLifetime()) + 2; // X from comment above
  115. $dateInPast = $this->dateTime->formatDate($this->authSession->getUpdatedAt() - $prolongsDiff);
  116. $this->adminSessionsManager->getCurrentSession()
  117. ->setData(
  118. 'updated_at',
  119. $dateInPast
  120. )
  121. ->save();
  122. $this->adminSessionInfo->load($sessionId, 'session_id');
  123. $oldUpdatedAt = $this->adminSessionInfo->getUpdatedAt();
  124. $this->authSession->prolong();
  125. $this->adminSessionInfo->load($sessionId, 'session_id');
  126. $updatedAt = $this->adminSessionInfo->getUpdatedAt();
  127. $this->assertGreaterThan(strtotime($oldUpdatedAt), strtotime($updatedAt));
  128. }
  129. }