BackupConfiguration(); $this->oiTopConfig->Set('log_level_min', 'Error'); $this->oiTopConfig->Set('login_debug', true); $this->BackupConfiguration(); static::$sLogin = "rest-user-".date('dmYHis').uniqid(); $this->CreateTestOrganization(); } public function testRestWithFormMode() { $this->AddLoginModeAndSaveConfiguration('form'); $this->CreateUserWithProfiles([self::$aURP_Profiles['Administrator'], self::$aURP_Profiles['REST Services User']]); $aPostFields = [ 'version' => '1.3', 'auth_user' => static::$sLogin, 'auth_pwd' => static::$sPassword, 'json_data' => '{"operation": "list_operations"}', ]; $sJSONResult = $this->CallItopUri("/webservices/rest.php", $aPostFields); $this->assertEquals($this->GetExpectedRestResponse(), $sJSONResult); } public function testRestWithBasicMode() { $this->AddLoginModeAndSaveConfiguration('basic'); $this->CreateUserWithProfiles([self::$aURP_Profiles['Administrator'], self::$aURP_Profiles['REST Services User']]); $aPostFields = [ 'version' => '1.3', 'json_data' => '{"operation": "list_operations"}', ]; $sToken = base64_encode(sprintf("%s:%s", static::$sLogin, static::$sPassword)); $aCurlOptions = [ CURLOPT_HTTPHEADER => ["Authorization: Basic $sToken"], ]; // Test regular JSON result $sJSONResult = $this->CallItopUri("/webservices/rest.php", $aPostFields, $aCurlOptions); $this->assertEquals($this->GetExpectedRestResponse(), $sJSONResult); } public function testRestWithUrlMode() { $this->AddLoginModeAndSaveConfiguration('url'); $this->CreateUserWithProfiles([self::$aURP_Profiles['Administrator'], self::$aURP_Profiles['REST Services User']]); $aPostFields = [ 'version' => '1.3', 'json_data' => '{"operation": "list_operations"}', ]; $aGetFields = [ 'auth_user' => static::$sLogin, 'auth_pwd' => static::$sPassword, ]; $sJSONResult = $this->CallItopUri("/webservices/rest.php?".http_build_query($aGetFields), $aPostFields); $this->assertEquals($this->GetExpectedRestResponse(), $sJSONResult); } public function testLaunchCronWithFormModeFailWhenNotAdmin() { $this->AddLoginModeAndSaveConfiguration('form'); $this->CreateUserWithProfiles([self::$aURP_Profiles['REST Services User']]); $sLogFileName = "crontest_".uniqid().'.log'; $aPostFields = [ 'version' => '1.3', 'auth_user' => static::$sLogin, 'auth_pwd' => static::$sPassword, 'verbose' => 1, 'debug' => 1, 'cron_log_file' => $sLogFileName, ]; $sJSONResult = $this->CallItopUri("/webservices/asynchronously_cron.php", $aPostFields); $this->assertEquals($this->GetExpectedCronResponse(), $sJSONResult); $sLogFile = $this->CheckLogFileIsGeneratedAndGetFullPath($sLogFileName); $this->CheckAdminAccessIssueWithCron($sLogFile); } public function testLaunchCronWithBasicModeFailWhenNotAdmin() { $this->AddLoginModeAndSaveConfiguration('basic'); $this->CreateUserWithProfiles([self::$aURP_Profiles['REST Services User']]); $sLogFileName = "crontest_".uniqid().'.log'; $aPostFields = [ 'version' => '1.3', 'verbose' => 1, 'debug' => 1, 'cron_log_file' => $sLogFileName, ]; $sToken = base64_encode(sprintf("%s:%s", static::$sLogin, static::$sPassword)); $aCurlOptions = [ CURLOPT_HTTPHEADER => ["Authorization: Basic $sToken"], ]; $sJSONResult = $this->CallItopUri("/webservices/asynchronously_cron.php", $aPostFields, $aCurlOptions); $this->assertEquals($this->GetExpectedCronResponse(), $sJSONResult); $sLogFile = $this->CheckLogFileIsGeneratedAndGetFullPath($sLogFileName); $this->CheckAdminAccessIssueWithCron($sLogFile); } public function testLaunchCronWithUrlModeFailWhenNotAdmin() { $this->AddLoginModeAndSaveConfiguration('url'); $this->CreateUserWithProfiles([self::$aURP_Profiles['REST Services User']]); $sLogFileName = "crontest_".uniqid().'.log'; $aPostFields = [ 'version' => '1.3', 'verbose' => 1, 'debug' => 1, 'cron_log_file' => $sLogFileName, ]; $aGetFields = [ 'auth_user' => static::$sLogin, 'auth_pwd' => static::$sPassword, ]; $sJSONResult = $this->CallItopUri("/webservices/asynchronously_cron.php?".http_build_query($aGetFields), $aPostFields); $this->assertEquals($this->GetExpectedCronResponse(), $sJSONResult); $sLogFile = $this->CheckLogFileIsGeneratedAndGetFullPath($sLogFileName); $this->CheckAdminAccessIssueWithCron($sLogFile); } public function ModeProvider() { return [ 'form' => [ 'LoginForm' ], 'basic' => [ 'LoginBasic'], 'url' => [ 'LoginURL'], ]; } /** * @dataProvider ModeProvider */ public function testGetUserLoginWithFormMode($sLoginModeClass) { $this->CreateUserWithProfiles([self::$aURP_Profiles['Administrator']]); $oLoginMode = new $sLoginModeClass(); $sUserLogin = $oLoginMode->GetUserLogin([static::$sLogin, static::$sPassword]); $this->assertEquals(static::$sLogin, $sUserLogin); } public function testGetCronStatus_FailWhenNotAdmin() { $this->AddLoginModeAndSaveConfiguration('url'); $this->CreateUserWithProfiles([self::$aURP_Profiles['REST Services User']]); $sLogFileName = "crontest_".uniqid().'.log'; $aPostFields = [ 'version' => '1.3', 'verbose' => 1, 'debug' => 1, ]; $aGetFields = [ 'auth_user' => static::$sLogin, 'auth_pwd' => static::$sPassword, ]; $sJSONResult = $this->CallItopUri("/webservices/get_cron_status.php?".http_build_query($aGetFields), $aPostFields); $this->assertEquals('{"message":"Access restricted to administrators"}', $sJSONResult); } public function testGetCronStatus_FailWhenLogFileDoesNotExist() { $this->AddLoginModeAndSaveConfiguration('url'); $this->CreateUserWithProfiles([self::$aURP_Profiles['Administrator']]); $aPostFields = [ 'version' => '1.3', 'verbose' => 1, 'debug' => 1, 'cron_log_file' => 'gabuzomeu.log', ]; $aGetFields = [ 'auth_user' => static::$sLogin, 'auth_pwd' => static::$sPassword, ]; $sJSONResult = $this->CallItopUri("/webservices/get_cron_status.php?".http_build_query($aGetFields), $aPostFields); $this->assertEquals('{"message":"Cannot read log file"}', $sJSONResult); } public static function GetCronStatusProvider() { return [ ["cron_alreadyrunning.log", 'Already running...', "error"], ["cron_dummyerror.log", 'Already running...', "error"], ['cron_maintenance.log', 'A maintenance is ongoing', "error"], ['cron_missingcreds_error.log', 'A maintenance is ongoing', "error"], ['cron_notanadmin.log', 'Access restricted to administrators', "error"], ['cron_starting.log', '', "running"], ['cron_stopped.log', '', "stopped"], ]; } /** * @dataProvider GetCronStatusProvider */ public function testGetCronStatus($sLogFilename, $expectedMsg, $expectedStatus) { $sLogFile = APPROOT."log/$sLogFilename"; file_put_contents($sLogFile, file_get_contents(__DIR__.'/resources/'.$sLogFilename)); $this->aFileToClean[] = $sLogFile; $this->AddLoginModeAndSaveConfiguration('url'); $this->CreateUserWithProfiles([self::$aURP_Profiles['Administrator']]); $aPostFields = [ 'version' => '1.3', 'verbose' => 1, 'debug' => 1, 'cron_log_file' => $sLogFile, ]; $aGetFields = [ 'auth_user' => static::$sLogin, 'auth_pwd' => static::$sPassword, ]; $sJSONResult = $this->CallItopUri("/webservices/get_cron_status.php?".http_build_query($aGetFields), $aPostFields); $this->assertEquals('{"message":"Cannot read log file"}', $sJSONResult); } private function CreateUserWithProfiles(array $aProfileIds): ?string { if (count($aProfileIds) > 0) { $oUser = null; foreach ($aProfileIds as $iProfileId) { if (is_null($oUser)) { $oUser = $this->CreateContactlessUser(static::$sLogin, $iProfileId, static::$sPassword); } else { $this->AddProfileToUser($oUser, $iProfileId); } $oUser->DBWrite(); } return $oUser->GetKey(); } return null; } private function GetExpectedRestResponse(): string { return <<assertTrue(is_file($sLogFile)); $this->aFileToClean[] = $sLogFile; return $sLogFile; } private function CheckAdminAccessIssueWithCron(string $sLogFile) { $aLines = Utils::ReadTail($sLogFile); $sLastLine = array_shift($aLines); $this->assertMatchesRegularExpression('/^Access restricted to administrators/', $sLastLine, "@$sLastLine@"); } }