2018-04-26 11:34:57 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-04-26 11:34:57 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-04-26 11:34:57 +03:00
|
|
|
/**
|
|
|
|
* @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2018-04-26 11:34:57 +03:00
|
|
|
* @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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-04-26 11:34:57 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\Tests\Unit\DAV\Controller;
|
|
|
|
|
|
|
|
use OCA\DAV\Controller\DirectController;
|
|
|
|
use OCA\DAV\Db\Direct;
|
|
|
|
use OCA\DAV\Db\DirectMapper;
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\AppFramework\OCS\OCSBadRequestException;
|
|
|
|
use OCP\AppFramework\OCS\OCSNotFoundException;
|
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\Files\Folder;
|
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class DirectControllerTest extends TestCase {
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-26 11:34:57 +03:00
|
|
|
private $rootFolder;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var DirectMapper|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-26 11:34:57 +03:00
|
|
|
private $directMapper;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ISecureRandom|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-26 11:34:57 +03:00
|
|
|
private $random;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-26 11:34:57 +03:00
|
|
|
private $timeFactory;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-26 11:34:57 +03:00
|
|
|
private $urlGenerator;
|
|
|
|
|
|
|
|
/** @var DirectController */
|
|
|
|
private $controller;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2018-04-26 11:34:57 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->rootFolder = $this->createMock(IRootFolder::class);
|
|
|
|
$this->directMapper = $this->createMock(DirectMapper::class);
|
|
|
|
$this->random = $this->createMock(ISecureRandom::class);
|
|
|
|
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
|
|
|
|
|
|
|
$this->controller = new DirectController(
|
|
|
|
'dav',
|
|
|
|
$this->createMock(IRequest::class),
|
|
|
|
$this->rootFolder,
|
|
|
|
'awesomeUser',
|
|
|
|
$this->directMapper,
|
|
|
|
$this->random,
|
|
|
|
$this->timeFactory,
|
|
|
|
$this->urlGenerator
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetUrlNonExistingFileId() {
|
|
|
|
$userFolder = $this->createMock(Folder::class);
|
|
|
|
$this->rootFolder->method('getUserFolder')
|
|
|
|
->with('awesomeUser')
|
|
|
|
->willReturn($userFolder);
|
|
|
|
|
|
|
|
$userFolder->method('getById')
|
|
|
|
->with(101)
|
|
|
|
->willReturn([]);
|
|
|
|
|
|
|
|
$this->expectException(OCSNotFoundException::class);
|
|
|
|
$this->controller->getUrl(101);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetUrlForFolder() {
|
|
|
|
$userFolder = $this->createMock(Folder::class);
|
|
|
|
$this->rootFolder->method('getUserFolder')
|
|
|
|
->with('awesomeUser')
|
|
|
|
->willReturn($userFolder);
|
|
|
|
|
|
|
|
$folder = $this->createMock(Folder::class);
|
|
|
|
|
|
|
|
$userFolder->method('getById')
|
|
|
|
->with(101)
|
|
|
|
->willReturn([$folder]);
|
|
|
|
|
|
|
|
$this->expectException(OCSBadRequestException::class);
|
|
|
|
$this->controller->getUrl(101);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetUrlValid() {
|
|
|
|
$userFolder = $this->createMock(Folder::class);
|
|
|
|
$this->rootFolder->method('getUserFolder')
|
|
|
|
->with('awesomeUser')
|
|
|
|
->willReturn($userFolder);
|
|
|
|
|
|
|
|
$file = $this->createMock(File::class);
|
|
|
|
|
|
|
|
$this->timeFactory->method('getTime')
|
|
|
|
->willReturn(42);
|
|
|
|
|
|
|
|
$userFolder->method('getById')
|
|
|
|
->with(101)
|
|
|
|
->willReturn([$file]);
|
|
|
|
|
|
|
|
$this->random->method('generate')
|
|
|
|
->with(
|
|
|
|
60,
|
|
|
|
ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS
|
|
|
|
)->willReturn('superduperlongtoken');
|
|
|
|
|
|
|
|
$this->directMapper->expects($this->once())
|
|
|
|
->method('insert')
|
|
|
|
->willReturnCallback(function (Direct $direct) {
|
|
|
|
$this->assertSame('awesomeUser', $direct->getUserId());
|
|
|
|
$this->assertSame(101, $direct->getFileId());
|
|
|
|
$this->assertSame('superduperlongtoken', $direct->getToken());
|
2020-10-05 16:12:57 +03:00
|
|
|
$this->assertSame(42 + 60 * 60 * 8, $direct->getExpiration());
|
2021-03-02 23:08:37 +03:00
|
|
|
|
|
|
|
return $direct;
|
2018-04-26 11:34:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->urlGenerator->method('getAbsoluteURL')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function (string $url) {
|
2018-04-26 11:34:57 +03:00
|
|
|
return 'https://my.nextcloud/'.$url;
|
|
|
|
});
|
|
|
|
|
|
|
|
$result = $this->controller->getUrl(101);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(DataResponse::class, $result);
|
|
|
|
$this->assertSame([
|
|
|
|
'url' => 'https://my.nextcloud/remote.php/direct/superduperlongtoken',
|
|
|
|
], $result->getData());
|
|
|
|
}
|
|
|
|
}
|