RememberBackupCodesJobTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\TwoFactorBackupCodes\Tests\Unit\BackgroundJob;
  25. use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\Authentication\TwoFactorAuth\IRegistry;
  28. use OCP\BackgroundJob\IJobList;
  29. use OCP\IUser;
  30. use OCP\IUserManager;
  31. use OCP\Notification\IManager;
  32. use OCP\Notification\INotification;
  33. use Test\TestCase;
  34. class RememberBackupCodesJobTest extends TestCase {
  35. /** @var IRegistry|\PHPUnit\Framework\MockObject\MockObject */
  36. private $registry;
  37. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  38. private $userManager;
  39. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  40. private $time;
  41. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  42. private $notificationManager;
  43. /** @var IJobList|\PHPUnit\Framework\MockObject\MockObject */
  44. private $jobList;
  45. /** @var RememberBackupCodesJob */
  46. private $job;
  47. protected function setUp(): void {
  48. parent::setUp();
  49. $this->registry = $this->createMock(IRegistry::class);
  50. $this->userManager = $this->createMock(IUserManager::class);
  51. $this->time = $this->createMock(ITimeFactory::class);
  52. $this->time->method('getTime')
  53. ->willReturn(10000000);
  54. $this->notificationManager = $this->createMock(IManager::class);
  55. $this->jobList = $this->createMock(IJobList::class);
  56. $this->job = new RememberBackupCodesJob(
  57. $this->registry,
  58. $this->userManager,
  59. $this->time,
  60. $this->notificationManager,
  61. $this->jobList
  62. );
  63. }
  64. public function testInvalidUID() {
  65. $this->userManager->method('get')
  66. ->with('invalidUID')
  67. ->willReturn(null);
  68. $this->notificationManager->expects($this->never())
  69. ->method($this->anything());
  70. $this->jobList->expects($this->never())
  71. ->method($this->anything());
  72. $this->invokePrivate($this->job, 'run', [['uid' => 'invalidUID']]);
  73. }
  74. public function testBackupCodesGenerated() {
  75. $user = $this->createMock(IUser::class);
  76. $user->method('getUID')
  77. ->willReturn('validUID');
  78. $this->userManager->method('get')
  79. ->with('validUID')
  80. ->willReturn($user);
  81. $this->registry->method('getProviderStates')
  82. ->with($user)
  83. ->willReturn([
  84. 'backup_codes' => true
  85. ]);
  86. $this->jobList->expects($this->once())
  87. ->method('remove')
  88. ->with(
  89. RememberBackupCodesJob::class,
  90. ['uid' => 'validUID']
  91. );
  92. $this->notificationManager->expects($this->never())
  93. ->method($this->anything());
  94. $this->invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
  95. }
  96. public function testNoActiveProvider() {
  97. $user = $this->createMock(IUser::class);
  98. $user->method('getUID')
  99. ->willReturn('validUID');
  100. $this->userManager->method('get')
  101. ->with('validUID')
  102. ->willReturn($user);
  103. $this->registry->method('getProviderStates')
  104. ->with($user)
  105. ->willReturn([
  106. 'backup_codes' => false,
  107. 'foo' => false,
  108. ]);
  109. $this->jobList->expects($this->once())
  110. ->method('remove')
  111. ->with(
  112. RememberBackupCodesJob::class,
  113. ['uid' => 'validUID']
  114. );
  115. $this->notificationManager->expects($this->never())
  116. ->method($this->anything());
  117. $this->invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
  118. }
  119. public function testNotificationSend() {
  120. $user = $this->createMock(IUser::class);
  121. $user->method('getUID')
  122. ->willReturn('validUID');
  123. $this->userManager->method('get')
  124. ->with('validUID')
  125. ->willReturn($user);
  126. $this->registry->method('getProviderStates')
  127. ->with($user)
  128. ->willReturn([
  129. 'backup_codes' => false,
  130. 'foo' => true,
  131. ]);
  132. $this->jobList->expects($this->never())
  133. ->method($this->anything());
  134. $date = new \DateTime();
  135. $date->setTimestamp($this->time->getTime());
  136. $this->notificationManager->method('createNotification')
  137. ->willReturn(\OC::$server->query(IManager::class)->createNotification());
  138. $this->notificationManager->expects($this->once())
  139. ->method('notify')
  140. ->with($this->callback(function (INotification $n) {
  141. return $n->getApp() === 'twofactor_backupcodes' &&
  142. $n->getUser() === 'validUID' &&
  143. $n->getDateTime()->getTimestamp() === 10000000 &&
  144. $n->getObjectType() === 'create' &&
  145. $n->getObjectId() === 'codes' &&
  146. $n->getSubject() === 'create_backupcodes';
  147. }));
  148. $this->invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
  149. }
  150. }