2014-10-15 13:58:44 +04:00
|
|
|
<?php
|
|
|
|
/**
|
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>
|
|
|
|
* @author Vincent Cloutier <vincent1cloutier@gmail.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @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;
|
|
|
|
use OCA\Files_Sharing\Application;
|
2015-03-24 13:21:58 +03:00
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
2014-10-15 13:58:44 +04:00
|
|
|
use OCP\AppFramework\IAppContainer;
|
|
|
|
use OCP\Files;
|
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
use OC\Files\View;
|
|
|
|
use OCP\Share;
|
|
|
|
use OC\URLGenerator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 IAppContainer */
|
|
|
|
private $container;
|
|
|
|
/** @var string */
|
|
|
|
private $user;
|
|
|
|
/** @var string */
|
|
|
|
private $token;
|
|
|
|
/** @var string */
|
|
|
|
private $oldUser;
|
|
|
|
/** @var ShareController */
|
|
|
|
private $shareController;
|
|
|
|
/** @var URLGenerator */
|
|
|
|
private $urlGenerator;
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
$app = new Application();
|
|
|
|
$this->container = $app->getContainer();
|
|
|
|
$this->container['Config'] = $this->getMockBuilder('\OCP\IConfig')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$this->container['AppName'] = 'files_sharing';
|
|
|
|
$this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$this->container['URLGenerator'] = $this->getMockBuilder('\OC\URLGenerator')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2015-03-24 13:21:58 +03:00
|
|
|
$this->container['UserManager'] = $this->getMockBuilder('\OCP\IUserManager')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2014-10-15 13:58:44 +04:00
|
|
|
$this->urlGenerator = $this->container['URLGenerator'];
|
|
|
|
$this->shareController = $this->container['ShareController'];
|
|
|
|
|
|
|
|
// Store current user
|
|
|
|
$this->oldUser = \OC_User::getUser();
|
|
|
|
|
|
|
|
// Create a dummy user
|
|
|
|
$this->user = \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate(12, ISecureRandom::CHAR_LOWER);
|
|
|
|
|
|
|
|
\OC_User::createUser($this->user, $this->user);
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_User::setUserId('');
|
|
|
|
Filesystem::tearDown();
|
|
|
|
\OC_User::setUserId($this->user);
|
|
|
|
\OC_Util::setupFS($this->user);
|
|
|
|
|
|
|
|
// Create a dummy shared file
|
|
|
|
$view = new View('/'. $this->user . '/files');
|
|
|
|
$view->file_put_contents('file1.txt', 'I am such an awesome shared file!');
|
|
|
|
$this->token = \OCP\Share::shareItem(
|
|
|
|
Filesystem::getFileInfo('file1.txt')->getType(),
|
|
|
|
Filesystem::getFileInfo('file1.txt')->getId(),
|
|
|
|
\OCP\Share::SHARE_TYPE_LINK,
|
|
|
|
'IAmPasswordProtected!',
|
|
|
|
1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown() {
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_User::setUserId('');
|
|
|
|
Filesystem::tearDown();
|
|
|
|
\OC_User::deleteUser($this->user);
|
|
|
|
\OC_User::setIncognitoMode(false);
|
|
|
|
|
|
|
|
\OC::$server->getSession()->set('public_link_authenticated', '');
|
|
|
|
|
|
|
|
// Set old user
|
|
|
|
\OC_User::setUserId($this->oldUser);
|
|
|
|
\OC_Util::setupFS($this->oldUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowAuthenticate() {
|
|
|
|
$linkItem = \OCP\Share::getShareByToken($this->token, false);
|
|
|
|
|
|
|
|
// Test without being authenticated
|
|
|
|
$response = $this->shareController->showAuthenticate($this->token);
|
|
|
|
$expectedResponse = new TemplateResponse($this->container['AppName'], 'authenticate', array(), 'guest');
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
|
|
|
|
// Test with being authenticated for another file
|
|
|
|
\OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']-1);
|
|
|
|
$response = $this->shareController->showAuthenticate($this->token);
|
|
|
|
$expectedResponse = new TemplateResponse($this->container['AppName'], 'authenticate', array(), 'guest');
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
|
|
|
|
// Test with being authenticated for the correct file
|
|
|
|
\OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']);
|
|
|
|
$response = $this->shareController->showAuthenticate($this->token);
|
|
|
|
$expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $this->token)));
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAuthenticate() {
|
|
|
|
// Test without a not existing token
|
|
|
|
$response = $this->shareController->authenticate('ThisTokenShouldHopefullyNeverExistSoThatTheUnitTestWillAlwaysPass :)');
|
2015-03-24 13:21:58 +03:00
|
|
|
$expectedResponse = new NotFoundResponse();
|
2014-10-15 13:58:44 +04:00
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
|
|
|
|
// Test with a valid password
|
|
|
|
$response = $this->shareController->authenticate($this->token, 'IAmPasswordProtected!');
|
|
|
|
$expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $this->token)));
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
|
|
|
|
// Test with a invalid password
|
|
|
|
$response = $this->shareController->authenticate($this->token, 'WrongPw!');
|
|
|
|
$expectedResponse = new TemplateResponse($this->container['AppName'], 'authenticate', array('wrongpw' => true), 'guest');
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowShare() {
|
2015-03-24 13:21:58 +03:00
|
|
|
$this->container['UserManager']->expects($this->exactly(2))
|
|
|
|
->method('userExists')
|
|
|
|
->with($this->user)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2014-10-15 13:58:44 +04:00
|
|
|
// Test without a not existing token
|
|
|
|
$response = $this->shareController->showShare('ThisTokenShouldHopefullyNeverExistSoThatTheUnitTestWillAlwaysPass :)');
|
2015-03-24 13:21:58 +03:00
|
|
|
$expectedResponse = new NotFoundResponse();
|
2014-10-15 13:58:44 +04:00
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
|
|
|
|
// Test with a password protected share and no authentication
|
|
|
|
$response = $this->shareController->showShare($this->token);
|
|
|
|
$expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', array('token' => $this->token)));
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
|
|
|
|
// Test with password protected share and authentication
|
|
|
|
$linkItem = Share::getShareByToken($this->token, false);
|
|
|
|
\OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']);
|
|
|
|
$response = $this->shareController->showShare($this->token);
|
|
|
|
$sharedTmplParams = array(
|
|
|
|
'displayName' => $this->user,
|
|
|
|
'filename' => 'file1.txt',
|
|
|
|
'directory_path' => '/file1.txt',
|
|
|
|
'mimetype' => 'text/plain',
|
|
|
|
'dirToken' => $this->token,
|
|
|
|
'sharingToken' => $this->token,
|
|
|
|
'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,
|
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\'');
|
2014-10-15 13:58:44 +04:00
|
|
|
$expectedResponse = new TemplateResponse($this->container['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() {
|
|
|
|
// Test with a password protected share and no authentication
|
|
|
|
$response = $this->shareController->downloadShare($this->token);
|
|
|
|
$expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate',
|
|
|
|
array('token' => $this->token)));
|
|
|
|
$this->assertEquals($expectedResponse, $response);
|
|
|
|
}
|
2015-03-24 13:21:58 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
|
|
|
* @expectedExceptionMessage No file found belonging to file.
|
|
|
|
*/
|
|
|
|
public function testShowShareWithDeletedFile() {
|
|
|
|
$this->container['UserManager']->expects($this->once())
|
|
|
|
->method('userExists')
|
|
|
|
->with($this->user)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$view = new View('/'. $this->user . '/files');
|
|
|
|
$view->unlink('file1.txt');
|
|
|
|
$linkItem = Share::getShareByToken($this->token, false);
|
|
|
|
\OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']);
|
|
|
|
$this->shareController->showShare($this->token);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
|
|
|
* @expectedExceptionMessage No file found belonging to file.
|
|
|
|
*/
|
|
|
|
public function testDownloadShareWithDeletedFile() {
|
|
|
|
$this->container['UserManager']->expects($this->once())
|
|
|
|
->method('userExists')
|
|
|
|
->with($this->user)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$view = new View('/'. $this->user . '/files');
|
|
|
|
$view->unlink('file1.txt');
|
|
|
|
$linkItem = Share::getShareByToken($this->token, false);
|
|
|
|
\OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']);
|
|
|
|
$this->shareController->downloadShare($this->token);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
|
|
|
* @expectedExceptionMessage Owner of the share does not exist anymore
|
|
|
|
*/
|
|
|
|
public function testShowShareWithNotExistingUser() {
|
|
|
|
$this->container['UserManager']->expects($this->once())
|
|
|
|
->method('userExists')
|
|
|
|
->with($this->user)
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$linkItem = Share::getShareByToken($this->token, false);
|
|
|
|
\OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']);
|
|
|
|
$this->shareController->showShare($this->token);
|
|
|
|
}
|
|
|
|
|
2014-10-15 13:58:44 +04:00
|
|
|
}
|