SettingsControllerTest.php 2.9 KB

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