1
0

ProviderLoaderTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace lib\Authentication\TwoFactorAuth;
  8. use OC\AppFramework\Bootstrap\Coordinator;
  9. use OC\AppFramework\Bootstrap\RegistrationContext;
  10. use OC\AppFramework\Bootstrap\ServiceRegistration;
  11. use OC\Authentication\TwoFactorAuth\ProviderLoader;
  12. use OCP\App\IAppManager;
  13. use OCP\Authentication\TwoFactorAuth\IProvider;
  14. use OCP\IUser;
  15. use PHPUnit\Framework\MockObject\MockObject;
  16. use Test\TestCase;
  17. class ProviderLoaderTest extends TestCase {
  18. /** @var IAppManager|MockObject */
  19. private $appManager;
  20. /** @var IUser|MockObject */
  21. private $user;
  22. /** @var RegistrationContext|MockObject */
  23. private $registrationContext;
  24. /** @var ProviderLoader */
  25. private $loader;
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->appManager = $this->createMock(IAppManager::class);
  29. $this->user = $this->createMock(IUser::class);
  30. $this->registrationContext = $this->createMock(RegistrationContext::class);
  31. $coordinator = $this->createMock(Coordinator::class);
  32. $coordinator->method('getRegistrationContext')
  33. ->willReturn($this->registrationContext);
  34. $this->loader = new ProviderLoader($this->appManager, $coordinator);
  35. }
  36. public function testFailHardIfProviderCanNotBeLoaded(): void {
  37. $this->expectException(\Exception::class);
  38. $this->expectExceptionMessage('Could not load two-factor auth provider \\OCA\\MyFaulty2faApp\\DoesNotExist');
  39. $this->appManager->expects($this->once())
  40. ->method('getEnabledAppsForUser')
  41. ->with($this->user)
  42. ->willReturn(['mail', 'twofactor_totp']);
  43. $this->appManager
  44. ->method('getAppInfo')
  45. ->willReturnMap([
  46. ['mail', false, null, []],
  47. ['twofactor_totp', false, null, [
  48. 'two-factor-providers' => [
  49. '\\OCA\\MyFaulty2faApp\\DoesNotExist',
  50. ],
  51. ]],
  52. ]);
  53. $this->loader->getProviders($this->user);
  54. }
  55. public function testGetProviders(): void {
  56. $provider = $this->createMock(IProvider::class);
  57. $provider->method('getId')->willReturn('test');
  58. \OC::$server->registerService('\\OCA\\TwoFactorTest\\Provider', function () use ($provider) {
  59. return $provider;
  60. });
  61. $this->appManager->expects($this->once())
  62. ->method('getEnabledAppsForUser')
  63. ->with($this->user)
  64. ->willReturn(['twofactor_test']);
  65. $this->appManager
  66. ->method('getAppInfo')
  67. ->with('twofactor_test')
  68. ->willReturn(['two-factor-providers' => [
  69. '\\OCA\\TwoFactorTest\\Provider',
  70. ]]);
  71. $providers = $this->loader->getProviders($this->user);
  72. $this->assertCount(1, $providers);
  73. $this->assertArrayHasKey('test', $providers);
  74. $this->assertSame($provider, $providers['test']);
  75. }
  76. public function testGetProvidersBootstrap(): void {
  77. $provider = $this->createMock(IProvider::class);
  78. $provider->method('getId')->willReturn('test');
  79. \OC::$server->registerService('\\OCA\\TwoFactorTest\\Provider', function () use ($provider) {
  80. return $provider;
  81. });
  82. $this->appManager->expects($this->once())
  83. ->method('getEnabledAppsForUser')
  84. ->with($this->user)
  85. ->willReturn([]);
  86. $this->registrationContext->method('getTwoFactorProviders')
  87. ->willReturn([
  88. new ServiceRegistration('twofactor_test', '\\OCA\\TwoFactorTest\\Provider')
  89. ]);
  90. $providers = $this->loader->getProviders($this->user);
  91. $this->assertCount(1, $providers);
  92. $this->assertArrayHasKey('test', $providers);
  93. $this->assertSame($provider, $providers['test']);
  94. }
  95. }