mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 15:34:12 +01:00
N°3455 - Passing json_data as file to REST API
This commit is contained in:
169
test/webservices/RestTest.php
Normal file
169
test/webservices/RestTest.php
Normal file
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
namespace Combodo\iTop\Test\UnitTest\Webservices;
|
||||
|
||||
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
|
||||
use Exception;
|
||||
|
||||
|
||||
/**
|
||||
* @runTestsInSeparateProcesses
|
||||
* @preserveGlobalState disabled
|
||||
* @backupGlobals disabled
|
||||
*/
|
||||
class RestTest extends ItopDataTestCase
|
||||
{
|
||||
const CREATE_TEST_ORG = true;
|
||||
const USE_TRANSACTION = true;
|
||||
|
||||
private $sTmpFile = "";
|
||||
private $bCallApiViaFile = false;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
if (!empty($this->sTmpFile)){
|
||||
unlink($this->sTmpFile);
|
||||
}
|
||||
}
|
||||
|
||||
private function GetTicketViaRest($iId){
|
||||
$sJsonGetContent = <<<JSON
|
||||
{
|
||||
"operation": "core/get",
|
||||
"class": "UserRequest",
|
||||
"key": "SELECT UserRequest WHERE id=$iId",
|
||||
"output_fields": "id, description"
|
||||
}
|
||||
JSON;
|
||||
|
||||
return $this->CallRestApi($sJsonGetContent);
|
||||
}
|
||||
|
||||
private function UpdateTicketViaApi($iId, $description){
|
||||
$sJsonUpdateContent = <<<JSON
|
||||
{"operation": "core/update","comment": "test","class": "UserRequest","key":"$iId","output_fields": "description","fields":{"description": "$description"}}
|
||||
JSON;
|
||||
|
||||
return $this->CallRestApi($sJsonUpdateContent);
|
||||
}
|
||||
|
||||
private function CreateTicketViaApi($description){
|
||||
$sJsonCreateContent = <<<JSON
|
||||
{
|
||||
"operation": "core/create",
|
||||
"comment": "test",
|
||||
"class": "UserRequest",
|
||||
"output_fields": "id",
|
||||
"fields":
|
||||
{
|
||||
"org_id": "SELECT Organization WHERE name = \"Demo\"",
|
||||
|
||||
"title": "Houston, got a problem",
|
||||
"description": "$description"
|
||||
}
|
||||
}
|
||||
JSON;
|
||||
|
||||
return $this->CallRestApi($sJsonCreateContent);
|
||||
}
|
||||
|
||||
private function DeleteTicketFromApi($iId){
|
||||
$sJson = <<<JSON
|
||||
{
|
||||
"operation": "core/delete",
|
||||
"comment": "Cleanup",
|
||||
"class": "UserRequest",
|
||||
"key":$iId,
|
||||
"simulate": false
|
||||
}
|
||||
JSON;
|
||||
return $this->CallRestApi($sJson);
|
||||
|
||||
}
|
||||
|
||||
public function BasicProvider(){
|
||||
return [
|
||||
'call rest call' => [ 'bCallApiViaFile' => false],
|
||||
'pass json_data as file' => [ 'bCallApiViaFile' => true]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider BasicProvider
|
||||
*/
|
||||
public function testBasic($bCallApiViaFile)
|
||||
{
|
||||
$this->bCallApiViaFile = $bCallApiViaFile;
|
||||
|
||||
//create ticket
|
||||
$description = date('dmY H:i:s');
|
||||
|
||||
$sOuputJson = $this->CreateTicketViaApi($description);
|
||||
$aJson = json_decode($sOuputJson, true);
|
||||
$this->assertContains("0", "".$aJson['code']);
|
||||
$sUserRequestKey = array_key_first($aJson['objects']);
|
||||
$this->assertContains('UserRequest::', $sUserRequestKey);
|
||||
$iId = $aJson['objects'][$sUserRequestKey]['key'];
|
||||
$sExpectedJsonOuput=<<<JSON
|
||||
{"objects":{"UserRequest::$iId":{"code":0,"message":"created","class":"UserRequest","key":"$iId","fields":{"id":"$iId"}}},"code":0,"message":null}
|
||||
JSON;
|
||||
$this->assertEquals($sExpectedJsonOuput, $sOuputJson);
|
||||
|
||||
$sExpectedJsonOuput=<<<JSON
|
||||
{"objects":{"UserRequest::$iId":{"code":0,"message":"","class":"UserRequest","key":"$iId","fields":{"id":"$iId","description":"<p>$description<\/p>"}}},"code":0,"message":"Found: 1"}
|
||||
JSON;
|
||||
$this->assertEquals($sExpectedJsonOuput, $this->GetTicketViaRest($iId));
|
||||
|
||||
//update ticket
|
||||
$description = date('Ymd H:i:s');
|
||||
$sExpectedJsonOuput=<<<JSON
|
||||
{"objects":{"UserRequest::$iId":{"code":0,"message":"updated","class":"UserRequest","key":"$iId","fields":{"description":"<p>$description<\/p>"}}},"code":0,"message":null}
|
||||
JSON;
|
||||
$this->assertEquals($sExpectedJsonOuput, $this->UpdateTicketViaApi($iId, $description));
|
||||
|
||||
//delete ticket
|
||||
$sExpectedJsonOuput=<<<JSON
|
||||
{"objects":{"UserRequest::$iId"
|
||||
JSON;
|
||||
$this->assertContains($sExpectedJsonOuput, $this->DeleteTicketFromApi($iId));
|
||||
|
||||
$sExpectedJsonOuput=<<<JSON
|
||||
{"objects":null,"code":0,"message":"Found: 0"}
|
||||
JSON;
|
||||
$this->assertEquals($sExpectedJsonOuput, $this->GetTicketViaRest($iId));
|
||||
}
|
||||
|
||||
private function CallRestApi($sJsonDataContent){
|
||||
$ch = curl_init();
|
||||
$aPostFields = [
|
||||
'version' => '1.3',
|
||||
'auth_user' => 'admin',
|
||||
'auth_pwd' => 'admin',
|
||||
];
|
||||
|
||||
if ($this->bCallApiViaFile){
|
||||
$this->sTmpFile = tempnam(sys_get_temp_dir(), 'jsondata_');
|
||||
file_put_contents($this->sTmpFile, $sJsonDataContent);
|
||||
|
||||
$oCurlFile = curl_file_create($this->sTmpFile);
|
||||
$aPostFields['json_data'] = $oCurlFile;
|
||||
}else{
|
||||
$aPostFields['json_data'] = $sJsonDataContent;
|
||||
}
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, "http://localhost/iTop/webservices/rest.php");
|
||||
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostFields);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
$sJson = curl_exec($ch);
|
||||
curl_close ($ch);
|
||||
|
||||
return $sJson;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -70,7 +70,21 @@ $oCtx = new ContextTag(ContextTag::TAG_REST);
|
||||
|
||||
$sVersion = utils::ReadParam('version', null, false, 'raw_data');
|
||||
$sOperation = utils::ReadParam('operation', null);
|
||||
$sJsonString = utils::ReadParam('json_data', null, false, 'raw_data');
|
||||
if(isset($_FILES['json_data']['name']) && !empty($_FILES['json_data']['name']))
|
||||
{
|
||||
$sTmpFilePath = $_FILES['json_data']['tmp_name'];
|
||||
if (is_file($sTmpFilePath)){
|
||||
$sValue = file_get_contents($sTmpFilePath);
|
||||
unlink($sTmpFilePath);
|
||||
if (! empty($sValue)){
|
||||
$sJsonString = utils::Sanitize($sValue, null, 'raw_data');
|
||||
}
|
||||
}
|
||||
}
|
||||
if (empty($sJsonString)){
|
||||
$sJsonString = utils::ReadParam('json_data', null, false, 'raw_data');
|
||||
}
|
||||
|
||||
$sProvider = '';
|
||||
|
||||
$oKPI = new ExecutionKPI();
|
||||
@@ -125,7 +139,7 @@ try
|
||||
{
|
||||
throw new Exception("Missing parameter 'version' (e.g. '1.0')", RestResult::MISSING_VERSION);
|
||||
}
|
||||
|
||||
|
||||
if ($sJsonString == null)
|
||||
{
|
||||
throw new Exception("Missing parameter 'json_data'", RestResult::MISSING_JSON);
|
||||
|
||||
Reference in New Issue
Block a user