EnableTest.php 2.4 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 Test\Core\Command\TwoFactorAuth;
  8. use OC\Authentication\TwoFactorAuth\ProviderManager;
  9. use OC\Core\Command\TwoFactorAuth\Enable;
  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 EnableTest extends TestCase {
  16. /** @var ProviderManager|MockObject */
  17. private $providerManager;
  18. /** @var IUserManager|MockObject */
  19. private $userManager;
  20. /** @var CommandTester */
  21. private $command;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $this->providerManager = $this->createMock(ProviderManager::class);
  25. $this->userManager = $this->createMock(IUserManager::class);
  26. $cmd = new Enable($this->providerManager, $this->userManager);
  27. $this->command = new CommandTester($cmd);
  28. }
  29. public function testInvalidUID(): void {
  30. $this->userManager->expects($this->once())
  31. ->method('get')
  32. ->with('nope')
  33. ->willReturn(null);
  34. $rc = $this->command->execute([
  35. 'uid' => 'nope',
  36. 'provider_id' => 'nope',
  37. ]);
  38. $this->assertEquals(1, $rc);
  39. $this->assertStringContainsString('Invalid UID', $this->command->getDisplay());
  40. }
  41. public function testEnableNotSupported(): void {
  42. $user = $this->createMock(IUser::class);
  43. $this->userManager->expects($this->once())
  44. ->method('get')
  45. ->with('belle')
  46. ->willReturn($user);
  47. $this->providerManager->expects($this->once())
  48. ->method('tryEnableProviderFor')
  49. ->with('totp', $user)
  50. ->willReturn(false);
  51. $rc = $this->command->execute([
  52. 'uid' => 'belle',
  53. 'provider_id' => 'totp',
  54. ]);
  55. $this->assertEquals(2, $rc);
  56. $this->assertStringContainsString('The provider does not support this operation', $this->command->getDisplay());
  57. }
  58. public function testEnabled(): void {
  59. $user = $this->createMock(IUser::class);
  60. $this->userManager->expects($this->once())
  61. ->method('get')
  62. ->with('belle')
  63. ->willReturn($user);
  64. $this->providerManager->expects($this->once())
  65. ->method('tryEnableProviderFor')
  66. ->with('totp', $user)
  67. ->willReturn(true);
  68. $rc = $this->command->execute([
  69. 'uid' => 'belle',
  70. 'provider_id' => 'totp',
  71. ]);
  72. $this->assertEquals(0, $rc);
  73. $this->assertStringContainsString('Two-factor provider totp enabled for user belle', $this->command->getDisplay());
  74. }
  75. }