RegistryTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php declare(strict_types=1);
  2. /**
  3. * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Authentication\TwoFactorAuth;
  24. use OC\Authentication\TwoFactorAuth\Db\ProviderUserAssignmentDao;
  25. use OC\Authentication\TwoFactorAuth\Registry;
  26. use OCP\Authentication\TwoFactorAuth\IProvider;
  27. use OCP\Authentication\TwoFactorAuth\IRegistry;
  28. use OCP\Authentication\TwoFactorAuth\RegistryEvent;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use OCP\IUser;
  31. use PHPUnit\Framework\MockObject\MockObject;
  32. use Test\TestCase;
  33. class RegistryTest extends TestCase {
  34. /** @var ProviderUserAssignmentDao|MockObject */
  35. private $dao;
  36. /** @var IEventDispatcher|MockObject */
  37. private $dispatcher;
  38. /** @var Registry */
  39. private $registry;
  40. protected function setUp() {
  41. parent::setUp();
  42. $this->dao = $this->createMock(ProviderUserAssignmentDao::class);
  43. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  44. $this->registry = new Registry($this->dao, $this->dispatcher);
  45. }
  46. public function testGetProviderStates() {
  47. $user = $this->createMock(IUser::class);
  48. $user->expects($this->once())->method('getUID')->willReturn('user123');
  49. $state = [
  50. 'twofactor_totp' => true,
  51. ];
  52. $this->dao->expects($this->once())->method('getState')->willReturn($state);
  53. $actual = $this->registry->getProviderStates($user);
  54. $this->assertEquals($state, $actual);
  55. }
  56. public function testEnableProvider() {
  57. $user = $this->createMock(IUser::class);
  58. $provider = $this->createMock(IProvider::class);
  59. $user->expects($this->once())->method('getUID')->willReturn('user123');
  60. $provider->expects($this->once())->method('getId')->willReturn('p1');
  61. $this->dao->expects($this->once())->method('persist')->with('p1', 'user123',
  62. true);
  63. $this->dispatcher->expects($this->once())
  64. ->method('dispatch')
  65. ->with(
  66. $this->equalTo(IRegistry::EVENT_PROVIDER_ENABLED),
  67. $this->callback(function(RegistryEvent $e) use ($user, $provider) {
  68. return $e->getUser() === $user && $e->getProvider() === $provider;
  69. })
  70. );
  71. $this->registry->enableProviderFor($provider, $user);
  72. }
  73. public function testDisableProvider() {
  74. $user = $this->createMock(IUser::class);
  75. $provider = $this->createMock(IProvider::class);
  76. $user->expects($this->once())->method('getUID')->willReturn('user123');
  77. $provider->expects($this->once())->method('getId')->willReturn('p1');
  78. $this->dao->expects($this->once())->method('persist')->with('p1', 'user123',
  79. false);
  80. $this->dispatcher->expects($this->once())
  81. ->method('dispatch')
  82. ->with(
  83. $this->equalTo(IRegistry::EVENT_PROVIDER_DISABLED),
  84. $this->callback(function(RegistryEvent $e) use ($user, $provider) {
  85. return $e->getUser() === $user && $e->getProvider() === $provider;
  86. })
  87. );
  88. $this->registry->disableProviderFor($provider, $user);
  89. }
  90. public function testCleanUp() {
  91. $this->dao->expects($this->once())
  92. ->method('deleteAll')
  93. ->with('twofactor_u2f');
  94. $this->registry->cleanUp('twofactor_u2f');
  95. }
  96. }