OCSControllerTest.php 6.0 KB

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