aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_sharing/tests
diff options
context:
space:
mode:
authorLukas Reschke <lukas@owncloud.com>2014-10-15 11:58:44 +0200
committerLukas Reschke <lukas@owncloud.com>2014-11-14 16:26:59 +0100
commit988c85d2922a03346389e3656dc71dfee514e645 (patch)
tree9be0d1674dd7e780c70c77a987c4a810145078ab /apps/files_sharing/tests
parent0f3c5d8541dcb41eebd00f22864a0a646c11124f (diff)
downloadnextcloud-server-988c85d2922a03346389e3656dc71dfee514e645.tar.gz
nextcloud-server-988c85d2922a03346389e3656dc71dfee514e645.zip
Refactor file sharing public link handling
fixes download issue introduced by #10755 Conflicts: apps/files_sharing/public.php
Diffstat (limited to 'apps/files_sharing/tests')
-rw-r--r--apps/files_sharing/tests/controller/sharecontroller.php170
-rw-r--r--apps/files_sharing/tests/middleware/sharingcheckmiddleware.php76
-rw-r--r--apps/files_sharing/tests/testcase.php5
3 files changed, 251 insertions, 0 deletions
diff --git a/apps/files_sharing/tests/controller/sharecontroller.php b/apps/files_sharing/tests/controller/sharecontroller.php
new file mode 100644
index 00000000000..357184ba692
--- /dev/null
+++ b/apps/files_sharing/tests/controller/sharecontroller.php
@@ -0,0 +1,170 @@
+<?php
+/**
+ * @author Lukas Reschke <lukas@owncloud.com>
+ * @copyright 2014 Lukas Reschke
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCA\Files_Sharing\Controllers;
+
+use OC\Files\Filesystem;
+use OCA\Files_Sharing\Application;
+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
+ */
+class ShareControllerTest extends \PHPUnit_Framework_TestCase {
+
+ /** @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();
+ $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 :)');
+ $expectedResponse = new TemplateResponse('core', '404', array(), 'guest');
+ $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() {
+ // Test without a not existing token
+ $response = $this->shareController->showShare('ThisTokenShouldHopefullyNeverExistSoThatTheUnitTestWillAlwaysPass :)');
+ $expectedResponse = new TemplateResponse('core', '404', array(), 'guest');
+ $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',
+ 'dir' => '/',
+ 'downloadURL' => null
+ );
+ $expectedResponse = new TemplateResponse($this->container['AppName'], 'public', $sharedTmplParams, 'base');
+ $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);
+ }
+}
diff --git a/apps/files_sharing/tests/middleware/sharingcheckmiddleware.php b/apps/files_sharing/tests/middleware/sharingcheckmiddleware.php
new file mode 100644
index 00000000000..90c9a7bba10
--- /dev/null
+++ b/apps/files_sharing/tests/middleware/sharingcheckmiddleware.php
@@ -0,0 +1,76 @@
+<?php
+/**
+ * @author Lukas Reschke <lukas@owncloud.com>
+ * @copyright 2014 Lukas Reschke
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCA\Files_Sharing\Middleware;
+
+
+/**
+ * @package OCA\Files_Sharing\Middleware\SharingCheckMiddleware
+ */
+class SharingCheckMiddlewareTest extends \PHPUnit_Framework_TestCase {
+
+ /** @var \OCP\IAppConfig */
+ private $appConfig;
+ /** @var \OCP\AppFramework\IApi */
+ private $api;
+ /** @var SharingCheckMiddleware */
+ private $sharingCheckMiddleware;
+
+ protected function setUp() {
+ $this->appConfig = $this->getMockBuilder('\OCP\IAppConfig')
+ ->disableOriginalConstructor()->getMock();
+ $this->api = $this->getMockBuilder('\OCP\AppFramework\IApi')
+ ->disableOriginalConstructor()->getMock();
+
+ $this->sharingCheckMiddleware = new SharingCheckMiddleware('files_sharing', $this->appConfig, $this->api);
+ }
+
+ public function testIsSharingEnabledWithEverythingEnabled() {
+ $this->api
+ ->expects($this->once())
+ ->method('isAppEnabled')
+ ->with('files_sharing')
+ ->will($this->returnValue(true));
+
+ $this->appConfig
+ ->expects($this->once())
+ ->method('getValue')
+ ->with('core', 'shareapi_allow_links', 'yes')
+ ->will($this->returnValue('yes'));
+
+ $this->assertTrue(\Test_Helper::invokePrivate($this->sharingCheckMiddleware, 'isSharingEnabled'));
+ }
+
+ public function testIsSharingEnabledWithAppDisabled() {
+ $this->api
+ ->expects($this->once())
+ ->method('isAppEnabled')
+ ->with('files_sharing')
+ ->will($this->returnValue(false));
+
+ $this->assertFalse(\Test_Helper::invokePrivate($this->sharingCheckMiddleware, 'isSharingEnabled'));
+ }
+
+ public function testIsSharingEnabledWithSharingDisabled() {
+ $this->api
+ ->expects($this->once())
+ ->method('isAppEnabled')
+ ->with('files_sharing')
+ ->will($this->returnValue(true));
+
+ $this->appConfig
+ ->expects($this->once())
+ ->method('getValue')
+ ->with('core', 'shareapi_allow_links', 'yes')
+ ->will($this->returnValue('no'));
+
+ $this->assertFalse(\Test_Helper::invokePrivate($this->sharingCheckMiddleware, 'isSharingEnabled'));
+ }
+}
diff --git a/apps/files_sharing/tests/testcase.php b/apps/files_sharing/tests/testcase.php
index 78277dc907f..034baa785da 100644
--- a/apps/files_sharing/tests/testcase.php
+++ b/apps/files_sharing/tests/testcase.php
@@ -22,6 +22,7 @@
namespace OCA\Files_Sharing\Tests;
+use OC\Files\Filesystem;
use OCA\Files\Share;
/**
@@ -115,6 +116,10 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
} else {
\OC_App::disable('files_encryption');
}
+
+ \OC_Util::tearDownFS();
+ \OC_User::setUserId('');
+ Filesystem::tearDown();
}
/**