CheckBackupCodeTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\TwoFactorBackupCodes\Tests\Unit\BackgroundJob;
  26. use OC\Authentication\TwoFactorAuth\Manager;
  27. use OCA\TwoFactorBackupCodes\BackgroundJob\CheckBackupCodes;
  28. use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\Authentication\TwoFactorAuth\IRegistry;
  31. use OCP\BackgroundJob\IJobList;
  32. use OCP\IUser;
  33. use OCP\IUserManager;
  34. use PHPUnit\Framework\MockObject\MockObject;
  35. use Test\TestCase;
  36. class CheckBackupCodeTest extends TestCase {
  37. /** @var IUserManager|MockObject */
  38. private $userManager;
  39. /** @var IJobList|MockObject */
  40. private $jobList;
  41. /** @var IRegistry|MockObject */
  42. private $registry;
  43. /** @var Manager|MockObject */
  44. private $manager;
  45. /** @var IUser|MockObject */
  46. private $user;
  47. /** @var CheckBackupCodes */
  48. private $checkBackupCodes;
  49. protected function setUp(): void {
  50. parent::setUp();
  51. $this->userManager = $this->createMock(IUserManager::class);
  52. $this->jobList = $this->createMock(IJobList::class);
  53. $this->registry = $this->createMock(IRegistry::class);
  54. $this->manager = $this->createMock(Manager::class);
  55. $this->user = $this->createMock(IUser::class);
  56. $this->userManager->method('callForSeenUsers')
  57. ->willReturnCallback(function (\Closure $e) {
  58. $e($this->user);
  59. });
  60. $this->checkBackupCodes = new CheckBackupCodes(
  61. $this->createMock(ITimeFactory::class),
  62. $this->userManager,
  63. $this->jobList,
  64. $this->manager,
  65. $this->registry
  66. );
  67. }
  68. public function testRunAlreadyGenerated() {
  69. $this->user->method('isEnabled')
  70. ->willReturn(true);
  71. $this->registry->method('getProviderStates')
  72. ->with($this->user)
  73. ->willReturn(['backup_codes' => true]);
  74. $this->manager->method('isTwoFactorAuthenticated')
  75. ->with($this->user)
  76. ->willReturn(true);
  77. $this->jobList->expects($this->never())
  78. ->method($this->anything());
  79. $this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
  80. }
  81. public function testRun() {
  82. $this->user->method('getUID')
  83. ->willReturn('myUID');
  84. $this->user->method('isEnabled')
  85. ->willReturn(true);
  86. $this->registry->expects($this->once())
  87. ->method('getProviderStates')
  88. ->with($this->user)
  89. ->willReturn([
  90. 'backup_codes' => false,
  91. ]);
  92. $this->jobList->expects($this->once())
  93. ->method('add')
  94. ->with(
  95. $this->equalTo(RememberBackupCodesJob::class),
  96. ['uid' => 'myUID']
  97. );
  98. $this->manager->method('isTwoFactorAuthenticated')
  99. ->with($this->user)
  100. ->willReturn(true);
  101. $this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
  102. }
  103. public function testRunDisabledUser() {
  104. $this->user->method('getUID')
  105. ->willReturn('myUID');
  106. $this->user->method('isEnabled')
  107. ->willReturn(false);
  108. $this->registry->expects($this->never())
  109. ->method('getProviderStates')
  110. ->with($this->user);
  111. $this->jobList->expects($this->never())
  112. ->method('add');
  113. $this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
  114. }
  115. public function testRunNoProviders() {
  116. $this->user->method('isEnabled')
  117. ->willReturn(true);
  118. $this->registry->expects($this->once())
  119. ->method('getProviderStates')
  120. ->with($this->user)
  121. ->willReturn([
  122. 'backup_codes' => false,
  123. ]);
  124. $this->jobList->expects($this->never())
  125. ->method($this->anything());
  126. $this->manager->method('isTwoFactorAuthenticated')
  127. ->with($this->user)
  128. ->willReturn(false);
  129. $this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
  130. }
  131. }