mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
✅ Simplify for the sake of readability
This commit is contained in:
@@ -15,10 +15,10 @@ class CliResetSessionTest extends ItopDataTestCase
|
|||||||
private $sCookieFile = "";
|
private $sCookieFile = "";
|
||||||
private $sUrl;
|
private $sUrl;
|
||||||
private $sLogin;
|
private $sLogin;
|
||||||
private $sUserId;
|
|
||||||
private $sPassword = "Iuytrez9876543ç_è-(";
|
private $sPassword = "Iuytrez9876543ç_è-(";
|
||||||
protected $sConfigTmpBackupFile;
|
protected $sConfigTmpBackupFile;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@@ -41,12 +41,12 @@ class CliResetSessionTest extends ItopDataTestCase
|
|||||||
|
|
||||||
if (is_object($oRestProfile) && is_object($oAdminProfile)) {
|
if (is_object($oRestProfile) && is_object($oAdminProfile)) {
|
||||||
$oUser = $this->CreateUser($this->sLogin, $oRestProfile->GetKey(), $this->sPassword);
|
$oUser = $this->CreateUser($this->sLogin, $oRestProfile->GetKey(), $this->sPassword);
|
||||||
$this->sUserId = $oUser->GetKey();
|
|
||||||
$this->AddProfileToUser($oUser, $oAdminProfile->GetKey());
|
$this->AddProfileToUser($oUser, $oAdminProfile->GetKey());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function tearDown(): void {
|
protected function tearDown(): void
|
||||||
|
{
|
||||||
parent::tearDown();
|
parent::tearDown();
|
||||||
|
|
||||||
if (! is_null($this->sConfigTmpBackupFile) && is_file($this->sConfigTmpBackupFile)){
|
if (! is_null($this->sConfigTmpBackupFile) && is_file($this->sConfigTmpBackupFile)){
|
||||||
@@ -64,47 +64,50 @@ class CliResetSessionTest extends ItopDataTestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function AddLoginMode($sLoginMode){
|
protected function GivenConfigFileAllowedLoginTypes($aAllowedLoginTypes): void
|
||||||
@chmod(MetaModel::GetConfig()->GetLoadedFile(), 0770);
|
{
|
||||||
$aAllowedLoginTypes = MetaModel::GetConfig()->GetAllowedLoginTypes();
|
|
||||||
if (! in_array($sLoginMode, $aAllowedLoginTypes)){
|
|
||||||
$aAllowedLoginTypes[] = $sLoginMode;
|
|
||||||
MetaModel::GetConfig()->SetAllowedLoginTypes($aAllowedLoginTypes);
|
|
||||||
MetaModel::GetConfig()->WriteToFile();
|
|
||||||
}
|
|
||||||
@chmod(MetaModel::GetConfig()->GetLoadedFile(), 0444);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function SetLoginModes($aAllowedLoginTypes){
|
|
||||||
@chmod(MetaModel::GetConfig()->GetLoadedFile(), 0770);
|
@chmod(MetaModel::GetConfig()->GetLoadedFile(), 0770);
|
||||||
MetaModel::GetConfig()->SetAllowedLoginTypes($aAllowedLoginTypes);
|
MetaModel::GetConfig()->SetAllowedLoginTypes($aAllowedLoginTypes);
|
||||||
MetaModel::GetConfig()->WriteToFile();
|
MetaModel::GetConfig()->WriteToFile();
|
||||||
@chmod(MetaModel::GetConfig()->GetLoadedFile(), 0444);
|
@chmod(MetaModel::GetConfig()->GetLoadedFile(), 0444);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function RestProvider(){
|
public function GivenAFirstQueryHasBeenSentWithCookiesEnabled(): void
|
||||||
|
{
|
||||||
|
$aPostFields = [
|
||||||
|
'version' => '1.2',
|
||||||
|
'auth_user' => $this->sLogin,
|
||||||
|
'auth_pwd' => $this->sPassword,
|
||||||
|
'json_data' => '{"operation": "core/get", "class": "User", "key": 99999, "output_fields": "id"}',
|
||||||
|
];
|
||||||
|
$sOutput = $this->SendHTTPRequestWithCookies('webservices/rest.php', $aPostFields);
|
||||||
|
$this->assertStringStartsWith('{"code":0,"message":"Found: 0"', $sOutput, "Failed to establish the given: the first call should be successful (and set the session)");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function LoginModesProvider()
|
||||||
|
{
|
||||||
return [
|
return [
|
||||||
'nominal / no login_mode forced' => [
|
'no login_mode specified' => [
|
||||||
'sConfiguredLoginModes' => 'form|external|basic',
|
'sConfiguredLoginModes' => 'form|external|basic',
|
||||||
'sForcedLoginMode' => null,
|
'sForcedLoginMode' => null,
|
||||||
],
|
],
|
||||||
'nominal / form forced' => [
|
'form' => [
|
||||||
'sConfiguredLoginModes' => 'form|external|basic',
|
'sConfiguredLoginModes' => 'form|external|basic',
|
||||||
'sForcedLoginMode' => 'form',
|
'sForcedLoginMode' => 'form',
|
||||||
],
|
],
|
||||||
'nominal / external forced' => [
|
'external' => [
|
||||||
'sConfiguredLoginModes' => 'form|external|basic',
|
'sConfiguredLoginModes' => 'form|external|basic',
|
||||||
'sForcedLoginMode' => 'external',
|
'sForcedLoginMode' => 'external',
|
||||||
],
|
],
|
||||||
'nominal / basic forced' => [
|
'basic' => [
|
||||||
'sConfiguredLoginModes' => 'form|external|basic',
|
'sConfiguredLoginModes' => 'form|external|basic',
|
||||||
'sForcedLoginMode' => 'basic',
|
'sForcedLoginMode' => 'basic',
|
||||||
],
|
],
|
||||||
'nominal / url forced' => [
|
'url' => [
|
||||||
'sConfiguredLoginModes' => 'form|external|basic|url',
|
'sConfiguredLoginModes' => 'form|external|basic|url',
|
||||||
'sForcedLoginMode' => 'url',
|
'sForcedLoginMode' => 'url',
|
||||||
],
|
],
|
||||||
'nominal / cas forced' => [
|
'cas' => [
|
||||||
'sConfiguredLoginModes' => 'form|external|basic|cas',
|
'sConfiguredLoginModes' => 'form|external|basic|cas',
|
||||||
'sForcedLoginMode' => 'cas',
|
'sForcedLoginMode' => 'cas',
|
||||||
],
|
],
|
||||||
@@ -112,53 +115,20 @@ class CliResetSessionTest extends ItopDataTestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider RestProvider
|
* @dataProvider LoginModesProvider
|
||||||
* @param $aLoginModes
|
|
||||||
* @param $sForcedLoginMode
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function testRest($sConfiguredLoginModes=null, $sForcedLoginMode=null, $sExpectedFailHttpCode="200"){
|
public function testVariousLoginModes($sAllowedLoginTypes, $sRequestedLoginMode)
|
||||||
if (! is_null($sConfiguredLoginModes)){
|
{
|
||||||
$this->SetLoginModes(explode('|', $sConfiguredLoginModes));
|
$this->GivenConfigFileAllowedLoginTypes(explode('|', $sAllowedLoginTypes));
|
||||||
}
|
$this->GivenAFirstQueryHasBeenSentWithCookiesEnabled();
|
||||||
|
|
||||||
$sJsonGetContent = <<<JSON
|
|
||||||
{
|
|
||||||
"operation": "core/get",
|
|
||||||
"class": "User",
|
|
||||||
"key": $this->sUserId,
|
|
||||||
"output_fields": "id"
|
|
||||||
}
|
|
||||||
JSON;
|
|
||||||
$aPostFields = [
|
|
||||||
'version' => '1.2',
|
|
||||||
'auth_user' => $this->sLogin,
|
|
||||||
'auth_pwd' => $this->sPassword,
|
|
||||||
'json_data' => $sJsonGetContent,
|
|
||||||
];
|
|
||||||
list($iHttpCode, $sJson) = $this->CallRestApi($aPostFields);
|
|
||||||
$this->assertEquals(200, $iHttpCode);
|
|
||||||
|
|
||||||
$aJson = json_decode($sJson, true);
|
|
||||||
$this->assertTrue(is_array($aJson), $sJson);
|
|
||||||
$this->assertEquals("0", $aJson['code'], $sJson);
|
|
||||||
|
|
||||||
//2nd call to REST API made with previous session cookie
|
//2nd call to REST API made with previous session cookie
|
||||||
//no need to pass auth_user/auth_pwd
|
$sOutput = $this->SendHTTPRequestWithCookies('webservices/rest.php', [], $sRequestedLoginMode);
|
||||||
$aPostFields = [
|
$this->assertStringContainsString('Invalid login', $sOutput, "Omitting auth_user/auth_pwd should not be allowed");
|
||||||
'version' => '1.2',
|
|
||||||
'json_data' => $sJsonGetContent,
|
|
||||||
];
|
|
||||||
list($iHttpCode, $sJson) = $this->CallRestApi($aPostFields, $sForcedLoginMode);
|
|
||||||
$this->debug($sJson);
|
|
||||||
$this->assertEquals($sExpectedFailHttpCode, $iHttpCode);
|
|
||||||
if ($iHttpCode === "200") {
|
|
||||||
$this->assertEquals('{"code":1,"message":"Error: Invalid login"}', $sJson);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function OtherCliProvider(){
|
public function OtherWebServicesProvider()
|
||||||
|
{
|
||||||
return [
|
return [
|
||||||
'import' => [ 'webservices/import.php' ],
|
'import' => [ 'webservices/import.php' ],
|
||||||
'synchro_exec' => [ 'synchro/synchro_exec.php' ],
|
'synchro_exec' => [ 'synchro/synchro_exec.php' ],
|
||||||
@@ -167,48 +137,21 @@ JSON;
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider OtherCliProvider
|
* @dataProvider OtherWebServicesProvider
|
||||||
*/
|
*/
|
||||||
public function testImport($sUri){
|
public function testVariousWebServices($sUri)
|
||||||
$sJsonGetContent = <<<JSON
|
{
|
||||||
{
|
$this->GivenAFirstQueryHasBeenSentWithCookiesEnabled();
|
||||||
"operation": "core/get",
|
|
||||||
"class": "User",
|
|
||||||
"key": $this->sUserId,
|
|
||||||
"output_fields": "id"
|
|
||||||
}
|
|
||||||
JSON;
|
|
||||||
$aPostFields = [
|
|
||||||
'version' => '1.2',
|
|
||||||
'auth_user' => $this->sLogin,
|
|
||||||
'auth_pwd' => $this->sPassword,
|
|
||||||
'json_data' => $sJsonGetContent,
|
|
||||||
];
|
|
||||||
list($iHttpCode, $sOutput) = $this->CallRestApi($aPostFields);
|
|
||||||
$this->assertEquals(200, $iHttpCode);
|
|
||||||
|
|
||||||
$aJson = json_decode($sOutput, true);
|
$sOutput = $this->SendHTTPRequestWithCookies($sUri, []);
|
||||||
$this->assertTrue(is_array($aJson), $sOutput);
|
$this->assertStringContainsString('Invalid login', $sOutput, "Omitting auth_user/auth_pwd should not be allowed");
|
||||||
$this->assertEquals("0", $aJson['code'], $sOutput);
|
|
||||||
|
|
||||||
//2nd call to REST API made with previous session cookie
|
|
||||||
//no need to pass auth_user/auth_pwd
|
|
||||||
$aPostFields = [
|
|
||||||
'version' => '1.2',
|
|
||||||
'json_data' => $sJsonGetContent,
|
|
||||||
];
|
|
||||||
list($iHttpCode, $sOutput) = $this->CallRestApi($aPostFields, null, $sUri);
|
|
||||||
$this->debug($sOutput);
|
|
||||||
$this->assertEquals("200", $iHttpCode);
|
|
||||||
$this->assertTrue(str_contains($sOutput, 'Invalid login'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $aPostFields
|
|
||||||
*
|
|
||||||
* @return array($iHttpCode, $sBody)
|
* @return array($iHttpCode, $sBody)
|
||||||
*/
|
*/
|
||||||
private function CallRestApi($aPostFields, $sForcedLoginMode=null, $sUri='webservices/rest.php'): array {
|
private function SendHTTPRequestWithCookies($sUri, $aPostFields, $sForcedLoginMode = null): string
|
||||||
|
{
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
|
|
||||||
curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->sCookieFile);
|
curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->sCookieFile);
|
||||||
@@ -247,6 +190,7 @@ JSON;
|
|||||||
}
|
}
|
||||||
curl_close ($ch);
|
curl_close ($ch);
|
||||||
|
|
||||||
return array($sHttpCode, $sBody);
|
$this->assertEquals(200, $sHttpCode, "The test logic assumes that the HTTP request is correctly handled");
|
||||||
|
return $sBody;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user