RememberBackupCodesJobTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 OCP\Server;
  34. use Test\TestCase;
  35. class RememberBackupCodesJobTest extends TestCase {
  36. /** @var IRegistry|\PHPUnit\Framework\MockObject\MockObject */
  37. private $registry;
  38. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  39. private $userManager;
  40. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  41. private $time;
  42. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  43. private $notificationManager;
  44. /** @var IJobList|\PHPUnit\Framework\MockObject\MockObject */
  45. private $jobList;
  46. /** @var RememberBackupCodesJob */
  47. private $job;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->registry = $this->createMock(IRegistry::class);
  51. $this->userManager = $this->createMock(IUserManager::class);
  52. $this->time = $this->createMock(ITimeFactory::class);
  53. $this->time->method('getTime')
  54. ->willReturn(10000000);
  55. $this->notificationManager = $this->createMock(IManager::class);
  56. $this->jobList = $this->createMock(IJobList::class);
  57. $this->job = new RememberBackupCodesJob(
  58. $this->registry,
  59. $this->userManager,
  60. $this->time,
  61. $this->notificationManager,
  62. $this->jobList
  63. );
  64. }
  65. public function testInvalidUID() {
  66. $this->userManager->method('get')
  67. ->with('invalidUID')
  68. ->willReturn(null);
  69. $this->notificationManager->expects($this->never())
  70. ->method($this->anything());
  71. $this->jobList->expects($this->once())
  72. ->method('remove')
  73. ->with(
  74. RememberBackupCodesJob::class,
  75. ['uid' => 'invalidUID']
  76. );
  77. $this->jobList->expects($this->never())
  78. ->method('add');
  79. self::invokePrivate($this->job, 'run', [['uid' => 'invalidUID']]);
  80. }
  81. public function testBackupCodesGenerated() {
  82. $user = $this->createMock(IUser::class);
  83. $user->method('getUID')
  84. ->willReturn('validUID');
  85. $user->method('isEnabled')
  86. ->willReturn(true);
  87. $this->userManager->method('get')
  88. ->with('validUID')
  89. ->willReturn($user);
  90. $this->registry->method('getProviderStates')
  91. ->with($user)
  92. ->willReturn([
  93. 'backup_codes' => true
  94. ]);
  95. $this->jobList->expects($this->once())
  96. ->method('remove')
  97. ->with(
  98. RememberBackupCodesJob::class,
  99. ['uid' => 'validUID']
  100. );
  101. $this->notificationManager->expects($this->never())
  102. ->method($this->anything());
  103. self::invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
  104. }
  105. public function testNoActiveProvider() {
  106. $user = $this->createMock(IUser::class);
  107. $user->method('getUID')
  108. ->willReturn('validUID');
  109. $this->userManager->method('get')
  110. ->with('validUID')
  111. ->willReturn($user);
  112. $this->registry->method('getProviderStates')
  113. ->with($user)
  114. ->willReturn([
  115. 'backup_codes' => false,
  116. 'foo' => false,
  117. ]);
  118. $this->jobList->expects($this->once())
  119. ->method('remove')
  120. ->with(
  121. RememberBackupCodesJob::class,
  122. ['uid' => 'validUID']
  123. );
  124. $this->notificationManager->expects($this->never())
  125. ->method($this->anything());
  126. self::invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
  127. }
  128. public function testNotificationSend() {
  129. $user = $this->createMock(IUser::class);
  130. $user->method('getUID')
  131. ->willReturn('validUID');
  132. $user->method('isEnabled')
  133. ->willReturn(true);
  134. $this->userManager->method('get')
  135. ->with('validUID')
  136. ->willReturn($user);
  137. $this->registry->method('getProviderStates')
  138. ->with($user)
  139. ->willReturn([
  140. 'backup_codes' => false,
  141. 'foo' => true,
  142. ]);
  143. $this->jobList->expects($this->never())
  144. ->method($this->anything());
  145. $date = new \DateTime();
  146. $date->setTimestamp($this->time->getTime());
  147. $this->notificationManager->method('createNotification')
  148. ->willReturn(Server::get(IManager::class)->createNotification());
  149. $this->notificationManager->expects($this->once())
  150. ->method('notify')
  151. ->with($this->callback(function (INotification $n) {
  152. return $n->getApp() === 'twofactor_backupcodes' &&
  153. $n->getUser() === 'validUID' &&
  154. $n->getDateTime()->getTimestamp() === 10000000 &&
  155. $n->getObjectType() === 'create' &&
  156. $n->getObjectId() === 'codes' &&
  157. $n->getSubject() === 'create_backupcodes';
  158. }));
  159. self::invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
  160. }
  161. public function testNotificationNotSendForDisabledUser() {
  162. $user = $this->createMock(IUser::class);
  163. $user->method('getUID')
  164. ->willReturn('validUID');
  165. $user->method('isEnabled')
  166. ->willReturn(false);
  167. $this->userManager->method('get')
  168. ->with('validUID')
  169. ->willReturn($user);
  170. $this->registry->method('getProviderStates')
  171. ->with($user)
  172. ->willReturn([
  173. 'backup_codes' => false,
  174. 'foo' => true,
  175. ]);
  176. $this->jobList->expects($this->once())
  177. ->method('remove')
  178. ->with(
  179. RememberBackupCodesJob::class,
  180. ['uid' => 'validUID']
  181. );
  182. $date = new \DateTime();
  183. $date->setTimestamp($this->time->getTime());
  184. $this->notificationManager->method('createNotification')
  185. ->willReturn(Server::get(IManager::class)->createNotification());
  186. $this->notificationManager->expects($this->once())
  187. ->method('markProcessed')
  188. ->with($this->callback(function (INotification $n) {
  189. return $n->getApp() === 'twofactor_backupcodes' &&
  190. $n->getUser() === 'validUID' &&
  191. $n->getObjectType() === 'create' &&
  192. $n->getObjectId() === 'codes' &&
  193. $n->getSubject() === 'create_backupcodes';
  194. }));
  195. $this->notificationManager->expects($this->never())
  196. ->method('notify');
  197. self::invokePrivate($this->job, 'run', [['uid' => 'validUID']]);
  198. }
  199. }