From 899a7ac189c4d1d2df52ee52b09eff12dc042ab3 Mon Sep 17 00:00:00 2001 From: Michael Kaufmann Date: Sun, 28 Oct 2018 08:53:25 +0100 Subject: [PATCH] add Cronjobs API command ad unit-tests Signed-off-by: Michael Kaufmann --- admin_cronjobs.php | 34 ++--- lib/classes/api/commands/class.Cronjobs.php | 136 +++++++++++++++++++- tests/Cronjobs/CronjobsTest.php | 93 +++++++++++++ 3 files changed, 234 insertions(+), 29 deletions(-) create mode 100644 tests/Cronjobs/CronjobsTest.php diff --git a/admin_cronjobs.php b/admin_cronjobs.php index 8cd17276..e70dbcaa 100644 --- a/admin_cronjobs.php +++ b/admin_cronjobs.php @@ -77,31 +77,21 @@ if ($page == 'cronjobs' || $page == 'overview') { * @TODO later */ } elseif ($action == 'edit' && $id != 0) { - $result_stmt = Database::prepare("SELECT * FROM `" . TABLE_PANEL_CRONRUNS . "` WHERE `id`= :id"); - Database::pexecute($result_stmt, array('id' => $id)); - $result = $result_stmt->fetch(PDO::FETCH_ASSOC); + try { + $json_result = Cronjobs::getLocal($userinfo, array( + 'id' => $id + ))->get(); + } catch (Exception $e) { + dynamic_error($e->getMessage()); + } + $result = json_decode($json_result, true)['data']; if ($result['cronfile'] != '') { if (isset($_POST['send']) && $_POST['send'] == 'send') { - $isactive = isset($_POST['isactive']) ? 1 : 0; - $interval_value = validate($_POST['interval_value'], 'interval_value', '/^([0-9]+)$/Di', 'stringisempty'); - $interval_interval = validate($_POST['interval_interval'], 'interval_interval'); - - if ($isactive != 1) { - $isactive = 0; + try { + Cronjobs::getLocal($userinfo, $_POST)->update(); + } catch (Exception $e) { + dynamic_error($e->getMessage()); } - - $interval = $interval_value . ' ' . strtoupper($interval_interval); - - $upd = Database::prepare(" - UPDATE `" . TABLE_PANEL_CRONRUNS . "` - SET `isactive` = :isactive, `interval` = :int - WHERE `id` = :id" - ); - Database::pexecute($upd, array('isactive' => $isactive, 'int' => $interval, 'id' => $id)); - - // insert task to re-generate the cron.d-file - inserttask('99'); - redirectTo($filename, array('page' => $page, 's' => $s)); } else { diff --git a/lib/classes/api/commands/class.Cronjobs.php b/lib/classes/api/commands/class.Cronjobs.php index ef858206..63bbefe0 100644 --- a/lib/classes/api/commands/class.Cronjobs.php +++ b/lib/classes/api/commands/class.Cronjobs.php @@ -17,20 +17,142 @@ */ class Cronjobs extends ApiCommand implements ResourceEntity { + + /** + * You cannot add new cronjobs yet. + */ public function add() - {} + { + throw new Exception('You cannot add new cronjobs yet.', 303); + } + /** + * return a cronjob entry by id + * + * @param int $id + * cronjob-id + * + * @access admin + * @throws Exception + * @return array + */ public function get() - {} + { + if ($this->isAdmin()) { + $id = $this->getParam('id'); + $result_stmt = Database::prepare(" + SELECT * FROM `" . TABLE_PANEL_CRONRUNS . "` WHERE `id` = :id + "); + $result = Database::pexecute_first($result_stmt, array( + 'id' => $id + ), true, true); + if ($result) { + return $this->response(200, "successfull", $result); + } + throw new Exception("cronjob with id #" . $id . " could not be found", 404); + } + throw new Exception("Not allowed to execute given command.", 403); + } + + /** + * update a cronjob entry by given id + * + * @param int $id + * @param bool $isactive + * optional whether the cronjob is active or not + * @param int $interval_value + * optional number of seconds/minutes/hours/etc. for the interval + * @param string $interval_interval + * optional interval for the cronjob (MINUTE, HOUR, DAY, WEEK or MONTH) + * + * @access admin + * @throws Exception + * @return array + */ public function update() - {} + { + if ($this->isAdmin() && $this->getUserDetail('change_serversettings') == 1) { + // required parameter + $id = $this->getParam('id'); + + $result = $this->apiCall('Cronjobs.get', array( + 'id' => $id + )); + + // split interval + $cur_int = explode(" ", $result['interval']); + + // parameter + $isactive = $this->getParam('isactive', true, $result['isactive']); + $interval_value = $this->getParam('interval_value', true, $cur_int[0]); + $interval_interval = $this->getParam('interval_interval', true, $cur_int[1]); + + // validation + if ($isactive != 1) { + $isactive = 0; + } + $interval_value = validate($interval_value, 'interval_value', '/^([0-9]+)$/Di', 'stringisempty', array(), true); + $interval_interval = validate($interval_interval, 'interval_interval', '', '', array(), true); + + // put together interval value + $interval = $interval_value . ' ' . strtoupper($interval_interval); + + $upd_stmt = Database::prepare(" + UPDATE `" . TABLE_PANEL_CRONRUNS . "` + SET `isactive` = :isactive, `interval` = :int + WHERE `id` = :id + "); + Database::pexecute($upd_stmt, array( + 'isactive' => $isactive, + 'int' => $interval, + 'id' => $id + ), true, true); + + // insert task to re-generate the cron.d-file + inserttask('99'); + $this->logger()->logAction(ADM_ACTION, LOG_INFO, "[API] cronjob with description '" . $result['module'] . '/' . $result['cronfile'] . "' has been updated by '" . $this->getUserDetail('loginname') . "'"); + $result = $this->apiCall('Cronjobs.get', array( + 'id' => $id + )); + return $this->response(200, "successfull", $result); + } + throw new Exception("Not allowed to execute given command.", 403); + } + + /** + * lists all cronjob entries + * + * @access admin + * @throws Exception + * @return array count|list + */ public function listing() - {} + { + if ($this->isAdmin()) { + $this->logger()->logAction(ADM_ACTION, LOG_NOTICE, "[API] list cronjobs"); + $result_stmt = Database::prepare(" + SELECT `c`.* FROM `" . TABLE_PANEL_CRONRUNS . "` `c` ORDER BY `module` ASC, `cronfile` ASC + "); + Database::pexecute($result_stmt); + $result = array(); + while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { + $result[] = $row; + } + return $this->response(200, "successfull", array( + 'count' => count($result), + 'list' => $result + )); + } + throw new Exception("Not allowed to execute given command.", 403); + } + /** + * You cannot delete system cronjobs. + */ public function delete() - {} - - + { + throw new Exception('You cannot delete system cronjobs.', 303); + } } diff --git a/tests/Cronjobs/CronjobsTest.php b/tests/Cronjobs/CronjobsTest.php new file mode 100644 index 00000000..f0e6c1ae --- /dev/null +++ b/tests/Cronjobs/CronjobsTest.php @@ -0,0 +1,93 @@ +listing(); + $result = json_decode($json_result, true)['data']; + $this->assertTrue(isset($result['list'][0]['module'])); + $this->assertTrue(isset($result['list'][0]['cronfile'])); + } + + public function testAdminCronjobsAdd() + { + global $admin_userdata; + $data = []; + $this->expectExceptionCode(303); + $this->expectExceptionMessage("You cannot add new cronjobs yet."); + Cronjobs::getLocal($admin_userdata, $data)->add(); + } + + public function testAdminCronjobsGetNotFound() + { + global $admin_userdata; + $this->expectExceptionCode(404); + $this->expectExceptionMessage("cronjob with id #999 could not be found"); + Cronjobs::getLocal($admin_userdata, array('id' => 999))->get(); + } + + public function testCustomerCronjobsGetNotAllowed() + { + 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."); + Cronjobs::getLocal($customer_userdata, array('id' => 1))->get(); + } + + public function testAdminCronjobsEdit() + { + global $admin_userdata; + $data = [ + 'id' => 1, + 'isactive' => 0, + 'interval_value' => 10 + ]; + $json_result = Cronjobs::getLocal($admin_userdata, $data)->update(); + $result = json_decode($json_result, true)['data']; + $this->assertEquals(0, $result['isactive']); + $this->assertEquals('10 MINUTE', $result['interval']); + } + + public function testResellerCronjobsEditNotAllowed() + { + global $admin_userdata; + // get reseller + $json_result = Admins::getLocal($admin_userdata, array( + 'loginname' => 'reseller' + ))->get(); + $reseller_userdata = json_decode($json_result, true)['data']; + $reseller_userdata['adminsession'] = 1; + $data = [ + 'id' => 1, + 'isactive' => 1 + ]; + $this->expectExceptionCode(403); + $this->expectExceptionMessage("Not allowed to execute given command."); + Cronjobs::getLocal($reseller_userdata, $data)->update(); + } + + public function testAdminCronjobsDelete() + { + global $admin_userdata; + $data = [ + 'id' => 3 + ]; + $this->expectExceptionCode(303); + $this->expectExceptionMessage("You cannot delete system cronjobs."); + Cronjobs::getLocal($admin_userdata, $data)->delete(); + } +}