Skip to content

Commit

Permalink
added code for getting current oCis version (#229)
Browse files Browse the repository at this point in the history
* added code for getting current oCis version

* added code to handle exception if no endpoint found

* addressing reviews

* updated code and drone env for master and stable

* adding guzzle mock for getting ocis version

* adding test for stable ocis for sharing drive via root
  • Loading branch information
S-Panta authored Jul 5, 2024
1 parent f4f6787 commit 01e96aa
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .drone.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# The version of OCIS to use in pipelines that test against OCIS
OCIS_COMMITID=c18d71788351ac878d2016b178e3e87c673e62a3
OCIS_COMMITID=aa6041abb6e8094306216fbe354403df14b47c70
OCIS_BRANCH=master
OCIS_STABLE_COMMITID=48ab941943a9eb3b510b7029ba39ba445a6aac1b
OCIS_STABLE_COMMITID=214491134f5674ac735635ea259969663d9ad4c7
OCIS_STABLE_BRANCH=stable-5.0
10 changes: 9 additions & 1 deletion src/Drive.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OpenAPI\Client\Model\OdataError;
use OpenAPI\Client\Model\Quota;
use Owncloud\OcisPhpSdk\Exception\BadRequestException;
use Owncloud\OcisPhpSdk\Exception\EndPointNotImplementedException;
use Owncloud\OcisPhpSdk\Exception\ExceptionHelper;
use Owncloud\OcisPhpSdk\Exception\ForbiddenException;
use Owncloud\OcisPhpSdk\Exception\HttpException;
Expand Down Expand Up @@ -42,6 +43,7 @@ class Drive
private array $connectionConfig;
private Configuration $graphApiConfig;
private string $serviceUrl;
private string $ocisVersion;

/**
* @ignore The developer using the SDK does not need to create drives manually, but should use the Ocis class
Expand All @@ -53,11 +55,13 @@ public function __construct(
ApiDrive $apiDrive,
array $connectionConfig,
string $serviceUrl,
string &$accessToken
string &$accessToken,
string $ocisVersion
) {
$this->apiDrive = $apiDrive;
$this->accessToken = &$accessToken;
$this->serviceUrl = $serviceUrl;
$this->ocisVersion = $ocisVersion;
if (!Ocis::isConnectionConfigValid($connectionConfig)) {
throw new \InvalidArgumentException('connection configuration not valid');
}
Expand Down Expand Up @@ -583,9 +587,13 @@ public function emptyTrashbin(): bool
* @throws UnauthorizedException
* @throws InvalidResponseException
* @throws InternalServerErrorException
* @throws EndPointNotImplementedException
*/
public function getRoles(): array
{
if((version_compare($this->ocisVersion, '6.0.0', '<'))) {
throw new EndPointNotImplementedException(Ocis::ENDPOINT_NOT_IMPLEMENTED_ERROR_MESSAGE);
};
$guzzle = new Client(
Ocis::createGuzzleConfig($this->connectionConfig, $this->accessToken)
);
Expand Down
10 changes: 10 additions & 0 deletions src/Exception/EndPointNotImplementedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Owncloud\OcisPhpSdk\Exception;

/**
* Exception for endpoint that are not available in Ocis version
*/
class EndPointNotImplementedException extends \Exception
{
}
43 changes: 38 additions & 5 deletions src/Ocis.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class Ocis
private const DECODE_TOKEN_ERROR_MESSAGE = 'Could not decode token.';
public const FUNCTION_NOT_IMPLEMENTED_YET_ERROR_MESSAGE =
'This function is not implemented yet! Place, name and signature of the function might change!';
public const ENDPOINT_NOT_IMPLEMENTED_ERROR_MESSAGE =
'This method is not implemented in this ocis version';
private string $serviceUrl;
private string $accessToken;
private Configuration $graphApiConfig;
Expand All @@ -63,6 +65,7 @@ class Ocis
* @phpstan-var ConnectionConfig
*/
private array $connectionConfig;
private string $ocisVersion = '';

/**
* @phpstan-param ConnectionConfig $connectionConfig
Expand Down Expand Up @@ -111,7 +114,6 @@ public function __construct(
$this->graphApiConfig = Configuration::getDefaultConfiguration()->setHost(
$this->serviceUrl . '/graph'
);

}

public function getServiceUrl(): string
Expand Down Expand Up @@ -306,6 +308,33 @@ private function getServiceUrlFromWebfinger(string $webfingerUrl): string
}
throw new InvalidResponseException('invalid webfinger response');
}

/**
* returns the current oCIS version in semantic versioning format ( e.g. "5.0.5" )
*
* @return string
* @throws InvalidResponseException
*/
public function getOcisVersion(): string
{
if(($this->ocisVersion)) {
return $this->ocisVersion;
} else {
$response = $this->guzzle->get($this->serviceUrl . '/ocs/v1.php/cloud/capabilities');
$responseContent = $response->getBody()->getContents();

$body = simplexml_load_string($responseContent);
if (!isset($body->data->version->productversion)) {
throw new InvalidResponseException('Missing productversion element in XML response');
}
$version = (string)$body->data->version->productversion;
$pattern = '(\d\.\d\.\d)';
preg_match($pattern, $version, $matches);
$this->ocisVersion = $matches[0];
return $this->ocisVersion;
}
}

/**
* Get all available drives
*
Expand Down Expand Up @@ -358,7 +387,8 @@ public function getAllDrives(
$apiDrive,
$this->connectionConfig,
$this->serviceUrl,
$this->accessToken
$this->accessToken,
$this->getOcisVersion()
);
$drives[] = $drive;
}
Expand Down Expand Up @@ -415,7 +445,8 @@ public function getMyDrives(
$apiDrive,
$this->connectionConfig,
$this->serviceUrl,
$this->accessToken
$this->accessToken,
$this->getOcisVersion()
);
$drives[] = $drive;
}
Expand Down Expand Up @@ -472,7 +503,8 @@ public function getDriveById(string $driveId): Drive
$apiDrive,
$this->connectionConfig,
$this->serviceUrl,
$this->accessToken
$this->accessToken,
$this->getOcisVersion()
);
}

Expand Down Expand Up @@ -524,7 +556,8 @@ public function createDrive(
$newlyCreatedDrive,
$this->connectionConfig,
$this->serviceUrl,
$this->accessToken
$this->accessToken,
$this->getOcisVersion()
);
}
throw new InvalidResponseException(
Expand Down
26 changes: 15 additions & 11 deletions tests/integration/Owncloud/OcisPhpSdk/DriveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Owncloud\OcisPhpSdk\Drive;
use Owncloud\OcisPhpSdk\Exception\BadRequestException;
use Owncloud\OcisPhpSdk\Exception\EndPointNotImplementedException;
use Owncloud\OcisPhpSdk\Exception\NotFoundException;
use Owncloud\OcisPhpSdk\Ocis;
use Owncloud\OcisPhpSdk\SharingRole;
Expand Down Expand Up @@ -72,19 +73,22 @@ public function testDeleteEnabledDrive(): void

public function testGetDriveRole(): void
{
// At the time of writing, "stable" is major version 5 of ocis.
// This functionality works with major version 6.
// When ocis major version 6 has been released as "stable" then remove this test skip.
//ocis stable doesn't support root endpoint
if (getenv('OCIS_VERSION') === "stable") {
$this->markTestSkipped(
'This test is skipped because root endpoint for drive share is not applicable for version 5 of OCIS.'
$this->expectException(EndPointNotImplementedException::class);
$this->expectExceptionMessage("This method is not implemented in this ocis version");
$this->drive->getRoles();
}
try {
$role = $this->drive->getRoles();
$this->assertContainsOnlyInstancesOf(
SharingRole::class,
$role,
"Array contains not only 'SharingRole' items"
);
} catch(EndPointNotImplementedException) {
//test should fail if ocis version is less than 6.0.0
$this->fail("EndPointNotImplementedException was thrown unexpectedly");
};
$role = $this->drive->getRoles();
$this->assertContainsOnlyInstancesOf(
SharingRole::class,
$role,
"Array contains not only 'SharingRole' items"
);
}
}
27 changes: 26 additions & 1 deletion tests/unit/Owncloud/OcisPhpSdk/OcisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Owncloud\OcisPhpSdk\Exception\HttpException;
use Owncloud\OcisPhpSdk\Exception\InvalidResponseException;
use Owncloud\OcisPhpSdk\Ocis;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
Expand Down Expand Up @@ -121,7 +122,8 @@ public function testSetAccessTokenPropagatesToDrives(): void
$ocis = new Ocis(
'https://localhost:9200',
'tokenWhenCreated',
['drivesGetDrivesApi' => $drivesGetDrivesApi]
/* @phpstan-ignore-next-line */
[ 'guzzle' => $this->setUpMocksForOcisVersion(), 'drivesGetDrivesApi' => $drivesGetDrivesApi]
);
$drives = $ocis->getAllDrives();
foreach ($drives as $drive) {
Expand Down Expand Up @@ -149,6 +151,29 @@ public function testSetAccessTokenPropagatesToNotifications(): void
$this->assertSame('changedToken', $notifications[1]->getAccessToken());
}

private function setUpMocksForOcisVersion(?string $responseContent = null): MockObject
{
if ($responseContent === null) {
$responseContent = <<<XML
<ocs>
<data>
<version>
<productversion>6.0.0</productversion>
</version>
</data>
</ocs>
XML;
};
$streamMock = $this->createMock(StreamInterface::class);
$streamMock->method('getContents')->willReturn($responseContent);
$responseMock = $this->createMock(ResponseInterface::class);
$responseMock->method('getBody')->willReturn($streamMock);
$guzzleMock = $this->createMock(Client::class);
$guzzleMock->method('get')
->willReturn($responseMock);
return $guzzleMock;
}

private function setupMocksForNotificationTests(
string $responseContent,
string $token = 'doesNotMatter'
Expand Down

0 comments on commit 01e96aa

Please sign in to comment.