This commit is contained in:
Gnieark 2017-05-18 21:49:46 +02:00
parent eeac0740d2
commit 9cfdde525f
18 changed files with 889 additions and 77 deletions

View File

@ -3,6 +3,7 @@ global $__autoload;
$__autoload['ApiKey'] = dirname(__FILE__).'/inc/class.rest.key.php'; $__autoload['ApiKey'] = dirname(__FILE__).'/inc/class.rest.key.php';
$__autoload['restAuth'] = dirname(__FILE__).'/inc/class.rest.auth.php'; $__autoload['restAuth'] = dirname(__FILE__).'/inc/class.rest.auth.php';
$__autoload['restDcNameSpace'] = dirname(__FILE__).'/inc/class.rest.DcNameSpace.php';
$__autoload['RestQuery'] = dirname(__FILE__).'/inc/class.rest.query.php'; $__autoload['RestQuery'] = dirname(__FILE__).'/inc/class.rest.query.php';
$__autoload['RestQueryGetBlogs'] = dirname(__FILE__).'/inc/class.rest.query.get.blogs.php'; $__autoload['RestQueryGetBlogs'] = dirname(__FILE__).'/inc/class.rest.query.get.blogs.php';
$__autoload['RestQueryGetBlog'] = dirname(__FILE__).'/inc/class.rest.query.get.blog.php'; $__autoload['RestQueryGetBlog'] = dirname(__FILE__).'/inc/class.rest.query.get.blog.php';
@ -12,3 +13,5 @@ $__autoload['ResQueryPatchBlogs'] = dirname(__FILE__).'/inc/class.rest.query.pat
$__autoload['ResQueryPutBlogs'] = dirname(__FILE__).'/inc/class.rest.query.put.blogs.php'; $__autoload['ResQueryPutBlogs'] = dirname(__FILE__).'/inc/class.rest.query.put.blogs.php';
$__autoload['ResQueryDeleteBlogs'] = dirname(__FILE__).'/inc/class.rest.query.delete.blogs.php'; $__autoload['ResQueryDeleteBlogs'] = dirname(__FILE__).'/inc/class.rest.query.delete.blogs.php';
$__autoload['RestQueryGetBlogSettings'] = dirname(__FILE__).'/inc/class.rest.query.get.blog.settings.php'; $__autoload['RestQueryGetBlogSettings'] = dirname(__FILE__).'/inc/class.rest.query.get.blog.settings.php';
$__autoload['RestQueryPostBlogSettings'] = dirname(__FILE__).'/inc/class.rest.query.post.blog.settings.php';
$__autoload['RestQueryDeleteBlogSettings'] = dirname(__FILE__).'/inc/class.rest.query.delete.blog.settings.php';

View File

