AuthtokensTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Tests\Settings\Personal\Security;
  8. use OC\Authentication\Token\IProvider as IAuthTokenProvider;
  9. use OC\Authentication\Token\PublicKeyToken;
  10. use OCA\Settings\Settings\Personal\Security\Authtokens;
  11. use OCP\AppFramework\Http\TemplateResponse;
  12. use OCP\AppFramework\Services\IInitialState;
  13. use OCP\Authentication\Token\IToken;
  14. use OCP\ISession;
  15. use OCP\IUserSession;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use Test\TestCase;
  18. class AuthtokensTest extends TestCase {
  19. /** @var IAuthTokenProvider|MockObject */
  20. private $authTokenProvider;
  21. /** @var ISession|MockObject */
  22. private $session;
  23. /** @var IUserSession|MockObject */
  24. private $userSession;
  25. /** @var IInitialState|MockObject */
  26. private $initialState;
  27. /** @var string */
  28. private $uid;
  29. /** @var Authtokens */
  30. private $section;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $this->authTokenProvider = $this->createMock(IAuthTokenProvider::class);
  34. $this->session = $this->createMock(ISession::class);
  35. $this->userSession = $this->createMock(IUserSession::class);
  36. $this->initialState = $this->createMock(IInitialState::class);
  37. $this->uid = 'test123';
  38. $this->section = new Authtokens(
  39. $this->authTokenProvider,
  40. $this->session,
  41. $this->userSession,
  42. $this->initialState,
  43. $this->uid
  44. );
  45. }
  46. public function testGetForm() {
  47. $token1 = new PublicKeyToken();
  48. $token1->setId(100);
  49. $token2 = new PublicKeyToken();
  50. $token2->setId(200);
  51. $tokens = [
  52. $token1,
  53. $token2,
  54. ];
  55. $sessionToken = new PublicKeyToken();
  56. $sessionToken->setId(100);
  57. $this->authTokenProvider->expects($this->once())
  58. ->method('getTokenByUser')
  59. ->with($this->uid)
  60. ->willReturn($tokens);
  61. $this->session->expects($this->once())
  62. ->method('getId')
  63. ->willReturn('session123');
  64. $this->authTokenProvider->expects($this->once())
  65. ->method('getToken')
  66. ->with('session123')
  67. ->willReturn($sessionToken);
  68. $this->initialState->expects($this->exactly(2))
  69. ->method('provideInitialState')
  70. ->withConsecutive(
  71. [
  72. 'app_tokens', [
  73. [
  74. 'id' => 100,
  75. 'name' => null,
  76. 'lastActivity' => 0,
  77. 'type' => 0,
  78. 'canDelete' => false,
  79. 'current' => true,
  80. 'scope' => [IToken::SCOPE_FILESYSTEM => true],
  81. 'canRename' => false,
  82. ],
  83. [
  84. 'id' => 200,
  85. 'name' => null,
  86. 'lastActivity' => 0,
  87. 'type' => 0,
  88. 'canDelete' => true,
  89. 'scope' => [IToken::SCOPE_FILESYSTEM => true],
  90. 'canRename' => true,
  91. ],
  92. ]
  93. ],
  94. ['can_create_app_token', true],
  95. );
  96. $form = $this->section->getForm();
  97. $expected = new TemplateResponse('settings', 'settings/personal/security/authtokens');
  98. $this->assertEquals($expected, $form);
  99. }
  100. }