1
0

OCSControllerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Core\Controller;
  24. use OC\CapabilitiesManager;
  25. use OC\Security\IdentityProof\Key;
  26. use OC\Security\IdentityProof\Manager;
  27. use OCP\AppFramework\Http\DataResponse;
  28. use OCP\IRequest;
  29. use OCP\IUser;
  30. use OCP\IUserManager;
  31. use OCP\IUserSession;
  32. use Test\TestCase;
  33. class OCSControllerTest extends TestCase {
  34. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  35. private $request;
  36. /** @var CapabilitiesManager|\PHPUnit\Framework\MockObject\MockObject */
  37. private $capabilitiesManager;
  38. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  39. private $userSession;
  40. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  41. private $userManager;
  42. /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */
  43. private $keyManager;
  44. /** @var OCSController */
  45. private $controller;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->request = $this->createMock(IRequest::class);
  49. $this->capabilitiesManager = $this->createMock(CapabilitiesManager::class);
  50. $this->userSession = $this->createMock(IUserSession::class);
  51. $this->userManager = $this->createMock(IUserManager::class);
  52. $this->keyManager = $this->createMock(Manager::class);
  53. $this->controller = new OCSController(
  54. 'core',
  55. $this->request,
  56. $this->capabilitiesManager,
  57. $this->userSession,
  58. $this->userManager,
  59. $this->keyManager
  60. );
  61. }
  62. public function testGetConfig() {
  63. $this->request->method('getServerHost')
  64. ->willReturn('awesomehost.io');
  65. $data = [
  66. 'version' => '1.7',
  67. 'website' => 'Nextcloud',
  68. 'host' => 'awesomehost.io',
  69. 'contact' => '',
  70. 'ssl' => 'false',
  71. ];
  72. $expected = new DataResponse($data);
  73. $this->assertEquals($expected, $this->controller->getConfig());
  74. return new DataResponse($data);
  75. }
  76. public function testGetCapabilities() {
  77. $this->userSession->expects($this->once())
  78. ->method('isLoggedIn')
  79. ->willReturn(true);
  80. [$major, $minor, $micro] = \OCP\Util::getVersion();
  81. $result = [];
  82. $result['version'] = [
  83. 'major' => $major,
  84. 'minor' => $minor,
  85. 'micro' => $micro,
  86. 'string' => \OC_Util::getVersionString(),
  87. 'edition' => '',
  88. 'extendedSupport' => false
  89. ];
  90. $capabilities = [
  91. 'foo' => 'bar',
  92. 'a' => [
  93. 'b' => true,
  94. 'c' => 11,
  95. ]
  96. ];
  97. $this->capabilitiesManager->method('getCapabilities')
  98. ->willReturn($capabilities);
  99. $result['capabilities'] = $capabilities;
  100. $expected = new DataResponse($result);
  101. $expected->setETag(md5(json_encode($result)));
  102. $this->assertEquals($expected, $this->controller->getCapabilities());
  103. }
  104. public function testGetCapabilitiesPublic() {
  105. $this->userSession->expects($this->once())
  106. ->method('isLoggedIn')
  107. ->willReturn(false);
  108. [$major, $minor, $micro] = \OCP\Util::getVersion();
  109. $result = [];
  110. $result['version'] = [
  111. 'major' => $major,
  112. 'minor' => $minor,
  113. 'micro' => $micro,
  114. 'string' => \OC_Util::getVersionString(),
  115. 'edition' => '',
  116. 'extendedSupport' => false
  117. ];
  118. $capabilities = [
  119. 'foo' => 'bar',
  120. 'a' => [
  121. 'b' => true,
  122. 'c' => 11,
  123. ]
  124. ];
  125. $this->capabilitiesManager->method('getCapabilities')
  126. ->with(true)
  127. ->willReturn($capabilities);
  128. $result['capabilities'] = $capabilities;
  129. $expected = new DataResponse($result);
  130. $expected->setETag(md5(json_encode($result)));
  131. $this->assertEquals($expected, $this->controller->getCapabilities());
  132. }
  133. public function testPersonCheckValid() {
  134. $this->userManager->method('checkPassword')
  135. ->with(
  136. $this->equalTo('user'),
  137. $this->equalTo('pass')
  138. )->willReturn($this->createMock(IUser::class));
  139. $expected = new DataResponse([
  140. 'person' => [
  141. 'personid' => 'user'
  142. ]
  143. ]);
  144. $this->assertEquals($expected, $this->controller->personCheck('user', 'pass'));
  145. }
  146. public function testPersonInvalid() {
  147. $this->userManager->method('checkPassword')
  148. ->with(
  149. $this->equalTo('user'),
  150. $this->equalTo('wrongpass')
  151. )->willReturn(false);
  152. $expected = new DataResponse([], 102);
  153. $expected->throttle();
  154. $this->assertEquals($expected, $this->controller->personCheck('user', 'wrongpass'));
  155. }
  156. public function testPersonNoLogin() {
  157. $this->userManager->method('checkPassword')
  158. ->with(
  159. $this->equalTo('user'),
  160. $this->equalTo('wrongpass')
  161. )->willReturn(false);
  162. $expected = new DataResponse([], 101);
  163. $this->assertEquals($expected, $this->controller->personCheck('', ''));
  164. }
  165. public function testGetIdentityProofWithNotExistingUser() {
  166. $this->userManager
  167. ->expects($this->once())
  168. ->method('get')
  169. ->with('NotExistingUser')
  170. ->willReturn(null);
  171. $expected = new DataResponse(['User not found'], 404);
  172. $this->assertEquals($expected, $this->controller->getIdentityProof('NotExistingUser'));
  173. }
  174. public function testGetIdentityProof() {
  175. $user = $this->createMock(IUser::class);
  176. $key = $this->createMock(Key::class);
  177. $this->userManager
  178. ->expects($this->once())
  179. ->method('get')
  180. ->with('ExistingUser')
  181. ->willReturn($user);
  182. $this->keyManager
  183. ->expects($this->once())
  184. ->method('getKey')
  185. ->with($user)
  186. ->willReturn($key);
  187. $key
  188. ->expects($this->once())
  189. ->method('getPublic')
  190. ->willReturn('Existing Users public key');
  191. $expected = new DataResponse([
  192. 'public' => 'Existing Users public key',
  193. ]);
  194. $this->assertEquals($expected, $this->controller->getIdentityProof('ExistingUser'));
  195. }
  196. }