GlobalStoragesServiceTest.php 14.7 KB
Newer Older
1
2
<?php
/**
Thomas Müller's avatar
Thomas Müller committed
3
 * @author Joas Schilling <coding@schilljs.com>
Thomas Müller's avatar
Thomas Müller committed
4
5
 * @author Robin Appelman <icewind@owncloud.com>
 * @author Robin McCorkell <robin@mccorkell.me.uk>
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
6
 * @author Vincent Petry <pvince81@owncloud.com>
7
 *
Thomas Müller's avatar
Thomas Müller committed
8
 * @copyright Copyright (c) 2016, ownCloud GmbH.
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
9
 * @license AGPL-3.0
10
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
11
12
13
 * 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.
14
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
17
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
19
 *
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
20
21
 * 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/>
22
23
 *
 */
24
namespace Test\Files\External\Service;
25
26

use \OC\Files\Filesystem;
27
28
use \OC\Files\External\Service\GlobalStoragesService;
use OCP\Files\External\IStorageConfig;
29

30
31
32
/**
 * @group DB
 */
33
34
35
class GlobalStoragesServiceTest extends StoragesServiceTest {
	public function setUp() {
		parent::setUp();
36
		$this->service = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache);
37
38
39
40
41
42
43
44
	}

	public function tearDown() {
		@unlink($this->dataDir . '/mount.json');
		parent::tearDown();
	}

	protected function makeTestStorageData() {
45
		return $this->makeStorageConfig([
46
			'mountPoint' => 'mountpoint',
47
			'backendIdentifier' => 'identifier:\Test\Files\External\Backend\DummyBackend',
48
			'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
49
50
51
52
53
54
55
56
			'backendOptions' => [
				'option1' => 'value1',
				'option2' => 'value2',
				'password' => 'testPassword',
			],
			'applicableUsers' => [],
			'applicableGroups' => [],
			'priority' => 15,
57
58
59
			'mountOptions' => [
				'preview' => false,
			]
60
61
62
63
64
65
66
		]);
	}

	function storageDataProvider() {
		return [
			// all users
			[
67
				[
68
					'mountPoint' => 'mountpoint',
69
					'backendIdentifier' => 'identifier:\Test\Files\External\Backend\DummyBackend',
70
					'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
71
72
73
74
75
76
77
78
					'backendOptions' => [
						'option1' => 'value1',
						'option2' => 'value2',
						'password' => 'testPassword',
					],
					'applicableUsers' => [],
					'applicableGroups' => [],
					'priority' => 15,
79
				],
80
81
82
			],
			// some users
			[
83
				[
84
					'mountPoint' => 'mountpoint',
85
					'backendIdentifier' => 'identifier:\Test\Files\External\Backend\DummyBackend',
86
					'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
87
88
89
90
91
92
93
94
					'backendOptions' => [
						'option1' => 'value1',
						'option2' => 'value2',
						'password' => 'testPassword',
					],
					'applicableUsers' => ['user1', 'user2'],
					'applicableGroups' => [],
					'priority' => 15,
95
				],
96
97
98
			],
			// some groups
			[
99
				[
100
					'mountPoint' => 'mountpoint',
101
					'backendIdentifier' => 'identifier:\Test\Files\External\Backend\DummyBackend',
102
					'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
103
104
105
106
107
108
109
110
					'backendOptions' => [
						'option1' => 'value1',
						'option2' => 'value2',
						'password' => 'testPassword',
					],
					'applicableUsers' => [],
					'applicableGroups' => ['group1', 'group2'],
					'priority' => 15,
111
				],
112
113
114
			],
			// both users and groups
			[
115
				[
116
					'mountPoint' => 'mountpoint',
117
					'backendIdentifier' => 'identifier:\Test\Files\External\Backend\DummyBackend',
118
					'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
119
120
121
122
123
124
125
126
					'backendOptions' => [
						'option1' => 'value1',
						'option2' => 'value2',
						'password' => 'testPassword',
					],
					'applicableUsers' => ['user1', 'user2'],
					'applicableGroups' => ['group1', 'group2'],
					'priority' => 15,
127
				],
128
129
130
131
			],
		];
	}

132
133
134
135
136
137
138
139
	private function assertEqualsArrays($expected, $actual) {
		sort($expected);
		sort($actual);

		$this->assertEquals($expected, $actual);
	}


140
141
142
	/**
	 * @dataProvider storageDataProvider
	 */
