OCSControllerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use OC\CapabilitiesManager;
  9. use OC\Security\IdentityProof\Key;
  10. use OC\Security\IdentityProof\Manager;
  11. use OCP\AppFramework\Http\DataResponse;
  12. use OCP\IRequest;
  13. use OCP\IUser;
  14. use OCP\IUserManager;
  15. use OCP\IUserSession;
  16. use Test\TestCase;
  17. class OCSControllerTest extends TestCase {
  18. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  19. private $request;
  20. /** @var CapabilitiesManager|\PHPUnit\Framework\MockObject\MockObject */
  21. private $capabilitiesManager;
  22. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  23. private $userSession;
  24. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  25. private $userManager;
  26. /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */
  27. private $keyManager;
  28. /** @var OCSController */
  29. private $controller;
  30. protected function setUp(): void {
  31. parent::setUp();
  32. $this->request = $this->createMock(IRequest::class);
  33. $this->capabilitiesManager = $this->createMock(CapabilitiesManager::class);
  34. $this->userSession = $this->createMock(IUserSession::class);
  35. $this->userManager = $this->createMock(IUserManager::class);
  36. $this->keyManager = $this->createMock(Manager::class);
  37. $this->controller = new OCSController(
  38. 'core',
  39. $this->request,
  40. $this->capabilitiesManager,
  41. $this->userSession,
  42. $this->userManager,
  43. $this->keyManager
  44. );
  45. }
  46. public function testGetConfig() {
  47. $this->request->method('getServerHost')
  48. ->willReturn('awesomehost.io');
  49. $data = [
  50. 'version' => '1.7',
  51. 'website' => 'Nextcloud',
  52. 'host' => 'awesomehost.io',
  53. 'contact' => '',
  54. 'ssl' => 'false',
  55. ];
  56. $expected = new DataResponse($data);
  57. $this->assertEquals($expected, $this->controller->getConfig());
  58. return new DataResponse($data);
  59. }
  60. public function testGetCapabilities() {
  61. $this->userSession->expects($this->once())
  62. ->method('isLoggedIn')
  63. ->willReturn(true);
  64. [$major, $minor, $micro] = \OCP\Util::getVersion();
  65. $result = [];
  66. $result['version'] = [
  67. 'major' => $major,
  68. 'minor' => $minor,
  69. 'micro' => $micro,
  70. 'string' => \OC_Util::getVersionString(),
  71. 'edition' => '',
  72. 'extendedSupport' => false
  73. ];
  74. $capabilities = [
  75. 'foo' => 'bar',
  76. 'a' => [
  77. 'b' => true,
  78. 'c' => 11,
  79. ]
  80. ];
  81. $this->capabilitiesManager->method('getCapabilities')
  82. ->willReturn($capabilities);
  83. $result['capabilities'] = $capabilities;
  84. $expected = new DataResponse($result);
  85. $expected->setETag(md5(json_encode($result)));
  86. $this->assertEquals($expected, $this->controller->getCapabilities());
  87. }
  88. public function testGetCapabilitiesPublic() {
  89. $this->userSession->expects($this->once())
  90. ->method('isLoggedIn')
  91. ->willReturn(false);
  92. [$major, $minor, $micro] = \OCP\Util::getVersion();
  93. $result = [];
  94. $result['version'] = [
  95. 'major' => $major,
  96. 'minor' => $minor,
  97. 'micro' => $micro,
  98. 'string' => \OC_Util::getVersionString(),
  99. 'edition' => '',
  100. 'extendedSupport' => false
  101. ];
  102. $capabilities = [
  103. 'foo' => 'bar',
  104. 'a' => [
  105. 'b' => true,
  106. 'c' => 11,
  107. ]
  108. ];
  109. $this->capabilitiesManager->method('getCapabilities')
  110. ->with(true)
  111. ->willReturn($capabilities);
  112. $result['capabilities'] = $capabilities;
  113. $expected = new DataResponse($result);
  114. $expected->setETag(md5(json_encode($result)));
  115. $this->assertEquals($expected, $this->controller->getCapabilities());
  116. }
  117. public function testPersonCheckValid() {
  118. $this->userManager->method('checkPassword')
  119. ->with(
  120. $this->equalTo('user'),
  121. $this->equalTo('pass')
  122. )->willReturn($this->createMock(IUser::class));
  123. $expected = new DataResponse([
  124. 'person' => [
  125. 'personid' => 'user'
  126. ]
  127. ]);
  128. $this->assertEquals($expected, $this->controller->personCheck('user', 'pass'));
  129. }
  130. public function testPersonInvalid() {
  131. $this->userManager->method('checkPassword')
  132. ->with(
  133. $this->equalTo('user'),
  134. $this->equalTo('wrongpass')
  135. )->willReturn(false);
  136. $expected = new DataResponse([], 102);
  137. $expected->throttle();
  138. $this->assertEquals($expected, $this->controller->personCheck('user', 'wrongpass'));
  139. }
  140. public function testPersonNoLogin() {
  141. $this->userManager->method('checkPassword')
  142. ->with(
  143. $this->equalTo('user'),
  144. $this->equalTo('wrongpass')
  145. )->willReturn(false);
  146. $expected = new DataResponse([], 101);
  147. $this->assertEquals($expected, $this->controller->personCheck('', ''));
  148. }
  149. public function testGetIdentityProofWithNotExistingUser() {
  150. $this->userManager
  151. ->expects($this->once())
  152. ->method('get')
  153. ->with('NotExistingUser')
  154. ->willReturn(null);
  155. $expected = new DataResponse(['Account not found'], 404);
  156. $this->assertEquals($expected, $this->controller->getIdentityProof('NotExistingUser'));
  157. }
  158. public function testGetIdentityProof() {
  159. $user = $this->createMock(IUser::class);
  160. $key = $this->createMock(Key::class);
  161. $this->userManager
  162. ->expects($this->once())
  163. ->method('get')
  164. ->with('ExistingUser')
  165. ->willReturn($user);
  166. $this->keyManager
  167. ->expects($this->once())
  168. ->method('getKey')
  169. ->with($user)
  170. ->willReturn($key);
  171. $key
  172. ->expects($this->once())
  173. ->method('getPublic')
  174. ->willReturn('Existing Users public key');
  175. $expected = new DataResponse([
  176. 'public' => 'Existing Users public key',
  177. ]);
  178. $this->assertEquals($expected, $this->controller->getIdentityProof('ExistingUser'));
  179. }
  180. }