@ -18,37 +18,43 @@ class rest extends dcUrlHandlers
switch($httpMethod){ switch($httpMethod){
case "GET": case "GET":
if($args == 'blogs'){ if($args == 'blogs'){
$queryObj = new RestQueryGetBlogs(); return new RestQueryGetBlogs();
break; break;
}elseif($args == 'specs'){ }elseif($args == 'specs'){
$queryObj = new RestQueryGetSpecs(); return new RestQueryGetSpecs();
break; break;
}elseif(preg_match('/^blogs\/(.*)$/', $args )){ }elseif(preg_match('/^blogs\/(.*)$/', $args )){
///blogs/{blog-id} ///blogs/{blog-id}
$queryObj = new RestQueryGetBlog($args); return new RestQueryGetBlog($args);
break; break;
}elseif(preg_match('/^(.*)\/settings$/', $args )){ }elseif(preg_match('/^(.*)\/settings$/', $args )){
$queryObj = new RestQueryGetBlogSettings($args); return new RestQueryGetBlogSettings($args);
break;
}elseif(preg_match('/^(.*)\/settings\/(.*)$/', $args )){
return new RestQueryGetBlogSettings($args);
break; break;
} }
break; break;
case "POST": case "POST":
if($args == 'blogs'){ if($args == 'blogs'){
$queryObj = new RestQueryPostBlogs($body); return new RestQueryPostBlogs($body);
}elseif(preg_match('/^(.*)\/settings\/(.*)$/', $args )){
return new RestQueryPostBlogSettings($args,$body);
break;
} }
break; break;
case "PUT": case "PUT":
if(preg_match('/^blogs\/(.*)$/', $args )){ if(preg_match('/^blogs\/(.*)$/', $args )){
$queryObj = new ResQueryPutBlogs($args,$body); return new ResQueryPutBlogs($args,$body);
break; break;
} }
break; break;
case "PATCH": case "PATCH":
if(preg_match('/^blogs\/(.*)$/', $args )){ if(preg_match('/^blogs\/(.*)$/', $args )){
$queryObj = new ResQueryPatchBlogs($args,$body); return new ResQueryPatchBlogs($args,$body);
break; break;
} }
@ -56,16 +62,16 @@ class rest extends dcUrlHandlers
case "DELETE": case "DELETE":
if(preg_match('/^blogs\/(.*)$/', $args )){ if(preg_match('/^blogs\/(.*)$/', $args )){
$queryObj = new ResQueryDeleteBlogs($args,$body); return new ResQueryDeleteBlogs($args,$body);
break; break;
}elseif(preg_match('/^(.*)\/settings\/(.*)$/', $args )){
return new RestQueryDeleteBlogSettings($args);
} }
break; break;
default: default:
$queryObj = new RestQuery();
break; break;
} }
return new RestQuery(); //will return a 404
return $queryObj;
} }

View File

@ -194,6 +194,95 @@ paths:
description: Unexpected error description: Unexpected error
schema: schema:
$ref: '#/definitions/Error' $ref: '#/definitions/Error'
'/{blog_id}/settings/{name_space}':
get:
summary: 'Get the settings inside the name-space'
description: same
parameters:
- name: x_dc_key
in: header
type: string
required: true
- name: blog_id
in: path
type: string
required: true
- name: name_space
in: path
type: string
required: true
responses:
'200':
description: OK
'404':
description: this blog id does not exists
default:
description: Unexpected error
schema:
$ref: '#/definitions/Error'
post:
summary: Create a new namespace and or a new setting'
description: "Let body empty if you just want to create a namespace without setting."
parameters:
- name: x_dc_key
in: header
type: string
required: true
- name: blog_id
in: path
type: string
required: true
- name: name_space
in: path
type: string
required: true
- name: setting
description: Can be empty, Can pass many settings. [{"id":"string","type":"string","value":"string","value_change":"string Optional","global":"bool optional"},{"id2":...}]
in: body
schema:
$ref: '#/definitions/settings'
responses:
'201':
description: OK
default:
description: Unexpected error
schema:
$ref: '#/definitions/Error'
'/{blog_id}/settings/{name_space}/{setting_id}':
get:
summary: Get value and type for one setting'
description: ""
parameters:
- name: x_dc_key
in: header
type: string
required: true
- name: blog_id
in: path
type: string
required: true
- name: name_space
in: path
type: string
required: true
- name: setting_id
in: path
type: string
required: true
responses:
'200':
description: OK
schema:
$ref: '#/definitions/OneSetting'
default:
description: Unexpected error
schema:
$ref: '#/definitions/Error'
'/{blog-id}/posts': '/{blog-id}/posts':
get: get:
summary: Get list of posts summary: Get list of posts
@ -504,6 +593,24 @@ definitions:
enum: enum:
- query_string - query_string
- path_info - path_info
OneSetting:
type: object
properties:
ns:
type: string
value:
type: string
type:
type: string
label:
type: string
global:
type: boolean
settings:
type: array
items:
type: object
Ids: Ids:
type: object type: object
properties: properties:

View File

