1
0

ProviderManagerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\Authentication\TwoFactorAuth\ProviderLoader;
  9. use OC\Authentication\TwoFactorAuth\ProviderManager;
  10. use OCP\Authentication\TwoFactorAuth\IActivatableByAdmin;
  11. use OCP\Authentication\TwoFactorAuth\IDeactivatableByAdmin;
  12. use OCP\Authentication\TwoFactorAuth\IProvider;
  13. use OCP\Authentication\TwoFactorAuth\IRegistry;
  14. use OCP\IUser;
  15. use PHPUnit\Framework\MockObject\MockObject;
  16. use Test\TestCase;
  17. class ProviderManagerTest extends TestCase {
  18. /** @var ProviderLoader|MockObject */
  19. private $providerLoader;
  20. /** @var IRegistry|MockObject */
  21. private $registry;
  22. /** @var ProviderManager */
  23. private $providerManager;
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $this->providerLoader = $this->createMock(ProviderLoader::class);
  27. $this->registry = $this->createMock(IRegistry::class);
  28. $this->providerManager = new ProviderManager(
  29. $this->providerLoader,
  30. $this->registry
  31. );
  32. }
  33. public function testTryEnableInvalidProvider() {
  34. $this->expectException(\OC\Authentication\Exceptions\InvalidProviderException::class);
  35. $user = $this->createMock(IUser::class);
  36. $this->providerManager->tryEnableProviderFor('none', $user);
  37. }
  38. public function testTryEnableUnsupportedProvider() {
  39. $user = $this->createMock(IUser::class);
  40. $provider = $this->createMock(IProvider::class);
  41. $this->providerLoader->expects($this->once())
  42. ->method('getProviders')
  43. ->with($user)
  44. ->willReturn([
  45. 'u2f' => $provider,
  46. ]);
  47. $this->registry->expects($this->never())
  48. ->method('enableProviderFor');
  49. $res = $this->providerManager->tryEnableProviderFor('u2f', $user);
  50. $this->assertFalse($res);
  51. }
  52. public function testTryEnableProvider() {
  53. $user = $this->createMock(IUser::class);
  54. $provider = $this->createMock(IActivatableByAdmin::class);
  55. $this->providerLoader->expects($this->once())
  56. ->method('getProviders')
  57. ->with($user)
  58. ->willReturn([
  59. 'u2f' => $provider,
  60. ]);
  61. $provider->expects($this->once())
  62. ->method('enableFor')
  63. ->with($user);
  64. $this->registry->expects($this->once())
  65. ->method('enableProviderFor')
  66. ->with($provider, $user);
  67. $res = $this->providerManager->tryEnableProviderFor('u2f', $user);
  68. $this->assertTrue($res);
  69. }
  70. public function testTryDisableInvalidProvider() {
  71. $this->expectException(\OC\Authentication\Exceptions\InvalidProviderException::class);
  72. $user = $this->createMock(IUser::class);
  73. $this->providerManager->tryDisableProviderFor('none', $user);
  74. }
  75. public function testTryDisableUnsupportedProvider() {
  76. $user = $this->createMock(IUser::class);
  77. $provider = $this->createMock(IProvider::class);
  78. $this->providerLoader->expects($this->once())
  79. ->method('getProviders')
  80. ->with($user)
  81. ->willReturn([
  82. 'u2f' => $provider,
  83. ]);
  84. $this->registry->expects($this->never())
  85. ->method('disableProviderFor');
  86. $res = $this->providerManager->tryDisableProviderFor('u2f', $user);
  87. $this->assertFalse($res);
  88. }
  89. public function testTryDisableProvider() {
  90. $user = $this->createMock(IUser::class);
  91. $provider = $this->createMock(IDeactivatableByAdmin::class);
  92. $this->providerLoader->expects($this->once())
  93. ->method('getProviders')
  94. ->with($user)
  95. ->willReturn([
  96. 'u2f' => $provider,
  97. ]);
  98. $provider->expects($this->once())
  99. ->method('disableFor')
  100. ->with($user);
  101. $this->registry->expects($this->once())
  102. ->method('disableProviderFor')
  103. ->with($provider, $user);
  104. $res = $this->providerManager->tryDisableProviderFor('u2f', $user);
  105. $this->assertTrue($res);
  106. }
  107. }