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 ();
@@ -5233,4 +5246,76 @@ public function testPopulateTags(): void {
5233
5246
['file_source ' => 42 , 'x ' => 'y ' , 'tags ' => ['tag1 ' , 'tag2 ' ]],
5234
5247
], $ result );
5235
5248
}
5249
+
5250
+ public function trustedServerProvider (): array {
5251
+ return [
5252
+ 'Trusted server ' => [true , true ],
5253
+ 'Untrusted server ' => [false , false ],
5254
+ ];
5255
+ }
5256
+
5257
+ /**
5258
+ * @dataProvider trustedServerProvider
5259
+ */
5260
+ public function testFormatShareWithFederatedShare (bool $ isKnownServer , bool $ isTrusted ): void {
5261
+ $ nodeId = 12 ;
5262
+ $ nodePath = '/test.txt ' ;
5263
+ $ share = $ this ->createShare (
5264
+ 1 ,
5265
+ IShare::TYPE_REMOTE ,
5266
+ 'recipient@remoteserver.com ' , // shared with
5267
+ 'sender@testserver.com ' , // shared by
5268
+ 'shareOwner ' , // share owner
5269
+ $ nodePath , // path
5270
+ Constants::PERMISSION_READ ,
5271
+ time (),
5272
+ null ,
5273
+ null ,
5274
+ $ nodePath ,
5275
+ $ nodeId
5276
+ );
5277
+
5278
+ $ node = $ this ->createMock (\OCP \Files \File::class);
5279
+ $ node ->method ('getId ' )->willReturn ($ nodeId );
5280
+ $ node ->method ('getPath ' )->willReturn ($ nodePath );
5281
+ $ node ->method ('getInternalPath ' )->willReturn (ltrim ($ nodePath , '/ ' ));
5282
+ $ mountPoint = $ this ->createMock (\OCP \Files \Mount \IMountPoint::class);
5283
+ $ mountPoint ->method ('getMountType ' )->willReturn ('local ' );
5284
+ $ node ->method ('getMountPoint ' )->willReturn ($ mountPoint );
5285
+ $ node ->method ('getMimetype ' )->willReturn ('text/plain ' );
5286
+ $ storage = $ this ->createMock (\OCP \Files \Storage::class);
5287
+ $ storageCache = $ this ->createMock (\OCP \Cache \Cache::class);
5288
+ $ storageCache ->method ('getNumericStorageId ' )->willReturn (1 );
5289
+ $ storage ->method ('getCache ' )->willReturn ($ storageCache );
5290
+ $ storage ->method ('getId ' )->willReturn ('home::shareOwner ' );
5291
+ $ node ->method ('getStorage ' )->willReturn ($ storage );
5292
+ $ parent = $ this ->createMock (\OCP \Files \Folder::class);
5293
+ $ parent ->method ('getId ' )->willReturn (2 );
5294
+ $ node ->method ('getParent ' )->willReturn ($ parent );
5295
+ $ node ->method ('getSize ' )->willReturn (1234 );
5296
+ $ node ->method ('getMTime ' )->willReturn (1234567890 );
5297
+
5298
+ $ this ->previewManager ->method ('isAvailable ' )->with ($ node )->willReturn (false );
5299
+
5300
+ $ this ->rootFolder ->method ('getUserFolder ' )
5301
+ ->with ($ this ->currentUser )
5302
+ ->willReturnSelf ();
5303
+
5304
+ $ this ->rootFolder ->method ('getFirstNodeById ' )
5305
+ ->with ($ share ->getNodeId ())
5306
+ ->willReturn ($ node );
5307
+
5308
+ $ this ->rootFolder ->method ('getRelativePath ' )
5309
+ ->with ($ node ->getPath ())
5310
+ ->willReturnArgument (0 );
5311
+
5312
+ $ serverName = 'testserver.com ' ;
5313
+ $ this ->trustedServers ->method ('isTrustedServer ' )
5314
+ ->with ($ serverName )
5315
+ ->willReturn ($ isKnownServer );
5316
+
5317
+ $ result = $ this ->invokePrivate ($ this ->ocs , 'formatShare ' , [$ share ]);
5318
+
5319
+ $ this ->assertSame ($ isTrusted , $ result ['is_trusted_server ' ]);
5320
+ }
5236
5321
}
0 commit comments