N°3464: move fix in itop-fence + fix/enhance rest api test

This commit is contained in:
odain
2020-12-07 00:44:39 +01:00
parent 0019595923
commit 57a085eec1
3 changed files with 70 additions and 18 deletions

View File

@@ -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();
}
/**

View File

@@ -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

View File

@@ -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=<<<JSON
@@ -61,6 +77,20 @@ JSON;
$this->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;
}