AuthtokensTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace Test\Settings\Personal\Security;
  26. use OC\Authentication\Token\DefaultToken;
  27. use OC\Authentication\Token\IProvider as IAuthTokenProvider;
  28. use OCA\Settings\Personal\Security;
  29. use OCA\Settings\Personal\Security\Authtokens;
  30. use OCP\AppFramework\Http\TemplateResponse;
  31. use OCP\IInitialStateService;
  32. use OCP\ISession;
  33. use PHPUnit\Framework\MockObject\MockObject;
  34. use Test\TestCase;
  35. class AuthtokensTest extends TestCase {
  36. /** @var IAuthTokenProvider|MockObject */
  37. private $authTokenProvider;
  38. /** @var ISession|MockObject */
  39. private $session;
  40. /** @var IInitialStateService|MockObject */
  41. private $initialStateService;
  42. /** @var string */
  43. private $uid;
  44. /** @var Security\Authtokens */
  45. private $section;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->authTokenProvider = $this->createMock(IAuthTokenProvider::class);
  49. $this->session = $this->createMock(ISession::class);
  50. $this->initialStateService = $this->createMock(IInitialStateService::class);
  51. $this->uid = 'test123';
  52. $this->section = new Authtokens(
  53. $this->authTokenProvider,
  54. $this->session,
  55. $this->initialStateService,
  56. $this->uid
  57. );
  58. }
  59. public function testGetForm() {
  60. $token1 = new DefaultToken();
  61. $token1->setId(100);
  62. $token2 = new DefaultToken();
  63. $token2->setId(200);
  64. $tokens = [
  65. $token1,
  66. $token2,
  67. ];
  68. $sessionToken = new DefaultToken();
  69. $sessionToken->setId(100);
  70. $this->authTokenProvider->expects($this->once())
  71. ->method('getTokenByUser')
  72. ->with($this->uid)
  73. ->willReturn($tokens);
  74. $this->session->expects($this->once())
  75. ->method('getId')
  76. ->willReturn('session123');
  77. $this->authTokenProvider->expects($this->once())
  78. ->method('getToken')
  79. ->with('session123')
  80. ->willReturn($sessionToken);
  81. $this->initialStateService->expects($this->once())
  82. ->method('provideInitialState')
  83. ->with('settings', 'app_tokens', [
  84. [
  85. 'id' => 100,
  86. 'name' => null,
  87. 'lastActivity' => 0,
  88. 'type' => 0,
  89. 'canDelete' => false,
  90. 'current' => true,
  91. 'scope' => ['filesystem' => true],
  92. 'canRename' => false,
  93. ],
  94. [
  95. 'id' => 200,
  96. 'name' => null,
  97. 'lastActivity' => 0,
  98. 'type' => 0,
  99. 'canDelete' => true,
  100. 'scope' => ['filesystem' => true],
  101. 'canRename' => true,
  102. ],
  103. ]);
  104. $form = $this->section->getForm();
  105. $expected = new TemplateResponse('settings', 'settings/personal/security/authtokens');
  106. $this->assertEquals($expected, $form);
  107. }
  108. }