BackupCodesProviderTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\TwoFactorBackupCodes\Tests\Unit\Provider;
  8. use OC\App\AppManager;
  9. use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider;
  10. use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage;
  11. use OCP\IInitialStateService;
  12. use OCP\IL10N;
  13. use OCP\IUser;
  14. use OCP\Template;
  15. use Test\TestCase;
  16. class BackupCodesProviderTest extends TestCase {
  17. /** @var string */
  18. private $appName;
  19. /** @var BackupCodeStorage|\PHPUnit\Framework\MockObject\MockObject */
  20. private $storage;
  21. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  22. private $l10n;
  23. /** @var AppManager|\PHPUnit\Framework\MockObject\MockObject */
  24. private $appManager;
  25. /** @var IInitialStateService|\PHPUnit\Framework\MockObject\MockObject */
  26. private $initialState;
  27. /** @var BackupCodesProvider */
  28. private $provider;
  29. protected function setUp(): void {
  30. parent::setUp();
  31. $this->appName = "twofactor_backupcodes";
  32. $this->storage = $this->createMock(BackupCodeStorage::class);
  33. $this->l10n = $this->createMock(IL10N::class);
  34. $this->appManager = $this->createMock(AppManager::class);
  35. $this->initialState = $this->createMock(IInitialStateService::class);
  36. $this->provider = new BackupCodesProvider($this->appName, $this->storage, $this->l10n, $this->appManager, $this->initialState);
  37. }
  38. public function testGetId() {
  39. $this->assertEquals('backup_codes', $this->provider->getId());
  40. }
  41. public function testGetDisplayName() {
  42. $this->l10n->expects($this->once())
  43. ->method('t')
  44. ->with('Backup code')
  45. ->willReturn('l10n backup code');
  46. $this->assertSame('l10n backup code', $this->provider->getDisplayName());
  47. }
  48. public function testGetDescription() {
  49. $this->l10n->expects($this->once())
  50. ->method('t')
  51. ->with('Use backup code')
  52. ->willReturn('l10n use backup code');
  53. $this->assertSame('l10n use backup code', $this->provider->getDescription());
  54. }
  55. public function testGetTempalte() {
  56. $user = $this->getMockBuilder(IUser::class)->getMock();
  57. $expected = new Template('twofactor_backupcodes', 'challenge');
  58. $this->assertEquals($expected, $this->provider->getTemplate($user));
  59. }
  60. public function testVerfiyChallenge() {
  61. $user = $this->getMockBuilder(IUser::class)->getMock();
  62. $challenge = 'xyz';
  63. $this->storage->expects($this->once())
  64. ->method('validateCode')
  65. ->with($user, $challenge)
  66. ->willReturn(false);
  67. $this->assertFalse($this->provider->verifyChallenge($user, $challenge));
  68. }
  69. public function testIsTwoFactorEnabledForUser() {
  70. $user = $this->getMockBuilder(IUser::class)->getMock();
  71. $this->storage->expects($this->once())
  72. ->method('hasBackupCodes')
  73. ->with($user)
  74. ->willReturn(true);
  75. $this->assertTrue($this->provider->isTwoFactorAuthEnabledForUser($user));
  76. }
  77. public function testIsActiveNoProviders() {
  78. $user = $this->getMockBuilder(IUser::class)->getMock();
  79. $this->appManager->expects($this->once())
  80. ->method('getEnabledAppsForUser')
  81. ->with($user)
  82. ->willReturn([
  83. 'twofactor_backupcodes',
  84. 'mail',
  85. ]);
  86. $this->appManager->expects($this->once())
  87. ->method('getAppInfo')
  88. ->with('mail')
  89. ->willReturn([
  90. 'two-factor-providers' => [],
  91. ]);
  92. $this->assertFalse($this->provider->isActive($user));
  93. }
  94. public function testIsActiveWithProviders() {
  95. $user = $this->getMockBuilder(IUser::class)->getMock();
  96. $this->appManager->expects($this->once())
  97. ->method('getEnabledAppsForUser')
  98. ->with($user)
  99. ->willReturn([
  100. 'twofactor_backupcodes',
  101. 'twofactor_u2f',
  102. ]);
  103. $this->appManager->expects($this->once())
  104. ->method('getAppInfo')
  105. ->with('twofactor_u2f')
  106. ->willReturn([
  107. 'two-factor-providers' => [
  108. 'OCA\TwoFactorU2F\Provider\U2FProvider',
  109. ],
  110. ]);
  111. $this->assertTrue($this->provider->isActive($user));
  112. }
  113. public function testDisable(): void {
  114. $user = $this->getMockBuilder(IUser::class)->getMock();
  115. $this->storage->expects(self::once())
  116. ->method('deleteCodes')
  117. ->with($user);
  118. $this->provider->disableFor($user);
  119. }
  120. }