AjaxControllerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files_External\Tests\Controller;
  7. use OCA\Files_External\Controller\AjaxController;
  8. use OCA\Files_External\Lib\Auth\Password\GlobalAuth;
  9. use OCA\Files_External\Lib\Auth\PublicKey\RSA;
  10. use OCP\AppFramework\Http\JSONResponse;
  11. use OCP\IGroupManager;
  12. use OCP\IRequest;
  13. use OCP\IUser;
  14. use OCP\IUserSession;
  15. use Test\TestCase;
  16. class AjaxControllerTest extends TestCase {
  17. /** @var IRequest */
  18. private $request;
  19. /** @var RSA */
  20. private $rsa;
  21. /** @var GlobalAuth */
  22. private $globalAuth;
  23. /** @var IUserSession */
  24. private $userSession;
  25. /** @var IGroupManager */
  26. private $groupManager;
  27. /** @var AjaxController */
  28. private $ajaxController;
  29. protected function setUp(): void {
  30. $this->request = $this->createMock(IRequest::class);
  31. $this->rsa = $this->getMockBuilder('\\OCA\\Files_External\\Lib\\Auth\\PublicKey\\RSA')
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $this->globalAuth = $this->getMockBuilder('\\OCA\\Files_External\\Lib\\Auth\\Password\GlobalAuth')
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->userSession = $this->createMock(IUserSession::class);
  38. $this->groupManager = $this->createMock(IGroupManager::class);
  39. $this->ajaxController = new AjaxController(
  40. 'files_external',
  41. $this->request,
  42. $this->rsa,
  43. $this->globalAuth,
  44. $this->userSession,
  45. $this->groupManager
  46. );
  47. parent::setUp();
  48. }
  49. public function testGetSshKeys(): void {
  50. $this->rsa
  51. ->expects($this->once())
  52. ->method('createKey')
  53. ->willReturn([
  54. 'privatekey' => 'MyPrivateKey',
  55. 'publickey' => 'MyPublicKey',
  56. ]);
  57. $expected = new JSONResponse(
  58. [
  59. 'data' => [
  60. 'private_key' => 'MyPrivateKey',
  61. 'public_key' => 'MyPublicKey',
  62. ],
  63. 'status' => 'success',
  64. ]
  65. );
  66. $this->assertEquals($expected, $this->ajaxController->getSshKeys());
  67. }
  68. public function testSaveGlobalCredentialsAsAdminForAnotherUser(): void {
  69. $user = $this->createMock(IUser::class);
  70. $user
  71. ->expects($this->once())
  72. ->method('getUID')
  73. ->willReturn('MyAdminUid');
  74. $this->userSession
  75. ->expects($this->once())
  76. ->method('getUser')
  77. ->willReturn($user);
  78. $this->globalAuth
  79. ->expects($this->never())
  80. ->method('saveAuth');
  81. $this->assertSame(false, $this->ajaxController->saveGlobalCredentials('UidOfTestUser', 'test', 'password'));
  82. }
  83. public function testSaveGlobalCredentialsAsAdminForSelf(): void {
  84. $user = $this->createMock(IUser::class);
  85. $user
  86. ->expects($this->once())
  87. ->method('getUID')
  88. ->willReturn('MyAdminUid');
  89. $this->userSession
  90. ->expects($this->once())
  91. ->method('getUser')
  92. ->willReturn($user);
  93. $this->globalAuth
  94. ->expects($this->once())
  95. ->method('saveAuth')
  96. ->with('MyAdminUid', 'test', 'password');
  97. $this->assertSame(true, $this->ajaxController->saveGlobalCredentials('MyAdminUid', 'test', 'password'));
  98. }
  99. public function testSaveGlobalCredentialsAsNormalUserForSelf(): void {
  100. $user = $this->createMock(IUser::class);
  101. $user
  102. ->method('getUID')
  103. ->willReturn('MyUserUid');
  104. $this->userSession
  105. ->method('getUser')
  106. ->willReturn($user);
  107. $this->globalAuth
  108. ->method('saveAuth')
  109. ->with('MyUserUid', 'test', 'password');
  110. $this->assertSame(true, $this->ajaxController->saveGlobalCredentials('MyUserUid', 'test', 'password'));
  111. }
  112. public function testSaveGlobalCredentialsAsNormalUserForAnotherUser(): void {
  113. $user = $this->createMock(IUser::class);
  114. $user
  115. ->method('getUID')
  116. ->willReturn('MyUserUid');
  117. $this->userSession
  118. ->method('getUser')
  119. ->willReturn($user);
  120. $this->globalAuth
  121. ->expects($this->never())
  122. ->method('saveAuth');
  123. $this->assertSame(false, $this->ajaxController->saveGlobalCredentials('AnotherUserUid', 'test', 'password'));
  124. }
  125. }