diff --git a/_prepend.php b/_prepend.php index 72048ff..74d6eee 100644 --- a/_prepend.php +++ b/_prepend.php @@ -3,6 +3,7 @@ global $__autoload; $__autoload['ApiKey'] = dirname(__FILE__).'/inc/class.rest.key.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['RestQueryGetBlogs'] = dirname(__FILE__).'/inc/class.rest.query.get.blogs.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['ResQueryDeleteBlogs'] = dirname(__FILE__).'/inc/class.rest.query.delete.blogs.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'; diff --git a/_public.php b/_public.php index 8cf4554..c49229b 100644 --- a/_public.php +++ b/_public.php @@ -18,37 +18,43 @@ class rest extends dcUrlHandlers switch($httpMethod){ case "GET": if($args == 'blogs'){ - $queryObj = new RestQueryGetBlogs(); + return new RestQueryGetBlogs(); break; }elseif($args == 'specs'){ - $queryObj = new RestQueryGetSpecs(); + return new RestQueryGetSpecs(); break; }elseif(preg_match('/^blogs\/(.*)$/', $args )){ ///blogs/{blog-id} - $queryObj = new RestQueryGetBlog($args); + return new RestQueryGetBlog($args); break; }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; case "POST": if($args == 'blogs'){ - $queryObj = new RestQueryPostBlogs($body); + return new RestQueryPostBlogs($body); + }elseif(preg_match('/^(.*)\/settings\/(.*)$/', $args )){ + return new RestQueryPostBlogSettings($args,$body); + break; } break; case "PUT": if(preg_match('/^blogs\/(.*)$/', $args )){ - $queryObj = new ResQueryPutBlogs($args,$body); + return new ResQueryPutBlogs($args,$body); break; } break; case "PATCH": if(preg_match('/^blogs\/(.*)$/', $args )){ - $queryObj = new ResQueryPatchBlogs($args,$body); + return new ResQueryPatchBlogs($args,$body); break; } @@ -56,16 +62,16 @@ class rest extends dcUrlHandlers case "DELETE": if(preg_match('/^blogs\/(.*)$/', $args )){ - $queryObj = new ResQueryDeleteBlogs($args,$body); + return new ResQueryDeleteBlogs($args,$body); break; - } + }elseif(preg_match('/^(.*)\/settings\/(.*)$/', $args )){ + return new RestQueryDeleteBlogSettings($args); + } break; default: - $queryObj = new RestQuery(); break; } - - return $queryObj; + return new RestQuery(); //will return a 404 } diff --git a/documentation/definition.yaml b/documentation/definition.yaml index 488867d..575b992 100644 --- a/documentation/definition.yaml +++ b/documentation/definition.yaml @@ -194,6 +194,95 @@ paths: description: Unexpected error schema: $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': get: summary: Get list of posts @@ -504,6 +593,24 @@ definitions: enum: - query_string - 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: type: object properties: diff --git a/documentation/swagger.json b/documentation/swagger.json index 885eeb9..23f7d3d 100644 --- a/documentation/swagger.json +++ b/documentation/swagger.json @@ -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": { "get": { "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": { "type": "object", "properties": { diff --git a/inc/class.rest.DcNameSpace.php b/inc/class.rest.DcNameSpace.php new file mode 100644 index 0000000..5302e21 --- /dev/null +++ b/inc/class.rest.DcNameSpace.php @@ -0,0 +1,12 @@ +{$array.'_settings'}[$id]); + } + + +} \ No newline at end of file diff --git a/inc/class.rest.query.delete.blog.settings.php b/inc/class.rest.query.delete.blog.settings.php new file mode 100644 index 0000000..e2f29ca --- /dev/null +++ b/inc/class.rest.query.delete.blog.settings.php @@ -0,0 +1,57 @@ +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; + } + } +} \ No newline at end of file diff --git a/inc/class.rest.query.delete.blogs.php b/inc/class.rest.query.delete.blogs.php index ccbae10..d21ad53 100644 --- a/inc/class.rest.query.delete.blogs.php +++ b/inc/class.rest.query.delete.blogs.php @@ -1,7 +1,7 @@ delBlog($blog_id); + public function __construct($args){ global $core; @@ -13,8 +13,6 @@ class ResQueryDeleteBlogs extends RestQuery //Is allowed? 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; } @@ -31,7 +29,7 @@ class ResQueryDeleteBlogs extends RestQuery $core->delBlog($this->blog_id); $this->response_code = 201; $this->response_message = array( - 'code' => 200, + 'code' => 201, 'message' => 'Successfully deleted blog '.$this->blog_id ); @@ -44,9 +42,5 @@ class ResQueryDeleteBlogs extends RestQuery 'message' => $e->getMessage() ); } - - } - - } \ No newline at end of file diff --git a/inc/class.rest.query.get.blog.php b/inc/class.rest.query.get.blog.php index 7669360..26fa866 100644 --- a/inc/class.rest.query.get.blog.php +++ b/inc/class.rest.query.get.blog.php @@ -12,9 +12,6 @@ class RestQueryGetBlog extends RestQuery $this->required_perms = 'unauth'; 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; } //instance diff --git a/inc/class.rest.query.get.blog.settings.php b/inc/class.rest.query.get.blog.settings.php index e661e49..86cc5ef 100644 --- a/inc/class.rest.query.get.blog.settings.php +++ b/inc/class.rest.query.get.blog.settings.php @@ -6,13 +6,11 @@ class RestQueryGetBlogSettings extends RestQuery { global $core; - - $this->blog_id = explode("/",$args)[0]; + $explodedArgs = explode("/",$args); + $this->blog_id = $explodedArgs[0]; //check if user is allowed $this->required_perms = 'admin'; if($this->is_allowed() === false){ - $this->response_code = 403; - $this->response_message = array('code' => 403, 'error' => 'No enough privileges'); return; } @@ -31,8 +29,26 @@ class RestQueryGetBlogSettings extends RestQuery $settings[$ns][$k] = $v; } } + $this->response_code = 200; - $this->response_message = $settings; + + 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; + } }catch (Exception $e){ $this->response_code = 500; $this->response_message = array( @@ -40,9 +56,7 @@ class RestQueryGetBlogSettings extends RestQuery 'message' => $e->getMessage() ); } - - return; + return; } - } \ No newline at end of file diff --git a/inc/class.rest.query.get.blogs.php b/inc/class.rest.query.get.blogs.php index 9186e48..d84c9f6 100644 --- a/inc/class.rest.query.get.blogs.php +++ b/inc/class.rest.query.get.blogs.php @@ -9,9 +9,6 @@ class RestQueryGetBlogs extends RestQuery $this->required_perms = 'none'; //I want user have an account 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; } //list the blogs the user can access diff --git a/inc/class.rest.query.get.specs.php b/inc/class.rest.query.get.specs.php index 100e514..18f20db 100644 --- a/inc/class.rest.query.get.specs.php +++ b/inc/class.rest.query.get.specs.php @@ -3,13 +3,9 @@ class RestQueryGetSpecs extends RestQuery { public function __construct(){ global $core; - $this->response_code = 200; - + $this->response_code = 200; $this->required_perms = 'unauth'; - if($this->is_allowed() === false){ - $this->response_code = 403; - $this->response_message = array('code' => 403, 'error' => 'this method is not open'); return; } diff --git a/inc/class.rest.query.patch.blogs.php b/inc/class.rest.query.patch.blogs.php index 01b98e3..fe9e665 100644 --- a/inc/class.rest.query.patch.blogs.php +++ b/inc/class.rest.query.patch.blogs.php @@ -9,14 +9,10 @@ class ResQueryPatchBlogs extends RestQuery global $core; $this->blog_id = substr($args,6); - $this->required_perms = 'admin'; //Is allowed? - 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'); + if($this->is_allowed() === false){; return; } @@ -29,7 +25,7 @@ class ResQueryPatchBlogs extends RestQuery //is it valid fields? if(!$this->check_for_required_fields( $inputArray, array(), array('blog_id','blog_url','blog_name','blog_desc','lang','blog_timezone','url_scan')) ){ - return; + return; } //does the blog exists? diff --git a/inc/class.rest.query.php b/inc/class.rest.query.php index f2bb93f..d2b9110 100644 --- a/inc/class.rest.query.php +++ b/inc/class.rest.query.php @@ -22,12 +22,13 @@ class RestQuery{ public function __construct() { - $this->response_code = 400; + $this->response_code = 404; $this->response_message = array( - "error" => "Unrecoknized method", - "code" => 400 + "error" => "Method not found", + "code" => 404 ); } + /** * Check if required fields are set * $strict => Go on error if a additionnal field is given @@ -84,7 +85,7 @@ class RestQuery{ if($ret = json_decode($body,true)){ return $ret; }else{ - $this->response_code = 301; + $this->response_code = 400; $this->response_message = array( 'error' => 'Can\'t parse input JSON', 'code' => 400 @@ -99,24 +100,23 @@ class RestQuery{ $perms = $core->auth->getAllPermissions(); } - + $is_allowed = false; switch($this->required_perms){ + case 'unauth': //on verifie quand même que l'API est ouverte - if((!$core->blog->settings->rest->rest_is_open) && ($core->auth === false)){ - return false; - }else{ - return true; + if( + (($core->blog->settings->rest->rest_is_open) && ($core->auth === false)) + ||($core->auth !== false) + ){ + $is_allowed = true; } - - break; + break; case 'none': //user must be valid - if($core->auth === false){ - return false; - }else{ - return true; + if($core->auth){ + $is_allowed = true; } break; case 'media_admin': @@ -134,16 +134,18 @@ class RestQuery{ case 'usage': break; case 'admin': - if($core->auth === false){ - return false; - } - if ($core->auth->isSuperAdmin()){ - return true; - }else{ - return false; + if (($core->auth !== false) && $core->auth->isSuperAdmin()){ + $is_allowed = true; } 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=''){ if($code == ''){ diff --git a/inc/class.rest.query.post.blog.settings.php b/inc/class.rest.query.post.blog.settings.php new file mode 100644 index 0000000..6aa6058 --- /dev/null +++ b/inc/class.rest.query.post.blog.settings.php @@ -0,0 +1,95 @@ +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); + } + } +} diff --git a/inc/class.rest.query.post.blogs.php b/inc/class.rest.query.post.blogs.php index 44191b8..bb4bcec 100644 --- a/inc/class.rest.query.post.blogs.php +++ b/inc/class.rest.query.post.blogs.php @@ -10,9 +10,6 @@ class RestQueryPostBlogs extends RestQuery $this->required_perms = 'admin'; //I want user have an account 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; } @@ -78,7 +75,7 @@ class RestQueryPostBlogs extends RestQuery $this->response_code = 201; $this->response_message = array( 'code' => 201, - 'id' => $blog_id + 'id' => $blog_id, 'message' => 'Successfully created blog'.$blog_id ); diff --git a/inc/class.rest.query.put.blogs.php b/inc/class.rest.query.put.blogs.php index f520290..fba2d63 100644 --- a/inc/class.rest.query.put.blogs.php +++ b/inc/class.rest.query.put.blogs.php @@ -9,14 +9,9 @@ class ResQueryPutBlogs extends RestQuery global $core; $this->blog_id = substr($args,6); - - $this->required_perms = 'admin'; - + $this->required_perms = 'admin'; //Is allowed? 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; } diff --git a/unit-tests/README.md b/unit-tests/README.md new file mode 100644 index 0000000..383019d --- /dev/null +++ b/unit-tests/README.md @@ -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. \ No newline at end of file diff --git a/unit-tests/test-API.php b/unit-tests/test-API.php new file mode 100644 index 0000000..40f0c90 --- /dev/null +++ b/unit-tests/test-API.php @@ -0,0 +1,378 @@ + ''){ + 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; + } + +}