AjaxControllerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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->groupManager
  98. ->expects($this->once())
  99. ->method('isAdmin')
  100. ->with('MyAdminUid')
  101. ->willReturn(true);
  102. $this->globalAuth
  103. ->expects($this->once())
  104. ->method('saveAuth')
  105. ->with('UidOfTestUser', 'test', 'password');
  106. $this->assertSame(true, $this->ajaxController->saveGlobalCredentials('UidOfTestUser', 'test', 'password'));
  107. }
  108. public function testSaveGlobalCredentialsAsAdminForSelf() {
  109. $user = $this->createMock(IUser::class);
  110. $user
  111. ->expects($this->once())
  112. ->method('getUID')
  113. ->willReturn('MyAdminUid');
  114. $this->userSession
  115. ->expects($this->once())
  116. ->method('getUser')
  117. ->willReturn($user);
  118. $this->groupManager
  119. ->expects($this->once())
  120. ->method('isAdmin')
  121. ->with('MyAdminUid')
  122. ->willReturn(true);
  123. $this->globalAuth
  124. ->expects($this->once())
  125. ->method('saveAuth')
  126. ->with('MyAdminUid', 'test', 'password');
  127. $this->assertSame(true, $this->ajaxController->saveGlobalCredentials('MyAdminUid', 'test', 'password'));
  128. }
  129. public function testSaveGlobalCredentialsAsNormalUserForSelf() {
  130. $user = $this->createMock(IUser::class);
  131. $user
  132. ->expects($this->exactly(2))
  133. ->method('getUID')
  134. ->willReturn('MyUserUid');
  135. $this->userSession
  136. ->expects($this->once())
  137. ->method('getUser')
  138. ->willReturn($user);
  139. $this->groupManager
  140. ->expects($this->once())
  141. ->method('isAdmin')
  142. ->with('MyUserUid')
  143. ->willReturn(false);
  144. $this->globalAuth
  145. ->expects($this->once())
  146. ->method('saveAuth')
  147. ->with('MyUserUid', 'test', 'password');
  148. $this->assertSame(true, $this->ajaxController->saveGlobalCredentials('MyUserUid', 'test', 'password'));
  149. }
  150. public function testSaveGlobalCredentialsAsNormalUserForAnotherUser() {
  151. $user = $this->createMock(IUser::class);
  152. $user
  153. ->expects($this->exactly(2))
  154. ->method('getUID')
  155. ->willReturn('MyUserUid');
  156. $this->userSession
  157. ->expects($this->once())
  158. ->method('getUser')
  159. ->willReturn($user);
  160. $this->groupManager
  161. ->expects($this->once())
  162. ->method('isAdmin')
  163. ->with('MyUserUid')
  164. ->willReturn(false);
  165. $this->assertSame(false, $this->ajaxController->saveGlobalCredentials('AnotherUserUid', 'test', 'password'));
  166. }
  167. }