ProviderManagerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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() {
  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. /**
  51. * @expectedException \OC\Authentication\Exceptions\InvalidProviderException
  52. */
  53. public function testTryEnableInvalidProvider() {
  54. $user = $this->createMock(IUser::class);
  55. $this->providerManager->tryEnableProviderFor('none', $user);
  56. }
  57. public function testTryEnableUnsupportedProvider() {
  58. $user = $this->createMock(IUser::class);
  59. $provider = $this->createMock(IProvider::class);
  60. $this->providerLoader->expects($this->once())
  61. ->method('getProviders')
  62. ->with($user)
  63. ->willReturn([
  64. 'u2f' => $provider,
  65. ]);
  66. $this->registry->expects($this->never())
  67. ->method('enableProviderFor');
  68. $res = $this->providerManager->tryEnableProviderFor('u2f', $user);
  69. $this->assertFalse($res);
  70. }
  71. public function testTryEnableProvider() {
  72. $user = $this->createMock(IUser::class);
  73. $provider = $this->createMock(IActivatableByAdmin::class);
  74. $this->providerLoader->expects($this->once())
  75. ->method('getProviders')
  76. ->with($user)
  77. ->willReturn([
  78. 'u2f' => $provider,
  79. ]);
  80. $provider->expects($this->once())
  81. ->method('enableFor')
  82. ->with($user);
  83. $this->registry->expects($this->once())
  84. ->method('enableProviderFor')
  85. ->with($provider, $user);
  86. $res = $this->providerManager->tryEnableProviderFor('u2f', $user);
  87. $this->assertTrue($res);
  88. }
  89. /**
  90. * @expectedException \OC\Authentication\Exceptions\InvalidProviderException
  91. */
  92. public function testTryDisableInvalidProvider() {
  93. $user = $this->createMock(IUser::class);
  94. $this->providerManager->tryDisableProviderFor('none', $user);
  95. }
  96. public function testTryDisableUnsupportedProvider() {
  97. $user = $this->createMock(IUser::class);
  98. $provider = $this->createMock(IProvider::class);
  99. $this->providerLoader->expects($this->once())
  100. ->method('getProviders')
  101. ->with($user)
  102. ->willReturn([
  103. 'u2f' => $provider,
  104. ]);
  105. $this->registry->expects($this->never())
  106. ->method('disableProviderFor');
  107. $res = $this->providerManager->tryDisableProviderFor('u2f', $user);
  108. $this->assertFalse($res);
  109. }
  110. public function testTryDisableProvider() {
  111. $user = $this->createMock(IUser::class);
  112. $provider = $this->createMock(IDeactivatableByAdmin::class);
  113. $this->providerLoader->expects($this->once())
  114. ->method('getProviders')
  115. ->with($user)
  116. ->willReturn([
  117. 'u2f' => $provider,
  118. ]);
  119. $provider->expects($this->once())
  120. ->method('disableFor')
  121. ->with($user);
  122. $this->registry->expects($this->once())
  123. ->method('disableProviderFor')
  124. ->with($provider, $user);
  125. $res = $this->providerManager->tryDisableProviderFor('u2f', $user);
  126. $this->assertTrue($res);
  127. }
  128. }