1
0

ProviderManagerTest.php 4.4 KB

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