2014-10-15 13:58:44 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Georg Ehrke <georg@owncloud.com>
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
2014-10-15 13:58:44 +04:00
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-06-25 12:43:55 +03:00
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Roeland Jago Douma <rullzer@owncloud.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Vincent Cloutier <vincent1cloutier@gmail.com>
|
|
|
|
*
|
2016-01-12 17:02:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-10-15 13:58:44 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-10-15 13:58:44 +04:00
|
|
|
namespace OCA\Files_Sharing\Controllers;
|
|
|
|
|
|
|
|
use OC\Files\Filesystem;
|
2016-02-02 16:07:11 +03:00
|
|
|
use OCP\Share\Exceptions\ShareNotFound;
|
2015-03-24 13:21:58 +03:00
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
2014-10-15 13:58:44 +04:00
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2016-01-15 11:41:51 +03:00
|
|
|
use OCP\ISession;
|
2014-10-15 13:58:44 +04:00
|
|
|
use OCP\Security\ISecureRandom;
|
2016-01-15 11:41:51 +03:00
|
|
|
use OCP\IURLGenerator;
|
2014-10-15 13:58:44 +04:00
|
|
|
|
|
|
|
/**
|
2015-11-20 13:27:11 +03:00
|
|
|
* @group DB
|
|
|
|
*
|
2014-10-15 13:58:44 +04:00
|
|
|
* @package OCA\Files_Sharing\Controllers
|
|
|
|
*/
|
2015-02-25 14:52:16 +03:00
|
|
|
class ShareControllerTest extends \Test\TestCase {
|
2014-10-15 13:58:44 +04:00
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $user;
|
|
|
|
/** @var string */
|
|
|
|
private $oldUser;
|
2016-01-20 12:14:03 +03:00
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $appName = 'files_sharing';
|
2014-10-15 13:58:44 +04:00
|
|
|
/** @var ShareController */
|
|
|
|
private $shareController;
|
2016-01-20 12:14:03 +03:00
|
|
|
/** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject */
|
2014-10-15 13:58:44 +04:00
|
|
|
private $urlGenerator;
|
2016-01-15 11:41:51 +03:00
|
|
|
/** @var ISession | \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $session;
|
2016-01-20 12:14:03 +03:00
|
|
|
/** @var \OCP\IPreview | \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $previewManager;
|
|
|
|
/** @var \OCP\IConfig | \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $config;
|
2016-01-15 11:41:51 +03:00
|
|
|
/** @var \OC\Share20\Manager | \PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $shareManager;
|
2014-10-15 13:58:44 +04:00
|
|
|
|
|
|
|
protected function setUp() {
|
2016-01-20 12:14:03 +03:00
|
|
|
$this->appName = 'files_sharing';
|
|
|
|
|
|
|
|
$this->shareManager = $this->getMockBuilder('\OC\Share20\Manager')->disableOriginalConstructor()->getMock();
|
|
|
|
$this->urlGenerator = $this->getMock('\OCP\IURLGenerator');
|
|
|
|
$this->session = $this->getMock('\OCP\ISession');
|
|
|
|
$this->previewManager = $this->getMock('\OCP\IPreview');
|
|
|
|
$this->config = $this->getMock('\OCP\IConfig');
|
|
|
|
|
|
|
|
$this->shareController = new \OCA\Files_Sharing\Controllers\ShareController(
|
|
|
|
$this->appName,
|
|
|
|
$this->getMock('\OCP\IRequest'),
|
|
|
|
$this->config,
|
|
|
|
$this->urlGenerator,
|
|
|
|
$this->getMock('\OCP\IUserManager'),
|
|
|
|
$this->getMock('\OCP\ILogger'),
|
|
|
|
$this->getMock('\OCP\Activity\IManager'),
|
|
|
|
$this->shareManager,
|
|
|
|
$this->session,
|
|
|
|
$this->previewManager,
|
|
|
|
$this->getMock('\OCP\Files\IRootFolder')
|
|
|
|
);
|
|
|
|
|
2014-10-15 13:58:44 +04:00
|
|
|
|
|
|
|
// Store current user
|
|
|
|
$this->oldUser = \OC_User::getUser();
|
|
|
|
|
|
|
|
// Create a dummy user
|
2016-01-11 21:59:15 +03:00
|
|
|
$this->user = \OC::$server->getSecureRandom()->generate(12, ISecureRandom::CHAR_LOWER);
|
2014-10-15 13:58:44 +04:00
|
|
|
|
2015-12-17 17:10:11 +03:00
|
|
|
\OC::$server->getUserManager()->createUser($this->user, $this->user);
|
2014-10-15 13:58:44 +04:00
|
|
|
\OC_Util::tearDownFS();
|
2015-05-11 16:22:05 +03:00
|
|
|
$this->loginAsUser($this->user);
|
2014-10-15 13:58:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown() {
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_User::setUserId('');
|
|
|
|
Filesystem::tearDown();
|
2015-12-17 17:59:23 +03:00
|
|
|
$user = \OC::$server->getUserManager()->get($this->user);
|
|
|
|
if ($user !== null) { $user->delete(); }
|
2014-10-15 13:58:44 +04:00
|
|
|
\OC_User::setIncognitoMode(false);
|
|
|
|
|
|
|
|
\OC::$server->getSession()->set('public_link_authenticated', '');
|
|
|
|
|
|
|
|
// Set old user
|
|
|
|
\OC_User::setUserId($this->oldUser);
|
|
|
|
\OC_Util::setupFS($this->oldUser);
|
|
|
|
}
|
|
|
|
|
2016-01-15 11:41:51 +03:00
|
|
|
public function testShowAuthenticateNotAuthenticated() {
|
2016-01-27 14:13:53 +03:00
|
|
|
$share = $this->getMock('\OCP\Share\IShare');
|
2014-10-15 13:58:44 +04:00
|
|
|
|
2016-01-15 11:41:51 +03:00
|
|
|
$this->shareManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->with('token')
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
$response = $this->shareController->showAuthenticate('token');
|
2016-01-20 12:14:03 +03:00
|
|
|
$expectedResponse = new TemplateResponse($this->appName, 'authenticate', [], 'guest');
|
2014-10-15 13:58:44 +04:00
|
|
|
$this->assertEquals($expectedResponse, $response);
|
2016-01-15 11:41:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowAuthenticateAuthenticatedForDifferentShare() {
|
2016-01-27 14:13:53 +03:00
|
|
|
$share = $this->getMock('\OCP\Share\IShare');
|
2016-01-15 11:41:51 +03:00
|
|
|
$share->method('getId')->willReturn(1);
|
|
|
|
|
|
|
|
$this->shareManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->with('token')
|
|
|
|
->willReturn($share);
|
2014-10-15 13:58:44 +04:00
|
|
|
|
2016-01-15 11:41:51 +03:00
|
|
|
$this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
|
|
|
|
$this->session->method('get')->with('public_link_authenticated')->willReturn('2');
|
|
|
|
|
|
|
|
$response = $this->shareController->showAuthenticate('token');
|
2016-01-20 12:14:03 +03:00
|
|
|
$expectedResponse = new TemplateResponse($this->appName, 'authenticate', [], 'guest');
|
2014-10-15 13:58:44 +04:00
|
|
|
$this->assertEquals($expectedResponse, $response);
|
2016-01-15 11:41:51 +03:00
|
|
|
}
|
2014-10-15 13:58:44 +04:00
|
|
|
|
2016-01-15 11:41:51 +03:00
|
|
|
public function testShowAuthenticateCorrectShare() {
|
2016-01-27 14:13:53 +03:00
|
|
|
$share = $this->getMock('\OCP\Share\IShare');
|
2016-01-15 11:41:51 +03:00
|
|
|
$share->method('getId')->willReturn(1);
|
|
|
|
|
|
|
|
$this->shareManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->with('token')
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
$this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
|
|
|
|
$this->session->method('get')->with('public_link_authenticated')->willReturn('1');
|
|
|
|
|
2016-01-20 12:14:03 +03:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('files_sharing.sharecontroller.showShare', ['token' => 'token'])
|
|
|
|
->willReturn('redirect');
|
|
|
|
|
2016-01-15 11:41:51 +03:00
|
|
|
$response = $this->shareController->showAuthenticate('token');
|
2016-01-20 12:14:03 +03:00
|
|
|
$expectedResponse = new RedirectResponse('redirect');
|
2014-10-15 13:58:44 +04:00
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
2016-01-15 11:41:51 +03:00
|
|
|
public function testAutehnticateInvalidToken() {
|
|
|
|
$this->shareManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->with('token')
|
2016-02-02 16:07:11 +03:00
|
|
|
->will($this->throwException(new \OCP\Share\Exceptions\ShareNotFound()));
|
2016-01-15 11:41:51 +03:00
|
|
|
|
|
|
|
$response = $this->shareController->authenticate('token');
|
2015-03-24 13:21:58 +03:00
|
|
|
$expectedResponse = new NotFoundResponse();
|
2014-10-15 13:58:44 +04:00
|
|
|
$this->assertEquals($expectedResponse, $response);
|
2016-01-15 11:41:51 +03:00
|
|
|
}
|
2014-10-15 13:58:44 +04:00
|
|
|
|
2016-01-15 11:41:51 +03:00
|
|
|
public function testAuthenticateValidPassword() {
|
2016-01-27 14:13:53 +03:00
|
|
|
$share = $this->getMock('\OCP\Share\IShare');
|
2016-01-15 11:41:51 +03:00
|
|
|
$share->method('getId')->willReturn(42);
|
|
|
|
|
|
|
|
$this->shareManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->with('token')
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
$this->shareManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('checkPassword')
|
|
|
|
->with($share, 'validpassword')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('set')
|
|
|
|
->with('public_link_authenticated', '42');
|
|
|
|
|
2016-01-20 12:14:03 +03:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('files_sharing.sharecontroller.showShare', ['token'=>'token'])
|
|
|
|
->willReturn('redirect');
|
|
|
|
|
2016-01-15 11:41:51 +03:00
|
|
|
$response = $this->shareController->authenticate('token', 'validpassword');
|
2016-01-20 12:14:03 +03:00
|
|
|
$expectedResponse = new RedirectResponse('redirect');
|
2014-10-15 13:58:44 +04:00
|
|
|
$this->assertEquals($expectedResponse, $response);
|
2016-01-15 11:41:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAuthenticateInvalidPassword() {
|
2016-01-27 14:13:53 +03:00
|
|
|
$share = $this->getMock('\OCP\Share\IShare');
|
2016-01-15 11:41:51 +03:00
|
|
|
$share->method('getId')->willReturn(42);
|
2014-10-15 13:58:44 +04:00
|
|
|
|
2016-01-15 11:41:51 +03:00
|
|
|
$this->shareManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->with('token')
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
$this->shareManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('checkPassword')
|
|
|
|
->with($share, 'invalidpassword')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->session
|
|
|
|
->expects($this->never())
|
|
|
|
->method('set');
|
|
|
|
|
|
|
|
$response = $this->shareController->authenticate('token', 'invalidpassword');
|
2016-01-20 12:14:03 +03:00
|
|
|
$expectedResponse = new TemplateResponse($this->appName, 'authenticate', array('wrongpw' => true), 'guest');
|
2014-10-15 13:58:44 +04:00
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
2016-01-15 11:41:51 +03:00
|
|
|
public function testShowShareInvalidToken() {
|
|
|
|
$this->shareManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->with('invalidtoken')
|
|
|
|
->will($this->throwException(new ShareNotFound()));
|
2015-03-24 13:21:58 +03:00
|
|
|
|
2014-10-15 13:58:44 +04:00
|
|
|
// Test without a not existing token
|
2016-01-15 11:41:51 +03:00
|
|
|
$response = $this->shareController->showShare('invalidtoken');
|
2015-03-24 13:21:58 +03:00
|
|
|
$expectedResponse = new NotFoundResponse();
|
2014-10-15 13:58:44 +04:00
|
|
|
$this->assertEquals($expectedResponse, $response);
|
2016-01-15 11:41:51 +03:00
|
|
|
}
|
2014-10-15 13:58:44 +04:00
|
|
|
|
2016-01-15 11:41:51 +03:00
|
|
|
public function testShowShareNotAuthenticated() {
|
2016-01-27 14:13:53 +03:00
|
|
|
$share = $this->getMock('\OCP\Share\IShare');
|
2016-01-15 11:41:51 +03:00
|
|
|
$share->method('getPassword')->willReturn('password');
|
|
|
|
|
|
|
|
$this->shareManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->with('validtoken')
|
|
|
|
->willReturn($share);
|
|
|
|
|
2016-01-20 12:14:03 +03:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('files_sharing.sharecontroller.authenticate', ['token' => 'validtoken'])
|
|
|
|
->willReturn('redirect');
|
|
|
|
|
2016-01-15 11:41:51 +03:00
|
|
|
// Test without a not existing token
|
|
|
|
$response = $this->shareController->showShare('validtoken');
|
2016-01-20 12:14:03 +03:00
|
|
|
$expectedResponse = new RedirectResponse('redirect');
|
2014-10-15 13:58:44 +04:00
|
|
|
$this->assertEquals($expectedResponse, $response);
|
2016-01-15 11:41:51 +03:00
|
|
|
}
|
2014-10-15 13:58:44 +04:00
|
|
|
|
2016-01-15 11:41:51 +03:00
|
|
|
|
|
|
|
public function testShowShare() {
|
|
|
|
$owner = $this->getMock('OCP\IUser');
|
|
|
|
$owner->method('getDisplayName')->willReturn('ownerDisplay');
|
|
|
|
$owner->method('getUID')->willReturn('ownerUID');
|
|
|
|
|
|
|
|
$file = $this->getMock('OCP\Files\File');
|
|
|
|
$file->method('getName')->willReturn('file1.txt');
|
|
|
|
$file->method('getMimetype')->willReturn('text/plain');
|
|
|
|
$file->method('getSize')->willReturn(33);
|
|
|
|
|
2016-01-27 14:13:53 +03:00
|
|
|
$share = $this->getMock('\OCP\Share\IShare');
|
2016-01-15 11:41:51 +03:00
|
|
|
$share->method('getId')->willReturn('42');
|
|
|
|
$share->method('getPassword')->willReturn('password');
|
|
|
|
$share->method('getShareOwner')->willReturn($owner);
|
2016-01-27 22:51:26 +03:00
|
|
|
$share->method('getNode')->willReturn($file);
|
2016-01-15 11:41:51 +03:00
|
|
|
$share->method('getTarget')->willReturn('/file1.txt');
|
|
|
|
|
|
|
|
$this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
|
|
|
|
$this->session->method('get')->with('public_link_authenticated')->willReturn('42');
|
|
|
|
|
2016-01-20 12:14:03 +03:00
|
|
|
$this->previewManager->method('isMimeSupported')->with('text/plain')->willReturn(true);
|
|
|
|
|
|
|
|
$this->config->method('getSystemValue')
|
|
|
|
->willReturnMap(
|
|
|
|
[
|
|
|
|
['max_filesize_animated_gifs_public_sharing', 10, 10],
|
|
|
|
['enable_previews', true, true],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10);
|
|
|
|
$shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true);
|
|
|
|
|
2016-01-15 11:41:51 +03:00
|
|
|
$this->shareManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->with('token')
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
$response = $this->shareController->showShare('token');
|
2014-10-15 13:58:44 +04:00
|
|
|
$sharedTmplParams = array(
|
2016-01-15 11:41:51 +03:00
|
|
|
'displayName' => 'ownerDisplay',
|
|
|
|
'owner' => 'ownerUID',
|
2014-10-15 13:58:44 +04:00
|
|
|
'filename' => 'file1.txt',
|
|
|
|
'directory_path' => '/file1.txt',
|
|
|
|
'mimetype' => 'text/plain',
|
2016-01-15 11:41:51 +03:00
|
|
|
'dirToken' => 'token',
|
|
|
|
'sharingToken' => 'token',
|
2014-10-15 13:58:44 +04:00
|
|
|
'server2serversharing' => true,
|
|
|
|
'protected' => 'true',
|
2014-11-18 16:54:08 +03:00
|
|
|
'dir' => '',
|
2014-10-16 04:59:07 +04:00
|
|
|
'downloadURL' => null,
|
2015-01-17 14:11:52 +03:00
|
|
|
'fileSize' => '33 B',
|
|
|
|
'nonHumanFileSize' => 33,
|
|
|
|
'maxSizeAnimateGif' => 10,
|
2015-03-16 14:09:03 +03:00
|
|
|
'previewSupported' => true,
|
2015-06-09 17:33:26 +03:00
|
|
|
'previewEnabled' => true,
|
2014-10-15 13:58:44 +04:00
|
|
|
);
|
2015-03-10 12:06:15 +03:00
|
|
|
|
|
|
|
$csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
|
|
|
|
$csp->addAllowedFrameDomain('\'self\'');
|
2016-01-20 12:14:03 +03:00
|
|
|
$expectedResponse = new TemplateResponse($this->appName, 'public', $sharedTmplParams, 'base');
|
2015-03-10 12:06:15 +03:00
|
|
|
$expectedResponse->setContentSecurityPolicy($csp);
|
|
|
|
|
2014-10-15 13:58:44 +04:00
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDownloadShare() {
|
2016-01-27 14:13:53 +03:00
|
|
|
$share = $this->getMock('\OCP\Share\IShare');
|
2016-01-15 13:49:50 +03:00
|
|
|
$share->method('getPassword')->willReturn('password');
|
|
|
|
|
|
|
|
$this->shareManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getShareByToken')
|
|
|
|
->with('validtoken')
|
|
|
|
->willReturn($share);
|
|
|
|
|
2016-01-20 12:14:03 +03:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('files_sharing.sharecontroller.authenticate', ['token' => 'validtoken'])
|
|
|
|
->willReturn('redirect');
|
|
|
|
|
2014-10-15 13:58:44 +04:00
|
|
|
// Test with a password protected share and no authentication
|
2016-01-15 13:49:50 +03:00
|
|
|
$response = $this->shareController->downloadShare('validtoken');
|
2016-01-20 12:14:03 +03:00
|
|
|
$expectedResponse = new RedirectResponse('redirect');
|
2014-10-15 13:58:44 +04:00
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
2015-03-24 13:21:58 +03:00
|
|
|
|
2014-10-15 13:58:44 +04:00
|
|
|
}
|