From f866471b6a9d95d7a9229e75b260e08d9ac9829a Mon Sep 17 00:00:00 2001 From: Marcin Haba Date: Thu, 31 Dec 2015 09:16:42 +0100 Subject: [PATCH] baculum: Add first unit tests --- gui/baculum/tests/test_common.php | 74 ++++++ gui/baculum/tests/units/Class/APITest.php | 87 +++++++ ...gurationManagerApplicationSettingsTest.php | 231 ++++++++++++++++++ .../units/Class/ConfigurationManagerTest.php | 113 +++++++++ .../ConfigurationManagerUsersConfigTest.php | 160 ++++++++++++ 5 files changed, 665 insertions(+) create mode 100644 gui/baculum/tests/test_common.php create mode 100644 gui/baculum/tests/units/Class/APITest.php create mode 100644 gui/baculum/tests/units/Class/ConfigurationManagerApplicationSettingsTest.php create mode 100644 gui/baculum/tests/units/Class/ConfigurationManagerTest.php create mode 100644 gui/baculum/tests/units/Class/ConfigurationManagerUsersConfigTest.php diff --git a/gui/baculum/tests/test_common.php b/gui/baculum/tests/test_common.php new file mode 100644 index 0000000000..1e813f48f9 --- /dev/null +++ b/gui/baculum/tests/test_common.php @@ -0,0 +1,74 @@ +initApplication(); + } +} + +function copy_path($source, $destination) { + if (is_dir($source)) { + if (!is_dir($destination)) { + mkdir($destination); + } + $path = dir($source); + while (($file = $path->read()) != false) { + if ($file == '.' || $file == '..') { + continue; + } + $pathDir = $source . '/' . $file; + copy($pathDir, $destination . '/' . $file); + } + $path->close(); + } +} + +function remove_path($path, $only_content = false) { + if(is_dir($path)) { + $dir = dir($path); + while (($file = $dir->read()) != false) { + if ($file == '.' || $file == '..') { + continue; + } + $pathDir = $path . '/' . $file; + unlink($pathDir); + } + if ($only_content === false) { + rmdir(dirname($pathDir)); + } + } +} + +?> diff --git a/gui/baculum/tests/units/Class/APITest.php b/gui/baculum/tests/units/Class/APITest.php new file mode 100644 index 0000000000..d8e54bd4ea --- /dev/null +++ b/gui/baculum/tests/units/Class/APITest.php @@ -0,0 +1,87 @@ +run(); + } + if (file_exists(BACULUM_ROOT_DIR . 'protected/Data')) { + copy_path(BACULUM_ROOT_DIR . 'protected/Data', BACKUP_FILES_PATH); + remove_path(BACULUM_ROOT_DIR . 'protected/Data', true); + } + } + + public function tearDown() { + if (file_exists(BACKUP_FILES_PATH)) { + remove_path(BACKUP_FILES_PATH . 'protected/Data', true); + copy_path(BACKUP_FILES_PATH, BACULUM_ROOT_DIR . 'protected/Data'); + remove_path(BACKUP_FILES_PATH); + } + } + + public function testApiVersion() { + $testData = array('result' => '0.1'); + $apiModule = self::$application->getModule('api'); + $this->assertEquals($testData['result'], $apiModule::API_VERSION); + } + + public function testGetConnection() { + // prepare example config + $testData = array('db' => array(), 'bconsole' => array(), 'baculum' => array()); + $testData['db']['type'] = 'pgsql'; + $testData['db']['name'] = 'my database 123'; + $testData['db']['login'] = 'admin321'; + $testData['db']['password'] = 'Str0NgPa$$w0Rd'; + $testData['db']['ip_addr'] = '127.0.0.1'; + $testData['db']['port'] = '5433'; + $testData['db']['path'] = ''; + $testData['bconsole']['bin_path'] = '/usr/local/bacula/sbin/bconsole'; + $testData['bconsole']['cfg_path'] = '/usr/local/bacula/etc/bconsole.conf'; + $testData['bconsole']['cfg_custom_path'] = '/etc/bacula/bconsole-{user}.conf'; + $testData['bconsole']['use_sudo'] = 1; + $testData['baculum']['login'] = 'ganiuszka'; + $testData['baculum']['password'] = 'AnOTHe3Str0n6 Pass W OR d'; + $testData['baculum']['debug'] = 0; + $testData['baculum']['lang'] = 'pl'; + + $testResult = array('type' => 'resource'); + + // save example config + self::$application->getModule('configuration')->setApplicationConfig($testData); + + $result = self::$application->getModule('api')->getConnection(); + $this->assertEquals($testResult['type'], gettype($result)); + } +} +?> diff --git a/gui/baculum/tests/units/Class/ConfigurationManagerApplicationSettingsTest.php b/gui/baculum/tests/units/Class/ConfigurationManagerApplicationSettingsTest.php new file mode 100644 index 0000000000..4cea1d46fa --- /dev/null +++ b/gui/baculum/tests/units/Class/ConfigurationManagerApplicationSettingsTest.php @@ -0,0 +1,231 @@ +run(); + } + if (file_exists(BACULUM_ROOT_DIR . 'protected/Data')) { + copy_path(BACULUM_ROOT_DIR . 'protected/Data', BACKUP_FILES_PATH); + remove_path(BACULUM_ROOT_DIR . 'protected/Data', true); + } + } + + public function tearDown() { + if (file_exists(BACKUP_FILES_PATH)) { + remove_path(BACKUP_FILES_PATH . 'protected/Data', true); + copy_path(BACKUP_FILES_PATH, BACULUM_ROOT_DIR . 'protected/Data'); + remove_path(BACKUP_FILES_PATH); + } + } + + public function testSetApplicationConfigPostgreSQL() { + $testData = array('db' => array(), 'bconsole' => array(), 'baculum' => array()); + $testData['db']['type'] = 'pgsql'; + $testData['db']['name'] = 'my database 123'; + $testData['db']['login'] = 'admin321'; + $testData['db']['password'] = 'Str0NgPa$$w0Rd'; + $testData['db']['ip_addr'] = '127.0.0.1'; + $testData['db']['port'] = '5433'; + $testData['db']['path'] = ''; + $testData['bconsole']['bin_path'] = '/usr/local/bacula/sbin/bconsole'; + $testData['bconsole']['cfg_path'] = '/usr/local/bacula/etc/bconsole.conf'; + $testData['bconsole']['cfg_custom_path'] = '/etc/bacula/bconsole-{user}.conf'; + $testData['bconsole']['use_sudo'] = 1; + $testData['baculum']['login'] = 'ganiuszka'; + $testData['baculum']['password'] = 'AnOTHe3Str0n6 Pass W OR d'; + $testData['baculum']['debug'] = 0; + $testData['baculum']['lang'] = 'pl'; + $resultContent = '[db] +type = "pgsql" +name = "my database 123" +login = "admin321" +password = "Str0NgPa$$w0Rd" +ip_addr = "127.0.0.1" +port = "5433" +path = "" + +[bconsole] +bin_path = "/usr/local/bacula/sbin/bconsole" +cfg_path = "/usr/local/bacula/etc/bconsole.conf" +cfg_custom_path = "/etc/bacula/bconsole-{user}.conf" +use_sudo = "1" + +[baculum] +login = "ganiuszka" +password = "AnOTHe3Str0n6 Pass W OR d" +debug = "0" +lang = "pl" + +'; + // check if config does not exist + $result = self::$application->getModule('configuration')->isApplicationConfig(); + $this->assertFalse($result); + + // check saving config + self::$application->getModule('configuration')->setApplicationConfig($testData); + $result = file_get_contents(BACULUM_ROOT_DIR . 'protected/Data/settings.conf'); + // compare written config with expected string + $this->assertEquals($resultContent, $result); + + // check if config already exists + $result = self::$application->getModule('configuration')->isApplicationConfig(); + $this->assertTrue($result); + + // check reading config + $configRead = self::$application->getModule('configuration')->getApplicationConfig(); + // compare test data array with read application config as array + $this->assertEquals($testData, $configRead); + } + + public function testSetApplicationConfigMySQL() { + $testData = array('db' => array(), 'bconsole' => array(), 'baculum' => array()); + $testData['db']['type'] = 'mysql'; + $testData['db']['name'] = 'mydatabase538'; + $testData['db']['login'] = 'administrator'; + $testData['db']['password'] = '#Str0NgPa$##$w0Rd^$#!&#*$@'; + $testData['db']['ip_addr'] = '192.168.0.4'; + $testData['db']['port'] = '3306'; + $testData['db']['path'] = ''; + $testData['bconsole']['bin_path'] = '/usr/local/bacula/sbin/bconsole'; + $testData['bconsole']['cfg_path'] = '/usr/local/bacula/etc/bconsole.conf'; + $testData['bconsole']['cfg_custom_path'] = '/etc/bacula/bconsole-{user}.conf'; + $testData['bconsole']['use_sudo'] = 0; + $testData['baculum']['login'] = 'ganiuszka'; + $testData['baculum']['password'] = 'Str0n6 Pass4W2OR d'; + $testData['baculum']['debug'] = 1; + $testData['baculum']['lang'] = 'en'; + $resultContent = '[db] +type = "mysql" +name = "mydatabase538" +login = "administrator" +password = "#Str0NgPa$##$w0Rd^$#!&#*$@" +ip_addr = "192.168.0.4" +port = "3306" +path = "" + +[bconsole] +bin_path = "/usr/local/bacula/sbin/bconsole" +cfg_path = "/usr/local/bacula/etc/bconsole.conf" +cfg_custom_path = "/etc/bacula/bconsole-{user}.conf" +use_sudo = "0" + +[baculum] +login = "ganiuszka" +password = "Str0n6 Pass4W2OR d" +debug = "1" +lang = "en" + +'; + + // check if config does not exist + $result = self::$application->getModule('configuration')->isApplicationConfig(); + $this->assertFalse($result); + + // check saving config + self::$application->getModule('configuration')->setApplicationConfig($testData); + $result = file_get_contents(BACULUM_ROOT_DIR . 'protected/Data/settings.conf'); + // compare written config with expected string + $this->assertEquals($resultContent, $result); + + // check if config already exists + $result = self::$application->getModule('configuration')->isApplicationConfig(); + $this->assertTrue($result); + + // check reading config + $configRead = self::$application->getModule('configuration')->getApplicationConfig(); + // compare test data array with read application config as array + $this->assertEquals($testData, $configRead); + } + + public function testSetApplicationConfigSQLite() { + $testData = array('db' => array(), 'bconsole' => array(), 'baculum' => array()); + $testData['db']['type'] = 'sqlite'; + $testData['db']['name'] = 'bacula'; + $testData['db']['login'] = 'bacula'; + $testData['db']['password'] = ''; + $testData['db']['ip_addr'] = ''; + $testData['db']['port'] = ''; + $testData['db']['path'] = '/home/gani/mydatabase.db'; + $testData['bconsole']['bin_path'] = '/usr/local/bacula/sbin/bconsole'; + $testData['bconsole']['cfg_path'] = '/usr/local/bacula/etc/bconsole.conf'; + $testData['bconsole']['cfg_custom_path'] = '/etc/bacula/users/bconsole-{user}.conf'; + $testData['bconsole']['use_sudo'] = 0; + $testData['baculum']['login'] = 'kekeke'; + $testData['baculum']['password'] = 'kekeke'; + $testData['baculum']['debug'] = 1; + $testData['baculum']['lang'] = 'en'; + $resultContent = '[db] +type = "sqlite" +name = "bacula" +login = "bacula" +password = "" +ip_addr = "" +port = "" +path = "/home/gani/mydatabase.db" + +[bconsole] +bin_path = "/usr/local/bacula/sbin/bconsole" +cfg_path = "/usr/local/bacula/etc/bconsole.conf" +cfg_custom_path = "/etc/bacula/users/bconsole-{user}.conf" +use_sudo = "0" + +[baculum] +login = "kekeke" +password = "kekeke" +debug = "1" +lang = "en" + +'; + // check if config does not exist + $result = self::$application->getModule('configuration')->isApplicationConfig(); + $this->assertFalse($result); + + // check saving config + self::$application->getModule('configuration')->setApplicationConfig($testData); + $result = file_get_contents(BACULUM_ROOT_DIR . 'protected/Data/settings.conf'); + // compare written config with expected string + $this->assertEquals($resultContent, $result); + + // check if config already exists + $result = self::$application->getModule('configuration')->isApplicationConfig(); + $this->assertTrue($result); + + // check reading config + $configRead = self::$application->getModule('configuration')->getApplicationConfig(); + // compare test data array with read application config as array + $this->assertEquals($testData, $configRead); + } +} +?> diff --git a/gui/baculum/tests/units/Class/ConfigurationManagerTest.php b/gui/baculum/tests/units/Class/ConfigurationManagerTest.php new file mode 100644 index 0000000000..05cece2110 --- /dev/null +++ b/gui/baculum/tests/units/Class/ConfigurationManagerTest.php @@ -0,0 +1,113 @@ +run(); + } + } + + public function testGetDbNameByTypeValid() { + $testData = array( + 'pgsql' => 'PostgreSQL', + 'mysql' => 'MySQL', + 'sqlite' => 'SQLite' + ); + foreach ($testData as $shortName => $longName) { + $result = self::$application->getModule('configuration')->getDbNameByType($shortName); + $this->assertEquals($longName, $result); + } + } + + public function testGetDbNameByTypeInvalid() { + $testData = array("pgsql\n", ' pgsql', ' pgsql ', 'Mysql', 'MYSQL', 'SQlite', 'MySqL', null, true, 0, -1); + for ($i = 0; $i < count($testData); $i++) { + $result = self::$application->getModule('configuration')->getDbNameByType($testData[$i]); + $this->assertNull($result); + } + } + + public function testIsPostgreSQLTypeValid() { + $testData = 'pgsql'; + $result = self::$application->getModule('configuration')->isPostgreSQLType($testData); + $this->assertTrue($result); + } + + public function testIsPostgreSQLTypeInvalid() { + $testData = array("pgsql\n", ' pgsql', ' pgsql ', 'mysql', 'MYSQL', 'sqlite', 'MySqL', null, true, 0, -1); + for ($i = 0; $i < count($testData); $i++) { + $result = self::$application->getModule('configuration')->isPostgreSQLType($testData[$i]); + $this->assertFalse($result); + } + } + + public function testIsMySQLTypeValid() { + $testData = 'mysql'; + $result = self::$application->getModule('configuration')->isMySQLType($testData); + $this->assertTrue($result); + } + + public function testIsMySQLTypeInvalid() { + $testData = array("mysql\n", ' mysql', ' mysql ', 'm ysql', 'MYSQ', 'sqlite', 'pgsql', 'mysq', null, true, 0, -1); + for ($i = 0; $i < count($testData); $i++) { + $result = self::$application->getModule('configuration')->isMySQLType($testData[$i]); + $this->assertFalse($result); + } + } + public function testIsSQLiteTypeValid() { + $testData = 'sqlite'; + $result = self::$application->getModule('configuration')->isSQLiteType($testData); + $this->assertTrue($result); + } + + public function testIsSQLiteTypeInvalid() { + $testData = array("sqlite\n", ' sqlite', ' sqlite ', 's qlite', 'sqlit', 'pgsql', 'mysqs', null, true, 0, -1); + for ($i = 0; $i < count($testData); $i++) { + $result = self::$application->getModule('configuration')->isSQLiteType($testData[$i]); + $this->assertFalse($result); + } + } + + public function testGetDefaultLanguageValid() { + $mockData = false; + $testData = 'en'; + + $mock = $this->getMockBuilder('ConfigurationManager')->setMethods(array('isApplicationConfig'))->getMock(); + $mock->expects($this->once())->method('isApplicationConfig')->will($this->returnValue($mockData)); + $result = $mock->getLanguage(); + $this->assertEquals($testData, $result); + } +} + +?> diff --git a/gui/baculum/tests/units/Class/ConfigurationManagerUsersConfigTest.php b/gui/baculum/tests/units/Class/ConfigurationManagerUsersConfigTest.php new file mode 100644 index 0000000000..69ac8f8f58 --- /dev/null +++ b/gui/baculum/tests/units/Class/ConfigurationManagerUsersConfigTest.php @@ -0,0 +1,160 @@ +run(); + } + if (file_exists(BACULUM_ROOT_DIR . 'protected/Data')) { + copy_path(BACULUM_ROOT_DIR . 'protected/Data', BACKUP_FILES_PATH); + remove_path(BACULUM_ROOT_DIR . 'protected/Data', true); + } + } + + public function tearDown() { + if (file_exists(BACKUP_FILES_PATH)) { + remove_path(BACKUP_FILES_PATH . 'protected/Data', true); + copy_path(BACKUP_FILES_PATH, BACULUM_ROOT_DIR . 'protected/Data'); + remove_path(BACKUP_FILES_PATH); + } + } + + public function testSetUsersConfig() { + $testData = array(); + $testData[] = array( // create new user admin432 + 'data' => array( + 'user' => 'admin432', + 'password' => 'myStrOnGPa$$!', + 'firstusage' => true, + 'olduser' => null + ), + 'result' => 'admin432:bXOrb7VkxRBYY' + ); + $testData[] = array( // add user willyWonka to config + 'data' => array( + 'user' => 'willyWonka', + 'password' => '#=ChOcOlAtEE=#', + 'firstusage' => false, + 'olduser' => null + ), + 'result' => "admin432:bXOrb7VkxRBYY\nwillyWonka:IzGy5zT.vOaeI" + ); + $testData[] = array( // clear users config and create user willyWonka again + 'data' => array( + 'user' => 'willyWonka', + 'password' => '#=ChOcOlAtEE=#', + 'firstusage' => true, + 'olduser' => null + ), + 'result' => "willyWonka:IzGy5zT.vOaeI" + ); + $testData[] = array( // add user donkamillo to config + 'data' => array( + 'user' => 'donkamillo', + 'password' => '!@#$%^&*()_+}{|"\'<>?/.,', + 'firstusage' => false, + 'olduser' => null + ), + 'result' => "willyWonka:IzGy5zT.vOaeI\ndonkamillo:IU029IHEUYWhY" + ); + $testData[] = array( // rename user willyWonka to user johny11 + 'data' => array( + 'user' => 'johny11', + 'password' => '#=ChOcOlAtEE=#', + 'firstusage' => false, + 'olduser' => 'willyWonka' + ), + 'result' => "donkamillo:IU029IHEUYWhY\njohny11:IzGy5zT.vOaeI" + ); + $testData[] = array( // change password for user johny11 + 'data' => array( + 'user' => 'johny11', + 'password' => 'CoffeeAndTee!@#$%^&*()_+}{|"\'<>?/.:,', + 'firstusage' => false, + 'olduser' => null + ), + 'result' => "donkamillo:IU029IHEUYWhY\njohny11:Q2XfKovUGQe52" + ); + $testData[] = array( // input the same password again for user johny11 + 'data' => array( + 'user' => 'johny11', + 'password' => 'CoffeeAndTee!@#$%^&*()_+}{|"\'<>?/.:,', + 'firstusage' => false, + 'olduser' => null + ), + 'result' => "donkamillo:IU029IHEUYWhY\njohny11:Q2XfKovUGQe52" + ); + for ($i = 0; $i < count($testData); $i++) { + $result = self::$application->getModule('configuration')->setUsersConfig( + $testData[$i]['data']['user'], + $testData[$i]['data']['password'], + $testData[$i]['data']['firstusage'], + $testData[$i]['data']['olduser'] + ); + $this->assertTrue($result); + $result = file_get_contents(BACULUM_ROOT_DIR . 'protected/Data/baculum.users'); + $this->assertEquals($testData[$i]['result'], $result); + } + } + + public function testCheckAndClearUsersConfig() { + $testData[] = array( // input the same password again for user johny11 + 'data' => array( + 'user' => 'johny11', + 'password' => 'CoffeeAndTee!@#$%^&*()_+}{|"\'<>?/.:,', + 'firstusage' => false, + 'olduser' => null + ), + 'result' => "johny11:Q2XfKovUGQe52" + ); + $result = self::$application->getModule('configuration')->isUsersConfig(); + $this->assertFalse($result); + for ($i = 0; $i < count($testData); $i++) { + $result = self::$application->getModule('configuration')->setUsersConfig( + $testData[$i]['data']['user'], + $testData[$i]['data']['password'], + $testData[$i]['data']['firstusage'], + $testData[$i]['data']['olduser'] + ); + $this->assertTrue($result); + $result = file_get_contents(BACULUM_ROOT_DIR . 'protected/Data/baculum.users'); + $this->assertEquals($testData[$i]['result'], $result); + } + $result = self::$application->getModule('configuration')->isUsersConfig(); + $this->assertTrue($result); + $result = self::$application->getModule('configuration')->clearUsersConfig(); + $this->assertTrue($result); + } +} +?> -- 2.39.5