RegistryTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 Test\Authentication\TwoFactorAuth;
  25. use OC\Authentication\TwoFactorAuth\Db\ProviderUserAssignmentDao;
  26. use OC\Authentication\TwoFactorAuth\Registry;
  27. use OCP\Authentication\TwoFactorAuth\IProvider;
  28. use OCP\Authentication\TwoFactorAuth\IRegistry;
  29. use OCP\Authentication\TwoFactorAuth\RegistryEvent;
  30. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderDisabled;
  31. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserRegistered;
  32. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserUnregistered;
  33. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderUserDeleted;
  34. use OCP\EventDispatcher\IEventDispatcher;
  35. use OCP\IUser;
  36. use PHPUnit\Framework\MockObject\MockObject;
  37. use Test\TestCase;
  38. class RegistryTest extends TestCase {
  39. /** @var ProviderUserAssignmentDao|MockObject */
  40. private $dao;
  41. /** @var IEventDispatcher|MockObject */
  42. private $dispatcher;
  43. /** @var Registry */
  44. private $registry;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->dao = $this->createMock(ProviderUserAssignmentDao::class);
  48. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  49. $this->registry = new Registry($this->dao, $this->dispatcher);
  50. }
  51. public function testGetProviderStates() {
  52. $user = $this->createMock(IUser::class);
  53. $user->expects($this->once())->method('getUID')->willReturn('user123');
  54. $state = [
  55. 'twofactor_totp' => true,
  56. ];
  57. $this->dao->expects($this->once())->method('getState')->willReturn($state);
  58. $actual = $this->registry->getProviderStates($user);
  59. $this->assertEquals($state, $actual);
  60. }
  61. public function testEnableProvider() {
  62. $user = $this->createMock(IUser::class);
  63. $provider = $this->createMock(IProvider::class);
  64. $user->expects($this->once())->method('getUID')->willReturn('user123');
  65. $provider->expects($this->once())->method('getId')->willReturn('p1');
  66. $this->dao->expects($this->once())->method('persist')->with('p1', 'user123',
  67. true);
  68. $this->dispatcher->expects($this->once())
  69. ->method('dispatch')
  70. ->with(
  71. $this->equalTo(IRegistry::EVENT_PROVIDER_ENABLED),
  72. $this->callback(function (RegistryEvent $e) use ($user, $provider) {
  73. return $e->getUser() === $user && $e->getProvider() === $provider;
  74. })
  75. );
  76. $this->dispatcher->expects($this->once())
  77. ->method('dispatchTyped')
  78. ->with(new TwoFactorProviderForUserRegistered(
  79. $user,
  80. $provider,
  81. ));
  82. $this->registry->enableProviderFor($provider, $user);
  83. }
  84. public function testDisableProvider() {
  85. $user = $this->createMock(IUser::class);
  86. $provider = $this->createMock(IProvider::class);
  87. $user->expects($this->once())->method('getUID')->willReturn('user123');
  88. $provider->expects($this->once())->method('getId')->willReturn('p1');
  89. $this->dao->expects($this->once())->method('persist')->with('p1', 'user123',
  90. false);
  91. $this->dispatcher->expects($this->once())
  92. ->method('dispatch')
  93. ->with(
  94. $this->equalTo(IRegistry::EVENT_PROVIDER_DISABLED),
  95. $this->callback(function (RegistryEvent $e) use ($user, $provider) {
  96. return $e->getUser() === $user && $e->getProvider() === $provider;
  97. })
  98. );
  99. $this->dispatcher->expects($this->once())
  100. ->method('dispatchTyped')
  101. ->with(new TwoFactorProviderForUserUnregistered(
  102. $user,
  103. $provider,
  104. ));
  105. $this->registry->disableProviderFor($provider, $user);
  106. }
  107. public function testDeleteUserData() {
  108. $user = $this->createMock(IUser::class);
  109. $user->expects($this->once())->method('getUID')->willReturn('user123');
  110. $this->dao->expects($this->once())
  111. ->method('deleteByUser')
  112. ->with('user123')
  113. ->willReturn([
  114. [
  115. 'provider_id' => 'twofactor_u2f',
  116. ]
  117. ]);
  118. $this->dispatcher->expects($this->exactly(2))
  119. ->method('dispatchTyped')
  120. ->withConsecutive(
  121. [new TwoFactorProviderDisabled('twofactor_u2f')],
  122. [new TwoFactorProviderUserDeleted($user, 'twofactor_u2f')],
  123. );
  124. $this->registry->deleteUserData($user);
  125. }
  126. public function testCleanUp() {
  127. $this->dao->expects($this->once())
  128. ->method('deleteAll')
  129. ->with('twofactor_u2f');
  130. $this->registry->cleanUp('twofactor_u2f');
  131. }
  132. }