AjaxControllerTest.php 5.1 KB

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