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