@ -299,6 +299,135 @@
} }
} }
}, },
"/{blog_id}/settings/{name_space}": {
"get": {
"summary": "Get the settings inside the name-space",
"description": "same",
"parameters": [
{
"name": "x_dc_key",
"in": "header",
"type": "string",
"required": true
},
{
"name": "blog_id",
"in": "path",
"type": "string",
"required": true
},
{
"name": "name_space",
"in": "path",
"type": "string",
"required": true
}
],
"responses": {
"200": {
"description": "OK"
},
"404": {
"description": "this blog id does not exists"
},
"default": {
"description": "Unexpected error",
"schema": {
"$ref": "#/definitions/Error"
}
}
}
},
"post": {
"summary": "Create a new namespace and or a new setting'",
"description": "Let body empty if you just want to create a namespace without setting.",
"parameters": [
{
"name": "x_dc_key",
"in": "header",
"type": "string",
"required": true
},
{
"name": "blog_id",
"in": "path",
"type": "string",
"required": true
},
{
"name": "name_space",
"in": "path",
"type": "string",
"required": true
},
{
"name": "setting",
"description": "Can be empty, Can pass many settings. [{\"id\":\"string\",\"type\":\"string\",\"value\":\"string\",\"value_change\":\"string Optional\",\"global\":\"bool optional\"},{\"id2\":...}]",
"in": "body",
"schema": {
"$ref": "#/definitions/settings"
}
}
],
"responses": {
"201": {
"description": "OK"
},
"default": {
"description": "Unexpected error",
"schema": {
"$ref": "#/definitions/Error"
}
}
}
}
},
"/{blog_id}/settings/{name_space}/{setting_id}": {
"get": {
"summary": "Get value and type for one setting'",
"description": "",
"parameters": [
{
"name": "x_dc_key",
"in": "header",
"type": "string",
"required": true
},
{
"name": "blog_id",
"in": "path",
"type": "string",
"required": true
},
{
"name": "name_space",
"in": "path",
"type": "string",
"required": true
},
{
"name": "setting_id",
"in": "path",
"type": "string",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/OneSetting"
}
},
"default": {
"description": "Unexpected error",
"schema": {
"$ref": "#/definitions/Error"
}
}
}
}
},
"/{blog-id}/posts": { "/{blog-id}/posts": {
"get": { "get": {
"summary": "Get list of posts", "summary": "Get list of posts",
@ -777,6 +906,32 @@
} }
} }
}, },
"OneSetting": {
"type": "object",
"properties": {
"ns": {
"type": "string"
},
"value": {
"type": "string"
},
"type": {
"type": "string"
},
"label": {
"type": "string"
},
"global": {
"type": "boolean"
}
}
},
"settings": {
"type": "array",
"items": {
"type": "object"
}
},
"Ids": { "Ids": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -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]);
}
}

View File

@ -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;
}
}
}

View File

@ -1,7 +1,7 @@
<?php <?php
class ResQueryDeleteBlogs extends RestQuery class ResQueryDeleteBlogs extends RestQuery
{ {
//$core->delBlog($blog_id);
public function __construct($args){ public function __construct($args){
global $core; global $core;
@ -13,8 +13,6 @@ class ResQueryDeleteBlogs extends RestQuery
//Is allowed? //Is allowed?
if($this->is_allowed() === false){ if($this->is_allowed() === false){
//need To be authentified //need To be authentified
$this->response_code = 403;
$this->response_message = array('code' => 403, 'error' => 'You need to be admin to patch a blog');
return; return;
} }
@ -31,7 +29,7 @@ class ResQueryDeleteBlogs extends RestQuery
$core->delBlog($this->blog_id); $core->delBlog($this->blog_id);
$this->response_code = 201; $this->response_code = 201;
$this->response_message = array( $this->response_message = array(
'code' => 200, 'code' => 201,
'message' => 'Successfully deleted blog '.$this->blog_id 'message' => 'Successfully deleted blog '.$this->blog_id
); );
@ -44,9 +42,5 @@ class ResQueryDeleteBlogs extends RestQuery
'message' => $e->getMessage() 'message' => $e->getMessage()
); );
} }
} }
} }

