44
44
use OCP \Share \IProviderFactory ;
45
45
use OCP \Share \IShare ;
46
46
use OCP \UserStatus \IManager as IUserStatusManager ;
47
+ use OCA \Federation \TrustedServers ;
47
48
use PHPUnit \Framework \MockObject \MockObject ;
48
49
use Psr \Container \ContainerInterface ;
49
50
use Psr \Log \LoggerInterface ;
@@ -79,6 +80,7 @@ class ShareAPIControllerTest extends TestCase {
79
80
private IProviderFactory &MockObject $ factory ;
80
81
private IMailer &MockObject $ mailer ;
81
82
private ITagManager &MockObject $ tagManager ;
83
+ private TrustedServers &MockObject $ trustedServers ;
82
84
83
85
protected function setUp (): void {
84
86
$ this ->shareManager = $ this ->createMock (IManager::class);
@@ -115,6 +117,7 @@ protected function setUp(): void {
115
117
$ this ->factory = $ this ->createMock (IProviderFactory::class);
116
118
$ this ->mailer = $ this ->createMock (IMailer::class);
117
119
$ this ->tagManager = $ this ->createMock (ITagManager::class);
120
+ $ this ->trustedServers = $ this ->createMock (TrustedServers::class);
118
121
119
122
$ this ->ocs = new ShareAPIController (
120
123
$ this ->appName ,
@@ -135,8 +138,10 @@ protected function setUp(): void {
135
138
$ this ->factory ,
136
139
$ this ->mailer ,
137
140
$ this ->tagManager ,
138
- $ this ->currentUser ,
141
+ $ this ->trustedServers ,
142
+ $ this ->currentUser
139
143
);
144
+
140
145
}
141
146
142
147
/**
@@ -163,6 +168,7 @@ private function mockFormatShare() {
163
168
$ this ->factory ,
164
169
$ this ->mailer ,
165
170
$ this ->tagManager ,
171
+ $ this ->trustedServers ,
166
172
$ this ->currentUser ,
167
173
])->onlyMethods (['formatShare ' ])
168
174
->getMock ();
@@ -848,6 +854,7 @@ public function testGetShare(IShare $share, array $result): void {
848
854
$ this ->factory ,
849
855
$ this ->mailer ,
850
856
$ this ->tagManager ,
857
+ $ this ->trustedServers ,
851
858
$ this ->currentUser ,
852
859
])
853
860
->onlyMethods (['canAccessShare ' ])
@@ -1481,6 +1488,7 @@ public function testGetShares(array $getSharesParameters, array $shares, array $
1481
1488
$ this ->factory ,
1482
1489
$ this ->mailer ,
1483
1490
$ this ->tagManager ,
1491
+ $ this ->trustedServers ,
1484
1492
$ this ->currentUser ,
1485
1493
])
1486
1494
->onlyMethods (['formatShare ' ])
@@ -1872,6 +1880,7 @@ public function testCreateShareUser(): void {
1872
1880
$ this ->factory ,
1873
1881
$ this ->mailer ,
1874
1882
$ this ->tagManager ,
1883
+ $ this ->trustedServers ,
1875
1884
$ this ->currentUser ,
1876
1885
])->onlyMethods (['formatShare ' ])
1877
1886
->getMock ();
@@ -1970,6 +1979,7 @@ public function testCreateShareGroup(): void {
1970
1979
$ this ->factory ,
1971
1980
$ this ->mailer ,
1972
1981
$ this ->tagManager ,
1982
+ $ this ->trustedServers ,
1973
1983
$ this ->currentUser ,
1974
1984
])->onlyMethods (['formatShare ' ])
1975
1985
->getMock ();
@@ -2396,6 +2406,7 @@ public function testCreateShareRemote(): void {
2396
2406
$ this ->factory ,
2397
2407
$ this ->mailer ,
2398
2408
$ this ->tagManager ,
2409
+ $ this ->trustedServers ,
2399
2410
$ this ->currentUser ,
2400
2411
])->onlyMethods (['formatShare ' ])
2401
2412
->getMock ();
@@ -2467,6 +2478,7 @@ public function testCreateShareRemoteGroup(): void {
2467
2478
$ this ->factory ,
2468
2479
$ this ->mailer ,
2469
2480
$ this ->tagManager ,
2481
+ $ this ->trustedServers ,
2470
2482
$ this ->currentUser ,
2471
2483
])->onlyMethods (['formatShare ' ])
2472
2484
->getMock ();
@@ -2705,6 +2717,7 @@ public function testCreateReshareOfFederatedMountNoDeletePermissions(): void {
2705
2717
$ this ->factory ,
2706
2718
$ this ->mailer ,
2707
2719
$ this ->tagManager ,
2720
+ $ this ->trustedServers ,
2708
2721
$ this ->currentUser ,
2709
2722
])->onlyMethods (['formatShare ' ])
2710
2723
->getMock ();
@@ -4497,6 +4510,7 @@ public function dataFormatShare() {
4497
4510
'mount-type ' => '' ,
4498
4511
'attributes ' => null ,
4499
4512
'item_permissions ' => 1 ,
4513
+ 'is_trusted_server ' => false ,
4500
4514
], $ share , [], false
4501
4515
];
4502
4516
@@ -4550,6 +4564,7 @@ public function dataFormatShare() {
4550
4564
'mount-type ' => '' ,
4551
4565
'attributes ' => null ,
4552
4566
'item_permissions ' => 1 ,
4567
+ 'is_trusted_server ' => false ,
4553
4568
], $ share , [], false
4554
4569
];
4555
4570
@@ -5233,4 +5248,138 @@ public function testPopulateTags(): void {
5233
5248
['file_source ' => 42 , 'x ' => 'y ' , 'tags ' => ['tag1 ' , 'tag2 ' ]],
5234
5249
], $ result );
5235
5250
}
5251
+
5252
+ public function trustedServerProvider (): array {
5253
+ return [
5254
+ 'Trusted server ' => [true , true ],
5255
+ 'Untrusted server ' => [false , false ],
5256
+ ];
5257
+ }
5258
+
5259
+ /**
5260
+ * @dataProvider trustedServerProvider
5261
+ */
5262
+ public function testFormatShareWithFederatedShare (bool $ isKnownServer , bool $ isTrusted ): void {
5263
+ $ nodeId = 12 ;
5264
+ $ nodePath = '/test.txt ' ;
5265
+ $ share = $ this ->createShare (
5266
+ 1 ,
5267
+ IShare::TYPE_REMOTE ,
5268
+ 'recipient@remoteserver.com ' , // shared with
5269
+ 'sender@testserver.com ' , // shared by
5270
+ 'shareOwner ' , // share owner
5271
+ $ nodePath , // path
5272
+ Constants::PERMISSION_READ ,
5273
+ time (),
5274
+ null ,
5275
+ null ,
5276
+ $ nodePath ,
5277
+ $ nodeId
5278
+ );
5279
+
5280
+ $ node = $ this ->createMock (\OCP \Files \File::class);
5281
+ $ node ->method ('getId ' )->willReturn ($ nodeId );
5282
+ $ node ->method ('getPath ' )->willReturn ($ nodePath );
5283
+ $ node ->method ('getInternalPath ' )->willReturn (ltrim ($ nodePath , '/ ' ));
5284
+ $ mountPoint = $ this ->createMock (\OCP \Files \Mount \IMountPoint::class);
5285
+ $ mountPoint ->method ('getMountType ' )->willReturn ('local ' );
5286
+ $ node ->method ('getMountPoint ' )->willReturn ($ mountPoint );
5287
+ $ node ->method ('getMimetype ' )->willReturn ('text/plain ' );
5288
+ $ storage = $ this ->createMock (\OCP \Files \Storage \IStorage::class);
5289
+ $ storageCache = $ this ->createMock (\OCP \Files \Cache \ICache::class);
5290
+ $ storageCache ->method ('getNumericStorageId ' )->willReturn (1 );
5291
+ $ storage ->method ('getCache ' )->willReturn ($ storageCache );
5292
+ $ storage ->method ('getId ' )->willReturn ('home::shareOwner ' );
5293
+ $ node ->method ('getStorage ' )->willReturn ($ storage );
5294
+ $ parent = $ this ->createMock (\OCP \Files \Folder::class);
5295
+ $ parent ->method ('getId ' )->willReturn (2 );
5296
+ $ node ->method ('getParent ' )->willReturn ($ parent );
5297
+ $ node ->method ('getSize ' )->willReturn (1234 );
5298
+ $ node ->method ('getMTime ' )->willReturn (1234567890 );
5299
+
5300
+ $ this ->previewManager ->method ('isAvailable ' )->with ($ node )->willReturn (false );
5301
+
5302
+ $ this ->rootFolder ->method ('getUserFolder ' )
5303
+ ->with ($ this ->currentUser )
5304
+ ->willReturnSelf ();
5305
+
5306
+ $ this ->rootFolder ->method ('getFirstNodeById ' )
5307
+ ->with ($ share ->getNodeId ())
5308
+ ->willReturn ($ node );
5309
+
5310
+ $ this ->rootFolder ->method ('getRelativePath ' )
5311
+ ->with ($ node ->getPath ())
5312
+ ->willReturnArgument (0 );
5313
+
5314
+ $ serverName = 'remoteserver.com ' ;
5315
+ $ this ->trustedServers ->method ('isTrustedServer ' )
5316
+ ->with ($ serverName )
5317
+ ->willReturn ($ isKnownServer );
5318
+
5319
+ $ result = $ this ->invokePrivate ($ this ->ocs , 'formatShare ' , [$ share ]);
5320
+
5321
+ $ this ->assertSame ($ isTrusted , $ result ['is_trusted_server ' ]);
5322
+ }
5323
+
5324
+ public function testFormatShareWithFederatedShareWithAtInUsername (): void {
5325
+ $ nodeId = 12 ;
5326
+ $ nodePath = '/test.txt ' ;
5327
+ $ share = $ this ->createShare (
5328
+ 1 ,
5329
+ IShare::TYPE_REMOTE ,
5330
+ 'recipient@domain.com@remoteserver.com ' , // shared with
5331
+ 'sender@testserver.com ' , // shared by
5332
+ 'shareOwner ' , // share owner
5333
+ $ nodePath , // path
5334
+ Constants::PERMISSION_READ ,
5335
+ time (),
5336
+ null ,
5337
+ null ,
5338
+ $ nodePath ,
5339
+ $ nodeId
5340
+ );
5341
+
5342
+ $ node = $ this ->createMock (\OCP \Files \File::class);
5343
+ $ node ->method ('getId ' )->willReturn ($ nodeId );
5344
+ $ node ->method ('getPath ' )->willReturn ($ nodePath );
5345
+ $ node ->method ('getInternalPath ' )->willReturn (ltrim ($ nodePath , '/ ' ));
5346
+ $ mountPoint = $ this ->createMock (\OCP \Files \Mount \IMountPoint::class);
5347
+ $ mountPoint ->method ('getMountType ' )->willReturn ('local ' );
5348
+ $ node ->method ('getMountPoint ' )->willReturn ($ mountPoint );
5349
+ $ node ->method ('getMimetype ' )->willReturn ('text/plain ' );
5350
+ $ storage = $ this ->createMock (\OCP \Files \Storage \IStorage::class);
5351
+ $ storageCache = $ this ->createMock (\OCP \Files \Cache \ICache::class);
5352
+ $ storageCache ->method ('getNumericStorageId ' )->willReturn (1 );
5353
+ $ storage ->method ('getCache ' )->willReturn ($ storageCache );
5354
+ $ storage ->method ('getId ' )->willReturn ('home::shareOwner ' );
5355
+ $ node ->method ('getStorage ' )->willReturn ($ storage );
5356
+ $ parent = $ this ->createMock (\OCP \Files \Folder::class);
5357
+ $ parent ->method ('getId ' )->willReturn (2 );
5358
+ $ node ->method ('getParent ' )->willReturn ($ parent );
5359
+ $ node ->method ('getSize ' )->willReturn (1234 );
5360
+ $ node ->method ('getMTime ' )->willReturn (1234567890 );
5361
+
5362
+ $ this ->previewManager ->method ('isAvailable ' )->with ($ node )->willReturn (false );
5363
+
5364
+ $ this ->rootFolder ->method ('getUserFolder ' )
5365
+ ->with ($ this ->currentUser )
5366
+ ->willReturnSelf ();
5367
+
5368
+ $ this ->rootFolder ->method ('getFirstNodeById ' )
5369
+ ->with ($ share ->getNodeId ())
5370
+ ->willReturn ($ node );
5371
+
5372
+ $ this ->rootFolder ->method ('getRelativePath ' )
5373
+ ->with ($ node ->getPath ())
5374
+ ->willReturnArgument (0 );
5375
+
5376
+ $ serverName = 'remoteserver.com ' ;
5377
+ $ this ->trustedServers ->method ('isTrustedServer ' )
5378
+ ->with ($ serverName )
5379
+ ->willReturn (true );
5380
+
5381
+ $ result = $ this ->invokePrivate ($ this ->ocs , 'formatShare ' , [$ share ]);
5382
+
5383
+ $ this ->assertTrue ($ result ['is_trusted_server ' ]);
5384
+ }
5236
5385
}
0 commit comments