EnableTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Core\Command\TwoFactorAuth;
  25. use OC\Authentication\TwoFactorAuth\ProviderManager;
  26. use OC\Core\Command\TwoFactorAuth\Enable;
  27. use OCP\IUser;
  28. use OCP\IUserManager;
  29. use PHPUnit\Framework\MockObject\MockObject;
  30. use Symfony\Component\Console\Tester\CommandTester;
  31. use Test\TestCase;
  32. class EnableTest extends TestCase {
  33. /** @var ProviderManager|MockObject */
  34. private $providerManager;
  35. /** @var IUserManager|MockObject */
  36. private $userManager;
  37. /** @var CommandTester */
  38. private $command;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->providerManager = $this->createMock(ProviderManager::class);
  42. $this->userManager = $this->createMock(IUserManager::class);
  43. $cmd = new Enable($this->providerManager, $this->userManager);
  44. $this->command = new CommandTester($cmd);
  45. }
  46. public function testInvalidUID() {
  47. $this->userManager->expects($this->once())
  48. ->method('get')
  49. ->with('nope')
  50. ->willReturn(null);
  51. $rc = $this->command->execute([
  52. 'uid' => 'nope',
  53. 'provider_id' => 'nope',
  54. ]);
  55. $this->assertEquals(1, $rc);
  56. $this->assertStringContainsString("Invalid UID", $this->command->getDisplay());
  57. }
  58. public function testEnableNotSupported() {
  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(false);
  68. $rc = $this->command->execute([
  69. 'uid' => 'belle',
  70. 'provider_id' => 'totp',
  71. ]);
  72. $this->assertEquals(2, $rc);
  73. $this->assertStringContainsString("The provider does not support this operation", $this->command->getDisplay());
  74. }
  75. public function testEnabled() {
  76. $user = $this->createMock(IUser::class);
  77. $this->userManager->expects($this->once())
  78. ->method('get')
  79. ->with('belle')
  80. ->willReturn($user);
  81. $this->providerManager->expects($this->once())
  82. ->method('tryEnableProviderFor')
  83. ->with('totp', $user)
  84. ->willReturn(true);
  85. $rc = $this->command->execute([
  86. 'uid' => 'belle',
  87. 'provider_id' => 'totp',
  88. ]);
  89. $this->assertEquals(0, $rc);
  90. $this->assertStringContainsString("Two-factor provider totp enabled for user belle", $this->command->getDisplay());
  91. }
  92. }