View File

@ -12,9 +12,6 @@ class RestQueryGetBlog extends RestQuery
$this->required_perms = 'unauth'; $this->required_perms = 'unauth';
if($this->is_allowed() === false){ if($this->is_allowed() === false){
//need To be authentified
$this->response_code = 403;
$this->response_message = array('code' => 403, 'error' => 'This API is not open without KEY');
return; return;
} }
//instance //instance

View File

@ -6,13 +6,11 @@ class RestQueryGetBlogSettings extends RestQuery
{ {
global $core; global $core;
$explodedArgs = explode("/",$args);
$this->blog_id = explode("/",$args)[0]; $this->blog_id = $explodedArgs[0];
//check if user is allowed //check if user is allowed
$this->required_perms = 'admin'; $this->required_perms = 'admin';
if($this->is_allowed() === false){ if($this->is_allowed() === false){
$this->response_code = 403;
$this->response_message = array('code' => 403, 'error' => 'No enough privileges');
return; return;
} }
@ -31,8 +29,26 @@ class RestQueryGetBlogSettings extends RestQuery
$settings[$ns][$k] = $v; $settings[$ns][$k] = $v;
} }
} }
$this->response_code = 200; $this->response_code = 200;
if(isset($explodedArgs[3])){
if(isset($settings[$explodedArgs[2]][$explodedArgs[3]])){
$this->response_message = $settings[$explodedArgs[2]][$explodedArgs[3]];
}else{
$this->response_code = 404;
$this->response_message = array('code' => 404, 'error' => 'Namespace or setting not found');
}
}elseif(isset($explodedArgs[2])){
if(isset($settings[$explodedArgs[2]])){
$this->response_message = $settings[$explodedArgs[2]];
}else{
$this->response_code = 404;
$this->response_message = array('code' => 404, 'error' => 'Namespace found');
}
}else{
$this->response_message = $settings; $this->response_message = $settings;
}
}catch (Exception $e){ }catch (Exception $e){
$this->response_code = 500; $this->response_code = 500;
$this->response_message = array( $this->response_message = array(
@ -40,9 +56,7 @@ class RestQueryGetBlogSettings extends RestQuery
'message' => $e->getMessage() 'message' => $e->getMessage()
); );
} }
return; return;
} }
} }

View File

@ -9,9 +9,6 @@ class RestQueryGetBlogs extends RestQuery
$this->required_perms = 'none'; //I want user have an account $this->required_perms = 'none'; //I want user have an account
if($this->is_allowed() === false){ if($this->is_allowed() === false){
//need To be authentified
$this->response_code = 403;
$this->response_message = array('code' => 403, 'error' => 'get Blogs methods requires to be authentified');
return; return;
} }
//list the blogs the user can access //list the blogs the user can access

View File

