AuthtokensTest.php 3.5 KB

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