From d15e4a827038b0640a974892733a3c83e86199b9 Mon Sep 17 00:00:00 2001 From: "Michael Kaufmann (d00p)" Date: Tue, 27 Mar 2018 14:43:24 +0200 Subject: [PATCH] more unit-tests Signed-off-by: Michael Kaufmann (d00p) --- lib/classes/api/commands/class.Admins.php | 34 +----- tests/Admins/AdminsTest.php | 75 +++++++++++- tests/Backup/CustomerBackupsTest.php | 73 +++++++++-- tests/Customers/CustomersTest.php | 141 ++++++++++++++++++++++ tests/Emails/EmailsTest.php | 65 ++++++++++ tests/Ftps/FtpsTest.php | 2 + tests/Mysqls/MysqlsTest.php | 2 + tests/SubDomains/SubDomainsTest.php | 2 + 8 files changed, 356 insertions(+), 38 deletions(-) diff --git a/lib/classes/api/commands/class.Admins.php b/lib/classes/api/commands/class.Admins.php index 18382d7b..4aeba76b 100644 --- a/lib/classes/api/commands/class.Admins.php +++ b/lib/classes/api/commands/class.Admins.php @@ -88,6 +88,7 @@ class Admins extends ApiCommand implements ResourceEntity * * @param string $name * @param string $email + * @param string $new_loginname * @param string $admin_password * optional, default auto-generated * @param string $def_language @@ -168,13 +169,13 @@ class Admins extends ApiCommand implements ResourceEntity // required parameters $name = $this->getParam('name'); $email = $this->getParam('email'); - + $loginname = $this->getParam('new_loginname'); + // parameters $def_language = $this->getParam('def_language', true, Settings::Get('panel.standardlanguage')); $custom_notes = $this->getParam('custom_notes', true, ''); $custom_notes_show = $this->getParam('custom_notes_show', true, 0); $password = $this->getParam('admin_password', true, ''); - $loginname = $this->getParam('new_loginname', true, ''); $diskspace = $this->getUlParam('diskspace', 'diskspace_ul', true, 0); $traffic = $this->getUlParam('traffic', 'traffic_ul', true, 0); @@ -239,28 +240,13 @@ class Admins extends ApiCommand implements ResourceEntity 'login' => $loginname ), true, true); - if ($loginname == '') { - standard_error(array( - 'stringisempty', - 'myloginname' - ), '', true); - } elseif (strtolower($loginname_check['loginname']) == strtolower($loginname) || strtolower($loginname_check_admin['loginname']) == strtolower($loginname)) { + if (strtolower($loginname_check['loginname']) == strtolower($loginname) || strtolower($loginname_check_admin['loginname']) == strtolower($loginname)) { standard_error('loginnameexists', $loginname, true); } // Accounts which match systemaccounts are not allowed, filtering them elseif (preg_match('/^' . preg_quote(Settings::Get('customer.accountprefix'), '/') . '([0-9]+)/', $loginname)) { standard_error('loginnameissystemaccount', Settings::Get('customer.accountprefix'), true); } elseif (! validateUsername($loginname)) { standard_error('loginnameiswrong', $loginname, true); - } elseif ($name == '') { - standard_error(array( - 'stringisempty', - 'myname' - ), '', true); - } elseif ($email == '') { - standard_error(array( - 'stringisempty', - 'emailadd' - ), '', true); } elseif (! validateEmail($email)) { standard_error('emailiswrong', $email, true); } else { @@ -548,17 +534,7 @@ class Admins extends ApiCommand implements ResourceEntity $theme = Settings::Get('panel.default_theme'); } - if ($name == '') { - standard_error(array( - 'stringisempty', - 'myname' - ), '', true); - } elseif ($email == '') { - standard_error(array( - 'stringisempty', - 'emailadd' - ), '', true); - } elseif (! validateEmail($email)) { + if (! validateEmail($email)) { standard_error('emailiswrong', $email, true); } else { diff --git a/tests/Admins/AdminsTest.php b/tests/Admins/AdminsTest.php index 79b110a2..da096256 100644 --- a/tests/Admins/AdminsTest.php +++ b/tests/Admins/AdminsTest.php @@ -51,6 +51,78 @@ class AdminsTest extends TestCase $this->assertEquals(0, $result['customers_see_all']); } + /** + * + * @depends testAdminAdminsAdd + */ + public function testAdminAdminsAddLoginnameExists() + { + global $admin_userdata; + + $data = [ + 'new_loginname' => 'reseller', + 'email' => 'testreseller@froxlor.org', + 'name' => 'Testreseller' + ]; + + $this->expectExceptionMessage('Loginname reseller already exists'); + Admins::getLocal($admin_userdata, $data)->add(); + } + + /** + * + * @depends testAdminAdminsAddLoginnameExists + */ + public function testAdminAdminsAddLoginnameIsSystemaccount() + { + global $admin_userdata; + + $data = [ + 'new_loginname' => 'web2', + 'email' => 'testreseller@froxlor.org', + 'name' => 'Testreseller' + ]; + + $this->expectExceptionMessage('You cannot create accounts which are similar to system accounts (as for example begin with "web"). Please enter another account name.'); + Admins::getLocal($admin_userdata, $data)->add(); + } + + /** + * + * @depends testAdminAdminsAddLoginnameIsSystemaccount + */ + public function testAdminAdminsAddLoginnameInvalid() + { + global $admin_userdata; + + $data = [ + 'new_loginname' => 'reslr-', + 'email' => 'testreseller@froxlor.org', + 'name' => 'Testreseller' + ]; + + $this->expectExceptionMessage('Loginname "reslr-" contains illegal characters.'); + Admins::getLocal($admin_userdata, $data)->add(); + } + + /** + * + * @depends testAdminAdminsAddLoginnameIsSystemaccount + */ + public function testAdminAdminsAddLoginnameInvalidEmail() + { + global $admin_userdata; + + $data = [ + 'new_loginname' => 'reslr', + 'email' => 'testreseller.froxlor.org', + 'name' => 'Testreseller' + ]; + + $this->expectExceptionMessage('Email-address testreseller.froxlor.org contains invalid characters or is incomplete'); + Admins::getLocal($admin_userdata, $data)->add(); + } + public function testAdminAdminsAddNotAllowed() { global $admin_userdata; @@ -160,8 +232,7 @@ class AdminsTest extends TestCase $data = [ 'new_loginname' => 'resellertest', 'email' => 'testreseller@froxlor.org', - 'name' => 'Testreseller', - 'admin_password' => 'h0lYmo1y' + 'name' => 'Testreseller' ]; $json_result = Admins::getLocal($admin_userdata, $data)->add(); diff --git a/tests/Backup/CustomerBackupsTest.php b/tests/Backup/CustomerBackupsTest.php index 210b36eb..6a865780 100644 --- a/tests/Backup/CustomerBackupsTest.php +++ b/tests/Backup/CustomerBackupsTest.php @@ -10,10 +10,69 @@ use PHPUnit\Framework\TestCase; class CustomerBackupsTest extends TestCase { + public function testAdminCustomerBackupsNotEnabled() + { + global $admin_userdata; + + Settings::Set('system.backupenabled', 0, true); + + // get customer + $json_result = Customers::getLocal($admin_userdata, array( + 'loginname' => 'test1' + ))->get(); + $customer_userdata = json_decode($json_result, true)['data']; + $this->expectExceptionCode(405); + $this->expectExceptionMessage("You cannot access this resource"); + CustomerBackups::getLocal($customer_userdata)->add(); + } + + /** + * @depends testAdminCustomerBackupsNotEnabled + */ + public function testAdminCustomerBackupsExtrasHidden() + { + global $admin_userdata; + + Settings::Set('system.backupenabled', 1, true); + Settings::Set('panel.customer_hide_options', 'extras', true); + + // get customer + $json_result = Customers::getLocal($admin_userdata, array( + 'loginname' => 'test1' + ))->get(); + $customer_userdata = json_decode($json_result, true)['data']; + $this->expectExceptionCode(405); + $this->expectExceptionMessage("You cannot access this resource"); + CustomerBackups::getLocal($customer_userdata)->add(); + } + + /** + * @depends testAdminCustomerBackupsExtrasHidden + */ + public function testAdminCustomerBackupsExtrasBackupHidden() + { + global $admin_userdata; + + Settings::Set('panel.customer_hide_options', 'extras.backup', true); + + // get customer + $json_result = Customers::getLocal($admin_userdata, array( + 'loginname' => 'test1' + ))->get(); + $customer_userdata = json_decode($json_result, true)['data']; + $this->expectExceptionCode(405); + $this->expectExceptionMessage("You cannot access this resource"); + CustomerBackups::getLocal($customer_userdata)->add(); + } + + /** + * @depends testAdminCustomerBackupsExtrasBackupHidden + */ public function testCustomerCustomerBackupsAdd() { global $admin_userdata; + Settings::Set('panel.customer_hide_options', '', true); Database::query("TRUNCATE TABLE `panel_tasks`;"); // get customer @@ -24,16 +83,16 @@ class CustomerBackupsTest extends TestCase $data = [ 'path' => '/my-backup', - 'backup_dbs' => 1, - 'backup_mail' => 2, - 'backup_web' => 1 + 'backup_dbs' => 2, + 'backup_mail' => 3, + 'backup_web' => 4 ]; $json_result = CustomerBackups::getLocal($customer_userdata, $data)->add(); $result = json_decode($json_result, true)['data']; $this->assertEquals($customer_userdata['documentroot'] . 'my-backup/', $result['destdir']); - $this->assertEquals('1', $result['backup_dbs']); + $this->assertEquals('0', $result['backup_dbs']); $this->assertEquals('0', $result['backup_mail']); - $this->assertEquals('1', $result['backup_web']); + $this->assertEquals('0', $result['backup_web']); } public function testAdminCustomerBackupsGet() @@ -61,9 +120,9 @@ class CustomerBackupsTest extends TestCase $json_result = CustomerBackups::getLocal($admin_userdata)->listing(); $result = json_decode($json_result, true)['data']; $this->assertEquals(1, $result['count']); - $this->assertEquals('1', $result['list'][0]['data']['backup_dbs']); + $this->assertEquals('0', $result['list'][0]['data']['backup_dbs']); $this->assertEquals('0', $result['list'][0]['data']['backup_mail']); - $this->assertEquals('1', $result['list'][0]['data']['backup_web']); + $this->assertEquals('0', $result['list'][0]['data']['backup_web']); } /** diff --git a/tests/Customers/CustomersTest.php b/tests/Customers/CustomersTest.php index d9ea0450..20b3eafb 100644 --- a/tests/Customers/CustomersTest.php +++ b/tests/Customers/CustomersTest.php @@ -2,9 +2,11 @@ use PHPUnit\Framework\TestCase; /** + * * @covers ApiCommand * @covers ApiParameter * @covers Customers + * @covers Admins */ class CustomersTest extends TestCase { @@ -83,6 +85,7 @@ class CustomersTest extends TestCase } /** + * * @depends testAdminCustomersAdd */ public function testAdminCustomersList() @@ -95,6 +98,7 @@ class CustomersTest extends TestCase } /** + * * @depends testAdminCustomersAdd */ public function testResellerCustomersList() @@ -112,6 +116,7 @@ class CustomersTest extends TestCase } /** + * * @depends testAdminCustomersAdd */ public function testCustomerCustomersList() @@ -130,6 +135,7 @@ class CustomersTest extends TestCase } /** + * * @depends testAdminCustomersAdd */ public function testCustomerCustomersGet() @@ -165,6 +171,7 @@ class CustomersTest extends TestCase } /** + * * @depends testAdminCustomersAdd */ public function testCustomerCustomersGetForeign() @@ -185,6 +192,7 @@ class CustomersTest extends TestCase } /** + * * @depends testAdminCustomersAdd */ public function testAdminCustomerUpdateDeactivate() @@ -209,6 +217,7 @@ class CustomersTest extends TestCase } /** + * * @depends testAdminCustomersAdd */ public function testCustomerCustomersGetWhenDeactivated() @@ -230,6 +239,7 @@ class CustomersTest extends TestCase } /** + * * @depends testAdminCustomersAdd */ public function testCustomerCustomersUpdate() @@ -266,6 +276,7 @@ class CustomersTest extends TestCase } /** + * * @depends testAdminCustomersAdd */ public function testResellerCustomersAddAllocateMore() @@ -403,4 +414,134 @@ class CustomersTest extends TestCase $this->assertEquals(2, $result['adminid']); } + + /** + * + * @depends testAdminCustomersMove + */ + public function testAdminCustomersAddLoginnameIsSystemaccount() + { + global $admin_userdata; + + $data = [ + 'new_loginname' => 'web1', + 'email' => 'team@froxlor.org', + 'firstname' => 'Test', + 'name' => 'Testman', + 'customernumber' => 1338, + 'diskspace' => - 1, + 'traffic' => - 1, + 'subdomains' => 15, + 'emails' => - 1, + 'email_accounts' => 15, + 'email_forwarders' => 15, + 'email_imap' => 1, + 'email_pop3' => 0, + 'ftps' => 15, + 'tickets' => 15, + 'mysqls' => 15, + 'createstdsubdomain' => 1, + 'new_customer_password' => 'h0lYmo1y', + 'sendpassword' => 1, + 'phpenabled' => 1, + 'store_defaultindex' => 1, + 'custom_notes' => 'secret', + 'custom_notes_show' => 0, + 'gender' => 5, + 'allowed_phpconfigs' => array( + 1 + ) + ]; + + $this->expectExceptionMessage('You cannot create accounts which are similar to system accounts (as for example begin with "web"). Please enter another account name.'); + Customers::getLocal($admin_userdata, $data)->add(); + } + + /** + * + * @depends testAdminCustomersAddLoginnameIsSystemaccount + */ + public function testAdminCustomersAddAutoLoginname() + { + global $admin_userdata; + + Settings::Set('system.lastaccountnumber', 0, true); + Settings::Set('ticket.enabled', 0, true); + + $data = [ + 'new_loginname' => '', + 'email' => 'team@froxlor.org', + 'firstname' => 'Test2', + 'name' => 'Testman2', + 'customernumber' => 1338, + 'sendpassword' => 0, + 'perlenabled' => 2, + 'dnsenabled' => 4 + ]; + + $json_result = Customers::getLocal($admin_userdata, $data)->add(); + $result = json_decode($json_result, true)['data']; + $this->assertEquals('web1', $result['loginname']); + $this->assertEquals(1338, $result['customernumber']); + } + + /** + * + * @depends testAdminCustomersAddAutoLoginname + */ + public function testAdminCustomersAddLoginnameExists() + { + global $admin_userdata; + + $data = [ + 'new_loginname' => 'test1', + 'email' => 'team@froxlor.org', + 'firstname' => 'Test2', + 'name' => 'Testman2', + 'customernumber' => 1339 + ]; + + $this->expectExceptionMessage('Loginname test1 already exists'); + Customers::getLocal($admin_userdata, $data)->add(); + } + + /** + * + * @depends testAdminCustomersAddLoginnameExists + */ + public function testAdminCustomersAddLoginnameInvalid() + { + global $admin_userdata; + + $data = [ + 'new_loginname' => 'user-', + 'email' => 'team@froxlor.org', + 'firstname' => 'Test2', + 'name' => 'Testman2', + 'customernumber' => 1339 + ]; + + $this->expectExceptionMessage('Loginname "user-" contains illegal characters.'); + Customers::getLocal($admin_userdata, $data)->add(); + } + + /** + * + * @depends testAdminCustomersAddLoginnameExists + */ + public function testAdminCustomersAddLoginnameInvalid2() + { + global $admin_userdata; + + $data = [ + 'new_loginname' => 'useruseruseruseruseruserX', + 'email' => 'team@froxlor.org', + 'firstname' => 'Test2', + 'name' => 'Testman2', + 'customernumber' => 1339 + ]; + + $this->expectExceptionMessage('Loginname contains too many characters. Only ' . (14 - strlen(Settings::Get('customer.mysqlprefix'))) . ' characters are allowed.'); + Customers::getLocal($admin_userdata, $data)->add(); + } } diff --git a/tests/Emails/EmailsTest.php b/tests/Emails/EmailsTest.php index 5ef1f0d8..9cfd1065 100644 --- a/tests/Emails/EmailsTest.php +++ b/tests/Emails/EmailsTest.php @@ -7,6 +7,8 @@ use PHPUnit\Framework\TestCase; * @covers Emails * @covers EmailForwarders * @covers EmailAccounts + * @covers Customers + * @covers Admins */ class MailsTest extends TestCase { @@ -96,11 +98,72 @@ class MailsTest extends TestCase $result = json_decode($json_result, true)['data']; $this->assertEquals('other@domain.tld', $result['destination']); } + + /** + * @depends testCustomerEmailForwardersAdd + */ + public function testCustomerEmailForwardersAddNoMoreResources() + { + global $admin_userdata; + + // get customer + $json_result = Customers::getLocal($admin_userdata, array( + 'loginname' => 'test1' + ))->get(); + $customer_userdata = json_decode($json_result, true)['data']; + $customer_userdata['email_forwarders_used'] = $customer_userdata['email_forwarders']; + $this->expectExceptionCode(406); + $this->expectExceptionMessage("No more resources available"); + EmailForwarders::getLocal($customer_userdata)->add(); + } + + /** + * @depends testCustomerEmailForwardersAddNoMoreResources + */ + public function testCustomerEmailForwardersAddEmailHidden() + { + global $admin_userdata; + + Settings::Set('panel.customer_hide_options', 'email', true); + + // get customer + $json_result = Customers::getLocal($admin_userdata, array( + 'loginname' => 'test1' + ))->get(); + $customer_userdata = json_decode($json_result, true)['data']; + $this->expectExceptionCode(405); + $this->expectExceptionMessage("You cannot access this resource"); + EmailForwarders::getLocal($customer_userdata)->add(); + } + /** + * @depends testCustomerEmailForwardersAddEmailHidden + */ + public function testCustomerEmailForwardersDeleteEmailHidden() + { + global $admin_userdata; + + Settings::Set('panel.customer_hide_options', 'email', true); + + // get customer + $json_result = Customers::getLocal($admin_userdata, array( + 'loginname' => 'test1' + ))->get(); + $customer_userdata = json_decode($json_result, true)['data']; + $this->expectExceptionCode(405); + $this->expectExceptionMessage("You cannot access this resource"); + EmailForwarders::getLocal($customer_userdata)->delete(); + } + + /** + * @depends testCustomerEmailForwardersDeleteEmailHidden + */ public function testCustomerEmailForwardersAddAnother() { global $admin_userdata; + Settings::Set('panel.customer_hide_options', '', true); + // get customer $json_result = Customers::getLocal($admin_userdata, array( 'loginname' => 'test1' @@ -245,6 +308,8 @@ class MailsTest extends TestCase { global $admin_userdata; + Settings::Set('panel.customer_hide_options', '', true); + // get customer $json_result = Customers::getLocal($admin_userdata, array( 'loginname' => 'test1' diff --git a/tests/Ftps/FtpsTest.php b/tests/Ftps/FtpsTest.php index 38389a44..09a3bfcd 100644 --- a/tests/Ftps/FtpsTest.php +++ b/tests/Ftps/FtpsTest.php @@ -5,6 +5,8 @@ use PHPUnit\Framework\TestCase; * @covers ApiCommand * @covers ApiParameter * @covers Ftps + * @covers Customers + * @covers Admins */ class FtpsTest extends TestCase { diff --git a/tests/Mysqls/MysqlsTest.php b/tests/Mysqls/MysqlsTest.php index bd2a15f9..fda7b559 100644 --- a/tests/Mysqls/MysqlsTest.php +++ b/tests/Mysqls/MysqlsTest.php @@ -6,6 +6,8 @@ use PHPUnit\Framework\TestCase; * @covers ApiCommand * @covers ApiParameter * @covers Mysqls + * @covers Customers + * @covers Admins */ class MysqlsTest extends TestCase { diff --git a/tests/SubDomains/SubDomainsTest.php b/tests/SubDomains/SubDomainsTest.php index d1aa4af0..54b9219a 100644 --- a/tests/SubDomains/SubDomainsTest.php +++ b/tests/SubDomains/SubDomainsTest.php @@ -6,6 +6,8 @@ use PHPUnit\Framework\TestCase; * @covers ApiParameter * @covers SubDomains * @covers Domains + * @covers Customers + * @covers Admins */ class SubDomainsTest extends TestCase {