Fix first set of API tests

This commit is contained in:
Andreas Gohr 2023-12-01 14:56:28 +01:00
parent 42e66c7a3a
commit 1468a1289a
5 changed files with 280 additions and 210 deletions

View File

@ -1,141 +1,22 @@
<?php
namespace dokuwiki\test\Remote;
use dokuwiki\Remote\AccessDeniedException;
use dokuwiki\Remote\ApiCall;
use dokuwiki\test\mock\AuthPlugin;
use dokuwiki\Extension\RemotePlugin;
use dokuwiki\Remote\Api;
use dokuwiki\Remote\RemoteException;
class RemoteAPICoreTest {
function getRemoteInfo() {
return array(
'wiki.stringTestMethod' => array(
'args' => array(),
'return' => 'string',
'doc' => 'Test method',
'name' => 'stringTestMethod',
), 'wiki.intTestMethod' => array(
'args' => array(),
'return' => 'int',
'doc' => 'Test method',
'name' => 'intTestMethod',
), 'wiki.floatTestMethod' => array(
'args' => array(),
'return' => 'float',
'doc' => 'Test method',
'name' => 'floatTestMethod',
), 'wiki.dateTestMethod' => array(
'args' => array(),
'return' => 'date',
'doc' => 'Test method',
'name' => 'dateTestMethod',
), 'wiki.fileTestMethod' => array(
'args' => array(),
'return' => 'file',
'doc' => 'Test method',
'name' => 'fileTestMethod',
), 'wiki.voidTestMethod' => array(
'args' => array(),
'return' => 'void',
'doc' => 'Test method',
'name' => 'voidTestMethod',
), 'wiki.oneStringArgMethod' => array(
'args' => array('string'),
'return' => 'string',
'doc' => 'Test method',
'name' => 'oneStringArgMethod',
), 'wiki.twoArgMethod' => array(
'args' => array('string', 'int'),
'return' => 'array',
'doc' => 'Test method',
'name' => 'twoArgMethod',
), 'wiki.twoArgWithDefaultArg' => array(
'args' => array('string', 'string'),
'return' => 'string',
'doc' => 'Test method',
'name' => 'twoArgWithDefaultArg',
), 'wiki.publicCall' => array(
'args' => array(),
'return' => 'boolean',
'doc' => 'testing for public access',
'name' => 'publicCall',
'public' => 1
)
);
}
function stringTestMethod() { return 'success'; }
function intTestMethod() { return 42; }
function floatTestMethod() { return 3.14159265; }
function dateTestMethod() { return 2623452346; }
function fileTestMethod() { return 'file content'; }
function voidTestMethod() { return null; }
function oneStringArgMethod($arg) {return $arg; }
function twoArgMethod($string, $int) { return array($string, $int); }
function twoArgWithDefaultArg($string1, $string2 = 'default') { return array($string1, $string2); }
function publicCall() {return true;}
}
class remote_plugin_testplugin extends RemotePlugin {
function _getMethods() {
return array(
'method1' => array(
'args' => array(),
'return' => 'void'
), 'methodString' => array(
'args' => array(),
'return' => 'string'
), 'method2' => array(
'args' => array('string', 'int'),
'return' => 'array',
'name' => 'method2',
), 'method2ext' => array(
'args' => array('string', 'int', 'bool'),
'return' => 'array',
'name' => 'method2',
), 'publicCall' => array(
'args' => array(),
'return' => 'boolean',
'doc' => 'testing for public access',
'name' => 'publicCall',
'public' => 1
)
);
}
function method1() { return null; }
function methodString() { return 'success'; }
function method2($str, $int, $bool = false) { return array($str, $int, $bool); }
function publicCall() {return true;}
}
class remote_plugin_testplugin2 extends RemotePlugin {
/**
* This is a dummy method
*
* @param string $str some more parameter description
* @param int $int
* @param bool $bool
* @param Object $unknown
* @return array
*/
public function commented($str, $int, $bool, $unknown) { return array($str, $int, $bool); }
private function privateMethod() {return true;}
protected function protectedMethod() {return true;}
public function _underscore() {return true;}
}
class remote_test extends DokuWikiTest {
class ApiTest extends \DokuWikiTest {
protected $userinfo;
/** @var Api */
protected $remote;
function setUp() : void {
public function setUp() : void {
parent::setUp();
global $plugin_controller;
global $conf;
@ -168,13 +49,13 @@ class remote_test extends DokuWikiTest {
$auth = new AuthPlugin();
}
function tearDown() : void {
public function tearDown() : void {
global $USERINFO;
$USERINFO = $this->userinfo;
}
function test_pluginMethods() {
public function testPluginMethods() {
$methods = $this->remote->getPluginMethods();
$actual = array_keys($methods);
sort($actual);
@ -191,86 +72,117 @@ class remote_test extends DokuWikiTest {
$this->assertEquals($expect,$actual);
}
function test_pluginDescriptors() {
public function testPluginDescriptors() {
$methods = $this->remote->getPluginMethods();
$this->assertEquals(array('string','int','bool','string'), $methods['plugin.testplugin2.commented']['args']);
$this->assertEquals('array', $methods['plugin.testplugin2.commented']['return']);
$this->assertEquals(0, $methods['plugin.testplugin2.commented']['public']);
$this->assertStringContainsString('This is a dummy method', $methods['plugin.testplugin2.commented']['doc']);
$this->assertStringContainsString('string $str some more parameter description', $methods['plugin.testplugin2.commented']['doc']);
$this->assertEquals(
[
'str' => [
'type' => 'string',
'description' => 'some more parameter description',
],
'int' => [
'type' => 'int',
'description' => '',
],
'bool' => [
'type' => 'bool',
'description' => '',
],
'unknown' => [
'type' => 'string',
'description' => '',
],
],
$methods['plugin.testplugin2.commented']->getArgs()
);
$this->assertEquals(
[
'type' => 'array',
'description' => '',
],
$methods['plugin.testplugin2.commented']->getReturn()
);
$this->assertEquals(0, $methods['plugin.testplugin2.commented']->isPublic());
$this->assertStringContainsString(
'This is a dummy method',
$methods['plugin.testplugin2.commented']->getSummary()
);
}
function test_hasAccessSuccess() {
public function testHasAccessSuccess()
{
global $conf;
$conf['remoteuser'] = '';
$this->assertTrue($this->remote->hasAccess());
$this->remote->ensureAccessIsAllowed(new ApiCall('time'));
$this->assertTrue(true);
}
function test_hasAccessFail() {
global $conf;
$conf['remote'] = 0;
// the hasAccess() should throw a Exception to keep the same semantics with xmlrpc.php.
// because the user(xmlrpc) check remote before .--> (!$conf['remote']) die('XML-RPC server not enabled.');
// so it must be a Exception when get here.
$this->expectException(\dokuwiki\Remote\AccessDeniedException::class);
$this->remote->hasAccess();
}
function test_hasAccessFailAcl() {
public function testHasAccessFailAcl() {
global $conf;
$conf['useacl'] = 1;
$this->assertFalse($this->remote->hasAccess());
$this->expectException(AccessDeniedException::class);
$this->remote->ensureAccessIsAllowed(new ApiCall('time'));
}
function test_hasAccessSuccessAclEmptyRemoteUser() {
public function testHasAccessSuccessAclEmptyRemoteUser() {
global $conf;
$conf['useacl'] = 1;
$conf['remoteuser'] = '';
$this->assertTrue($this->remote->hasAccess());
$this->remote->ensureAccessIsAllowed(new ApiCall('time'));
$this->assertTrue(true);
}
function test_hasAccessSuccessAcl() {
public function testHasAccessSuccessAcl() {
global $conf;
global $USERINFO;
$conf['useacl'] = 1;
$conf['remoteuser'] = '@grp,@grp2';
$USERINFO['grps'] = array('grp');
$this->assertTrue($this->remote->hasAccess());
$this->remote->ensureAccessIsAllowed(new ApiCall('time'));
$this->assertTrue(true);
}
function test_hasAccessFailAcl2() {
public function testHasAccessFailAcl2() {
global $conf;
global $USERINFO;
$conf['useacl'] = 1;
$conf['remoteuser'] = '@grp';
$USERINFO['grps'] = array('grp1');
$this->assertFalse($this->remote->hasAccess());
$this->expectException(AccessDeniedException::class);
$this->remote->ensureAccessIsAllowed(new ApiCall('time'));
}
public function testIsEnabledFail1() {
global $conf;
$conf['remote'] = 0;
$this->expectException(AccessDeniedException::class);
$this->remote->ensureApiIsEnabled();
}
function test_forceAccessSuccess() {
public function testIsEnabledFail2() {
global $conf;
$conf['remoteuser'] = '!!not set!!';
$this->expectException(AccessDeniedException::class);
$this->remote->ensureApiIsEnabled();
}
public function testIsEnabledSuccess() {
global $conf;
$conf['remote'] = 1;
$conf['remoteuser'] = '';
$this->remote->forceAccess(); // no exception should occur
$this->assertTrue(true); // avoid being marked as risky for having no assertion
$this->remote->ensureApiIsEnabled();
$this->assertTrue(true);
}
function test_forceAccessFail() {
global $conf;
$conf['remote'] = 0;
try {
$this->remote->forceAccess();
$this->fail('Expects RemoteException to be raised');
} catch (RemoteException $th) {
$this->assertEquals(-32604, $th->getCode());
}
}
function test_generalCoreFunctionWithoutArguments() {
public function testGeneralCoreFunctionWithoutArguments() {
global $conf;
global $USERINFO;
$conf['remote'] = 1;
@ -278,7 +190,7 @@ class remote_test extends DokuWikiTest {
$conf['useacl'] = 1;
$USERINFO['grps'] = array('grp');
$remoteApi = new Api();
$remoteApi->getCoreMethods(new RemoteAPICoreTest());
$remoteApi->getCoreMethods(new MockApiCore());
$this->assertEquals($remoteApi->call('wiki.stringTestMethod'), 'success');
$this->assertEquals($remoteApi->call('wiki.intTestMethod'), 42);
@ -288,11 +200,11 @@ class remote_test extends DokuWikiTest {
$this->assertEquals($remoteApi->call('wiki.voidTestMethod'), null);
}
function test_generalCoreFunctionOnArgumentMismatch() {
public function testGeneralCoreFunctionOnArgumentMismatch() {
global $conf;
$conf['remote'] = 1;
$remoteApi = new Api();
$remoteApi->getCoreMethods(new RemoteAPICoreTest());
$remoteApi->getCoreMethods(new MockApiCore());
try {
$remoteApi->call('wiki.voidTestMethod', array('something'));
@ -302,7 +214,7 @@ class remote_test extends DokuWikiTest {
}
}
function test_generalCoreFunctionWithArguments() {
public function testGeneralCoreFunctionWithArguments() {
global $conf;
global $USERINFO;
$conf['remote'] = 1;
@ -310,7 +222,7 @@ class remote_test extends DokuWikiTest {
$conf['useacl'] = 1;
$remoteApi = new Api();
$remoteApi->getCoreMethods(new RemoteAPICoreTest());
$remoteApi->getCoreMethods(new MockApiCore());
$this->assertEquals($remoteApi->call('wiki.oneStringArgMethod', array('string')), 'string');
$this->assertEquals($remoteApi->call('wiki.twoArgMethod', array('string', 1)), array('string' , 1));
@ -318,12 +230,12 @@ class remote_test extends DokuWikiTest {
$this->assertEquals($remoteApi->call('wiki.twoArgWithDefaultArg', array('string', 'another')), array('string', 'another'));
}
function test_generalCoreFunctionOnArgumentMissing() {
public function testGeneralCoreFunctionOnArgumentMissing() {
global $conf;
$conf['remote'] = 1;
$conf['remoteuser'] = '';
$remoteApi = new Api();
$remoteApi->getCoreMethods(new RemoteAPICoreTest());
$remoteApi->getCoreMethods(new MockApiCore());
try {
$remoteApi->call('wiki.twoArgWithDefaultArg', array());
@ -333,7 +245,7 @@ class remote_test extends DokuWikiTest {
}
}
function test_pluginCallMethods() {
public function testPluginCallMethods() {
global $conf;
global $USERINFO;
$conf['remote'] = 1;
@ -347,12 +259,12 @@ class remote_test extends DokuWikiTest {
$this->assertEquals($remoteApi->call('plugin.testplugin.methodString'), 'success');
}
function test_pluginCallMethodsOnArgumentMissing() {
public function testPluginCallMethodsOnArgumentMissing() {
global $conf;
$conf['remote'] = 1;
$conf['remoteuser'] = '';
$remoteApi = new Api();
$remoteApi->getCoreMethods(new RemoteAPICoreTest());
$remoteApi->getCoreMethods(new MockApiCore());
try {
$remoteApi->call('plugin.testplugin.method2', array());
@ -362,9 +274,10 @@ class remote_test extends DokuWikiTest {
}
}
function test_notExistingCall() {
public function testNotExistingCall() {
global $conf;
$conf['remote'] = 1;
$conf['remoteuser'] = '';
$this->expectException(RemoteException::class);
$this->expectExceptionCode(-32603);
@ -374,53 +287,76 @@ class remote_test extends DokuWikiTest {
$remoteApi->call('does.not exist'); // unknown method type
}
function test_publicCallCore() {
public function testPublicCallCore() {
global $conf;
$conf['useacl'] = 1;
$conf['remote'] = 1;
$conf['remoteuser'] = 'restricted';
$remoteApi = new Api();
$remoteApi->getCoreMethods(new RemoteAPICoreTest());
$remoteApi->getCoreMethods(new MockApiCore());
$this->assertTrue($remoteApi->call('wiki.publicCall'));
}
function test_publicCallPlugin() {
public function testPublicCallPlugin() {
global $conf;
$conf['useacl'] = 1;
$conf['remote'] = 1;
$conf['remoteuser'] = 'restricted';
$remoteApi = new Api();
$this->assertTrue($remoteApi->call('plugin.testplugin.publicCall'));
}
function test_publicCallCoreDeny() {
public function testPublicCallCoreDeny() {
global $conf;
$conf['useacl'] = 1;
$this->expectException(\dokuwiki\Remote\AccessDeniedException::class);
$this->expectException(AccessDeniedException::class);
$remoteApi = new Api();
$remoteApi->getCoreMethods(new RemoteAPICoreTest());
$remoteApi->getCoreMethods(new MockApiCore());
$remoteApi->call('wiki.stringTestMethod');
}
function test_publicCallPluginDeny() {
public function testPublicCallPluginDeny() {
global $conf;
$conf['useacl'] = 1;
$this->expectException(\dokuwiki\Remote\AccessDeniedException::class);
$this->expectException(AccessDeniedException::class);
$remoteApi = new Api();
$remoteApi->call('plugin.testplugin.methodString');
}
function test_pluginCallCustomPath() {
global $conf;
global $USERINFO;
$conf['remote'] = 1;
$conf['remoteuser'] = '';
$conf['useacl'] = 1;
global $EVENT_HANDLER;
$EVENT_HANDLER->register_hook('RPC_CALL_ADD', 'BEFORE', $this, 'pluginCallCustomPathRegister');
$remoteApi = new Api();
$result = $remoteApi->call('custom.path');
$this->assertEquals($result, 'success');
}
function pluginCallCustomPathRegister(&$event, $param) {
$event->data['custom.path'] = array('testplugin', 'methodString');
}
}
class remote_plugin_testplugin extends RemotePlugin {
function getMethods() {
$methods = parent::getMethods(); // auto add all methods, then adjust
$methods['method2']->limitArgs(['str', 'int']); // skip optional parameter
$methods['method2ext'] = new ApiCall([$this, 'method2']); // add second call with all params
$methods['publicCall']->setPublic(); // make this one public
return $methods;
}
function method1() { return null; }
function methodString() { return 'success'; }
function method2($str, $int, $bool = false) { return array($str, $int, $bool); }
function publicCall() {return true;}
}
class remote_plugin_testplugin2 extends RemotePlugin {
/**
* This is a dummy method
*
* @param string $str some more parameter description
* @param int $int
* @param bool $bool
* @param Object $unknown
* @return array
*/
public function commented($str, $int, $bool, $unknown) { return array($str, $int, $bool); }
private function privateMethod() {return true;}
protected function protectedMethod() {return true;}
public function _underscore() {return true;}
}

View File

@ -0,0 +1,134 @@
<?php
namespace dokuwiki\test\Remote;
use dokuwiki\Remote\ApiCall;
class MockApiCore
{
function getRemoteInfo()
{
return [
'wiki.stringTestMethod' => new ApiCall([$this, 'stringTestMethod']),
'wiki.intTestMethod' => new ApiCall([$this, 'intTestMethod']),
'wiki.floatTestMethod' => new ApiCall([$this, 'floatTestMethod']),
'wiki.dateTestMethod' => new ApiCall([$this, 'dateTestMethod']),
'wiki.fileTestMethod' => new ApiCall([$this, 'fileTestMethod']),
'wiki.voidTestMethod' => new ApiCall([$this, 'voidTestMethod']),
'wiki.oneStringArgMethod' => new ApiCall([$this, 'oneStringArgMethod']),
'wiki.twoArgMethod' => new ApiCall([$this, 'twoArgMethod']),
'wiki.twoArgWithDefaultArg' => new ApiCall([$this, 'twoArgWithDefaultArg']),
'wiki.publicCall' => (new ApiCall([$this, 'publicCall']))->setPublic(),
];
/*
array(
'wiki.stringTestMethod' => array(
'args' => array(),
'return' => 'string',
'doc' => 'Test method',
'name' => 'stringTestMethod',
), 'wiki.intTestMethod' => array(
'args' => array(),
'return' => 'int',
'doc' => 'Test method',
'name' => 'intTestMethod',
), 'wiki.floatTestMethod' => array(
'args' => array(),
'return' => 'float',
'doc' => 'Test method',
'name' => 'floatTestMethod',
), 'wiki.dateTestMethod' => array(
'args' => array(),
'return' => 'date',
'doc' => 'Test method',
'name' => 'dateTestMethod',
), 'wiki.fileTestMethod' => array(
'args' => array(),
'return' => 'file',
'doc' => 'Test method',
'name' => 'fileTestMethod',
), 'wiki.voidTestMethod' => array(
'args' => array(),
'return' => 'void',
'doc' => 'Test method',
'name' => 'voidTestMethod',
), 'wiki.oneStringArgMethod' => array(
'args' => array('string'),
'return' => 'string',
'doc' => 'Test method',
'name' => 'oneStringArgMethod',
), 'wiki.twoArgMethod' => array(
'args' => array('string', 'int'),
'return' => 'array',
'doc' => 'Test method',
'name' => 'twoArgMethod',
), 'wiki.twoArgWithDefaultArg' => array(
'args' => array('string', 'string'),
'return' => 'string',
'doc' => 'Test method',
'name' => 'twoArgWithDefaultArg',
), 'wiki.publicCall' => array(
'args' => array(),
'return' => 'boolean',
'doc' => 'testing for public access',
'name' => 'publicCall',
'public' => 1
)
);
*/
}
function stringTestMethod()
{
return 'success';
}
function intTestMethod()
{
return 42;
}
function floatTestMethod()
{
return 3.14159265;
}
function dateTestMethod()
{
return 2623452346;
}
function fileTestMethod()
{
return 'file content';
}
function voidTestMethod()
{
return null;
}
function oneStringArgMethod($arg)
{
return $arg;
}
function twoArgMethod($string, $int)
{
return array($string, $int);
}
function twoArgWithDefaultArg($string1, $string2 = 'default')
{
return array($string1, $string2);
}
function publicCall()
{
return true;
}
}

View File

@ -30,7 +30,7 @@ abstract class RemotePlugin extends Plugin
* By default it exports all public methods of a remote plugin. Methods beginning
* with an underscore are skipped.
*
* @return ApiCall[] Information about all provided methods.
* @return ApiCall[] Information about all provided methods. ('methodname' => ApiCall)
* @throws ReflectionException
*/
public function getMethods()

View File

@ -158,11 +158,11 @@ class Api
* @return void
* @throws RemoteException thrown when the API is disabled
*/
protected function ensureApiIsEnabled()
public function ensureApiIsEnabled()
{
global $conf;
if (!$conf['remote'] || trim($conf['remoteuser']) == '!!not set!!') {
throw new RemoteException('Server Error. API is not enabled in config.', -32604);
throw new AccessDeniedException('Server Error. API is not enabled in config.', -32604);
}
}
@ -173,7 +173,7 @@ class Api
* @return void
* @throws AccessDeniedException Thrown when the user is not allowed to call the method
*/
protected function ensureAccessIsAllowed(ApiCall $method)
public function ensureAccessIsAllowed(ApiCall $method)
{
global $conf;
global $INPUT;

View File

@ -296,7 +296,7 @@ class ApiCall
if (isset($tags['return'])) {
$return = $tags['return'][0];
if (preg_match('/^(\w+)(\s+(.*))$/m', $return, $m)) {
if (preg_match('/^(\w+)(\s+(.*))?$/m', $return, $m)) {
$result['return'] = [
'type' => $this->cleanTypeHint($m[1]),
'description' => trim($m[2] ?? '')