RegistryTest.php 4.2 KB

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