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