WIP
parent
eeac0740d2
commit
9cfdde525f
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
class restDcNameSpace extends dcSettings
|
||||
{
|
||||
//this function is private on the parent class
|
||||
public function settingExists($id,$global=false)
|
||||
{
|
||||
$array = $global ? 'global' : 'local';
|
||||
return isset($this->{$array.'_settings'}[$id]);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
class RestQueryDeleteBlogSettings extends RestQuery
|
||||
{
|
||||
|
||||
public function __construct($args){
|
||||
global $core;
|
||||
$explodedArgs = explode("/",$args);
|
||||
$nameSpace = $explodedArgs[2];
|
||||
$this->blog_id = $explodedArgs[0];
|
||||
|
||||
|
||||
if($core->auth === false){
|
||||
$core->auth = new restAuth($core); //class dcBlog need it
|
||||
$unauth = true;
|
||||
}
|
||||
$this->required_perms = 'admin';
|
||||
|
||||
//Is allowed?
|
||||
if($this->is_allowed() === false){
|
||||
//need To be authentified
|
||||
return;
|
||||
}
|
||||
|
||||
//does the blog exists?
|
||||
$core->blog = new dcBlog($core, $this->blog_id);
|
||||
$blog_settings = new dcSettings($core,$this->blog_id);
|
||||
|
||||
if(!$core->blog->id){
|
||||
$this->response_code = 404;
|
||||
$this->response_message = array('code' => 404, 'error' => 'Resource '.$this -> blog_id.' not found');
|
||||
return;
|
||||
}
|
||||
|
||||
//Delete namespace or just a setting?
|
||||
$nS = new restDcNameSpace($core, $this->blog_id,$explodedArgs[2]);
|
||||
|
||||
if(isset($explodedArgs[3])){
|
||||
if($nS->settingExists($explodedArgs[3])){
|
||||
$nS->drop($explodedArgs[3]);
|
||||
//OK
|
||||
$this->response_code = 200;
|
||||
$this->response_message = array('code' => 200, 'message' => 'Setting '.$explodedArgs[3].' deleted.');
|
||||
return;
|
||||
}else{
|
||||
$this->response_code = 404;
|
||||
$this->response_message = array('code' => 404, 'error' => 'Setting '.$explodedArgs[3].' not found');
|
||||
return;
|
||||
}
|
||||
}else{
|
||||
//delete nameSpace
|
||||
$core->blog->settings->delNamespace($explodedArgs[2]);
|
||||
$this->response_code = 200;
|
||||
$this->response_message = array('code' => 200, 'message' => 'NameSpace '.$explodedArgs[2].' deleted.');
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
class RestQueryPostBlogSettings extends RestQuery
|
||||
{
|
||||
private function arrayToSubArray($array){
|
||||
|
||||
return array($array);
|
||||
}
|
||||
|
||||
private function setSetting($nameSpace,$body){
|
||||
global $core;
|
||||
//verifier le JSON
|
||||
|
||||
//permettre à l'user de fournir un ou plusieurs settings à la fois.
|
||||
$querryArr = json_decode($body,true);
|
||||
if(empty($querryArr)){
|
||||
$this->response_code = 400;
|
||||
$this->response_message = array(
|
||||
'error' => 'Can\'t parse input JSON',
|
||||
'code' => 400
|
||||
);
|
||||
return;
|
||||
}
|
||||
if(isset($querryArr['id'])){
|
||||
//l'user n'a envoyé qu'un seul setting sans le mettre dans un objet
|
||||
$querryArr = $this->arrayToSubArray($querryArr);
|
||||
}
|
||||
|
||||
//tester la présence des bonnes clefs
|
||||
foreach($querryArr as $setting){
|
||||
if($this->check_for_required_fields($setting,
|
||||
array('id','value'),
|
||||
array('type','label','value_change','global')
|
||||
) === false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
foreach($querryArr as $setting){
|
||||
//set falcutative fields
|
||||
if(!isset($setting['type'])){
|
||||
$setting['type'] = null;
|
||||
}
|
||||
if(!isset($setting['value_change'])){
|
||||
$setting['value_change'] = true;
|
||||
}
|
||||
if(!isset($setting['global'])){
|
||||
$setting['global'] = false;
|
||||
}
|
||||
|
||||
$core->blog->settings->$nameSpace->put($setting['id'],$setting['value'],
|
||||
$setting['type'],$setting['value_change'],
|
||||
$setting['global']);
|
||||
}
|
||||
|
||||
$this -> response_code = 201;
|
||||
$this -> response_message = array(
|
||||
'code' => 201,
|
||||
'message' => 'settings Successfully created'
|
||||
);
|
||||
return;
|
||||
}
|
||||
public function __construct($args,$body){
|
||||
global $core;
|
||||
|
||||
$explodedArgs = explode("/",$args);
|
||||
$nameSpace = $explodedArgs[2];
|
||||
$this->blog_id = $explodedArgs[0];
|
||||
$this->required_perms = 'admin';
|
||||
|
||||
if($core->auth === false){
|
||||
$core->auth = new restAuth($core); //class dcBlog need it
|
||||
$unauth = true;
|
||||
}
|
||||
$core->blog = new dcBlog($core, $this->blog_id);
|
||||
$blog_settings = new dcSettings($core,$this->blog_id);
|
||||
|
||||
|
||||
|
||||
if($this->is_allowed() === false){
|
||||
return;
|
||||
}
|
||||
$core->blog->settings->addNamespace($nameSpace);
|
||||
//error_log($body);
|
||||
if(empty($body)){
|
||||
$this -> response_code = 201;
|
||||
$this -> response_message = array(
|
||||
'code' => 201,
|
||||
'message' => 'namespace '.$nameSpace.' Successfully created'
|
||||
);
|
||||
}else{
|
||||
$this-> setSetting($nameSpace,$body);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
#Unit tests
|
||||
|
||||
Change the parameters at the beginning of test-API.php and use it in console mode
|
||||
|
||||
php test-API.php
|
||||
|
||||
This will test all the API methods.
|
||||
Use this tests only on a developpement platform of Dotclear.
|
||||
|
||||
These tests are for the developer (me). I'm tired of systematically retest everything as I progress in development.
|
||||
Incidentally, they also serve as an example on the use of the API.
|
@ -0,0 +1,378 @@
|
||||
<?php
|
||||
|
||||
$api_key = '1zhoGXv9PA0lvPNG81GUlCNfqT45gVuV'; //super admin api_key, change it by your
|
||||
$api_url = 'http://dotclear.localhost/dotclear/index.php?rest'; //my local dev platform
|
||||
|
||||
|
||||
function check_json_content($content,$aKeyToCheck){
|
||||
|
||||
$arr=json_decode($content,true);
|
||||
|
||||
if($aKeyToCheck === false){
|
||||
if (is_array($arr)){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if(isset($arr[$aKeyToCheck])){
|
||||
if(is_array($arr[$aKeyToCheck])){
|
||||
return json_encode($arr[$aKeyToCheck],true);
|
||||
}else{
|
||||
return $arr[$aKeyToCheck];
|
||||
}
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function test($url, $method, $body, $expectedCode, $expectedKeyOnResponse, $x_dc_key){
|
||||
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
|
||||
if($x_dc_key <> ''){
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('x_dc_key: '.$x_dc_key));
|
||||
}
|
||||
if($body <> ''){
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
|
||||
}
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch)['http_code'];
|
||||
|
||||
if($httpCode <> $expectedCode){
|
||||
echo "\nQuery type ".$method." on url ".$url." didn't return the expected code.\n".
|
||||
"return: ".$httpCode." Expected: ".$expectedCode."\n".
|
||||
"Response content:\n".$response;
|
||||
//on va pas plus loin, ça pourrait mettre la m... dans la suite
|
||||
die();
|
||||
return;
|
||||
}
|
||||
|
||||
$r = check_json_content($response ,$expectedKeyOnResponse);
|
||||
if($r === false){
|
||||
echo "\nQuery type ".$method." on url ".$url." JSON parse error or missing propertie.\n".
|
||||
"return code: ".$httpCode."\n".
|
||||
"Response content:\n".$response;
|
||||
//on va pas plus loin, ça pourrait mettre la m... dans la suite
|
||||
die();
|
||||
return;
|
||||
}
|
||||
curl_close($ch);
|
||||
return $r;
|
||||
}
|
||||
|
||||
|
||||
$allTests = array(
|
||||
array(
|
||||
'title' => 'test 404 page',
|
||||
'url' => $api_url.'/kgdghui',
|
||||
'method' => 'GET',
|
||||
'expectedResponseCode' => '404',
|
||||
'expectedKeyOnResponse' => 'error',
|
||||
'body' => '',
|
||||
'x_dc_key' => '',
|
||||
'saveAs' => ''
|
||||
|
||||
),
|
||||
array(
|
||||
'title' => 'test specs Method',
|
||||
'url' => $api_url.'/specs',
|
||||
'method' => 'GET',
|
||||
'expectedResponseCode' => '200',
|
||||
'expectedKeyOnResponse' => 'swagger',
|
||||
'body' => '',
|
||||
'x_dc_key' => '',
|
||||
'saveAs' => ''
|
||||
|
||||
),
|
||||
array(
|
||||
'title' => 'get /blogs without api_key',
|
||||
'url' => $api_url.'/blogs',
|
||||
'method' => 'GET',
|
||||
'expectedResponseCode' => '403',
|
||||
'expectedKeyOnResponse' => 'error',
|
||||
'body' => '',
|
||||
'x_dc_key' => '',
|
||||
'saveAs' => ''
|
||||
|
||||
),
|
||||
array(
|
||||
'title' => 'get /blogs with wrong api_key',
|
||||
'url' => $api_url.'/blogs',
|
||||
'method' => 'GET',
|
||||
'expectedResponseCode' => '403',
|
||||
'expectedKeyOnResponse' => 'error',
|
||||
'body' => '',
|
||||
'x_dc_key' => '7777777777777',
|
||||
'saveAs' => ''
|
||||
|
||||
),
|
||||
array(
|
||||
'title' => 'get /blogs with good api_key',
|
||||
'url' => $api_url.'/blogs',
|
||||
'method' => 'GET',
|
||||
'expectedResponseCode' => '200',
|
||||
'expectedKeyOnResponse' => false, //is a single list
|
||||
'body' => '',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
|
||||
),
|
||||
//creer un blog qui servira aux tests suivants
|
||||
array(
|
||||
'title' => 'post /blogs/ with good api_key',
|
||||
'url' => $api_url.'/blogs',
|
||||
'method' => 'POST',
|
||||
'expectedResponseCode' => '201',
|
||||
'expectedKeyOnResponse' => 'id', //is a single list
|
||||
'body' => json_encode(array(
|
||||
"blog_id" => "test-api",
|
||||
"blog_name" => "Test de l'API",
|
||||
"blog_url" => "http://test.localhost/",
|
||||
"blog_desc"=> "un test"
|
||||
)),
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => 'blog_id'
|
||||
|
||||
),
|
||||
|
||||
//test JSON deffectueux
|
||||
array(
|
||||
'title' => 'post /blogs/ with good api_key and bad JSON',
|
||||
'url' => $api_url.'/blogs',
|
||||
'method' => 'POST',
|
||||
'expectedResponseCode' => '400',
|
||||
'expectedKeyOnResponse' => 'error',
|
||||
'body' => '{"ce n\'est pas du" => "JSON"}',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
|
||||
),
|
||||
|
||||
//get blog Properties
|
||||
array(
|
||||
'title' => 'Blogs /blogs/%blog_id% with good api_key',
|
||||
'url' => $api_url.'/blogs/%blog_id%',
|
||||
'method' => 'GET',
|
||||
'expectedResponseCode' => '200',
|
||||
'expectedKeyOnResponse' => 'blog_url',
|
||||
'body' => '',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
|
||||
),
|
||||
|
||||
//Patch blog properties with error ON JSON
|
||||
array(
|
||||
'title' => 'PATCH /blogs/%blog_id% with with error ON JSON',
|
||||
'url' => $api_url.'/blogs/%blog_id%',
|
||||
'method' => 'PATCH',
|
||||
'expectedResponseCode' => '400',
|
||||
'expectedKeyOnResponse' => 'error',
|
||||
'body' => '{JSON IS FUN}',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
|
||||
),
|
||||
|
||||
array(
|
||||
'title' => 'PATCH /blogs/%blog_id% with with error ON api_key',
|
||||
'url' => $api_url.'/blogs/%blog_id%',
|
||||
'method' => 'PATCH',
|
||||
'expectedResponseCode' => '403',
|
||||
'expectedKeyOnResponse' => 'error',
|
||||
'body' => '{"blog_name": "Patched Blog name"}',
|
||||
'x_dc_key' => 'heyHey!',
|
||||
'saveAs' => ''
|
||||
|
||||
),
|
||||
|
||||
array(
|
||||
'title' => 'PATCH /blogs/%blog_id% without error',
|
||||
'url' => $api_url.'/blogs/%blog_id%',
|
||||
'method' => 'PATCH',
|
||||
'expectedResponseCode' => '200',
|
||||
'expectedKeyOnResponse' => 'message',
|
||||
'body' => '{"blog_name": "Patched Blog name"}',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
|
||||
),
|
||||
|
||||
//PUT
|
||||
//Patch blog properties with error ON JSON
|
||||
array(
|
||||
'title' => 'PUT /blogs/%blog_id% with with error ON JSON',
|
||||
'url' => $api_url.'/blogs/%blog_id%',
|
||||
'method' => 'PUT',
|
||||
'expectedResponseCode' => '400',
|
||||
'expectedKeyOnResponse' => 'error',
|
||||
'body' => '{JSON IS FUN}',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
),
|
||||
|
||||
array(
|
||||
'title' => 'PUT /blogs/%blog_id% with with error ON api_key',
|
||||
'url' => $api_url.'/blogs/%blog_id%',
|
||||
'method' => 'PUT',
|
||||
'expectedResponseCode' => '403',
|
||||
'expectedKeyOnResponse' => 'error',
|
||||
'body' => '{"blog_id": "%blog_id%","blog_url":"https://plop.local/", "blog_name": "Patched Blog name", "blog_desc": "blahblah"}',
|
||||
'x_dc_key' => 'heyHey!',
|
||||
'saveAs' => ''
|
||||
),
|
||||
|
||||
array(
|
||||
'title' => 'PUT/blogs/%blog_id% without error',
|
||||
'url' => $api_url.'/blogs/%blog_id%',
|
||||
'method' => 'PUT',
|
||||
'expectedResponseCode' => '200',
|
||||
'expectedKeyOnResponse' => 'message',
|
||||
'body' => '{"blog_id": "%blog_id%","blog_url":"https://plop.local/", "blog_name": "Patched Blog name", "blog_desc": "blahblah"}',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
),
|
||||
|
||||
//get settings
|
||||
array(
|
||||
'title' => 'GET /%blog_id%/settings without error',
|
||||
'url' => $api_url.'/%blog_id%/settings',
|
||||
'method' => 'GET',
|
||||
'expectedResponseCode' => '200',
|
||||
'expectedKeyOnResponse' => 'system',
|
||||
'body' => '',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
),
|
||||
//get settings
|
||||
array(
|
||||
'title' => 'GET /%blog_id%/settings/system without error',
|
||||
'url' => $api_url.'/%blog_id%/settings/system',
|
||||
'method' => 'GET',
|
||||
'expectedResponseCode' => '200',
|
||||
'expectedKeyOnResponse' => 'url_scan',
|
||||
'body' => '',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
),
|
||||
array(
|
||||
'title' => 'GET /%blog_id%/settings/system/url_scan without error',
|
||||
'url' => $api_url.'/%blog_id%/settings/system/url_scan',
|
||||
'method' => 'GET',
|
||||
'expectedResponseCode' => '200',
|
||||
'expectedKeyOnResponse' => 'value',
|
||||
'body' => '',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
),
|
||||
array(
|
||||
'title' => 'GET /%blog_id%/settings/HEYHEY/url_scan without error',
|
||||
'url' => $api_url.'/%blog_id%/settings/HEYHEY/url_scan',
|
||||
'method' => 'GET',
|
||||
'expectedResponseCode' => '404',
|
||||
'expectedKeyOnResponse' => 'code',
|
||||
'body' => '',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
),
|
||||
|
||||
array(
|
||||
'title' => 'POST /%blog_id%/settings/test without error',
|
||||
'url' => $api_url.'/%blog_id%/settings/test',
|
||||
'method' => 'POST',
|
||||
'expectedResponseCode' => '201',
|
||||
'expectedKeyOnResponse' => 'code',
|
||||
'body' => '[{"id":"test","value":"hey","type":"string"},{"id":"test2","value":"hey","type":"string"}]',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
),
|
||||
array(
|
||||
'title' => 'POST /%blog_id%/settings/test without api key',
|
||||
'url' => $api_url.'/%blog_id%/settings/test',
|
||||
'method' => 'POST',
|
||||
'expectedResponseCode' => '403',
|
||||
'expectedKeyOnResponse' => 'code',
|
||||
'body' => '[{"id":"test2","value":"hey","type":"string"}]',
|
||||
'x_dc_key' => '',
|
||||
'saveAs' => ''
|
||||
),
|
||||
array(
|
||||
'title' => 'POST /%blog_id%/settings/test with fail JSON',
|
||||
'url' => $api_url.'/%blog_id%/settings/test',
|
||||
'method' => 'POST',
|
||||
'expectedResponseCode' => '400',
|
||||
'expectedKeyOnResponse' => 'code',
|
||||
'body' => '[{"id"=> Hey"test2","value":"hey","type":"string"}]',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
),
|
||||
|
||||
//delete the settings
|
||||
array(
|
||||
'title' => 'DELETE /%blog_id%/settings/test/test2 without key error',
|
||||
'url' => $api_url.'/%blog_id%/settings/test/test2',
|
||||
'method' => 'DELETE',
|
||||
'expectedResponseCode' => '403',
|
||||
'expectedKeyOnResponse' => 'code',
|
||||
'body' => '',
|
||||
'x_dc_key' => 'lkjmlhkjb:b:kjb',
|
||||
'saveAs' => ''
|
||||
),
|
||||
|
||||
array(
|
||||
'title' => 'DELETE /%blog_id%/settings/test/test2 without error',
|
||||
'url' => $api_url.'/%blog_id%/settings/test/test2',
|
||||
'method' => 'DELETE',
|
||||
'expectedResponseCode' => '200',
|
||||
'expectedKeyOnResponse' => 'code',
|
||||
'body' => '',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
),
|
||||
|
||||
|
||||
//remove blog test
|
||||
array(
|
||||
'title' => 'Blogs /blogs/%blog_id% with good api_key',
|
||||
'url' => $api_url.'/blogs/%blog_id%',
|
||||
'method' => 'DELETE',
|
||||
'expectedResponseCode' => '201',
|
||||
'expectedKeyOnResponse' => 'message', //is a single list
|
||||
'body' => '',
|
||||
'x_dc_key' => $api_key,
|
||||
'saveAs' => ''
|
||||
|
||||
),
|
||||
);
|
||||
|
||||
$saveIds = array();
|
||||
foreach($allTests as $oneTest){
|
||||
|
||||
//replaces
|
||||
|
||||
foreach($oneTest as $key => $value){
|
||||
foreach($saveIds as $find => $replace){
|
||||
$oneTest[$key] = str_replace('%'.$find.'%', $replace, $value);
|
||||
}
|
||||
}
|
||||
|
||||
echo "\nTesting ".$oneTest['title']." ".$oneTest['url']." method ". $oneTest['method'];
|
||||
$t = test(
|
||||
$oneTest['url'],
|
||||
$oneTest['method'],
|
||||
$oneTest['body'],
|
||||
$oneTest['expectedResponseCode'],
|
||||
$oneTest['expectedKeyOnResponse'],
|
||||
$oneTest['x_dc_key']
|
||||
);
|
||||
echo "\nSUCCESS ".$t;
|
||||
|
||||
if ($oneTest['saveAs'] <> ''){
|
||||
$saveIds[$oneTest['saveAs']] = $t;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue