Skip to content

Commit a982408

Browse files
committed
Added endpoint to list APIs from whitelist
1 parent b3e89a6 commit a982408

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

API.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@
2424
*/
2525
class API extends \Piwik\Plugin\API
2626
{
27+
/**
28+
* Return the OpenApiDocs plugin whitelist from config/plugins.php.
29+
*
30+
* /index.php?module=API&method=OpenApiDocs.getPluginWhitelist
31+
*
32+
* @return array<int, string>
33+
* @throws \Exception
34+
*/
35+
public function getPluginWhitelist(): array
36+
{
37+
Piwik::checkUserHasSomeViewAccess();
38+
39+
$pluginWhitelist = $this->loadPluginWhitelist();
40+
if (!is_array($pluginWhitelist)) {
41+
throw new \Exception('OpenApiDocs plugin whitelist config is invalid.');
42+
}
43+
44+
return $pluginWhitelist;
45+
}
46+
2747
/**
2848
* Get a pre-generated OpenAPI spec file if it exists. This endpoint only reads
2949
* the generated JSON file and does not trigger spec generation.
@@ -71,6 +91,14 @@ protected function getSpecFilePath(string $pluginName): string
7191
return $this->getSpecPathResolver()->getSpecFilePath($pluginName);
7292
}
7393

94+
/**
95+
* @return mixed
96+
*/
97+
protected function loadPluginWhitelist()
98+
{
99+
return require __DIR__ . '/config/plugins.php';
100+
}
101+
74102
protected function isSpecFileReadable(string $filePath): bool
75103
{
76104
return is_file($filePath) && is_readable($filePath);

tests/Unit/APITest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,31 @@ public function testGetOpenApiSpecReturnsDecodedJsonForPlugin()
6161
$this->assertSame($expectedSpec, $result);
6262
}
6363

64+
public function testGetPluginWhitelistReturnsConfigValuesInOrder()
65+
{
66+
$expectedWhitelist = ['RollUpReporting', 'Login', 'ActivityLog'];
67+
68+
$api = $this->getMockBuilder(API::class)
69+
->onlyMethods(['loadPluginWhitelist'])
70+
->getMock();
71+
$api->method('loadPluginWhitelist')->willReturn($expectedWhitelist);
72+
73+
$this->assertSame($expectedWhitelist, $api->getPluginWhitelist());
74+
}
75+
76+
public function testGetPluginWhitelistThrowsExceptionWhenConfigIsInvalid()
77+
{
78+
$api = $this->getMockBuilder(API::class)
79+
->onlyMethods(['loadPluginWhitelist'])
80+
->getMock();
81+
$api->method('loadPluginWhitelist')->willReturn('invalid');
82+
83+
$this->expectException(\Exception::class);
84+
$this->expectExceptionMessage('OpenApiDocs plugin whitelist config is invalid.');
85+
86+
$api->getPluginWhitelist();
87+
}
88+
6489
public function testGetOpenApiSpecThrowsExceptionWhenFileMissing()
6590
{
6691
$api = $this->buildApiMock('/tmp/CustomAlerts_openapi_spec_v1.0.0.json', false);

0 commit comments

Comments
 (0)