StateTest.php 2.9 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 Core\Command\TwoFactorAuth;
  25. use OC\Core\Command\TwoFactorAuth\State;
  26. use OCP\Authentication\TwoFactorAuth\IRegistry;
  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 StateTest extends TestCase {
  33. /** @var IRegistry|MockObject */
  34. private $registry;
  35. /** @var IUserManager|MockObject */
  36. private $userManager;
  37. /** @var CommandTester|MockObject */
  38. private $cmd;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->registry = $this->createMock(IRegistry::class);
  42. $this->userManager = $this->createMock(IUserManager::class);
  43. $cmd = new State($this->registry, $this->userManager);
  44. $this->cmd = new CommandTester($cmd);
  45. }
  46. public function testWrongUID() {
  47. $this->cmd->execute([
  48. 'uid' => 'nope',
  49. ]);
  50. $output = $this->cmd->getDisplay();
  51. $this->assertStringContainsString("Invalid UID", $output);
  52. }
  53. public function testStateNoProvidersActive() {
  54. $user = $this->createMock(IUser::class);
  55. $this->userManager->expects($this->once())
  56. ->method('get')
  57. ->with('eldora')
  58. ->willReturn($user);
  59. $states = [
  60. 'u2f' => false,
  61. 'totp' => false,
  62. ];
  63. $this->registry->expects($this->once())
  64. ->method('getProviderStates')
  65. ->with($user)
  66. ->willReturn($states);
  67. $this->cmd->execute([
  68. 'uid' => 'eldora',
  69. ]);
  70. $output = $this->cmd->getDisplay();
  71. $this->assertStringContainsString("Two-factor authentication is not enabled for user eldora", $output);
  72. }
  73. public function testStateOneProviderActive() {
  74. $user = $this->createMock(IUser::class);
  75. $this->userManager->expects($this->once())
  76. ->method('get')
  77. ->with('mohamed')
  78. ->willReturn($user);
  79. $states = [
  80. 'u2f' => true,
  81. 'totp' => false,
  82. ];
  83. $this->registry->expects($this->once())
  84. ->method('getProviderStates')
  85. ->with($user)
  86. ->willReturn($states);
  87. $this->cmd->execute([
  88. 'uid' => 'mohamed',
  89. ]);
  90. $output = $this->cmd->getDisplay();
  91. $this->assertStringContainsString("Two-factor authentication is enabled for user mohamed", $output);
  92. }
  93. }