RememberBackupCodesJobTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\TwoFactorBackupCodes\Tests\Unit\BackgroundJob;
  8. use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\Authentication\TwoFactorAuth\IRegistry;
  11. use OCP\BackgroundJob\IJobList;
  12. use OCP\IUser;
  13. use OCP\IUserManager;
  14. use OCP\Notification\IManager;
  15. use OCP\Notification\INotification;
  16. use OCP\Server;
  17. use Test\TestCase;
  18. class RememberBackupCodesJobTest extends TestCase {
  19. /** @var IRegistry|\PHPUnit\Framework\MockObject\MockObject */
  20. private $registry;
  21. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  22. private $userManager;
  23. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  24. private $time;
  25. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  26. private $notificationManager;
  27. /** @var IJobList|\PHPUnit\Framework\MockObject\MockObject */
  28. private $jobList;
  29. /** @var RememberBackupCodesJob */
  30. private $job;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $this->registry = $this->createMock(IRegistry::class);
  34. $this->userManager = $this->createMock(IUserManager::class);
  35. $this->time = $this->createMock(ITimeFactory::class);
  36. $this->time->method('getTime')
  37. ->willReturn(10000000);
  38. $this->notificationManager = $this->createMock(IManager::class);
  39. $this->jobList = $this->createMock(IJobList::class);
  40. $this->job = new RememberBackupCodesJob(
  41. $this->registry,
  42. $this->userManager,
  43. $this->time,
  44. $this->notificationManager,
  45. $this->jobList
  46. );
  47. }
  48. public function testInvalidUID() {
  49. $this->userManager->method('get')
  50. ->with('invalidUID')
  51. ->willReturn(null);
  52. $this->notificationManager->expects($this->never())
  53. ->method($this->anything());
  54. $this->jobList->expects($this->once())
  55. ->method('remove')
  56. ->with(
  57. RememberBackupCodesJob::class,
  58. ['uid' => 'invalidUID']
  59. );
  60. $this->jobList->expects($this->never())
  61. ->method('add');
  62. self::invokePrivate($this->job, 'run', [['uid' => 'invalidUID']]);
  63. }
  64. public function testBackupCodesGenerated() {
  65. $user = $this->createMock(IUser::class);
  66. $user->method('getUID')
  67. ->willReturn('validUID');
  68. $user->method('isEnabled')
  69. ->willReturn(true);
  70. $this->userManager->method('get')
  71. ->with('validUID')
  72. ->willReturn($user);
  73. $this->registry->method('getProviderStates')
  74. ->with($user)
  75. ->willReturn([
  76. 'backup_codes' => true
  77. ]);
  78. $this->jobList->expects($this->once())
  79. ->method('remove')
  80. ->with(
  81. RememberBackupCodesJob::class,
  82. ['uid' => 'validUID']
  83. );
  84. $this->notificationManager->expects($this->never())
  85. ->method($this->anything());
  86. self::invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
  87. }
  88. public function testNoActiveProvider() {
  89. $user = $this->createMock(IUser::class);
  90. $user->method('getUID')
  91. ->willReturn('validUID');
  92. $this->userManager->method('get')
  93. ->with('validUID')
  94. ->willReturn($user);
  95. $this->registry->method('getProviderStates')
  96. ->with($user)
  97. ->willReturn([
  98. 'backup_codes' => false,
  99. 'foo' => false,
  100. ]);
  101. $this->jobList->expects($this->once())
  102. ->method('remove')
  103. ->with(
  104. RememberBackupCodesJob::class,
  105. ['uid' => 'validUID']
  106. );
  107. $this->notificationManager->expects($this->never())
  108. ->method($this->anything());
  109. self::invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
  110. }
  111. public function testNotificationSend() {
  112. $user = $this->createMock(IUser::class);
  113. $user->method('getUID')
  114. ->willReturn('validUID');
  115. $user->method('isEnabled')
  116. ->willReturn(true);
  117. $this->userManager->method('get')
  118. ->with('validUID')
  119. ->willReturn($user);
  120. $this->registry->method('getProviderStates')
  121. ->with($user)
  122. ->willReturn([
  123. 'backup_codes' => false,
  124. 'foo' => true,
  125. ]);
  126. $this->jobList->expects($this->never())
  127. ->method($this->anything());
  128. $date = new \DateTime();
  129. $date->setTimestamp($this->time->getTime());
  130. $this->notificationManager->method('createNotification')
  131. ->willReturn(Server::get(IManager::class)->createNotification());
  132. $this->notificationManager->expects($this->once())
  133. ->method('notify')
  134. ->with($this->callback(function (INotification $n) {
  135. return $n->getApp() === 'twofactor_backupcodes' &&
  136. $n->getUser() === 'validUID' &&
  137. $n->getDateTime()->getTimestamp() === 10000000 &&
  138. $n->getObjectType() === 'create' &&
  139. $n->getObjectId() === 'codes' &&
  140. $n->getSubject() === 'create_backupcodes';
  141. }));
  142. self::invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
  143. }
  144. public function testNotificationNotSendForDisabledUser() {
  145. $user = $this->createMock(IUser::class);
  146. $user->method('getUID')
  147. ->willReturn('validUID');
  148. $user->method('isEnabled')
  149. ->willReturn(false);
  150. $this->userManager->method('get')
  151. ->with('validUID')
  152. ->willReturn($user);
  153. $this->registry->method('getProviderStates')
  154. ->with($user)
  155. ->willReturn([
  156. 'backup_codes' => false,
  157. 'foo' => true,
  158. ]);
  159. $this->jobList->expects($this->once())
  160. ->method('remove')
  161. ->with(
  162. RememberBackupCodesJob::class,
  163. ['uid' => 'validUID']
  164. );
  165. $date = new \DateTime();
  166. $date->setTimestamp($this->time->getTime());
  167. $this->notificationManager->method('createNotification')
  168. ->willReturn(Server::get(IManager::class)->createNotification());
  169. $this->notificationManager->expects($this->once())
  170. ->method('markProcessed')
  171. ->with($this->callback(function (INotification $n) {
  172. return $n->getApp() === 'twofactor_backupcodes' &&
  173. $n->getUser() === 'validUID' &&
  174. $n->getObjectType() === 'create' &&
  175. $n->getObjectId() === 'codes' &&
  176. $n->getSubject() === 'create_backupcodes';
  177. }));
  178. $this->notificationManager->expects($this->never())
  179. ->method('notify');
  180. self::invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
  181. }
  182. }