1
0

RegistryTest.php 4.3 KB

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