OCSControllerTest.php 5.8 KB

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