143
144
	public function testAddStorage($storageParams) {
		$storage = $this->makeStorageConfig($storageParams);
145
146
		$newStorage = $this->service->addStorage($storage);

147
		$baseId = $newStorage->getId();
148

149
		$newStorage = $this->service->getStorage($baseId);
150
151

		$this->assertEquals($storage->getMountPoint(), $newStorage->getMountPoint());
152
		$this->assertEquals($storage->getBackend(), $newStorage->getBackend());
153
		$this->assertEquals($storage->getAuthMechanism(), $newStorage->getAuthMechanism());
154
155
156
		$this->assertEqualsArrays($storage->getBackendOptions(), $newStorage->getBackendOptions());
		$this->assertEqualsArrays($storage->getApplicableUsers(), $newStorage->getApplicableUsers());
		$this->assertEqualsArrays($storage->getApplicableGroups(), $newStorage->getApplicableGroups());
157
158
159
160
		$this->assertEquals($storage->getPriority(), $newStorage->getPriority());
		$this->assertEquals(0, $newStorage->getStatus());

		$nextStorage = $this->service->addStorage($storage);
161
		$this->assertEquals($baseId + 1, $nextStorage->getId());
162
163
164
165
166
	}

	/**
	 * @dataProvider storageDataProvider
	 */
