CheckBackupCodeTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 OC\Authentication\TwoFactorAuth\Manager;
  9. use OCA\TwoFactorBackupCodes\BackgroundJob\CheckBackupCodes;
  10. use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob;
  11. use OCP\AppFramework\Utility\ITimeFactory;
  12. use OCP\Authentication\TwoFactorAuth\IRegistry;
  13. use OCP\BackgroundJob\IJobList;
  14. use OCP\IUser;
  15. use OCP\IUserManager;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use Test\TestCase;
  18. class CheckBackupCodeTest extends TestCase {
  19. /** @var IUserManager|MockObject */
  20. private $userManager;
  21. /** @var IJobList|MockObject */
  22. private $jobList;
  23. /** @var IRegistry|MockObject */
  24. private $registry;
  25. /** @var Manager|MockObject */
  26. private $manager;
  27. /** @var IUser|MockObject */
  28. private $user;
  29. /** @var CheckBackupCodes */
  30. private $checkBackupCodes;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $this->userManager = $this->createMock(IUserManager::class);
  34. $this->jobList = $this->createMock(IJobList::class);
  35. $this->registry = $this->createMock(IRegistry::class);
  36. $this->manager = $this->createMock(Manager::class);
  37. $this->user = $this->createMock(IUser::class);
  38. $this->userManager->method('callForSeenUsers')
  39. ->willReturnCallback(function (\Closure $e) {
  40. $e($this->user);
  41. });
  42. $this->checkBackupCodes = new CheckBackupCodes(
  43. $this->createMock(ITimeFactory::class),
  44. $this->userManager,
  45. $this->jobList,
  46. $this->manager,
  47. $this->registry
  48. );
  49. }
  50. public function testRunAlreadyGenerated() {
  51. $this->user->method('isEnabled')
  52. ->willReturn(true);
  53. $this->registry->method('getProviderStates')
  54. ->with($this->user)
  55. ->willReturn(['backup_codes' => true]);
  56. $this->manager->method('isTwoFactorAuthenticated')
  57. ->with($this->user)
  58. ->willReturn(true);
  59. $this->jobList->expects($this->never())
  60. ->method($this->anything());
  61. $this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
  62. }
  63. public function testRun() {
  64. $this->user->method('getUID')
  65. ->willReturn('myUID');
  66. $this->user->method('isEnabled')
  67. ->willReturn(true);
  68. $this->registry->expects($this->once())
  69. ->method('getProviderStates')
  70. ->with($this->user)
  71. ->willReturn([
  72. 'backup_codes' => false,
  73. ]);
  74. $this->jobList->expects($this->once())
  75. ->method('add')
  76. ->with(
  77. $this->equalTo(RememberBackupCodesJob::class),
  78. ['uid' => 'myUID']
  79. );
  80. $this->manager->method('isTwoFactorAuthenticated')
  81. ->with($this->user)
  82. ->willReturn(true);
  83. $this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
  84. }
  85. public function testRunDisabledUser() {
  86. $this->user->method('getUID')
  87. ->willReturn('myUID');
  88. $this->user->method('isEnabled')
  89. ->willReturn(false);
  90. $this->registry->expects($this->never())
  91. ->method('getProviderStates')
  92. ->with($this->user);
  93. $this->jobList->expects($this->never())
  94. ->method('add');
  95. $this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
  96. }
  97. public function testRunNoProviders() {
  98. $this->user->method('isEnabled')
  99. ->willReturn(true);
  100. $this->registry->expects($this->once())
  101. ->method('getProviderStates')
  102. ->with($this->user)
  103. ->willReturn([
  104. 'backup_codes' => false,
  105. ]);
  106. $this->jobList->expects($this->never())
  107. ->method($this->anything());
  108. $this->manager->method('isTwoFactorAuthenticated')
  109. ->with($this->user)
  110. ->willReturn(false);
  111. $this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
  112. }
  113. }