SettingsControllerTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\TwoFactorBackupCodes\Tests\Unit\Controller;
  8. use OCA\TwoFactorBackupCodes\Controller\SettingsController;
  9. use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage;
  10. use OCP\AppFramework\Http\JSONResponse;
  11. use OCP\IRequest;
  12. use OCP\IUser;
  13. use OCP\IUserSession;
  14. use Test\TestCase;
  15. class SettingsControllerTest extends TestCase {
  16. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  17. private $request;
  18. /** @var BackupCodeStorage|\PHPUnit\Framework\MockObject\MockObject */
  19. private $storage;
  20. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  21. private $userSession;
  22. /** @var SettingsController */
  23. private $controller;
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  27. $this->storage = $this->getMockBuilder(BackupCodeStorage::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $this->userSession = $this->getMockBuilder(IUserSession::class)->getMock();
  31. $this->controller = new SettingsController('twofactor_backupcodes', $this->request, $this->storage, $this->userSession);
  32. }
  33. public function testCreateCodes(): void {
  34. $user = $this->getMockBuilder(IUser::class)->getMock();
  35. $codes = ['a', 'b'];
  36. $this->userSession->expects($this->once())
  37. ->method('getUser')
  38. ->willReturn($user);
  39. $this->storage->expects($this->once())
  40. ->method('createCodes')
  41. ->with($user)
  42. ->willReturn($codes);
  43. $this->storage->expects($this->once())
  44. ->method('getBackupCodesState')
  45. ->with($user)
  46. ->willReturn(['state']);
  47. $expected = [
  48. 'codes' => $codes,
  49. 'state' => ['state'],
  50. ];
  51. $response = $this->controller->createCodes();
  52. $this->assertInstanceOf(JSONResponse::class, $response);
  53. $this->assertEquals($expected, $response->getData());
  54. }
  55. }