Skip to content

Commit f5d8004

Browse files
committed
tests(ocm): Update tests for capabilities and permissions
Signed-off-by: Micke Nordin <kano@sunet.se>
1 parent 9711f2a commit f5d8004

6 files changed

Lines changed: 142 additions & 16 deletions

File tree

‎apps/federatedfilesharing/tests/Controller/RequestHandlerControllerTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OCA\FederatedFileSharing\Controller\RequestHandlerController;
1313
use OCA\FederatedFileSharing\FederatedShareProvider;
1414
use OCP\AppFramework\Http\DataResponse;
15+
use OCP\Constants;
1516
use OCP\EventDispatcher\IEventDispatcher;
1617
use OCP\Federation\ICloudFederationFactory;
1718
use OCP\Federation\ICloudFederationProvider;
@@ -115,6 +116,19 @@ public function testCreateShare(): void {
115116
$this->assertInstanceOf(DataResponse::class, $result);
116117
}
117118

119+
public function testUpdatePermissions(): void {
120+
$this->cloudFederationProviderManager->expects($this->once())
121+
->method('getCloudFederationProvider')->with('file')->willReturn($this->cloudFederationProvider);
122+
$this->cloudFederationProvider->expects($this->once())
123+
->method('notificationReceived')
124+
->with('RESHARE_CHANGE_PERMISSION', '42', [
125+
'sharedSecret' => 'token',
126+
'permission' => ['share', 'read', 'write'],
127+
]);
128+
129+
$this->assertInstanceOf(DataResponse::class, $this->requestHandler->updatePermissions('42', 'token', Constants::PERMISSION_ALL));
130+
}
131+
118132
public function testDeclineShare(): void {
119133
$id = '42';
120134

‎apps/federatedfilesharing/tests/FederatedShareProviderTest.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ public function testCreate(?\DateTime $expirationDate, ?string $expectedDataDate
154154
'shareOwner',
155155
'shareOwner@http://localhost',
156156
'sharedBy',
157-
'sharedBy@http://localhost'
157+
'sharedBy@http://localhost',
158+
IShare::TYPE_REMOTE,
159+
19
158160
)
159161
->willReturn(true);
160162

‎apps/federatedfilesharing/tests/NotificationsTest.php‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
use OCA\FederatedFileSharing\BackgroundJob\RetryJob;
1414
use OCA\FederatedFileSharing\Notifications;
1515
use OCP\BackgroundJob\IJobList;
16+
use OCP\Constants;
1617
use OCP\EventDispatcher\IEventDispatcher;
1718
use OCP\Federation\ICloudFederationFactory;
1819
use OCP\Federation\ICloudFederationProviderManager;
20+
use OCP\Federation\ICloudFederationShare;
1921
use OCP\Http\Client\IClientService;
2022
use OCP\OCS\IDiscoveryService;
23+
use OCP\Share\IShare;
2124
use PHPUnit\Framework\MockObject\MockObject;
2225
use Psr\Log\LoggerInterface;
2326

@@ -78,6 +81,61 @@ private function getInstance(array $mockedMethods = []) {
7881
->getMock();
7982
}
8083

84+
public function testSendRemoteSharePermissions(): void {
85+
$permissions = Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE;
86+
$this->addressHandler->method('splitUserRemote')->willReturn(['recipient', 'https://remote.example']);
87+
$this->addressHandler->method('generateRemoteURL')->willReturn('https://local.example');
88+
$this->addressHandler->method('urlContainProtocol')->willReturn(true);
89+
$share = $this->createMock(ICloudFederationShare::class);
90+
$this->cloudFederationFactory->expects($this->once())
91+
->method('getCloudFederationShare')
92+
->with(
93+
'recipient@https://remote.example',
94+
'document.odt',
95+
'',
96+
'123',
97+
'owner@local.example',
98+
'Owner',
99+
'owner@local.example',
100+
'Owner',
101+
'share-secret',
102+
IShare::TYPE_REMOTE,
103+
'file',
104+
$permissions,
105+
)
106+
->willReturn($share);
107+
$this->cloudFederationProviderManager->expects($this->once())
108+
->method('sendShare')->with($share)->willReturn([]);
109+
$this->httpClientService->expects($this->never())->method('newClient');
110+
111+
$this->assertTrue($this->getInstance()->sendRemoteShare(
112+
'share-secret', 'recipient@remote.example', 'document.odt', '123',
113+
'Owner', 'owner@local.example', 'Owner', 'owner@local.example',
114+
IShare::TYPE_REMOTE, $permissions,
115+
));
116+
}
117+
118+
public static function legacyPermissionsProvider(): array {
119+
return [
120+
'new share' => ['share', ['token' => 'share-secret']],
121+
'permission update' => ['permissions', ['token' => 'share-secret', 'permissions' => Constants::PERMISSION_READ]],
122+
];
123+
}
124+
125+
#[\PHPUnit\Framework\Attributes\DataProvider('legacyPermissionsProvider')]
126+
public function testLegacyPermissions(string $action, array $expectedFields): void {
127+
$this->addressHandler->method('urlContainProtocol')->willReturn(true);
128+
$fields = ['token' => 'share-secret', 'permissions' => Constants::PERMISSION_READ];
129+
$result = ['success' => true, 'result' => ''];
130+
$instance = $this->getInstance(['tryOCMEndPoint', 'tryLegacyEndPoint']);
131+
$instance->expects($this->once())->method('tryOCMEndPoint')
132+
->with('https://remote.example', $fields, $action)->willReturn(false);
133+
$instance->expects($this->once())->method('tryLegacyEndPoint')
134+
->with('https://remote.example', '', $expectedFields)->willReturn($result);
135+
136+
$this->assertSame($result, $this->invokePrivate($instance, 'tryHttpPostToShareEndpoint', ['https://remote.example', '', $fields, $action]));
137+
}
138+
81139
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'dataTestSendUpdateToRemote')]
82140
public function testSendUpdateToRemote(int $try, array $httpRequestResult, bool $expected): void {
83141
$remote = 'http://remote';

‎tests/lib/Federation/CloudFederationFactoryTest.php‎

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use OC\Federation\CloudFederationFactory;
1313
use OC\OCM\Model\OCMProvider;
14+
use OCP\Constants;
1415
use OCP\Federation\ICloudFederationShare;
1516
use OCP\Federation\ICloudId;
1617
use OCP\Federation\ICloudIdManager;
@@ -58,8 +59,24 @@ protected function setUp(): void {
5859
);
5960
}
6061

