From 57a085eec1d450b7af83539a001febbfc18e8172 Mon Sep 17 00:00:00 2001 From: odain Date: Mon, 7 Dec 2020 00:44:39 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B03464:=20move=20fix=20in=20itop-fence=20+?= =?UTF-8?q?=20fix/enhance=20rest=20api=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/applicationextension.inc.php | 5 --- test/ItopDataTestCase.php | 33 ++++++++++++++-- test/webservices/RestTest.php | 50 +++++++++++++++++++----- 3 files changed, 70 insertions(+), 18 deletions(-) diff --git a/application/applicationextension.inc.php b/application/applicationextension.inc.php index d40870065..7b1baa97d 100644 --- a/application/applicationextension.inc.php +++ b/application/applicationextension.inc.php @@ -1345,11 +1345,6 @@ class RestUtils { $sComment = self::GetMandatoryParam($oData, 'comment'); CMDBObject::SetTrackInfo($sComment); - - /** @var \CMDBChange $oChange */ - $oChange = CMDBObject::GetCurrentChange(); - $oChange->Set("userinfo", $sComment); - $oChange->DBUpdate(); } /** diff --git a/test/ItopDataTestCase.php b/test/ItopDataTestCase.php index eddbffcc6..70b0a06f4 100644 --- a/test/ItopDataTestCase.php +++ b/test/ItopDataTestCase.php @@ -73,7 +73,7 @@ class ItopDataTestCase extends ItopTestCase protected function setUp() { parent::setUp(); - //require_once(APPROOT.'/application/startup.inc.php'); + require_once(APPROOT.'/application/startup.inc.php'); require_once(APPROOT.'application/utils.inc.php'); @@ -408,8 +408,12 @@ class ItopDataTestCase extends ItopTestCase * @return \DBObject * @throws Exception */ - protected function CreateUser($sLogin, $iProfileId) + protected function CreateUser($sLogin, $iProfileId, $sPassword=null) { + if (empty($sPassword)){ + $sPassword = $sLogin; + } + $oUserProfile = new URP_UserProfile(); $oUserProfile->Set('profileid', $iProfileId); $oUserProfile->Set('reason', 'UNIT Tests'); @@ -417,7 +421,7 @@ class ItopDataTestCase extends ItopTestCase $oUser = $this->createObject('UserLocal', array( 'contactid' => 2, 'login' => $sLogin, - 'password' => $sLogin, + 'password' => $sPassword, 'language' => 'EN US', 'profile_list' => $oSet, )); @@ -426,6 +430,29 @@ class ItopDataTestCase extends ItopTestCase return $oUser; } + /** + * @param \DBObject $oUser + * @param int $iProfileId + * + * @return \DBObject + * @throws Exception + */ + protected function AddProfileToUser($oUser, $iProfileId) + { + $oUserProfile = new URP_UserProfile(); + $oUserProfile->Set('profileid', $iProfileId); + $oUserProfile->Set('reason', 'UNIT Tests'); + /** @var DBObjectSet $oSet */ + $oSet = $oUser->Get('profile_list'); + $oSet->AddObject($oUserProfile); + $oUser = $this->updateObject('UserLocal', $oUser->GetKey(), array( + 'profile_list' => $oSet, + )); + $this->debug("Updated {$oUser->GetName()} ({$oUser->GetKey()})"); + + return $oUser; + } + /** * Create a Hypervisor in database diff --git a/test/webservices/RestTest.php b/test/webservices/RestTest.php index 08625c3fa..46ebb24d9 100644 --- a/test/webservices/RestTest.php +++ b/test/webservices/RestTest.php @@ -13,8 +13,12 @@ use Exception; */ class RestTest extends ItopDataTestCase { + const USE_TRANSACTION = false; + private $sTmpFile = ""; private $bPassJsonDataAsFile = false; + private $sLogin; + private $sPassword = "Iuytrez9876543ç_è-("; /** * @throws Exception @@ -22,10 +26,22 @@ class RestTest extends ItopDataTestCase protected function setUp() { parent::setUp(); + require_once(APPROOT.'application/startup.inc.php'); + $this->sLogin = "rest-user-" . date('dmYHis'); + $this->CreateTestOrganization(); if (!empty($this->sTmpFile)){ unlink($this->sTmpFile); } + + $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); + $this->AddProfileToUser($oUser, $oAdminProfile->GetKey()); + } } /** @@ -40,8 +56,8 @@ class RestTest extends ItopDataTestCase $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("0", "".$aJson['code'], $sOuputJson); + $sUserRequestKey = $this->array_key_first($aJson['objects']); $this->assertContains('UserRequest::', $sUserRequestKey); $iId = $aJson['objects'][$sUserRequestKey]['key']; $sExpectedJsonOuput=<<DeleteTicketFromApi($iId); } + /** + * array_key_first comes with PHP7.3 + * itop should also work with previous PHP versions + */ + private function array_key_first($aTab){ + if (!is_array($aTab) || empty($aTab)){ + return false; + } + + foreach ($aTab as $sKey => $sVal){ + return $sKey; + } + } + /** * @dataProvider BasicProvider * @param bool $bPassJsonDataAsFile @@ -73,8 +103,8 @@ JSON; $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("0", "".$aJson['code'], $sOuputJson); + $sUserRequestKey = $this->array_key_first($aJson['objects']); $this->assertContains('UserRequest::', $sUserRequestKey); $iId = $aJson['objects'][$sUserRequestKey]['key']; @@ -105,8 +135,8 @@ JSON; $sOuputJson = $this->CreateTicketViaApi($description); $aJson = json_decode($sOuputJson, true); - $this->assertContains("0", "".$aJson['code']); - $sUserRequestKey = array_key_first($aJson['objects']); + $this->assertContains("0", "".$aJson['code'], $sOuputJson); + $sUserRequestKey = $this->array_key_first($aJson['objects']); $this->assertContains('UserRequest::', $sUserRequestKey); $iId = $aJson['objects'][$sUserRequestKey]['key']; @@ -188,8 +218,8 @@ JSON; $ch = curl_init(); $aPostFields = [ 'version' => '1.3', - 'auth_user' => 'admin', - 'auth_pwd' => 'admin', + 'auth_user' => $this->sLogin, + 'auth_pwd' => $this->sPassword, ]; if ($this->bPassJsonDataAsFile){ @@ -202,14 +232,14 @@ JSON; $aPostFields['json_data'] = $sJsonDataContent; } - curl_setopt($ch, CURLOPT_URL, "http://webserver/iTop/webservices/rest.php"); + $sUrl = \MetaModel::GetConfig()->Get('app_root_url'); + curl_setopt($ch, CURLOPT_URL, "$sUrl/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); - var_dump($sJson); return $sJson; }