Skip to content

Commit 43cf724

Browse files
committed
Authoritate share mounts test updates
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 6ec9d4b commit 43cf724

21 files changed

Lines changed: 1254 additions & 298 deletions

apps/files_sharing/tests/ApiTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,8 @@ public function testGetShareMultipleSharedFolder(): void {
826826
$share3->setStatus(IShare::STATUS_ACCEPTED);
827827
$this->shareManager->updateShare($share3);
828828

829+
$this->logout();
830+
829831
// $request = $this->createRequest(['path' => $this->subfolder]);
830832
$ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER2);
831833
$result1 = $ocs->getShares('false', 'false', 'false', $this->subfolder);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Files_Sharing\Tests\Listener;
10+
11+
use OCA\Files_Sharing\AppInfo\Application;
12+
use OCA\Files_Sharing\Config\ConfigLexicon;
13+
use OCA\Files_Sharing\Listener\UserHomeSetupListener;
14+
use OCA\Files_Sharing\ShareRecipientUpdater;
15+
use OCP\Config\IUserConfig;
16+
use OCP\Files\Events\UserHomeSetupEvent;
17+
use OCP\Files\Mount\IMountPoint;
18+
use OCP\IUser;
19+
use PHPUnit\Framework\MockObject\MockObject;
20+
use Test\Mock\Config\MockUserConfig;
21+
use Test\TestCase;
22+
23+
class UserHomeSetupListenerTest extends TestCase {
24+
private ShareRecipientUpdater&MockObject $updater;
25+
private IUserConfig $userConfig;
26+
private UserHomeSetupListener $listener;
27+
private IUser $user;
28+
29+
protected function setUp(): void {
30+
parent::setUp();
31+
32+
$this->updater = $this->createMock(ShareRecipientUpdater::class);
33+
$this->userConfig = new MockUserConfig([]);
34+
$this->listener = new UserHomeSetupListener($this->updater, $this->userConfig);
35+
$this->user = $this->createMock(IUser::class);
36+
$this->user->method('getUID')
37+
->willReturn('test');
38+
}
39+
40+
private function getEvent(): UserHomeSetupEvent {
41+
$homeMount = $this->createMock(IMountPoint::class);
42+
return new UserHomeSetupEvent($this->user, $homeMount);
43+
}
44+
45+
public function testClearNeedsUpdate(): void {
46+
$this->userConfig->setValueBool('test', Application::APP_ID, ConfigLexicon::USER_NEEDS_SHARE_REFRESH, true);
47+
$this->updater->expects($this->once())
48+
->method('updateForUser');
49+
50+
$this->listener->handle($this->getEvent());
51+
$this->assertFalse($this->userConfig->getValueBool('test', Application::APP_ID, ConfigLexicon::USER_NEEDS_SHARE_REFRESH, true));
52+
}
53+
54+
public function testNoUpdateIfNotNeeded(): void {
55+
$this->userConfig->setValueBool('test', Application::APP_ID, ConfigLexicon::USER_NEEDS_SHARE_REFRESH, false);
56+
$this->updater->expects($this->never())
57+
->method('updateForUser');
58+
59+
$this->listener->handle($this->getEvent());
60+
$this->assertFalse($this->userConfig->getValueBool('test', Application::APP_ID, ConfigLexicon::USER_NEEDS_SHARE_REFRESH, true));
61+
}
62+
}

apps/files_sharing/tests/Repair/CleanupShareTargetTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
namespace OCA\Files_Sharing\Tests\Repair;
88

9+
use OC\Files\Filesystem;
910
use OC\Migration\NullOutput;
1011
use OCA\Files_Sharing\Repair\CleanupShareTarget;
1112
use OCA\Files_Sharing\Tests\TestCase;
@@ -49,6 +50,7 @@ private function createUserShare(string $by, string $target = self::TEST_FOLDER_
4950

5051
$share->setTarget($target);
5152
$this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2);
53+
Filesystem::getMountManager()->moveMount('/' . self::TEST_FILES_SHARING_API_USER2 . '/files' . self::TEST_FOLDER_NAME . '/', '/' . self::TEST_FILES_SHARING_API_USER2 . '/files' . $target . '/');
5254