@ -4,12 +4,8 @@ class RestQueryGetSpecs extends RestQuery
public function __construct(){ public function __construct(){
global $core; global $core;
$this->response_code = 200; $this->response_code = 200;
$this->required_perms = 'unauth'; $this->required_perms = 'unauth';
if($this->is_allowed() === false){ if($this->is_allowed() === false){
$this->response_code = 403;
$this->response_message = array('code' => 403, 'error' => 'this method is not open');
return; return;
} }

View File

@ -9,14 +9,10 @@ class ResQueryPatchBlogs extends RestQuery
global $core; global $core;
$this->blog_id = substr($args,6); $this->blog_id = substr($args,6);
$this->required_perms = 'admin'; $this->required_perms = 'admin';
//Is allowed? //Is allowed?
if($this->is_allowed() === false){ if($this->is_allowed() === false){;
//need To be authentified
$this->response_code = 403;
$this->response_message = array('code' => 403, 'error' => 'You need to be admin to patch a blog');
return; return;
} }

View File

@ -22,12 +22,13 @@ class RestQuery{
public function __construct() public function __construct()
{ {
$this->response_code = 400; $this->response_code = 404;
$this->response_message = array( $this->response_message = array(
"error" => "Unrecoknized method", "error" => "Method not found",
"code" => 400 "code" => 404
); );
} }
/** /**
* Check if required fields are set * Check if required fields are set
* $strict => Go on error if a additionnal field is given * $strict => Go on error if a additionnal field is given
@ -84,7 +85,7 @@ class RestQuery{
if($ret = json_decode($body,true)){ if($ret = json_decode($body,true)){
return $ret; return $ret;
}else{ }else{
$this->response_code = 301; $this->response_code = 400;
$this->response_message = array( $this->response_message = array(
'error' => 'Can\'t parse input JSON', 'error' => 'Can\'t parse input JSON',
'code' => 400 'code' => 400
@ -99,24 +100,23 @@ class RestQuery{
$perms = $core->auth->getAllPermissions(); $perms = $core->auth->getAllPermissions();
} }
$is_allowed = false;
switch($this->required_perms){ switch($this->required_perms){
case 'unauth': case 'unauth':
//on verifie quand même que l'API est ouverte //on verifie quand même que l'API est ouverte
if((!$core->blog->settings->rest->rest_is_open) && ($core->auth === false)){ if(
return false; (($core->blog->settings->rest->rest_is_open) && ($core->auth === false))
}else{ ||($core->auth !== false)
return true; ){
$is_allowed = true;
} }
break; break;
case 'none': case 'none':
//user must be valid //user must be valid
if($core->auth === false){ if($core->auth){
return false; $is_allowed = true;
}else{
return true;
} }
break; break;
case 'media_admin': case 'media_admin':
@ -134,16 +134,18 @@ class RestQuery{
case 'usage': case 'usage':
break; break;
case 'admin': case 'admin':
if($core->auth === false){ if (($core->auth !== false) && $core->auth->isSuperAdmin()){
return false; $is_allowed = true;
}
if ($core->auth->isSuperAdmin()){
return true;
}else{
return false;
} }
break; break;
} }
if($is_allowed){
return true;
}else{
$this->response_code = 403;
$this->response_message = array('code' => 403, 'error' => 'Unauthorized');
return false;
}
} }
public function get_full_code_header($code=''){ public function get_full_code_header($code=''){
if($code == ''){ if($code == ''){

View File

@ -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);
}
}
}

View File

@ -10,9 +10,6 @@ class RestQueryPostBlogs extends RestQuery
$this->required_perms = 'admin'; //I want user have an account $this->required_perms = 'admin'; //I want user have an account
if($this->is_allowed() === false){ if($this->is_allowed() === false){
//need To be authentified
$this->response_code = 403;
$this->response_message = array('code' => 403, 'error' => 'You need to be admin to create a new blog');
return; return;
} }
@ -78,7 +75,7 @@ class RestQueryPostBlogs extends RestQuery
$this->response_code = 201; $this->response_code = 201;
$this->response_message = array( $this->response_message = array(
'code' => 201, 'code' => 201,
'id' => $blog_id 'id' => $blog_id,
'message' => 'Successfully created blog'.$blog_id 'message' => 'Successfully created blog'.$blog_id
); );

View File

@ -9,14 +9,9 @@ class ResQueryPutBlogs extends RestQuery
global $core; global $core;
$this->blog_id = substr($args,6); $this->blog_id = substr($args,6);
$this->required_perms = 'admin'; $this->required_perms = 'admin';
//Is allowed? //Is allowed?
if($this->is_allowed() === false){ if($this->is_allowed() === false){
//need To be authentified
$this->response_code = 403;
$this->response_message = array('code' => 403, 'error' => 'You need to be admin to put a blog');
return; return;
} }

11
unit-tests/README.md Normal file
View File

@ -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.

378
unit-tests/test-API.php Normal file
View File

@ -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;
}
}