BackupCodesProviderTest.php 4.5 KB

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