Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions API.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@
*/
class API extends \Piwik\Plugin\API
{
/**
* Return the OpenApiDocs plugin whitelist from config/plugins.php.
*
* /index.php?module=API&method=OpenApiDocs.getPluginWhitelist
*
* @return array<int, string>
* @throws \Exception
*/
public function getPluginWhitelist(): array
{
Piwik::checkUserHasSomeViewAccess();

$pluginWhitelist = $this->loadPluginWhitelist();
if (!is_array($pluginWhitelist)) {
throw new \Exception('OpenApiDocs plugin whitelist config is invalid.');
}

return $pluginWhitelist;
}

/**
* Get a pre-generated OpenAPI spec file if it exists. This endpoint only reads
* the generated JSON file and does not trigger spec generation.
Expand Down Expand Up @@ -71,6 +91,14 @@ protected function getSpecFilePath(string $pluginName): string
return $this->getSpecPathResolver()->getSpecFilePath($pluginName);
}

/**
* @return mixed
*/
protected function loadPluginWhitelist()
{
return require __DIR__ . '/config/plugins.php';
}

protected function isSpecFileReadable(string $filePath): bool
{
return is_file($filePath) && is_readable($filePath);
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/APITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,31 @@ public function testGetOpenApiSpecReturnsDecodedJsonForPlugin()
$this->assertSame($expectedSpec, $result);
}

public function testGetPluginWhitelistReturnsConfigValuesInOrder()
{
$expectedWhitelist = ['RollUpReporting', 'Login', 'ActivityLog'];

$api = $this->getMockBuilder(API::class)
->onlyMethods(['loadPluginWhitelist'])
->getMock();
$api->method('loadPluginWhitelist')->willReturn($expectedWhitelist);

$this->assertSame($expectedWhitelist, $api->getPluginWhitelist());
}

public function testGetPluginWhitelistThrowsExceptionWhenConfigIsInvalid()
{
$api = $this->getMockBuilder(API::class)
->onlyMethods(['loadPluginWhitelist'])
->getMock();
$api->method('loadPluginWhitelist')->willReturn('invalid');

$this->expectException(\Exception::class);
$this->expectExceptionMessage('OpenApiDocs plugin whitelist config is invalid.');

$api->getPluginWhitelist();
}

public function testGetOpenApiSpecThrowsExceptionWhenFileMissing()
{
$api = $this->buildApiMock('/tmp/CustomAlerts_openapi_spec_v1.0.0.json', false);
Expand Down
Loading