From ba01ac715fc209f3d96032dbedeabe51b6fb081e Mon Sep 17 00:00:00 2001 From: odain Date: Wed, 25 Nov 2020 18:32:54 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B03455=20-=20Passing=20json=5Fdata=20as=20?= =?UTF-8?q?file=20to=20REST=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/webservices/RestTest.php | 169 ++++++++++++++++++++++++++++++++++ webservices/rest.php | 18 +++- 2 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 test/webservices/RestTest.php diff --git a/test/webservices/RestTest.php b/test/webservices/RestTest.php new file mode 100644 index 000000000..0adc3525d --- /dev/null +++ b/test/webservices/RestTest.php @@ -0,0 +1,169 @@ +sTmpFile)){ + unlink($this->sTmpFile); + } + } + + private function GetTicketViaRest($iId){ + $sJsonGetContent = <<CallRestApi($sJsonGetContent); + } + + private function UpdateTicketViaApi($iId, $description){ + $sJsonUpdateContent = <<CallRestApi($sJsonUpdateContent); + } + + private function CreateTicketViaApi($description){ + $sJsonCreateContent = <<CallRestApi($sJsonCreateContent); + } + + private function DeleteTicketFromApi($iId){ + $sJson = <<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=<<assertEquals($sExpectedJsonOuput, $sOuputJson); + + $sExpectedJsonOuput=<<$description<\/p>"}}},"code":0,"message":"Found: 1"} +JSON; + $this->assertEquals($sExpectedJsonOuput, $this->GetTicketViaRest($iId)); + + //update ticket + $description = date('Ymd H:i:s'); + $sExpectedJsonOuput=<<$description<\/p>"}}},"code":0,"message":null} +JSON; + $this->assertEquals($sExpectedJsonOuput, $this->UpdateTicketViaApi($iId, $description)); + + //delete ticket + $sExpectedJsonOuput=<<assertContains($sExpectedJsonOuput, $this->DeleteTicketFromApi($iId)); + + $sExpectedJsonOuput=<<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; + } + +} diff --git a/webservices/rest.php b/webservices/rest.php index 647a035f2..32cc6ac77 100644 --- a/webservices/rest.php +++ b/webservices/rest.php @@ -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);