2016-11-16 21:49:04 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-11-16 21:49:04 +03:00
|
|
|
namespace OC\Core\Controller;
|
|
|
|
|
|
|
|
use OC\CapabilitiesManager;
|
2016-11-18 13:55:37 +03:00
|
|
|
use OC\Security\IdentityProof\Key;
|
|
|
|
use OC\Security\IdentityProof\Manager;
|
2016-11-16 21:49:04 +03:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class OCSControllerTest extends TestCase {
|
|
|
|
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $request;
|
|
|
|
/** @var CapabilitiesManager|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $capabilitiesManager;
|
|
|
|
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $userSession;
|
|
|
|
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $userManager;
|
2016-11-18 13:55:37 +03:00
|
|
|
/** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $keyManager;
|
2016-11-16 21:49:04 +03:00
|
|
|
/** @var OCSController */
|
|
|
|
private $controller;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-11-16 21:49:04 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->request = $this->createMock(IRequest::class);
|
|
|
|
$this->capabilitiesManager = $this->createMock(CapabilitiesManager::class);
|
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
|
|
|
$this->userManager = $this->createMock(IUserManager::class);
|
2016-11-18 13:55:37 +03:00
|
|
|
$this->keyManager = $this->createMock(Manager::class);
|
2016-11-16 21:49:04 +03:00
|
|
|
|
|
|
|
$this->controller = new OCSController(
|
|
|
|
'core',
|
|
|
|
$this->request,
|
|
|
|
$this->capabilitiesManager,
|
|
|
|
$this->userSession,
|
|
|
|
$this->userManager,
|
2016-11-18 13:55:37 +03:00
|
|
|
$this->keyManager
|
2016-11-16 21:49:04 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetConfig() {
|
|
|
|
$this->request->method('getServerHost')
|
|
|
|
->willReturn('awesomehost.io');
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'version' => '1.7',
|
|
|
|
'website' => 'Nextcloud',
|
|
|
|
'host' => 'awesomehost.io',
|
|
|
|
'contact' => '',
|
|
|
|
'ssl' => 'false',
|
|
|
|
];
|
|
|
|
|
|
|
|
$expected = new DataResponse($data);
|
|
|
|
$this->assertEquals($expected, $this->controller->getConfig());
|
|
|
|
|
|
|
|
return new DataResponse($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetCapabilities() {
|
2017-06-30 12:21:15 +03:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(true);
|
2016-11-16 21:49:04 +03:00
|
|
|
list($major, $minor, $micro) = \OCP\Util::getVersion();
|
|
|
|
|
|
|
|
$result = [];
|
2020-03-26 11:30:18 +03:00
|
|
|
$result['version'] = [
|
2016-11-16 21:49:04 +03:00
|
|
|
'major' => $major,
|
|
|
|
'minor' => $minor,
|
|
|
|
'micro' => $micro,
|
|
|
|
'string' => \OC_Util::getVersionString(),
|
|
|
|
'edition' => '',
|
2019-06-13 21:23:29 +03:00
|
|
|
'extendedSupport' => false
|
2020-03-26 11:30:18 +03:00
|
|
|
];
|
2016-11-16 21:49:04 +03:00
|
|
|
|
|
|
|
$capabilities = [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'a' => [
|
|
|
|
'b' => true,
|
|
|
|
'c' => 11,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->capabilitiesManager->method('getCapabilities')
|
|
|
|
->willReturn($capabilities);
|
|
|
|
|
|
|
|
$result['capabilities'] = $capabilities;
|
|
|
|
|
2017-06-30 12:21:15 +03:00
|
|
|
$expected = new DataResponse($result);
|
|
|
|
$this->assertEquals($expected, $this->controller->getCapabilities());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetCapabilitiesPublic() {
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(false);
|
|
|
|
list($major, $minor, $micro) = \OCP\Util::getVersion();
|
|
|
|
|
|
|
|
$result = [];
|
2020-03-26 11:30:18 +03:00
|
|
|
$result['version'] = [
|
2017-06-30 12:21:15 +03:00
|
|
|
'major' => $major,
|
|
|
|
'minor' => $minor,
|
|
|
|
'micro' => $micro,
|
|
|
|
'string' => \OC_Util::getVersionString(),
|
|
|
|
'edition' => '',
|
2019-06-13 21:23:29 +03:00
|
|
|
'extendedSupport' => false
|
2020-03-26 11:30:18 +03:00
|
|
|
];
|
2017-06-30 12:21:15 +03:00
|
|
|
|
|
|
|
$capabilities = [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'a' => [
|
|
|
|
'b' => true,
|
|
|
|
'c' => 11,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
$this->capabilitiesManager->method('getCapabilities')
|
|
|
|
->with(true)
|
|
|
|
->willReturn($capabilities);
|
|
|
|
|
|
|
|
$result['capabilities'] = $capabilities;
|
|
|
|
|
2016-11-16 21:49:04 +03:00
|
|
|
$expected = new DataResponse($result);
|
|
|
|
$this->assertEquals($expected, $this->controller->getCapabilities());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPersonCheckValid() {
|
|
|
|
$this->userManager->method('checkPassword')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('user'),
|
|
|
|
$this->equalTo('pass')
|
|
|
|
)->willReturn($this->createMock(IUser::class));
|
|
|
|
|
|
|
|
$expected = new DataResponse([
|
|
|
|
'person' => [
|
|
|
|
'personid' => 'user'
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
$this->assertEquals($expected, $this->controller->personCheck('user', 'pass'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPersonInvalid() {
|
|
|
|
$this->userManager->method('checkPassword')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('user'),
|
|
|
|
$this->equalTo('wrongpass')
|
|
|
|
)->willReturn(false);
|
|
|
|
|
2017-07-25 16:12:49 +03:00
|
|
|
$expected = new DataResponse([], 102);
|
2017-04-14 14:42:40 +03:00
|
|
|
$expected->throttle();
|
2016-11-16 21:49:04 +03:00
|
|
|
$this->assertEquals($expected, $this->controller->personCheck('user', 'wrongpass'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPersonNoLogin() {
|
|
|
|
$this->userManager->method('checkPassword')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('user'),
|
|
|
|
$this->equalTo('wrongpass')
|
|
|
|
)->willReturn(false);
|
|
|
|
|
2017-07-25 16:12:49 +03:00
|
|
|
$expected = new DataResponse([], 101);
|
2016-11-16 21:49:04 +03:00
|
|
|
$this->assertEquals($expected, $this->controller->personCheck('', ''));
|
|
|
|
}
|
2016-11-18 13:55:37 +03:00
|
|
|
|
|
|
|
public function testGetIdentityProofWithNotExistingUser() {
|
|
|
|
$this->userManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('NotExistingUser')
|
|
|
|
->willReturn(null);
|
|
|
|
|
2017-07-25 16:12:49 +03:00
|
|
|
$expected = new DataResponse(['User not found'], 404);
|
2016-11-18 13:55:37 +03:00
|
|
|
$this->assertEquals($expected, $this->controller->getIdentityProof('NotExistingUser'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetIdentityProof() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$key = $this->createMock(Key::class);
|
|
|
|
$this->userManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('ExistingUser')
|
|
|
|
->willReturn($user);
|
|
|
|
$this->keyManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getKey')
|
|
|
|
->with($user)
|
|
|
|
->willReturn($key);
|
|
|
|
$key
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getPublic')
|
|
|
|
->willReturn('Existing Users public key');
|
|
|
|
|
|
|
|
$expected = new DataResponse([
|
|
|
|
'public' => 'Existing Users public key',
|
|
|
|
]);
|
|
|
|
$this->assertEquals($expected, $this->controller->getIdentityProof('ExistingUser'));
|
|
|
|
}
|
2016-11-16 21:49:04 +03:00
|
|
|
}
|