Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sharecontroller.php « controller « tests « files_sharing « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 204422853d081a10b821278f233e92da548a3e7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?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\Http\NotFoundResponse;
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 \Test\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->container['UserManager'] = $this->getMockBuilder('\OCP\IUserManager')
			->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 NotFoundResponse();
		$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() {
		$this->container['UserManager']->expects($this->exactly(2))
			->method('userExists')
			->with($this->user)
			->will($this->returnValue(true));

		// Test without a not existing token
		$response = $this->shareController->showShare('ThisTokenShouldHopefullyNeverExistSoThatTheUnitTestWillAlwaysPass :)');
		$expectedResponse =  new NotFoundResponse();
		$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,
			'fileSize' => '33 B',
			'nonHumanFileSize' => 33,
			'maxSizeAnimateGif' => 10,
			'previewSupported' => true,
		);

		$csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
		$csp->addAllowedFrameDomain('\'self\'');
		$expectedResponse = new TemplateResponse($this->container['AppName'], 'public', $sharedTmplParams, 'base');
		$expectedResponse->setContentSecurityPolicy($csp);

		$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);
	}

	/**
	 * @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);
	}

}