Skip to content

Commit

Permalink
Fix unauthorised access to check the version as for aux object, (#29)
Browse files Browse the repository at this point in the history
* fix unauthorized access to check the version as for aux object,
fixes #28

* checker and tests improvements
  • Loading branch information
ferishili authored Sep 20, 2024
1 parent 34dee9a commit 1ad2ce3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/OpencastApi/Rest/OcRestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ public function hasVersion($version)
// We have to use an aux object, in order to prevent overwriting arguments of current object.
$aux = clone $this;
$aux->enableNoHeader();
$defaultVersion = $aux->performGet('/api/version/default');
$options = [];
if (!empty($this->username) && !empty($this->password)) {
$options['auth'] = [$this->username, $this->password];
}
$defaultVersion = $aux->performGet('/api/version/default', $options);
if (!empty($defaultVersion['body']) && isset($defaultVersion['body']->default)) {
$this->setVersion(str_replace(['application/', 'v', '+json'], ['', '', ''], $defaultVersion['body']->default));
} else {
Expand Down
20 changes: 14 additions & 6 deletions tests/DataProvider/SetupDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,23 @@ public static function getConfig($version = ''): array
return $config;
}

public static function getMockResponses($fileName): array
public static function getMockResponses($data): array
{
$responseNames = [];
if (!is_array($data)) {
$responseNames[] = $data;
} else {
$responseNames = $data;
}
$mockResponse = [];
$mockResponsesDir = __DIR__ . "/mock_responses";
$fileFullName = basename($fileName, ".json") . '.json';
$filePath = $mockResponsesDir . "/" . $fileFullName;
if (file_exists($filePath)) {
$responseStr = file_get_contents($filePath);
$mockResponse = json_decode($responseStr, true);
foreach ($responseNames as $fileName) {
$fileFullName = basename($fileName, ".json") . '.json';
$filePath = $mockResponsesDir . "/" . $fileFullName;
if (file_exists($filePath)) {
$responseStr = file_get_contents($filePath);
$mockResponse = array_merge($mockResponse, json_decode($responseStr, true));
}
}
return $mockResponse !== false ? $mockResponse : [];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/UnitMock/OcEventsApiTestMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class OcEventsApiTestMock extends TestCase
protected function setUp(): void
{
parent::setUp();
$mockResponse = \Tests\DataProvider\SetupDataProvider::getMockResponses('api_events');
$mockResponse = \Tests\DataProvider\SetupDataProvider::getMockResponses(['api_events', 'api_base']);
if (empty($mockResponse)) {
$this->markTestIncomplete('No mock responses for events api could be found!');
}
Expand Down

0 comments on commit 1ad2ce3

Please sign in to comment.