167
168
	public function testUpdateStorage($updatedStorageParams) {
		$updatedStorage = $this->makeStorageConfig($updatedStorageParams);
169
170
		$storage = $this->makeStorageConfig([
			'mountPoint' => 'mountpoint',
171
			'backendIdentifier' => 'identifier:\Test\Files\External\Backend\DummyBackend',
172
			'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
173
174
175
176
177
178
179
180
181
182
183
			'backendOptions' => [
				'option1' => 'value1',
				'option2' => 'value2',
				'password' => 'testPassword',
			],
			'applicableUsers' => [],
			'applicableGroups' => [],
			'priority' => 15,
		]);

		$newStorage = $this->service->addStorage($storage);
184
		$id = $newStorage->getId();
185

186
		$updatedStorage->setId($id);
187
188

		$this->service->updateStorage($updatedStorage);
189
		$newStorage = $this->service->getStorage($id);
190
191
192

		$this->assertEquals($updatedStorage->getMountPoint(), $newStorage->getMountPoint());
		$this->assertEquals($updatedStorage->getBackendOptions()['password'], $newStorage->getBackendOptions()['password']);
193
194
		$this->assertEqualsArrays($updatedStorage->getApplicableUsers(), $newStorage->getApplicableUsers());
		$this->assertEqualsArrays($updatedStorage->getApplicableGroups(), $newStorage->getApplicableGroups());
195
196
197
198
199
200
201
202
203
204
205
206
207
208
		$this->assertEquals($updatedStorage->getPriority(), $newStorage->getPriority());
		$this->assertEquals(0, $newStorage->getStatus());
	}

	function hooksAddStorageDataProvider() {
		return [
			// applicable all
			[
				[],
				[],
				// expected hook calls
				[
					[
						Filesystem::signal_create_mount,
209
						IStorageConfig::MOUNT_TYPE_USER,
210
211
212
213
214
215
216
217
218
219
220
221
						'all'
					],
				],
			],
			// single user
			[
				['user1'],
				[],
				// expected hook calls
				[
					[
						Filesystem::signal_create_mount,
222
						IStorageConfig::MOUNT_TYPE_USER,
223
224
225
226
227
228
229
230
231
232
233
234
						'user1',
					],
				],
			],
			// single group
			[
				[],
				['group1'],
				// expected hook calls
				[
					[
						Filesystem::signal_create_mount,
235
						IStorageConfig::MOUNT_TYPE_GROUP,
236
237
238
239
240
241
242
243
244
245
246
						'group1',
					],
				],
			],
			// multiple users
			[
				['user1', 'user2'],
				[],
				[
					[
						Filesystem::signal_create_mount,
247
						IStorageConfig::MOUNT_TYPE_USER,
248
249
250
251
						'user1',
					],
					[
						Filesystem::signal_create_mount,
252
						IStorageConfig::MOUNT_TYPE_USER,
253
254
255
256
257
258
259
260
261
262
263
264
						'user2',
					],
				],
			],
			// multiple groups
			[
				[],
				['group1', 'group2'],
				// expected hook calls
				[
					[
						Filesystem::signal_create_mount,
265
						IStorageConfig::MOUNT_TYPE_GROUP,
266
267
268
269
						'group1'
					],
					[
						Filesystem::signal_create_mount,
270
						IStorageConfig::MOUNT_TYPE_GROUP,
271
272
273
274
275
276
277
278
279
280
281
282
						'group2'
					],
				],
			],
			// mixed groups and users 
			[
				['user1', 'user2'],
				['group1', 'group2'],
				// expected hook calls
				[
					[
						Filesystem::signal_create_mount,
283
						IStorageConfig::MOUNT_TYPE_USER,
284
285
286
287
						'user1',
					],
					[
						Filesystem::signal_create_mount,
288
						IStorageConfig::MOUNT_TYPE_USER,
289
290
291
292
						'user2',
					],
					[
						Filesystem::signal_create_mount,
293
						IStorageConfig::MOUNT_TYPE_GROUP,
294
295
296
297
						'group1'
					],
					[
						Filesystem::signal_create_mount,
298
						IStorageConfig::MOUNT_TYPE_GROUP,
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
						'group2'
					],
				],
			],
		];
	}

	/**
	 * @dataProvider hooksAddStorageDataProvider
	 */
	public function testHooksAddStorage($applicableUsers, $applicableGroups, $expectedCalls) {
		$storage = $this->makeTestStorageData();
		$storage->setApplicableUsers($applicableUsers);
		$storage->setApplicableGroups($applicableGroups);
		$this->service->addStorage($storage);

		$this->assertCount(count($expectedCalls), self::$hookCalls);

		foreach ($expectedCalls as $index => $call) {
			$this->assertHookCall(
				self::$hookCalls[$index],
				$call[0],
				$storage->getMountPoint(),
				$call[1],
				$call[2]
			);
		}
	}

	function hooksUpdateStorageDataProvider() {
		return [
			[
				// nothing to multiple users and groups
				[],
				[],
				['user1', 'user2'],
				['group1', 'group2'],
				// expected hook calls
				[
					// delete the "all entry"
					[
						Filesystem::signal_delete_mount,
341
						IStorageConfig::MOUNT_TYPE_USER,
342
343
344
345
						'all',
					],
					[
						Filesystem::signal_create_mount,
346
						IStorageConfig::MOUNT_TYPE_USER,
347
348
349
350
						'user1',
					],
					[
						Filesystem::signal_create_mount,
351
						IStorageConfig::MOUNT_TYPE_USER,
352
353
354
355
						'user2',
					],
					[
						Filesystem::signal_create_mount,
356
						IStorageConfig::MOUNT_TYPE_GROUP,
357
358
359
360
						'group1'
					],
					[
						Filesystem::signal_create_mount,
361
						IStorageConfig::MOUNT_TYPE_GROUP,
362
363
364
365
366
367
368
369
370
371
372
373
374
375
						'group2'
					],
				],
			],
			[
				// adding a user and a group
				['user1'],
				['group1'],
				['user1', 'user2'],
				['group1', 'group2'],
				// expected hook calls
				[
					[
						Filesystem::signal_create_mount,
376
						IStorageConfig::MOUNT_TYPE_USER,
377
378
379
380
						'user2',
					],
					[
						Filesystem::signal_create_mount,
381
						IStorageConfig::MOUNT_TYPE_GROUP,
382
383
384
385
386
387
388
389
390
391
392
393
394
395
						'group2'
					],
				],
			],
			[
				// removing a user and a group
				['user1', 'user2'],
				['group1', 'group2'],
				['user1'],
				['group1'],
				// expected hook calls
				[
					[
						Filesystem::signal_delete_mount,
396
						IStorageConfig::MOUNT_TYPE_USER,
397
398
399
400
						'user2',
					],
					[
						Filesystem::signal_delete_mount,
401
						IStorageConfig::MOUNT_TYPE_GROUP,
402
403
404
405
406
407
408
409
410
411
412
413
414
415
						'group2'
					],
				],
			],
			[
				// removing all
				['user1'],
				['group1'],
				[],
				[],
				// expected hook calls
				[
					[
						Filesystem::signal_delete_mount,
416
						IStorageConfig::MOUNT_TYPE_USER,
417
418
419
420
						'user1',
					],
					[
						Filesystem::signal_delete_mount,
421
						IStorageConfig::MOUNT_TYPE_GROUP,
422
423
424
425
426
						'group1'
					],
					// create the "all" entry
					[
						Filesystem::signal_create_mount,
427
						IStorageConfig::MOUNT_TYPE_USER,
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
						'all'
					],
				],
			],
			[
				// no changes
				['user1'],
				['group1'],
				['user1'],
				['group1'],
				// no hook calls
				[]
			]
		];
	}

	/**
	 * @dataProvider hooksUpdateStorageDataProvider
	 */
	public function testHooksUpdateStorage(
		$sourceApplicableUsers,
		$sourceApplicableGroups,
		$updatedApplicableUsers,
		$updatedApplicableGroups,
452
		$expectedCalls) {
453
454
455
456
457
458

		$storage = $this->makeTestStorageData();
		$storage->setApplicableUsers($sourceApplicableUsers);
		$storage->setApplicableGroups($sourceApplicableGroups);
		$storage = $this->service->addStorage($storage);

459
460
		$storage->setApplicableUsers($updatedApplicableUsers);
		$storage->setApplicableGroups($updatedApplicableGroups);
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499

		// reset calls
		self::$hookCalls = [];

		$this->service->updateStorage($storage);

		$this->assertCount(count($expectedCalls), self::$hookCalls);

		foreach ($expectedCalls as $index => $call) {
			$this->assertHookCall(
				self::$hookCalls[$index],
				$call[0],
				'/mountpoint',
				$call[1],
				$call[2]
			);
		}
	}

	/**
	 */
	public function testHooksRenameMountPoint() {
		$storage = $this->makeTestStorageData();
		$storage->setApplicableUsers(['user1', 'user2']);
		$storage->setApplicableGroups(['group1', 'group2']);
		$storage = $this->service->addStorage($storage);

		$storage->setMountPoint('renamedMountpoint');

		// reset calls
		self::$hookCalls = [];

		$this->service->updateStorage($storage);

		$expectedCalls = [
			// deletes old mount
			[
				Filesystem::signal_delete_mount,
				'/mountpoint',
500
				IStorageConfig::MOUNT_TYPE_USER,
501
502
503
504
505
				'user1',
			],
			[
				Filesystem::signal_delete_mount,
				'/mountpoint',
506
				IStorageConfig::MOUNT_TYPE_USER,
507
508
509
510
511
				'user2',
			],
			[
				Filesystem::signal_delete_mount,
				'/mountpoint',
512
				IStorageConfig::MOUNT_TYPE_GROUP,
513
514
515
516
517
				'group1',
			],
			[
				Filesystem::signal_delete_mount,
				'/mountpoint',
518
				IStorageConfig::MOUNT_TYPE_GROUP,
519
520
521
522
523
524
				'group2',
			],
			// creates new one
			[
				Filesystem::signal_create_mount,
				'/renamedMountpoint',
525
				IStorageConfig::MOUNT_TYPE_USER,
526
527
528
529
530
				'user1',
			],
			[
				Filesystem::signal_create_mount,
				'/renamedMountpoint',
531
				IStorageConfig::MOUNT_TYPE_USER,
532
533
534
535
536
				'user2',
			],
			[
				Filesystem::signal_create_mount,
				'/renamedMountpoint',
537
				IStorageConfig::MOUNT_TYPE_GROUP,
538
539
540
541
542
				'group1',
			],
			[
				Filesystem::signal_create_mount,
				'/renamedMountpoint',
543
				IStorageConfig::MOUNT_TYPE_GROUP,
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
				'group2',
			],
		];

		$this->assertCount(count($expectedCalls), self::$hookCalls);

		foreach ($expectedCalls as $index => $call) {
			$this->assertHookCall(
				self::$hookCalls[$index],
				$call[0],
				$call[1],
				$call[2],
				$call[3]
			);
		}
	}

	function hooksDeleteStorageDataProvider() {
		return [
			[
				['user1', 'user2'],
				['group1', 'group2'],
				// expected hook calls
				[
					[
						Filesystem::signal_delete_mount,
570
						IStorageConfig::MOUNT_TYPE_USER,
571
572
573
574
						'user1',
					],
					[
						Filesystem::signal_delete_mount,
575
						IStorageConfig::MOUNT_TYPE_USER,
576
577
578
579
						'user2',
					],
					[
						Filesystem::signal_delete_mount,
580
						IStorageConfig::MOUNT_TYPE_GROUP,
581
582
583
584
						'group1'
					],
					[
						Filesystem::signal_delete_mount,
585
						IStorageConfig::MOUNT_TYPE_GROUP,
586
587
588
589
590
591
592
593
594
595
596
						'group2'
					],
				],
			],
			[
				// deleting "all" entry
				[],
				[],
				[
					[
						Filesystem::signal_delete_mount,
597
						IStorageConfig::MOUNT_TYPE_USER,
598
599
600
601
602
603
604
605
606
607
608
609
610
						'all',
					],
				],
			],
		];
	}

	/**
	 * @dataProvider hooksDeleteStorageDataProvider
	 */
	public function testHooksDeleteStorage(
		$sourceApplicableUsers,
		$sourceApplicableGroups,
611
		$expectedCalls) {
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636

		$storage = $this->makeTestStorageData();
		$storage->setApplicableUsers($sourceApplicableUsers);
		$storage->setApplicableGroups($sourceApplicableGroups);
		$storage = $this->service->addStorage($storage);

		// reset calls
		self::$hookCalls = [];

		$this->service->removeStorage($storage->getId());

		$this->assertCount(count($expectedCalls), self::$hookCalls);

		foreach ($expectedCalls as $index => $call) {
			$this->assertHookCall(
				self::$hookCalls[$index],
				$call[0],
				'/mountpoint',
				$call[1],
				$call[2]
			);
		}
	}

}