61-
public function testExchangeTokenUsesLocalWebDavUri(): void {
62-
$this->remoteProvider->setCapabilities(['exchange-token']);
62+
public static function protocolObjectProvider(): array {
63+
return [
64+
'default permissions' => [['protocol-object'], null, ['read']],
65+
'no permissions' => [['protocol-object'], 0, []],
66+
'read' => [['protocol-object'], Constants::PERMISSION_READ, ['read']],
67+
'share' => [['protocol-object'], Constants::PERMISSION_SHARE, ['share']],
68+
'create' => [['protocol-object'], Constants::PERMISSION_CREATE, ['write']],
69+
'update' => [['protocol-object'], Constants::PERMISSION_UPDATE, ['write']],
70+
'create and update' => [['protocol-object'], Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE, ['write']],
71+
'delete only' => [['protocol-object'], Constants::PERMISSION_DELETE, []],
72+
'all permissions' => [['protocol-object'], Constants::PERMISSION_ALL, ['share', 'read', 'write']],
73+
'with token exchange' => [['protocol-object', 'exchange-token'], Constants::PERMISSION_READ, ['read']],
74+
];
75+
}
76+
77+
#[DataProvider('protocolObjectProvider')]
78+
public function testProtocolObjectUsesLocalWebDavUri(array $capabilities, ?int $permissions, array $expectedPermissions): void {
79+
$this->remoteProvider->setCapabilities($capabilities);
6380
$localProvider = new OCMProvider();
6481
$resource = $localProvider->createNewResourceType();
6582
$resource->setName('file')->setProtocols(['webdav' => '/public.php/webdav/']);
@@ -74,14 +91,14 @@ public function testExchangeTokenUsesLocalWebDavUri(): void {
7491
->willReturn('https://local.example/nextcloud/public.php/webdav/');
7592
$this->logger->expects($this->never())->method('warning');
7693

77-
$share = $this->createShare();
94+
$share = $this->createShare($permissions);
7895

7996
$this->assertSame([
8097
'name' => 'webdav',
8198
'webdav' => [
8299
'uri' => 'https://local.example/nextcloud/public.php/webdav/',
83100
'sharedSecret' => 'share-secret',
84-
'permissions' => ['{http://open-cloud-mesh.org/ns}share-permissions'],
101+
'permissions' => $expectedPermissions,
85102
],
86103
], $share->getProtocol());
87104
$this->assertSame('share-secret', $share->getShareSecret());
@@ -96,7 +113,7 @@ public static function missingLocalWebDavProvider(): array {
96113

97114
#[DataProvider('missingLocalWebDavProvider')]
98115
public function testMissingLocalWebDavUsesLegacyShare(bool $hasFileResource, string $exceptionMessage): void {
99-
$this->remoteProvider->setCapabilities(['exchange-token']);
116+
$this->remoteProvider->setCapabilities(['protocol-object']);
100117
$localProvider = new OCMProvider();
101118
if ($hasFileResource) {
102119
$resource = $localProvider->createNewResourceType();
@@ -117,17 +134,26 @@ public function testMissingLocalWebDavUsesLegacyShare(bool $hasFileResource, str
117134
$this->assertLegacyShare($this->createShare());
118135
}
119136

120-
public function testLegacyShareDoesNotQueryLocalDiscovery(): void {
121-
$this->remoteProvider->setCapabilities(['shares']);
137+
public static function legacyCapabilitiesProvider(): array {
138+
return [
139+
'no capabilities' => [[]],
140+
'shares only' => [['shares']],
141+
'token exchange only' => [['exchange-token']],
142+
];
143+
}
144+
145+
#[DataProvider('legacyCapabilitiesProvider')]
146+
public function testLegacyShareDoesNotQueryLocalDiscovery(array $capabilities): void {
147+
$this->remoteProvider->setCapabilities($capabilities);
122148
$this->discoveryService->expects($this->never())->method('getLocalOCMProvider');
123149
$this->urlGenerator->expects($this->never())->method('getAbsoluteURL');
124150
$this->logger->expects($this->never())->method('warning');
125151

126-
$this->assertLegacyShare($this->createShare());
152+
$this->assertLegacyShare($this->createShare(Constants::PERMISSION_ALL));
127153
}
128154

129-
private function createShare(): ICloudFederationShare {
130-
return $this->factory->getCloudFederationShare(
155+
private function createShare(?int $permissions = null): ICloudFederationShare {
156+
$arguments = [
131157
'recipient@remote.example',
132158
'document.odt',
133159
'',
@@ -139,7 +165,11 @@ private function createShare(): ICloudFederationShare {
139165
'share-secret',
140166
'user',
141167
'file',
142-
);
168+
];
169+
if ($permissions !== null) {
170+
$arguments[] = $permissions;
171+
}
172+
return $this->factory->getCloudFederationShare(...$arguments);
143173
}
144174

145175
private function assertLegacyShare(ICloudFederationShare $share): void {

‎tests/lib/OCM/DiscoveryServiceTest.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,11 @@ public function testOCMRequest(string $path, int $expectedStatus, ?array $expect
124124
}
125125
}
126126

127-
public function testLocalBaseCapability(): void {
128-
$local = $this->discoveryService->getLocalOCMProvider();
129-
$this->assertEmpty(array_diff(['notifications', 'shares'], $local->getCapabilities()->toArray()));
127+
#[\PHPUnit\Framework\Attributes\TestWith([true])]
128+
#[\PHPUnit\Framework\Attributes\TestWith([false])]
129+
public function testLocalBaseCapability(bool $fullDetails): void {
130+
$local = $this->discoveryService->getLocalOCMProvider($fullDetails);
131+
$this->assertEmpty(array_diff(['notifications', 'shares', 'exchange-token', 'protocol-object'], $local->getCapabilities()->toArray()));
130132
}
131133

132134
public function testLocalCapabilitiesAdvertiseHttpSigByDefault(): void {
@@ -151,7 +153,7 @@ public function testLocalAddedCapability(): void {
151153
$this->context->for('ocm-capability-app')->registerEventListener(LocalOCMDiscoveryEvent::class, LocalOCMDiscoveryTestEvent::class);
152154
$this->context->delegateEventListenerRegistrations($this->dispatcher);
153155
$local = $this->discoveryService->getLocalOCMProvider();
154-
$this->assertEmpty(array_diff(['notifications', 'shares', 'ocm-capability-test'], $local->getCapabilities()->toArray()));
156+
$this->assertEmpty(array_diff(['notifications', 'shares', 'exchange-token', 'protocol-object', 'ocm-capability-test'], $local->getCapabilities()->toArray()));
155157
}
156158

157159
}

‎tests/lib/OCM/OCMProviderTest.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@ public function testAddResourceTypeMergeOverwritesSameProtocol(): void {
7070
);
7171
}
7272

73+
public static function invitationCapabilitiesProvider(): array {
74+
return [
75+
'standard' => [['invites'], true],
76+
'legacy' => [['invite-accepted'], true],
77+
'both' => [['invites', 'invite-accepted'], true],
78+
'none' => [[], false],
79+
'unrelated' => [['shares'], false],
80+
];
81+
}
82+
83+
#[\PHPUnit\Framework\Attributes\DataProvider('invitationCapabilitiesProvider')]
84+
public function testInvitationCapabilities(array $capabilities, bool $expected): void {
85+
$this->provider->setCapabilities($capabilities);
86+
$actual = $this->provider->getCapabilities();
87+
88+
$this->assertSame($expected, $actual->hasInvites());
89+
$this->assertSame($expected, $actual->hasInviteAccepted());
90+
$this->assertSame($capabilities, $actual->toArray());
91+
}
92+
7393
public function testJwksUriImportedFromDiscoveryData(): void {
7494
$this->provider->import([
7595
'enabled' => true,

0 commit comments

Comments
 (0)