StateTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 Core\Command\TwoFactorAuth;
  8. use OC\Core\Command\TwoFactorAuth\State;
  9. use OCP\Authentication\TwoFactorAuth\IRegistry;
  10. use OCP\IUser;
  11. use OCP\IUserManager;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. use Symfony\Component\Console\Tester\CommandTester;
  14. use Test\TestCase;
  15. class StateTest extends TestCase {
  16. /** @var IRegistry|MockObject */
  17. private $registry;
  18. /** @var IUserManager|MockObject */
  19. private $userManager;
  20. /** @var CommandTester|MockObject */
  21. private $cmd;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $this->registry = $this->createMock(IRegistry::class);
  25. $this->userManager = $this->createMock(IUserManager::class);
  26. $cmd = new State($this->registry, $this->userManager);
  27. $this->cmd = new CommandTester($cmd);
  28. }
  29. public function testWrongUID() {
  30. $this->cmd->execute([
  31. 'uid' => 'nope',
  32. ]);
  33. $output = $this->cmd->getDisplay();
  34. $this->assertStringContainsString("Invalid UID", $output);
  35. }
  36. public function testStateNoProvidersActive() {
  37. $user = $this->createMock(IUser::class);
  38. $this->userManager->expects($this->once())
  39. ->method('get')
  40. ->with('eldora')
  41. ->willReturn($user);
  42. $states = [
  43. 'u2f' => false,
  44. 'totp' => false,
  45. ];
  46. $this->registry->expects($this->once())
  47. ->method('getProviderStates')
  48. ->with($user)
  49. ->willReturn($states);
  50. $this->cmd->execute([
  51. 'uid' => 'eldora',
  52. ]);
  53. $output = $this->cmd->getDisplay();
  54. $this->assertStringContainsString("Two-factor authentication is not enabled for user eldora", $output);
  55. }
  56. public function testStateOneProviderActive() {
  57. $user = $this->createMock(IUser::class);
  58. $this->userManager->expects($this->once())
  59. ->method('get')
  60. ->with('mohamed')
  61. ->willReturn($user);
  62. $states = [
  63. 'u2f' => true,
  64. 'totp' => false,
  65. ];
  66. $this->registry->expects($this->once())
  67. ->method('getProviderStates')
  68. ->with($user)
  69. ->willReturn($states);
  70. $this->cmd->execute([
  71. 'uid' => 'mohamed',
  72. ]);
  73. $output = $this->cmd->getDisplay();
  74. $this->assertStringContainsString("Two-factor authentication is enabled for user mohamed", $output);
  75. }
  76. }