1
0

ProviderLoaderTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  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 lib\Authentication\TwoFactorAuth;
  25. use OC\AppFramework\Bootstrap\Coordinator;
  26. use OC\AppFramework\Bootstrap\RegistrationContext;
  27. use OC\AppFramework\Bootstrap\ServiceRegistration;
  28. use OC\Authentication\TwoFactorAuth\ProviderLoader;
  29. use OCP\App\IAppManager;
  30. use OCP\Authentication\TwoFactorAuth\IProvider;
  31. use OCP\IUser;
  32. use PHPUnit\Framework\MockObject\MockObject;
  33. use Test\TestCase;
  34. class ProviderLoaderTest extends TestCase {
  35. /** @var IAppManager|MockObject */
  36. private $appManager;
  37. /** @var IUser|MockObject */
  38. private $user;
  39. /** @var RegistrationContext|MockObject */
  40. private $registrationContext;
  41. /** @var ProviderLoader */
  42. private $loader;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->appManager = $this->createMock(IAppManager::class);
  46. $this->user = $this->createMock(IUser::class);
  47. $this->registrationContext = $this->createMock(RegistrationContext::class);
  48. $coordinator = $this->createMock(Coordinator::class);
  49. $coordinator->method('getRegistrationContext')
  50. ->willReturn($this->registrationContext);
  51. $this->loader = new ProviderLoader($this->appManager, $coordinator);
  52. }
  53. public function testFailHardIfProviderCanNotBeLoaded() {
  54. $this->expectException(\Exception::class);
  55. $this->expectExceptionMessage('Could not load two-factor auth provider \\OCA\\MyFaulty2faApp\\DoesNotExist');
  56. $this->appManager->expects($this->once())
  57. ->method('getEnabledAppsForUser')
  58. ->with($this->user)
  59. ->willReturn(['mail', 'twofactor_totp']);
  60. $this->appManager
  61. ->method('getAppInfo')
  62. ->willReturnMap([
  63. ['mail', false, null, []],
  64. ['twofactor_totp', false, null, [
  65. 'two-factor-providers' => [
  66. '\\OCA\\MyFaulty2faApp\\DoesNotExist',
  67. ],
  68. ]],
  69. ]);
  70. $this->loader->getProviders($this->user);
  71. }
  72. public function testGetProviders() {
  73. $provider = $this->createMock(IProvider::class);
  74. $provider->method('getId')->willReturn('test');
  75. \OC::$server->registerService('\\OCA\\TwoFactorTest\\Provider', function () use ($provider) {
  76. return $provider;
  77. });
  78. $this->appManager->expects($this->once())
  79. ->method('getEnabledAppsForUser')
  80. ->with($this->user)
  81. ->willReturn(['twofactor_test']);
  82. $this->appManager
  83. ->method('getAppInfo')
  84. ->with('twofactor_test')
  85. ->willReturn(['two-factor-providers' => [
  86. '\\OCA\\TwoFactorTest\\Provider',
  87. ]]);
  88. $providers = $this->loader->getProviders($this->user);
  89. $this->assertCount(1, $providers);
  90. $this->assertArrayHasKey('test', $providers);
  91. $this->assertSame($provider, $providers['test']);
  92. }
  93. public function testGetProvidersBootstrap() {
  94. $provider = $this->createMock(IProvider::class);
  95. $provider->method('getId')->willReturn('test');
  96. \OC::$server->registerService('\\OCA\\TwoFactorTest\\Provider', function () use ($provider) {
  97. return $provider;
  98. });
  99. $this->appManager->expects($this->once())
  100. ->method('getEnabledAppsForUser')
  101. ->with($this->user)
  102. ->willReturn([]);
  103. $this->registrationContext->method('getTwoFactorProviders')
  104. ->willReturn([
  105. new ServiceRegistration('twofactor_test', '\\OCA\\TwoFactorTest\\Provider')
  106. ]);
  107. $providers = $this->loader->getProviders($this->user);
  108. $this->assertCount(1, $providers);
  109. $this->assertArrayHasKey('test', $providers);
  110. $this->assertSame($provider, $providers['test']);
  111. }
  112. }