AuthenticationTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model;
  7. use Magento\Backend\App\ConfigInterface;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Customer\Model\Authentication;
  10. use Magento\Customer\Model\CustomerRegistry;
  11. use Magento\Customer\Model\Data\CustomerSecure;
  12. use Magento\Framework\Stdlib\DateTime;
  13. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  14. /**
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class AuthenticationTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var \Magento\Backend\App\ConfigInterface | \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $backendConfigMock;
  23. /**
  24. * @var \Magento\Customer\Model\CustomerRegistry | \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $customerRegistryMock;
  27. /**
  28. * @var \Magento\Framework\Encryption\EncryptorInterface | \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $encryptorMock;
  31. /**
  32. * @var CustomerRepositoryInterface | \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $customerRepositoryMock;
  35. /**
  36. * @var \Magento\Customer\Model\Data\CustomerSecure | \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $customerSecureMock;
  39. /**
  40. * @var \Magento\Customer\Model\Authentication
  41. */
  42. private $authentication;
  43. /**
  44. * @var DateTime
  45. */
  46. private $dateTimeMock;
  47. /**
  48. * @var \Magento\Customer\Model\CustomerAuthUpdate | \PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $customerAuthUpdate;
  51. /**
  52. * @var ObjectManagerHelper
  53. */
  54. protected $objectManager;
  55. protected function setUp()
  56. {
  57. $this->objectManager = new ObjectManagerHelper($this);
  58. $this->backendConfigMock = $this->getMockBuilder(ConfigInterface::class)
  59. ->disableOriginalConstructor()
  60. ->setMethods(['getValue'])
  61. ->getMockForAbstractClass();
  62. $this->customerRegistryMock = $this->createPartialMock(
  63. CustomerRegistry::class,
  64. ['retrieveSecureData', 'retrieve']
  65. );
  66. $this->customerRepositoryMock = $this->getMockBuilder(CustomerRepositoryInterface::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->encryptorMock = $this->getMockBuilder(\Magento\Framework\Encryption\EncryptorInterface::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->dateTimeMock = $this->getMockBuilder(DateTime::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->dateTimeMock->expects($this->any())
  76. ->method('formatDate')
  77. ->willReturn('formattedDate');
  78. $this->customerSecureMock = $this->createPartialMock(CustomerSecure::class, [
  79. 'getId',
  80. 'getPasswordHash',
  81. 'isCustomerLocked',
  82. 'getFailuresNum',
  83. 'getFirstFailure',
  84. 'getLockExpires',
  85. 'setFirstFailure',
  86. 'setFailuresNum',
  87. 'setLockExpires'
  88. ]);
  89. $this->customerAuthUpdate = $this->getMockBuilder(\Magento\Customer\Model\CustomerAuthUpdate::class)
  90. ->disableOriginalConstructor()
  91. ->getMock();
  92. $this->authentication = $this->objectManager->getObject(
  93. Authentication::class,
  94. [
  95. 'customerRegistry' => $this->customerRegistryMock,
  96. 'backendConfig' => $this->backendConfigMock,
  97. 'customerRepository' => $this->customerRepositoryMock,
  98. 'encryptor' => $this->encryptorMock,
  99. 'dateTime' => $this->dateTimeMock,
  100. ]
  101. );
  102. $this->objectManager->setBackwardCompatibleProperty(
  103. $this->authentication,
  104. 'customerAuthUpdate',
  105. $this->customerAuthUpdate
  106. );
  107. }
  108. public function testProcessAuthenticationFailureLockingIsDisabled()
  109. {
  110. $customerId = 1;
  111. $this->backendConfigMock->expects($this->exactly(2))
  112. ->method('getValue')
  113. ->withConsecutive(
  114. [\Magento\Customer\Model\Authentication::LOCKOUT_THRESHOLD_PATH],
  115. [\Magento\Customer\Model\Authentication::MAX_FAILURES_PATH]
  116. )
  117. ->willReturnOnConsecutiveCalls(0, 0);
  118. $this->customerRegistryMock->expects($this->once())
  119. ->method('retrieveSecureData')
  120. ->with($customerId)
  121. ->willReturn($this->customerSecureMock);
  122. $this->authentication->processAuthenticationFailure($customerId);
  123. }
  124. /**
  125. * @param int $failureNum
  126. * @param string $firstFailure
  127. * @param string $lockExpires
  128. * @param int $setFailureNumCallCtr
  129. * @param int $setFailureNumValue
  130. * @param int $setFirstFailureCallCtr
  131. * @param int $setFirstFailureValue
  132. * @param int $setLockExpiresCallCtr
  133. * @param int $setLockExpiresValue
  134. * @dataProvider processAuthenticationFailureDataProvider
  135. */
  136. public function testProcessAuthenticationFailureFirstAttempt(
  137. $failureNum,
  138. $firstFailure,
  139. $lockExpires,
  140. $setFailureNumCallCtr,
  141. $setFailureNumValue,
  142. $setFirstFailureCallCtr,
  143. $setLockExpiresCallCtr,
  144. $setLockExpiresValue
  145. ) {
  146. $customerId = 1;
  147. $this->backendConfigMock->expects($this->exactly(2))
  148. ->method('getValue')
  149. ->withConsecutive(
  150. [\Magento\Customer\Model\Authentication::LOCKOUT_THRESHOLD_PATH],
  151. [\Magento\Customer\Model\Authentication::MAX_FAILURES_PATH]
  152. )
  153. ->willReturnOnConsecutiveCalls(10, 5);
  154. $this->customerRegistryMock->expects($this->once())
  155. ->method('retrieveSecureData')
  156. ->with($customerId)
  157. ->willReturn($this->customerSecureMock);
  158. $this->customerAuthUpdate->expects($this->once())
  159. ->method('saveAuth')
  160. ->with($customerId)
  161. ->willReturnSelf();
  162. $this->customerSecureMock->expects($this->once())->method('getFailuresNum')->willReturn($failureNum);
  163. $this->customerSecureMock->expects($this->once())
  164. ->method('getFirstFailure')
  165. ->willReturn($firstFailure ? (new \DateTime())->modify($firstFailure)->format('Y-m-d H:i:s') : null);
  166. $this->customerSecureMock->expects($this->once())
  167. ->method('getLockExpires')
  168. ->willReturn($lockExpires ? (new \DateTime())->modify($lockExpires)->format('Y-m-d H:i:s') : null);
  169. $this->customerSecureMock->expects($this->exactly($setFirstFailureCallCtr))->method('setFirstFailure');
  170. $this->customerSecureMock->expects($this->exactly($setFailureNumCallCtr))
  171. ->method('setFailuresNum')
  172. ->with($setFailureNumValue);
  173. $this->customerSecureMock->expects($this->exactly($setLockExpiresCallCtr))
  174. ->method('setLockExpires')
  175. ->with($setLockExpiresValue);
  176. $this->authentication->processAuthenticationFailure($customerId);
  177. }
  178. /**
  179. * @return array
  180. */
  181. public function processAuthenticationFailureDataProvider()
  182. {
  183. return [
  184. 'first attempt' => [0, null, null, 1, 1, 1, 1, null],
  185. 'not locked' => [3, '-400 second', null, 1, 4, 0, 0, null],
  186. 'lock expired' => [5, '-400 second', '-100 second', 1, 1, 1, 1, null],
  187. 'max attempt' => [4, '-400 second', null, 1, 5, 0, 1, 'formattedDate'],
  188. ];
  189. }
  190. public function testUnlock()
  191. {
  192. $customerId = 1;
  193. $this->customerRegistryMock->expects($this->once())
  194. ->method('retrieveSecureData')
  195. ->with($customerId)
  196. ->willReturn($this->customerSecureMock);
  197. $this->customerAuthUpdate->expects($this->once())
  198. ->method('saveAuth')
  199. ->with($customerId)
  200. ->willReturnSelf();
  201. $this->customerSecureMock->expects($this->once())->method('setFailuresNum')->with(0);
  202. $this->customerSecureMock->expects($this->once())->method('setFirstFailure')->with(null);
  203. $this->customerSecureMock->expects($this->once())->method('setLockExpires')->with(null);
  204. $this->authentication->unlock($customerId);
  205. }
  206. /**
  207. * @return array
  208. */
  209. public function validatePasswordAndLockStatusDataProvider()
  210. {
  211. return [[true], [false]];
  212. }
  213. /**
  214. * @return void
  215. */
  216. public function testIsLocked()
  217. {
  218. $customerId = 7;
  219. $customerModelMock = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
  220. ->disableOriginalConstructor()
  221. ->getMock();
  222. $customerModelMock->expects($this->once())
  223. ->method('isCustomerLocked');
  224. $this->customerRegistryMock->expects($this->once())
  225. ->method('retrieve')
  226. ->with($customerId)
  227. ->willReturn($customerModelMock);
  228. $this->authentication->isLocked($customerId);
  229. }
  230. /**
  231. * @param bool $result
  232. * @dataProvider validateCustomerPassword
  233. */
  234. public function testAuthenticate($result)
  235. {
  236. $customerId = 7;
  237. $password = '1234567';
  238. $hash = '1b2af329dd0';
  239. $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  240. $this->customerRepositoryMock->expects($this->any())
  241. ->method('getById')
  242. ->willReturn($customerMock);
  243. $this->customerSecureMock->expects($this->any())
  244. ->method('getId')
  245. ->willReturn($customerId);
  246. $this->customerSecureMock->expects($this->once())
  247. ->method('getPasswordHash')
  248. ->willReturn($hash);
  249. $this->customerRegistryMock->expects($this->any())
  250. ->method('retrieveSecureData')
  251. ->with($customerId)
  252. ->willReturn($this->customerSecureMock);
  253. $this->encryptorMock->expects($this->once())
  254. ->method('validateHash')
  255. ->with($password, $hash)
  256. ->willReturn($result);
  257. if ($result) {
  258. $this->assertTrue($this->authentication->authenticate($customerId, $password));
  259. } else {
  260. $this->backendConfigMock->expects($this->exactly(2))
  261. ->method('getValue')
  262. ->withConsecutive(
  263. [\Magento\Customer\Model\Authentication::LOCKOUT_THRESHOLD_PATH],
  264. [\Magento\Customer\Model\Authentication::MAX_FAILURES_PATH]
  265. )
  266. ->willReturnOnConsecutiveCalls(1, 1);
  267. $this->customerSecureMock->expects($this->once())
  268. ->method('isCustomerLocked')
  269. ->willReturn(false);
  270. $this->customerRegistryMock->expects($this->once())
  271. ->method('retrieve')
  272. ->with($customerId)
  273. ->willReturn($this->customerSecureMock);
  274. $this->customerAuthUpdate->expects($this->once())
  275. ->method('saveAuth')
  276. ->with($customerId)
  277. ->willReturnSelf();
  278. $this->expectException(\Magento\Framework\Exception\InvalidEmailOrPasswordException::class);
  279. $this->authentication->authenticate($customerId, $password);
  280. }
  281. }
  282. /**
  283. * @return array
  284. */
  285. public function validateCustomerPassword()
  286. {
  287. return [
  288. [true],
  289. [false],
  290. ];
  291. }
  292. }