CheckBackupCodeTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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->registry->method('getProviderStates')
  70. ->with($this->user)
  71. ->willReturn(['backup_codes' => true]);
  72. $this->manager->method('isTwoFactorAuthenticated')
  73. ->with($this->user)
  74. ->willReturn(true);
  75. $this->jobList->expects($this->never())
  76. ->method($this->anything());
  77. $this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
  78. }
  79. public function testRun() {
  80. $this->user->method('getUID')
  81. ->willReturn('myUID');
  82. $this->registry->expects($this->once())
  83. ->method('getProviderStates')
  84. ->with($this->user)
  85. ->willReturn([
  86. 'backup_codes' => false,
  87. ]);
  88. $this->jobList->expects($this->once())
  89. ->method('add')
  90. ->with(
  91. $this->equalTo(RememberBackupCodesJob::class),
  92. ['uid' => 'myUID']
  93. );
  94. $this->manager->method('isTwoFactorAuthenticated')
  95. ->with($this->user)
  96. ->willReturn(true);
  97. $this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
  98. }
  99. public function testRunNoProviders() {
  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. }