opzimize ApiParameter::getModFunctionString(); corrected FpmDaemons::update(); added a few more unit-tests

Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann (d00p)
2018-04-01 10:31:38 +02:00
parent 85407abfb4
commit 3f69c97874
4 changed files with 175 additions and 21 deletions

View File

@@ -117,30 +117,34 @@ abstract class ApiParameter
* returns "module::function()" for better error-messages (missing parameter etc.) * returns "module::function()" for better error-messages (missing parameter etc.)
* makes debugging a whole lot more comfortable * makes debugging a whole lot more comfortable
* *
* @param int $level
* depth of backtrace, default 2
*
* @return string * @return string
*/ */
private function getModFunctionString() private function getModFunctionString($level = 1, $max_level = 5, $trace = null)
{ {
// which class called us
$_class = get_called_class(); $_class = get_called_class();
$level = 2; if (empty($trace)) {
if (version_compare(PHP_VERSION, "5.4.0", "<")) { // check php version for backtrace flags
$trace = debug_backtrace(); $_traceopts = false;
} else { if (version_compare(PHP_VERSION, "5.3.6", ">")) {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $_traceopts = DEBUG_BACKTRACE_IGNORE_ARGS;
} }
while (true) { // get backtrace
$trace = debug_backtrace($_traceopts);
}
// check class and function
$class = $trace[$level]['class']; $class = $trace[$level]['class'];
$func = $trace[$level]['function']; $func = $trace[$level]['function'];
if ($class != $_class) { // is it the one we are looking for?
$level ++; if ($class != $_class && $level <= $max_level) {
if ($level > 5) { // check one level deeper
break; return $this->getModFunctionString(++ $level, $max_level, $trace);
}
continue;
} }
return $class . ':' . $func; return $class . ':' . $func;
} }
}
/** /**
* run 'trim' function on an array recursively * run 'trim' function on an array recursively

View File

@@ -270,7 +270,10 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
inserttask('1'); inserttask('1');
$this->logger()->logAction(ADM_ACTION, LOG_INFO, "[API] fpm-daemon with description '" . $description . "' has been updated by '" . $this->getUserDetail('loginname') . "'"); $this->logger()->logAction(ADM_ACTION, LOG_INFO, "[API] fpm-daemon with description '" . $description . "' has been updated by '" . $this->getUserDetail('loginname') . "'");
return $this->response(200, "successfull", $upd_data); $result = $this->apiCall('FpmDaemons.get', array(
'id' => $id
));
return $this->response(200, "successfull", $result);
} }
throw new Exception("Not allowed to execute given command.", 403); throw new Exception("Not allowed to execute given command.", 403);
} }

View File

@@ -21,7 +21,8 @@ class CustomersTest extends TestCase
'firstname' => 'Test', 'firstname' => 'Test',
'name' => 'Testman', 'name' => 'Testman',
'customernumber' => 1337, 'customernumber' => 1337,
'diskspace' => - 1, 'diskspace' => 0,
'diskspace_ul' => 1,
'traffic' => - 1, 'traffic' => - 1,
'subdomains' => 15, 'subdomains' => 15,
'emails' => - 1, 'emails' => - 1,

View File

@@ -17,7 +17,8 @@ class FpmDaemonsTest extends TestCase
$data = [ $data = [
'description' => 'test2 fpm', 'description' => 'test2 fpm',
'reload_cmd' => 'service php7.1-fpm reload', 'reload_cmd' => 'service php7.1-fpm reload',
'config_dir' => '/etc/php/7.1/fpm/pool.d' 'config_dir' => '/etc/php/7.1/fpm/pool.d',
'limit_extensions' => ''
]; ];
$json_result = FpmDaemons::getLocal($admin_userdata, $data)->add(); $json_result = FpmDaemons::getLocal($admin_userdata, $data)->add();
$result = json_decode($json_result, true)['data']; $result = json_decode($json_result, true)['data'];
@@ -27,6 +28,33 @@ class FpmDaemonsTest extends TestCase
self::$id = $result['id']; self::$id = $result['id'];
} }
public function testAdminFpmDaemonsAddUnknownPM()
{
global $admin_userdata;
$data = [
'description' => 'test2 fpm',
'reload_cmd' => 'service php7.3-fpm reload',
'config_dir' => '/etc/php/7.3/fpm/pool.d',
'pm' => 'supermegapm'
];
$this->expectExceptionCode(406);
$this->expectExceptionMessage("Unknown process manager");
FpmDaemons::getLocal($admin_userdata, $data)->add();
}
public function testAdminFpmDaemonsAddInvalidDesc()
{
// max 50. characters
global $admin_userdata;
$data = [
'description' => str_repeat('test', 30),
'reload_cmd' => 'service php7.3-fpm reload',
'config_dir' => '/etc/php/7.3/fpm/pool.d'
];
$this->expectExceptionMessage("The description is too short, too long or contains illegal characters.");
FpmDaemons::getLocal($admin_userdata, $data)->add();
}
/** /**
* @depends testAdminFpmDaemonsAdd * @depends testAdminFpmDaemonsAdd
*/ */
@@ -48,6 +76,51 @@ class FpmDaemonsTest extends TestCase
$this->assertEquals('.php .php.xml', $result['limit_extensions']); $this->assertEquals('.php .php.xml', $result['limit_extensions']);
} }
/**
* @depends testAdminFpmDaemonsUpdate
*/
public function testAdminFpmDaemonsUpdate2()
{
global $admin_userdata;
$data = [
'id' => self::$id,
'limit_extensions' => '',
];
$json_result = FpmDaemons::getLocal($admin_userdata, $data)->update();
$result = json_decode($json_result, true)['data'];
$this->assertEquals('.php', $result['limit_extensions']);
}
/**
* @depends testAdminFpmDaemonsAdd
*/
public function testAdminFpmDaemonsUpdateUnknownPM()
{
global $admin_userdata;
$data = [
'id' => self::$id,
'pm' => 'supermegapm'
];
$this->expectExceptionCode(406);
$this->expectExceptionMessage("Unknown process manager");
FpmDaemons::getLocal($admin_userdata, $data)->update();
}
/**
* @depends testAdminFpmDaemonsAdd
*/
public function testAdminFpmDaemonsUpdateInvalidDesc()
{
// max 50. characters
global $admin_userdata;
$data = [
'id' => self::$id,
'description' => str_repeat('test', 30)
];
$this->expectExceptionMessage("The description is too short, too long or contains illegal characters.");
FpmDaemons::getLocal($admin_userdata, $data)->update();
}
/** /**
* @depends testAdminFpmDaemonsUpdate * @depends testAdminFpmDaemonsUpdate
*/ */
@@ -61,6 +134,79 @@ class FpmDaemonsTest extends TestCase
$this->assertEquals('test2 fpm edit', $result['list'][1]['description']); $this->assertEquals('test2 fpm edit', $result['list'][1]['description']);
} }
public function testAdminFpmDaemonsGetNotFound()
{
global $admin_userdata;
$this->expectExceptionCode(404);
$this->expectExceptionMessage("fpm-daemon with id #-1 could not be found");
FpmDaemons::getLocal($admin_userdata, array('id' => -1))->get();
}
public function testCustomerFpmDaemonsAdd()
{
global $admin_userdata;
// get customer
$json_result = Customers::getLocal($admin_userdata, array(
'loginname' => 'test1'
))->get();
$customer_userdata = json_decode($json_result, true)['data'];
$this->expectExceptionCode(403);
$this->expectExceptionMessage("Not allowed to execute given command.");
FpmDaemons::getLocal($customer_userdata)->add();
}
public function testCustomerFpmDaemonsGet()
{
global $admin_userdata;
// get customer
$json_result = Customers::getLocal($admin_userdata, array(
'loginname' => 'test1'
))->get();
$customer_userdata = json_decode($json_result, true)['data'];
$this->expectExceptionCode(403);
$this->expectExceptionMessage("Not allowed to execute given command.");
FpmDaemons::getLocal($customer_userdata)->get();
}
public function testCustomerFpmDaemonsList()
{
global $admin_userdata;
// get customer
$json_result = Customers::getLocal($admin_userdata, array(
'loginname' => 'test1'
))->get();
$customer_userdata = json_decode($json_result, true)['data'];
$this->expectExceptionCode(403);
$this->expectExceptionMessage("Not allowed to execute given command.");
FpmDaemons::getLocal($customer_userdata)->listing();
}
public function testCustomerFpmDaemonsUpdate()
{
global $admin_userdata;
// get customer
$json_result = Customers::getLocal($admin_userdata, array(
'loginname' => 'test1'
))->get();
$customer_userdata = json_decode($json_result, true)['data'];
$this->expectExceptionCode(403);
$this->expectExceptionMessage("Not allowed to execute given command.");
FpmDaemons::getLocal($customer_userdata)->update();
}
public function testCustomerFpmDaemonsDelete()
{
global $admin_userdata;
// get customer
$json_result = Customers::getLocal($admin_userdata, array(
'loginname' => 'test1'
))->get();
$customer_userdata = json_decode($json_result, true)['data'];
$this->expectExceptionCode(403);
$this->expectExceptionMessage("Not allowed to execute given command.");
FpmDaemons::getLocal($customer_userdata)->delete();
}
/** /**
* @depends testAdminFpmDaemonsList * @depends testAdminFpmDaemonsList
*/ */
@@ -74,7 +220,7 @@ class FpmDaemonsTest extends TestCase
$result = json_decode($json_result, true)['data']; $result = json_decode($json_result, true)['data'];
$this->assertEquals('/etc/php/7.1/fpm/pool.d/', $result['config_dir']); $this->assertEquals('/etc/php/7.1/fpm/pool.d/', $result['config_dir']);
$this->assertEquals(10, $result['max_children']); $this->assertEquals(10, $result['max_children']);
$this->assertEquals('.php .php.xml', $result['limit_extensions']); $this->assertEquals('.php', $result['limit_extensions']);
} }
/** /**