BackupCodesProviderTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\TwoFactorBackupCodes\Tests\Unit\Provider;
  27. use OC\App\AppManager;
  28. use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider;
  29. use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage;
  30. use OCP\IInitialStateService;
  31. use OCP\IL10N;
  32. use OCP\IUser;
  33. use OCP\Template;
  34. use Test\TestCase;
  35. class BackupCodesProviderTest extends TestCase {
  36. /** @var string */
  37. private $appName;
  38. /** @var BackupCodeStorage|\PHPUnit\Framework\MockObject\MockObject */
  39. private $storage;
  40. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  41. private $l10n;
  42. /** @var AppManager|\PHPUnit\Framework\MockObject\MockObject */
  43. private $appManager;
  44. /** @var IInitialStateService|\PHPUnit\Framework\MockObject\MockObject */
  45. private $initialState;
  46. /** @var BackupCodesProvider */
  47. private $provider;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->appName = "twofactor_backupcodes";
  51. $this->storage = $this->createMock(BackupCodeStorage::class);
  52. $this->l10n = $this->createMock(IL10N::class);
  53. $this->appManager = $this->createMock(AppManager::class);
  54. $this->initialState = $this->createMock(IInitialStateService::class);
  55. $this->provider = new BackupCodesProvider($this->appName, $this->storage, $this->l10n, $this->appManager, $this->initialState);
  56. }
  57. public function testGetId() {
  58. $this->assertEquals('backup_codes', $this->provider->getId());
  59. }
  60. public function testGetDisplayName() {
  61. $this->l10n->expects($this->once())
  62. ->method('t')
  63. ->with('Backup code')
  64. ->willReturn('l10n backup code');
  65. $this->assertSame('l10n backup code', $this->provider->getDisplayName());
  66. }
  67. public function testGetDescription() {
  68. $this->l10n->expects($this->once())
  69. ->method('t')
  70. ->with('Use backup code')
  71. ->willReturn('l10n use backup code');
  72. $this->assertSame('l10n use backup code', $this->provider->getDescription());
  73. }
  74. public function testGetTempalte() {
  75. $user = $this->getMockBuilder(IUser::class)->getMock();
  76. $expected = new Template('twofactor_backupcodes', 'challenge');
  77. $this->assertEquals($expected, $this->provider->getTemplate($user));
  78. }
  79. public function testVerfiyChallenge() {
  80. $user = $this->getMockBuilder(IUser::class)->getMock();
  81. $challenge = 'xyz';
  82. $this->storage->expects($this->once())
  83. ->method('validateCode')
  84. ->with($user, $challenge)
  85. ->willReturn(false);
  86. $this->assertFalse($this->provider->verifyChallenge($user, $challenge));
  87. }
  88. public function testIsTwoFactorEnabledForUser() {
  89. $user = $this->getMockBuilder(IUser::class)->getMock();
  90. $this->storage->expects($this->once())
  91. ->method('hasBackupCodes')
  92. ->with($user)
  93. ->willReturn(true);
  94. $this->assertTrue($this->provider->isTwoFactorAuthEnabledForUser($user));
  95. }
  96. public function testIsActiveNoProviders() {
  97. $user = $this->getMockBuilder(IUser::class)->getMock();
  98. $this->appManager->expects($this->once())
  99. ->method('getEnabledAppsForUser')
  100. ->with($user)
  101. ->willReturn([
  102. 'twofactor_backupcodes',
  103. 'mail',
  104. ]);
  105. $this->appManager->expects($this->once())
  106. ->method('getAppInfo')
  107. ->with('mail')
  108. ->willReturn([
  109. 'two-factor-providers' => [],
  110. ]);
  111. $this->assertFalse($this->provider->isActive($user));
  112. }
  113. public function testIsActiveWithProviders() {
  114. $user = $this->getMockBuilder(IUser::class)->getMock();
  115. $this->appManager->expects($this->once())
  116. ->method('getEnabledAppsForUser')
  117. ->with($user)
  118. ->willReturn([
  119. 'twofactor_backupcodes',
  120. 'twofactor_u2f',
  121. ]);
  122. $this->appManager->expects($this->once())
  123. ->method('getAppInfo')
  124. ->with('twofactor_u2f')
  125. ->willReturn([
  126. 'two-factor-providers' => [
  127. 'OCA\TwoFactorU2F\Provider\U2FProvider',
  128. ],
  129. ]);
  130. $this->assertTrue($this->provider->isActive($user));
  131. }
  132. public function testDisable(): void {
  133. $user = $this->getMockBuilder(IUser::class)->getMock();
  134. $this->storage->expects(self::once())
  135. ->method('deleteCodes')
  136. ->with($user);
  137. $this->provider->disableFor($user);
  138. }
  139. }