SecurityManagerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Security\Model;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. class SecurityManagerTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Security\Model\SecurityManager
  13. */
  14. protected $securityManager;
  15. /**
  16. * @var AccountManagementInterface
  17. */
  18. protected $accountManagement;
  19. /**
  20. * @var \Magento\Framework\ObjectManagerInterface
  21. */
  22. protected $objectManager;
  23. /**
  24. * @var \Magento\Security\Model\PasswordResetRequestEvent
  25. */
  26. protected $passwordResetRequestEvent;
  27. /**
  28. * Set up
  29. */
  30. public function setUp()
  31. {
  32. $this->objectManager = Bootstrap::getObjectManager();
  33. $this->accountManagement = $this->objectManager->create(
  34. \Magento\Customer\Api\AccountManagementInterface::class
  35. );
  36. $this->securityManager = $this->objectManager->create(\Magento\Security\Model\SecurityManager::class);
  37. $this->passwordResetRequestEvent = $this->objectManager
  38. ->get(\Magento\Security\Model\PasswordResetRequestEvent::class);
  39. }
  40. /**
  41. * Tear down
  42. */
  43. protected function tearDown()
  44. {
  45. $this->objectManager = null;
  46. $this->accountManagement = null;
  47. $this->securityManager = null;
  48. parent::tearDown();
  49. }
  50. /**
  51. * Test for performSecurityCheck() method
  52. *
  53. * @magentoConfigFixture current_store customer/password/limit_password_reset_requests_method 0
  54. * @magentoDbIsolation enabled
  55. */
  56. public function testPerformSecurityCheck()
  57. {
  58. $collection = $this->getPasswordResetRequestEventCollection();
  59. $sizeBefore = $collection->getSize();
  60. $requestType = \Magento\Security\Model\PasswordResetRequestEvent::CUSTOMER_PASSWORD_RESET_REQUEST;
  61. $longIp = 127001;
  62. $accountReference = 'customer@example.com';
  63. $this->assertInstanceOf(
  64. \Magento\Security\Model\SecurityManager::class,
  65. $this->securityManager->performSecurityCheck(
  66. $requestType,
  67. $accountReference,
  68. $longIp
  69. )
  70. );
  71. $collection = $this->getPasswordResetRequestEventCollection();
  72. $sizeAfter = $collection->getSize();
  73. $this->assertEquals(1, $sizeAfter - $sizeBefore);
  74. }
  75. /**
  76. * Get PasswordResetRequestEvent collection
  77. *
  78. * @return \Magento\Security\Model\ResourceModel\PasswordResetRequestEvent\Collection
  79. */
  80. protected function getPasswordResetRequestEventCollection()
  81. {
  82. $collection = $this->passwordResetRequestEvent->getResourceCollection();
  83. $collection->load();
  84. return $collection;
  85. }
  86. /**
  87. * Test for performSecurityCheck() method when number of password reset events is exceeded
  88. *
  89. * @magentoConfigFixture current_store customer/password/limit_password_reset_requests_method 1
  90. * @magentoConfigFixture current_store customer/password/max_number_password_reset_requests 1
  91. * @magentoConfigFixture current_store customer/password/min_time_between_password_reset_requests 0
  92. * @magentoConfigFixture current_store contact/email/recipient_email hi@example.com
  93. * @expectedException \Magento\Framework\Exception\SecurityViolationException
  94. * @magentoDbIsolation enabled
  95. */
  96. public function testPerformSecurityCheckLimitNumber()
  97. {
  98. $attempts = 2;
  99. $requestType = \Magento\Security\Model\PasswordResetRequestEvent::CUSTOMER_PASSWORD_RESET_REQUEST;
  100. $longIp = 127001;
  101. $accountReference = 'customer@example.com';
  102. try {
  103. for ($i = 0; $i < $attempts; $i++) {
  104. $this->securityManager->performSecurityCheck($requestType, $accountReference, $longIp);
  105. }
  106. } catch (\Magento\Framework\Exception\SecurityViolationException $e) {
  107. $this->assertEquals(1, $i);
  108. throw new \Magento\Framework\Exception\SecurityViolationException(
  109. __($e->getMessage())
  110. );
  111. }
  112. $this->expectExceptionMessage(
  113. 'We received too many requests for password resets. '
  114. . 'Please wait and try again later or contact hi@example.com.'
  115. );
  116. }
  117. /**
  118. * Test for performSecurityCheck() method when time between password reset events is exceeded
  119. *
  120. * @magentoConfigFixture current_store customer/password/limit_password_reset_requests_method 1
  121. * @magentoConfigFixture current_store customer/password/max_number_password_reset_requests 0
  122. * @magentoConfigFixture current_store customer/password/min_time_between_password_reset_requests 1
  123. * @magentoConfigFixture current_store contact/email/recipient_email hi@example.com
  124. * @expectedException \Magento\Framework\Exception\SecurityViolationException
  125. * @magentoDbIsolation enabled
  126. */
  127. public function testPerformSecurityCheckLimitTime()
  128. {
  129. $attempts = 2;
  130. $requestType = \Magento\Security\Model\PasswordResetRequestEvent::CUSTOMER_PASSWORD_RESET_REQUEST;
  131. $longIp = 127001;
  132. $accountReference = 'customer@example.com';
  133. try {
  134. for ($i = 0; $i < $attempts; $i++) {
  135. $this->securityManager->performSecurityCheck($requestType, $accountReference, $longIp);
  136. }
  137. } catch (\Magento\Framework\Exception\SecurityViolationException $e) {
  138. $this->assertEquals(1, $i);
  139. throw new \Magento\Framework\Exception\SecurityViolationException(
  140. __($e->getMessage())
  141. );
  142. }
  143. $this->fail('Something went wrong. Please check method execution logic.');
  144. $this->expectExceptionMessage(
  145. 'We received too many requests for password resets. '
  146. . 'Please wait and try again later or contact hi@example.com.'
  147. );
  148. }
  149. }