AdminSessionsManagerTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Model;
  7. class AdminSessionsManagerTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Backend\Model\Auth
  11. */
  12. protected $auth;
  13. /**
  14. * @var \Magento\Backend\Model\Auth\Session
  15. */
  16. protected $authSession;
  17. /**
  18. * @var \Magento\Security\Model\AdminSessionInfo
  19. */
  20. protected $adminSessionInfo;
  21. /**
  22. * @var \Magento\Security\Model\AdminSessionsManager
  23. */
  24. protected $adminSessionsManager;
  25. /**
  26. * @var \Magento\Framework\Message\ManagerInterface
  27. */
  28. protected $messageManager;
  29. /**
  30. * @var \Magento\Framework\ObjectManagerInterface
  31. */
  32. protected $objectManager;
  33. /**
  34. * Set up
  35. */
  36. protected function setUp()
  37. {
  38. parent::setUp();
  39. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  40. $this->objectManager->get(\Magento\Framework\Config\ScopeInterface::class)
  41. ->setCurrentScope(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE);
  42. $this->auth = $this->objectManager->create(\Magento\Backend\Model\Auth::class);
  43. $this->authSession = $this->objectManager->create(\Magento\Backend\Model\Auth\Session::class);
  44. $this->adminSessionInfo = $this->objectManager->create(\Magento\Security\Model\AdminSessionInfo::class);
  45. $this->auth->setAuthStorage($this->authSession);
  46. $this->messageManager = $this->objectManager->get(\Magento\Framework\Message\ManagerInterface::class);
  47. $this->adminSessionsManager = $this->objectManager->create(\Magento\Security\Model\AdminSessionsManager::class);
  48. }
  49. /**
  50. * Tear down
  51. */
  52. protected function tearDown()
  53. {
  54. $this->auth = null;
  55. $this->authSession = null;
  56. $this->adminSessionInfo = null;
  57. $this->adminSessionsManager = null;
  58. $this->objectManager = null;
  59. parent::tearDown();
  60. }
  61. /**
  62. * Test if current admin user is logged out
  63. *
  64. * @magentoDbIsolation enabled
  65. */
  66. public function testProcessLogout()
  67. {
  68. $this->auth->login(
  69. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  70. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  71. );
  72. $sessionId = $this->authSession->getSessionId();
  73. $this->auth->logout();
  74. $this->adminSessionInfo->load($sessionId, 'session_id');
  75. $this->assertEquals($this->adminSessionInfo->getStatus(), AdminSessionInfo::LOGGED_OUT);
  76. }
  77. /**
  78. * Test if the admin session is created in database
  79. *
  80. * @magentoDbIsolation enabled
  81. */
  82. public function testIsAdminSessionIsCreated()
  83. {
  84. $this->auth->login(
  85. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  86. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  87. );
  88. $sessionId = $this->authSession->getSessionId();
  89. $this->adminSessionInfo->load($sessionId, 'session_id');
  90. $this->assertGreaterThanOrEqual(1, (int)$this->adminSessionInfo->getId());
  91. $this->auth->logout();
  92. }
  93. /**
  94. * Test if other sessions are terminated if admin_account_sharing is disabled
  95. *
  96. * @magentoAdminConfigFixture admin/security/session_lifetime 100
  97. * @magentoConfigFixture default_store admin/security/admin_account_sharing 0
  98. * @magentoDbIsolation enabled
  99. */
  100. public function testTerminateOtherSessionsProcessLogin()
  101. {
  102. $session = $this->objectManager->create(\Magento\Security\Model\AdminSessionInfo::class);
  103. $session->setSessionId('669e2e3d752e8')
  104. ->setUserId(1)
  105. ->setStatus(1)
  106. ->setCreatedAt(time() - 10)
  107. ->setUpdatedAt(time() - 9)
  108. ->save();
  109. $this->auth->login(
  110. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  111. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  112. );
  113. $session->load('669e2e3d752e8', 'session_id');
  114. $this->assertEquals(
  115. AdminSessionInfo::LOGGED_OUT_BY_LOGIN,
  116. (int) $session->getStatus()
  117. );
  118. }
  119. /**
  120. * Test if current session is retrieved
  121. *
  122. * @magentoDbIsolation enabled
  123. */
  124. public function testGetCurrentSession()
  125. {
  126. $this->auth->login(
  127. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  128. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  129. );
  130. $sessionId = $this->authSession->getSessionId();
  131. $this->adminSessionInfo->load($sessionId, 'session_id');
  132. $this->assertEquals(
  133. $this->adminSessionInfo->getSessionId(),
  134. $this->adminSessionsManager->getCurrentSession()->getSessionId()
  135. );
  136. }
  137. /**
  138. * Test if other sessions were logged out if logoutOtherUserSessions() action was performed
  139. *
  140. * @magentoAdminConfigFixture admin/security/session_lifetime 100
  141. * @magentoConfigFixture default_store admin/security/admin_account_sharing 1
  142. * @magentoDbIsolation enabled
  143. */
  144. public function testLogoutOtherUserSessions()
  145. {
  146. /** @var \Magento\Security\Model\AdminSessionInfo $session */
  147. $session = $this->objectManager->create(\Magento\Security\Model\AdminSessionInfo::class);
  148. $session->setSessionId('669e2e3d752e8')
  149. ->setUserId(1)
  150. ->setStatus(1)
  151. ->setCreatedAt(time() - 50)
  152. ->setUpdatedAt(time() - 49)
  153. ->save();
  154. $this->auth->login(
  155. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  156. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  157. );
  158. $collection = $this->getCollectionForLogoutOtherUserSessions($session);
  159. $this->assertGreaterThanOrEqual(1, $collection->getSize());
  160. $this->adminSessionsManager->logoutOtherUserSessions();
  161. $collection = $this->getCollectionForLogoutOtherUserSessions($session);
  162. $this->assertEquals(0, $collection->getSize());
  163. }
  164. /**
  165. * Collection getter with filters populated for testLogoutOtherUserSessions() method
  166. *
  167. * @param AdminSessionInfo $session
  168. * @return ResourceModel\AdminSessionInfo\Collection
  169. */
  170. protected function getCollectionForLogoutOtherUserSessions(\Magento\Security\Model\AdminSessionInfo $session)
  171. {
  172. /** @var \Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection $collection */
  173. $collection = $session->getResourceCollection();
  174. $collection->filterByUser(
  175. $this->authSession->getUser()->getId(),
  176. \Magento\Security\Model\AdminSessionInfo::LOGGED_IN,
  177. $this->authSession->getSessionId()
  178. )
  179. ->filterExpiredSessions(100)
  180. ->load();
  181. return $collection;
  182. }
  183. /**
  184. * Test for cleanExpiredSessions() method
  185. *
  186. * @magentoDataFixture Magento/Security/_files/adminsession.php
  187. * @magentoAdminConfigFixture admin/security/session_lifetime 1
  188. * @magentoDbIsolation enabled
  189. */
  190. public function testCleanExpiredSessions()
  191. {
  192. /** @var \Magento\Security\Model\AdminSessionInfo $session */
  193. $session = $this->objectManager->create(\Magento\Security\Model\AdminSessionInfo::class);
  194. $collection = $this->getCollectionForCleanExpiredSessions($session);
  195. $sizeBefore = $collection->getSize();
  196. $this->adminSessionsManager->cleanExpiredSessions();
  197. $collection = $this->getCollectionForCleanExpiredSessions($session);
  198. $sizeAfter = $collection->getSize();
  199. $this->assertGreaterThan($sizeAfter, $sizeBefore);
  200. }
  201. /**
  202. * Collection getter with filters populated for testCleanExpiredSessions() method
  203. *
  204. * @param AdminSessionInfo $session
  205. * @return ResourceModel\AdminSessionInfo\Collection
  206. */
  207. protected function getCollectionForCleanExpiredSessions(\Magento\Security\Model\AdminSessionInfo $session)
  208. {
  209. /** @var \Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection $collection */
  210. $collection = $session->getResourceCollection()
  211. ->load();
  212. return $collection;
  213. }
  214. }