5355
$share = $this->shareManager->getShareById($share->getFullId());
5456
$this->assertEquals($target, $share->getTarget());
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: AGPL-3.0-only
6+
*/
7+
8+
namespace OCA\Files_Sharing\Tests;
9+
10+
use OCA\Files_Sharing\MountProvider;
11+
use OCA\Files_Sharing\ShareRecipientUpdater;
12+
use OCA\Files_Sharing\ShareTargetValidator;
13+
use OCP\Files\Cache\ICacheEntry;
14+
use OCP\Files\Config\ICachedMountInfo;
15+
use OCP\Files\Config\IUserMountCache;
16+
use OCP\Files\Mount\IMountPoint;
17+
use OCP\Files\Node;
18+
use OCP\Files\Storage\IStorageFactory;
19+
use OCP\IUser;
20+
use OCP\Share\IShare;
21+
use PHPUnit\Framework\MockObject\MockObject;
22+
use Test\Traits\UserTrait;
23+
24+
class ShareRecipientUpdaterTest extends \Test\TestCase {
25+
use UserTrait;
26+
27+
private IUserMountCache&MockObject $userMountCache;
28+
private MountProvider&MockObject $shareMountProvider;
29+
private ShareTargetValidator&MockObject $shareTargetValidator;
30+
private IStorageFactory&MockObject $storageFactory;
31+
private ShareRecipientUpdater $updater;
32+
33+
protected function setUp(): void {
34+
parent::setUp();
35+
36+
$this->userMountCache = $this->createMock(IUserMountCache::class);
37+
$this->shareMountProvider = $this->createMock(MountProvider::class);
38+
$this->shareTargetValidator = $this->createMock(ShareTargetValidator::class);
39+
$this->storageFactory = $this->createMock(IStorageFactory::class);
40+
41+
$this->updater = new ShareRecipientUpdater(
42+
$this->userMountCache,
43+
$this->shareMountProvider,
44+
$this->shareTargetValidator,
45+
$this->storageFactory,
46+
);
47+
}
48+
49+
public function testUpdateForShare() {
50+
$share = $this->createMock(IShare::class);
51+
$node = $this->createMock(Node::class);
52+
$cacheEntry = $this->createMock(ICacheEntry::class);
53+
$share->method('getNode')
54+
->willReturn($node);
55+
$node->method('getData')
56+
->willReturn($cacheEntry);
57+
$user1 = $this->createUser('user1', '');
58+
59+
$this->userMountCache->method('getMountsForUser')
60+
->with($user1)
61+
->willReturn([]);
62+
63+
$this->shareTargetValidator->method('verifyMountPoint')
64+
->with($user1, $share, fn ($path) => null, [$share])
65+
->willReturn('/new-target');
66+
67+
$this->userMountCache->expects($this->exactly(1))
68+
->method('addMount')
69+
->with($user1, '/user1/files/new-target/', $cacheEntry, MountProvider::class);
70+
71+
$this->updater->updateForAddedShare($user1, $share);
72+
}
73+
74+
/**
75+
* @param IUser $user
76+
* @param list<array{fileid: int, mount_point: string, provider: string}> $mounts
77+
* @return void
78+
*/
79+
private function setCachedMounts(IUser $user, array $mounts) {
80+
$cachedMounts = array_map(function (array $mount): ICachedMountInfo {
81+
$cachedMount = $this->createMock(ICachedMountInfo::class);
82+
$cachedMount->method('getRootId')
83+
->willReturn($mount['fileid']);
84+
$cachedMount->method('getMountPoint')
85+
->willReturn($mount['mount_point']);
86+
$cachedMount->method('getMountProvider')
87+
->willReturn($mount['provider']);
88+
return $cachedMount;
89+
}, $mounts);
90+
$mountKeys = array_map(function (array $mount): string {
91+
return $mount['fileid'] . '::' . $mount['mount_point'];
92+
}, $mounts);
93+
94+
$this->userMountCache->method('getMountsForUser')
95+
->with($user)
96+
->willReturn(array_combine($mountKeys, $cachedMounts));
97+
}
98+
99+
public function testUpdateForUserAddedNoExisting() {
100+
$share = $this->createMock(IShare::class);
101+
$share->method('getTarget')
102+
->willReturn('/target');
103+
$share->method('getNodeId')
104+
->willReturn(111);
105+
$user1 = $this->createUser('user1', '');
106+
$newMount = $this->createMock(IMountPoint::class);
107+
108+
$this->shareMountProvider->method('getSuperSharesForUser')
109+
->with($user1, [])
110+
->willReturn([[
111+
$share,
112+
[$share],
113+
]]);
114+
115+
$this->shareMountProvider->method('getMountsFromSuperShares')
116+
->with($user1, [[
117+
$share,
118+
[$share],
119+
]], $this->storageFactory)
120+
->willReturn([$newMount]);
121+
122+
$this->setCachedMounts($user1, []);
123+
124+
$this->shareTargetValidator->method('verifyMountPoint')
125+
->with($user1, $share, fn ($path) => null, [$share])
126+
->willReturn('/new-target');
127+
128+
$this->userMountCache->expects($this->exactly(1))
129+
->method('registerMounts')
130+
->with($user1, [$newMount], [MountProvider::class]);
131+
132+
$this->updater->updateForUser($user1);
133+
}
134+
135+
public function testUpdateForUserNoChanges() {
136+
$share = $this->createMock(IShare::class);
137+
$share->method('getTarget')
138+
->willReturn('/target');
139+
$share->method('getNodeId')
140+
->willReturn(111);
141+
$user1 = $this->createUser('user1', '');
142+
143+
$this->shareMountProvider->method('getSuperSharesForUser')
144+
->with($user1, [])
145+
->willReturn([[
146+
$share,
147+
[$share],
148+
]]);
149+
150+
$this->setCachedMounts($user1, [
151+
['fileid' => 111, 'mount_point' => '/user1/files/target/', 'provider' => MountProvider::class],
152+
]);
153+
154+
$this->shareTargetValidator->expects($this->never())
155+
->method('verifyMountPoint');
156+
157+
$this->userMountCache->expects($this->never())
158+
->method('registerMounts');
159+
160+
$this->updater->updateForUser($user1);
161+
}
162+
163+
public function testUpdateForUserRemoved() {
164+
$share = $this->createMock(IShare::class);
165+
$share->method('getTarget')
166+
->willReturn('/target');
167+
$share->method('getNodeId')
168+
->willReturn(111);
169+
$user1 = $this->createUser('user1', '');
170+
171+
$this->shareMountProvider->method('getSuperSharesForUser')
172+
->with($user1, [])
173+
->willReturn([]);
174+
175+
$this->setCachedMounts($user1, [
176+
['fileid' => 111, 'mount_point' => '/user1/files/target/', 'provider' => MountProvider::class],
177+
]);
178+
179+
$this->shareTargetValidator->expects($this->never())
180+
->method('verifyMountPoint');
181+
182+
$this->userMountCache->expects($this->exactly(1))
183+
->method('registerMounts')
184+
->with($user1, [], [MountProvider::class]);
185+
186+
$this->updater->updateForUser($user1);
187+
}
188+
189+
public function testDeletedShare() {
190+
$share = $this->createMock(IShare::class);
191+
$share->method('getTarget')
192+
->willReturn('/target');
193+
$share->method('getNodeId')
194+
->willReturn(111);
195+
$user1 = $this->createUser('user1', '');
196+
197+
$this->shareTargetValidator->expects($this->never())
198+
->method('verifyMountPoint');
199+
200+
$this->userMountCache->expects($this->exactly(1))
201+
->method('removeMount')
202+
->with('/user1/files/target/');
203+
204+
$this->updater->updateForDeletedShare($user1, $share);
205+
}
206+
}

0 commit comments

Comments
 (0)