From 096ed9a63afc44d79b63271854144ba2e26871c6 Mon Sep 17 00:00:00 2001 From: Molkobain Date: Tue, 18 Apr 2023 22:14:39 +0200 Subject: [PATCH 1/2] =?UTF-8?q?N=C2=B06204=20-=20Improve=20REST=20API=20un?= =?UTF-8?q?it=20test=20readability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unitary-tests/webservices/RestTest.php | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/tests/php-unit-tests/unitary-tests/webservices/RestTest.php b/tests/php-unit-tests/unitary-tests/webservices/RestTest.php index 2bb7b10d2..658b5dfab 100644 --- a/tests/php-unit-tests/unitary-tests/webservices/RestTest.php +++ b/tests/php-unit-tests/unitary-tests/webservices/RestTest.php @@ -4,6 +4,8 @@ namespace Combodo\iTop\Test\UnitTest\Webservices; use Combodo\iTop\Test\UnitTest\ItopDataTestCase; use Exception; +use MetaModel; +use utils; /** @@ -19,11 +21,13 @@ class RestTest extends ItopDataTestCase { const USE_TRANSACTION = false; - const MODE = [ 'JSONDATA_AS_STRING' => 0, 'JSONDATA_AS_FILE' => 1 , 'NO_JSONDATA' => 2 ]; + const ENUM_JSONDATA_AS_STRING = 0; + const ENUM_JSONDATA_AS_FILE = 1; + const ENUM_JSONDATA_NONE = 2; private $sTmpFile = ""; /** @var int $iJsonDataMode */ - private $sJsonDataMode; + private $iJsonDataMode; private $sUrl; private $sLogin; private $sPassword = "Iuytrez9876543ç_è-("; @@ -42,10 +46,10 @@ class RestTest extends ItopDataTestCase unlink($this->sTmpFile); } - $this->sUrl = \MetaModel::GetConfig()->Get('app_root_url'); + $this->sUrl = MetaModel::GetConfig()->Get('app_root_url'); - $oRestProfile = \MetaModel::GetObjectFromOQL("SELECT URP_Profiles WHERE name = :name", array('name' => 'REST Services User'), true); - $oAdminProfile = \MetaModel::GetObjectFromOQL("SELECT URP_Profiles WHERE name = :name", array('name' => 'Administrator'), true); + $oRestProfile = MetaModel::GetObjectFromOQL("SELECT URP_Profiles WHERE name = :name", array('name' => 'REST Services User'), true); + $oAdminProfile = MetaModel::GetObjectFromOQL("SELECT URP_Profiles WHERE name = :name", array('name' => 'Administrator'), true); if (is_object($oRestProfile) && is_object($oAdminProfile)) { $oUser = $this->CreateUser($this->sLogin, $oRestProfile->GetKey(), $this->sPassword); @@ -67,7 +71,7 @@ class RestTest extends ItopDataTestCase $aJson = json_decode($sOuputJson, true); $this->assertNotNull($aJson, "Cannot decode returned JSON : $sOuputJson"); - if ($this->iJsonDataMode === self::MODE['NO_JSONDATA']){ + if ($this->iJsonDataMode === static::ENUM_JSONDATA_NONE){ $this->assertStringContainsString("3", "".$aJson['code'], $sOuputJson); $this->assertStringContainsString("Error: Missing parameter 'json_data'", "".$aJson['message'], $sOuputJson); return; @@ -121,7 +125,7 @@ JSON; $sOuputJson = $this->CreateTicketViaApi($description); $aJson = json_decode($sOuputJson, true); - if ($this->iJsonDataMode === self::MODE['NO_JSONDATA']){ + if ($this->iJsonDataMode === static::ENUM_JSONDATA_NONE){ $this->assertStringContainsString("3", "".$aJson['code'], $sOuputJson); $this->assertStringContainsString("Error: Missing parameter 'json_data'", "".$aJson['message'], $sOuputJson); return; @@ -160,7 +164,7 @@ JSON; $sOuputJson = $this->CreateTicketViaApi($description); $aJson = json_decode($sOuputJson, true); - if ($this->iJsonDataMode === self::MODE['NO_JSONDATA']){ + if ($this->iJsonDataMode === static::ENUM_JSONDATA_NONE){ $this->assertStringContainsString("3", "".$aJson['code'], $sOuputJson); $this->assertStringContainsString("Error: Missing parameter 'json_data'", "".$aJson['message'], $sOuputJson); return; @@ -198,9 +202,9 @@ JSON; public function BasicProvider(){ return [ - 'call rest call' => [ 'sJsonDataMode' => self::MODE['JSONDATA_AS_STRING']], - 'pass json_data as file' => [ 'sJsonDataMode' => self::MODE['JSONDATA_AS_FILE']], - 'no json data' => [ 'sJsonDataMode' => self::MODE['NO_JSONDATA']] + 'call rest call' => [ 'sJsonDataMode' => static::ENUM_JSONDATA_AS_STRING], + 'pass json_data as file' => [ 'sJsonDataMode' => static::ENUM_JSONDATA_AS_FILE], + 'no json data' => [ 'sJsonDataMode' => static::ENUM_JSONDATA_NONE] ]; } @@ -254,13 +258,13 @@ JSON; 'auth_pwd' => $this->sPassword, ]; - if ($this->iJsonDataMode === self::MODE['JSONDATA_AS_STRING']){ + if ($this->iJsonDataMode === static::ENUM_JSONDATA_AS_STRING) { $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 if ($this->iJsonDataMode === self::MODE['JSONDATA_AS_FILE']){ + } else if ($this->iJsonDataMode === static::ENUM_JSONDATA_AS_FILE) { $aPostFields['json_data'] = $sJsonDataContent; } From 778118cfb401bcf2d771b513e6b067fb819783ed Mon Sep 17 00:00:00 2001 From: Molkobain Date: Tue, 18 Apr 2023 22:34:11 +0200 Subject: [PATCH 2/2] =?UTF-8?q?N=C2=B06204=20-=20REST=20API:=20Add=20unit?= =?UTF-8?q?=20test=20for=20callback=20parameter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unitary-tests/webservices/RestTest.php | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/tests/php-unit-tests/unitary-tests/webservices/RestTest.php b/tests/php-unit-tests/unitary-tests/webservices/RestTest.php index 658b5dfab..bfb980cb7 100644 --- a/tests/php-unit-tests/unitary-tests/webservices/RestTest.php +++ b/tests/php-unit-tests/unitary-tests/webservices/RestTest.php @@ -27,7 +27,7 @@ class RestTest extends ItopDataTestCase private $sTmpFile = ""; /** @var int $iJsonDataMode */ - private $iJsonDataMode; + private $iJsonDataMode = self::ENUM_JSONDATA_AS_STRING; private $sUrl; private $sLogin; private $sPassword = "Iuytrez9876543ç_è-("; @@ -57,6 +57,28 @@ class RestTest extends ItopDataTestCase } } + public function testJSONPCallback() + { + $sCallbackName = 'fooCallback'; + $sJsonData = <<CallRestApi($sJsonData); + // - Try to decode JSON to array to check if it is well-formed + $aJSONResultAsArray = json_decode($sJSONResult, true); + if (false === is_array($aJSONResultAsArray)) { + $this->fail('JSON result could not be decoded as array, it might be malformed'); + } + + // Test JSONP with callback by checking that it is the same as the regular JSON but within the JS callback + $sJSONPResult = $this->CallRestApi($sJsonData, $sCallbackName); + $this->assertEquals($sCallbackName.'('.$sJSONResult.')', $sJSONPResult, 'JSONP response callback does not match expected result'); + } + /** * @dataProvider BasicProvider * @param int $iJsonDataMode @@ -250,7 +272,7 @@ JSON; } - private function CallRestApi($sJsonDataContent){ + private function CallRestApi(string $sJsonDataContent, string $sCallbackName = null){ $ch = curl_init(); $aPostFields = [ 'version' => '1.3', @@ -268,6 +290,10 @@ JSON; $aPostFields['json_data'] = $sJsonDataContent; } + if (utils::IsNotNullOrEmptyString($sCallbackName)) { + $aPostFields['callback'] = $sCallbackName; + } + curl_setopt($ch, CURLOPT_URL, "$this->sUrl/webservices/rest.php"); curl_setopt($ch, CURLOPT_POST, 1);// set post data to true curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostFields);