TwoFactorSettingsControllerTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 OCA\Settings\Tests\Controller;
  25. use OC\Authentication\TwoFactorAuth\EnforcementState;
  26. use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
  27. use OCA\Settings\Controller\TwoFactorSettingsController;
  28. use OCP\AppFramework\Http\JSONResponse;
  29. use OCP\IRequest;
  30. use PHPUnit\Framework\MockObject\MockObject;
  31. use Test\TestCase;
  32. class TwoFactorSettingsControllerTest extends TestCase {
  33. /** @var IRequest|MockObject */
  34. private $request;
  35. /** @var MandatoryTwoFactor|MockObject */
  36. private $mandatoryTwoFactor;
  37. /** @var TwoFactorSettingsController */
  38. private $controller;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->request = $this->createMock(IRequest::class);
  42. $this->mandatoryTwoFactor = $this->createMock(MandatoryTwoFactor::class);
  43. $this->controller = new TwoFactorSettingsController(
  44. 'settings',
  45. $this->request,
  46. $this->mandatoryTwoFactor
  47. );
  48. }
  49. public function testIndex() {
  50. $state = new EnforcementState(true);
  51. $this->mandatoryTwoFactor->expects($this->once())
  52. ->method('getState')
  53. ->willReturn($state);
  54. $expected = new JSONResponse($state);
  55. $resp = $this->controller->index();
  56. $this->assertEquals($expected, $resp);
  57. }
  58. public function testUpdate() {
  59. $state = new EnforcementState(true);
  60. $this->mandatoryTwoFactor->expects($this->once())
  61. ->method('setState')
  62. ->with($this->equalTo(new EnforcementState(true)));
  63. $this->mandatoryTwoFactor->expects($this->once())
  64. ->method('getState')
  65. ->willReturn($state);
  66. $expected = new JSONResponse($state);
  67. $resp = $this->controller->update(true);
  68. $this->assertEquals($expected, $resp);
  69. }
  70. }