1
0

AjaxControllerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  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 OCA\Files_External\Tests\Controller;
  26. use OCA\Files_External\Controller\AjaxController;
  27. use OCA\Files_External\Lib\Auth\Password\GlobalAuth;
  28. use OCA\Files_External\Lib\Auth\PublicKey\RSA;
  29. use OCP\AppFramework\Http\JSONResponse;
  30. use OCP\IGroupManager;
  31. use OCP\IRequest;
  32. use OCP\IUser;
  33. use OCP\IUserSession;
  34. use Test\TestCase;
  35. class AjaxControllerTest extends TestCase {
  36. /** @var IRequest */
  37. private $request;
  38. /** @var RSA */
  39. private $rsa;
  40. /** @var GlobalAuth */
  41. private $globalAuth;
  42. /** @var IUserSession */
  43. private $userSession;
  44. /** @var IGroupManager */
  45. private $groupManager;
  46. /** @var AjaxController */
  47. private $ajaxController;
  48. protected function setUp(): void {
  49. $this->request = $this->createMock(IRequest::class);
  50. $this->rsa = $this->getMockBuilder('\\OCA\\Files_External\\Lib\\Auth\\PublicKey\\RSA')
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->globalAuth = $this->getMockBuilder('\\OCA\\Files_External\\Lib\\Auth\\Password\GlobalAuth')
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->userSession = $this->createMock(IUserSession::class);
  57. $this->groupManager = $this->createMock(IGroupManager::class);
  58. $this->ajaxController = new AjaxController(
  59. 'files_external',
  60. $this->request,
  61. $this->rsa,
  62. $this->globalAuth,
  63. $this->userSession,
  64. $this->groupManager
  65. );
  66. parent::setUp();
  67. }
  68. public function testGetSshKeys() {
  69. $this->rsa
  70. ->expects($this->once())
  71. ->method('createKey')
  72. ->willReturn([
  73. 'privatekey' => 'MyPrivateKey',
  74. 'publickey' => 'MyPublicKey',
  75. ]);
  76. $expected = new JSONResponse(
  77. [
  78. 'data' => [
  79. 'private_key' => 'MyPrivateKey',
  80. 'public_key' => 'MyPublicKey',
  81. ],
  82. 'status' => 'success',
  83. ]
  84. );
  85. $this->assertEquals($expected, $this->ajaxController->getSshKeys());
  86. }
  87. public function testSaveGlobalCredentialsAsAdminForAnotherUser() {
  88. $user = $this->createMock(IUser::class);
  89. $user
  90. ->expects($this->once())
  91. ->method('getUID')
  92. ->willReturn('MyAdminUid');
  93. $this->userSession
  94. ->expects($this->once())
  95. ->method('getUser')
  96. ->willReturn($user);
  97. $this->globalAuth
  98. ->expects($this->never())
  99. ->method('saveAuth');
  100. $this->assertSame(false, $this->ajaxController->saveGlobalCredentials('UidOfTestUser', 'test', 'password'));
  101. }
  102. public function testSaveGlobalCredentialsAsAdminForSelf() {
  103. $user = $this->createMock(IUser::class);
  104. $user
  105. ->expects($this->once())
  106. ->method('getUID')
  107. ->willReturn('MyAdminUid');
  108. $this->userSession
  109. ->expects($this->once())
  110. ->method('getUser')
  111. ->willReturn($user);
  112. $this->globalAuth
  113. ->expects($this->once())
  114. ->method('saveAuth')
  115. ->with('MyAdminUid', 'test', 'password');
  116. $this->assertSame(true, $this->ajaxController->saveGlobalCredentials('MyAdminUid', 'test', 'password'));
  117. }
  118. public function testSaveGlobalCredentialsAsNormalUserForSelf() {
  119. $user = $this->createMock(IUser::class);
  120. $user
  121. ->method('getUID')
  122. ->willReturn('MyUserUid');
  123. $this->userSession
  124. ->method('getUser')
  125. ->willReturn($user);
  126. $this->globalAuth
  127. ->method('saveAuth')
  128. ->with('MyUserUid', 'test', 'password');
  129. $this->assertSame(true, $this->ajaxController->saveGlobalCredentials('MyUserUid', 'test', 'password'));
  130. }
  131. public function testSaveGlobalCredentialsAsNormalUserForAnotherUser() {
  132. $user = $this->createMock(IUser::class);
  133. $user
  134. ->method('getUID')
  135. ->willReturn('MyUserUid');
  136. $this->userSession
  137. ->method('getUser')
  138. ->willReturn($user);
  139. $this->globalAuth
  140. ->expects($this->never())
  141. ->method('saveAuth');
  142. $this->assertSame(false, $this->ajaxController->saveGlobalCredentials('AnotherUserUid', 'test', 'password'));
  143. }
  144. }