forked from Wavyzz/dolibarr
Qual: Add AbstractRestApiTest class, refactor RestApiUserTest, add RestApiMosTest (#28484)
* New: AbstractRestAPITest class * Qual: Create RestAPIMosTest
This commit is contained in:
@@ -25,22 +25,10 @@
|
||||
* \remarks To run this script as CLI: phpunit filename.php
|
||||
*/
|
||||
|
||||
global $conf,$user,$langs,$db;
|
||||
//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
|
||||
//require_once 'PHPUnit/Autoload.php';
|
||||
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
|
||||
require_once dirname(__FILE__).'/../../htdocs/core/lib/date.lib.php';
|
||||
require_once dirname(__FILE__).'/../../htdocs/core/lib/geturl.lib.php';
|
||||
require_once dirname(__FILE__).'/CommonClassTest.class.php';
|
||||
|
||||
if (empty($user->id)) {
|
||||
print "Load permissions for admin user nb 1\n";
|
||||
$user->fetch(1);
|
||||
$user->getrights();
|
||||
}
|
||||
$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
|
||||
$conf->global->MAIN_UMASK = '0666';
|
||||
|
||||
require_once __DIR__."/AbstractRestAPITest.php";
|
||||
|
||||
/**
|
||||
* Class for PHPUnit tests
|
||||
@@ -49,66 +37,8 @@ $conf->global->MAIN_UMASK = '0666';
|
||||
* @backupStaticAttributes enabled
|
||||
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
|
||||
*/
|
||||
class RestAPIUserTest extends CommonClassTest
|
||||
class RestAPIUserTest extends AbstractRestAPITest
|
||||
{
|
||||
protected $api_url;
|
||||
protected $api_key;
|
||||
|
||||
/**
|
||||
* setUpBeforeClass
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function setUpBeforeClass(): void
|
||||
{
|
||||
global $conf,$user,$langs,$db;
|
||||
$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
|
||||
|
||||
if (!isModEnabled('api')) {
|
||||
print __METHOD__." module api must be enabled.\n";
|
||||
die(1);
|
||||
}
|
||||
|
||||
print __METHOD__."\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Init phpunit tests
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
global $conf,$user,$langs,$db;
|
||||
$conf = $this->savconf;
|
||||
$user = $this->savuser;
|
||||
$langs = $this->savlangs;
|
||||
$db = $this->savdb;
|
||||
|
||||
$this->api_url = DOL_MAIN_URL_ROOT.'/api/index.php';
|
||||
|
||||
$test = "API Test Setup - ";
|
||||
$login = 'admin';
|
||||
$password = 'admin';
|
||||
$url = $this->api_url.'/login?login='.$login.'&password='.$password;
|
||||
// Call the API login method to save api_key for this test class.
|
||||
// At first call, if token is not defined a random value is generated and returned.
|
||||
$result = getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
|
||||
print __METHOD__." result = ".var_export($result, true)."\n";
|
||||
print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
|
||||
$this->assertEquals('', $result['curl_error_no'], "$test Should not have a curl error");
|
||||
$object = json_decode($result['content'], true); // If success content is just an id, if not an array
|
||||
|
||||
$this->assertNotNull($object, "$test Parsing of JSON result must not be null");
|
||||
$this->assertNotEquals(500, (empty($object['error']['code']) ? 0 : $object['error']['code']), "$test Error".(empty($object['error']['message']) ? '' : ' '.$object['error']['message']));
|
||||
$this->assertEquals('200', $object['success']['code']);
|
||||
|
||||
$this->api_key = $object['success']['token'];
|
||||
|
||||
print __METHOD__." api_key: $this->api_key \n";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* testRestGetUser
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user