SettingsControllerTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\TwoFactorBackupCodes\Tests\Unit\Controller;
  28. use OCA\TwoFactorBackupCodes\Controller\SettingsController;
  29. use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage;
  30. use OCP\AppFramework\Http\JSONResponse;
  31. use OCP\IRequest;
  32. use OCP\IUser;
  33. use OCP\IUserSession;
  34. use Test\TestCase;
  35. class SettingsControllerTest extends TestCase {
  36. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  37. private $request;
  38. /** @var BackupCodeStorage|\PHPUnit\Framework\MockObject\MockObject */
  39. private $storage;
  40. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  41. private $userSession;
  42. /** @var SettingsController */
  43. private $controller;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  47. $this->storage = $this->getMockBuilder(BackupCodeStorage::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->userSession = $this->getMockBuilder(IUserSession::class)->getMock();
  51. $this->controller = new SettingsController('twofactor_backupcodes', $this->request, $this->storage, $this->userSession);
  52. }
  53. public function testCreateCodes() {
  54. $user = $this->getMockBuilder(IUser::class)->getMock();
  55. $codes = ['a', 'b'];
  56. $this->userSession->expects($this->once())
  57. ->method('getUser')
  58. ->willReturn($user);
  59. $this->storage->expects($this->once())
  60. ->method('createCodes')
  61. ->with($user)
  62. ->willReturn($codes);
  63. $this->storage->expects($this->once())
  64. ->method('getBackupCodesState')
  65. ->with($user)
  66. ->willReturn(['state']);
  67. $expected = [
  68. 'codes' => $codes,
  69. 'state' => ['state'],
  70. ];
  71. $response = $this->controller->createCodes();
  72. $this->assertInstanceOf(JSONResponse::class, $response);
  73. $this->assertEquals($expected, $response->getData());
  74. }
  75. }