From f184c23c3106860ccaf519f036a3dd7009deac48 Mon Sep 17 00:00:00 2001 From: Bob Girard Date: Wed, 14 Jan 2015 12:50:16 -0700 Subject: [PATCH] Add unit tests (#1) * Add unit tests for cfg.py, context_processors.py, and menu.py * Add new plinth/tests/data directory for miscellaneous test data * In cfg.py, add an explicit check to verify the existence of the secondary (non-default) plinth.config file * In cfg.py, replace deprecated configparser.SafeConfigParser with configparser.ConfigParser --- plinth/cfg.py | 4 +- .../data/plinth.config.with_missing_options | 8 + .../data/plinth.config.with_missing_sections | 4 + plinth/tests/test_cfg.py | 164 ++++++++++++++++++ plinth/tests/test_context_processors.py | 49 ++++++ plinth/tests/test_menu.py | 139 +++++++++++++++ 6 files changed, 367 insertions(+), 1 deletion(-) create mode 100644 plinth/tests/data/plinth.config.with_missing_options create mode 100644 plinth/tests/data/plinth.config.with_missing_sections create mode 100644 plinth/tests/test_cfg.py create mode 100644 plinth/tests/test_context_processors.py create mode 100644 plinth/tests/test_menu.py diff --git a/plinth/cfg.py b/plinth/cfg.py index e951b942e..0d1e467cb 100644 --- a/plinth/cfg.py +++ b/plinth/cfg.py @@ -57,8 +57,10 @@ def read(): directory = os.path.dirname(os.path.realpath(__file__)) directory = os.path.join(directory, '..') CONFIG_FILE = os.path.join(directory, 'plinth.config') + if not os.path.isfile(CONFIG_FILE): + raise FileNotFoundError('No plinth.config file could be found.') - parser = configparser.SafeConfigParser( + parser = configparser.ConfigParser( defaults={ 'root': os.path.realpath(directory), }) diff --git a/plinth/tests/data/plinth.config.with_missing_options b/plinth/tests/data/plinth.config.with_missing_options new file mode 100644 index 000000000..2ad26600a --- /dev/null +++ b/plinth/tests/data/plinth.config.with_missing_options @@ -0,0 +1,8 @@ +[Name] +product_name = Plinth +box_name = FreedomBox + +[Path] + +[Network] + diff --git a/plinth/tests/data/plinth.config.with_missing_sections b/plinth/tests/data/plinth.config.with_missing_sections new file mode 100644 index 000000000..5870c0b81 --- /dev/null +++ b/plinth/tests/data/plinth.config.with_missing_sections @@ -0,0 +1,4 @@ +[Name] +product_name = Plinth +box_name = FreedomBox + diff --git a/plinth/tests/test_cfg.py b/plinth/tests/test_cfg.py new file mode 100644 index 000000000..75590f60f --- /dev/null +++ b/plinth/tests/test_cfg.py @@ -0,0 +1,164 @@ +#!/usr/bin/python3 +# -*- mode: python; mode: auto-fill; fill-column: 80 -*- +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import configparser +import os +import shutil +import unittest + +from plinth import cfg + + +CONFIG_FILENAME = 'plinth.config' +SAVED_CONFIG_FILE = CONFIG_FILENAME + '.official' +CONFIG_FILE_WITH_MISSING_OPTIONS = CONFIG_FILENAME +\ + '.with_missing_options' +CONFIG_FILE_WITH_MISSING_SECTIONS = CONFIG_FILENAME +\ + '.with_missing_sections' + + +class CfgTestCase(unittest.TestCase): + """Verify that the Plinth configuration module behaves as expected.""" + + config_file = '' + directory = '' + + @classmethod + def setUpClass(cls): + """Locate the official plinth.config file.""" + if os.path.isfile(cfg.DEFAULT_CONFIG_FILE): + cls.config_file = cfg.DEFAULT_CONFIG_FILE + cls.directory = cfg.DEFAULT_ROOT + else: + cls.directory = os.path.realpath(".") + cls.config_file = os.path.join(cls.directory, + CONFIG_FILENAME) + if not(os.path.isfile(cls.config_file)): + raise FileNotFoundError('File {} could not be found.', + format(CONFIG_FILENAME)) + + #Tests + + def test_read_main_menu(self): + """Verify that the cfg.main_menu container is initially empty.""" + # menu should be empty before... + self.assertTrue(len(cfg.main_menu.items) == 0) + cfg.read() + # ...and after reading the config file + self.assertTrue(len(cfg.main_menu.items) == 0) + + def test_read_official_config_file(self): + """Verify that the plinth.config file can be read correctly.""" + + # read the plinth.config file directly + parser = self.read_config_file(CfgTestCase.config_file) + + # read the plinth.config file via the cfg module + cfg.read() + + # compare the two sets of configuration values + # Note that the count of items within each section includes the number + # of default items (1, for 'root') + self.assertEqual(3, len(parser.items('Name'))) + self.assertEqual(parser.get('Name', 'product_name'), cfg.product_name) + self.assertEqual(parser.get('Name', 'box_name'), cfg.box_name) + + self.assertEqual(13, len(parser.items('Path'))) + self.assertEqual(parser.get('Path', 'root'), cfg.root) + self.assertEqual(parser.get('Path', 'file_root'), cfg.file_root) + self.assertEqual(parser.get('Path', 'config_dir'), cfg.config_dir) + self.assertEqual(parser.get('Path', 'data_dir'), cfg.data_dir) + self.assertEqual(parser.get('Path', 'store_file'), cfg.store_file) + self.assertEqual(parser.get('Path', 'actions_dir'), + cfg.actions_dir) + self.assertEqual(parser.get('Path', 'doc_dir'), cfg.doc_dir) + self.assertEqual(parser.get('Path', 'status_log_file'), + cfg.status_log_file) + self.assertEqual(parser.get('Path', 'access_log_file'), + cfg.access_log_file) + self.assertEqual(parser.get('Path', 'pidfile'), cfg.pidfile) + + self.assertEqual(5, len(parser.items('Network'))) + self.assertEqual(parser.get('Network', 'host'), cfg.host) + self.assertEqual(int(parser.get('Network', 'port')), cfg.port) + self.assertEqual(parser.get('Network', 'secure_proxy_ssl_header'), + cfg.secure_proxy_ssl_header) + self.assertEqual(parser.get('Network', 'use_x_forwarded_host'), + cfg.use_x_forwarded_host) + + def test_read_missing_config_file(self): + """Verify that an exception is raised when there's no config file.""" + with self.assertRaises(FileNotFoundError): + try: + self.rename_official_config_file() + cfg.read() + finally: + self.restore_official_config_file() + + def test_read_config_file_with_missing_sections(self): + """Verify that missing configuration sections can be detected.""" + self.assertRaises(configparser.NoSectionError, + self.read_test_config_file, + CONFIG_FILE_WITH_MISSING_SECTIONS) + + def test_read_config_file_with_missing_options(self): + """Verify that missing configuration options can be detected.""" + self.assertRaises(configparser.NoOptionError, + self.read_test_config_file, + CONFIG_FILE_WITH_MISSING_OPTIONS) + + # Helper Methods + + def read_config_file(self, file): + """Read the configuration file independently from cfg.py.""" + parser = configparser.ConfigParser( + defaults={'root': CfgTestCase.directory}) + parser.read(file) + return parser + + def read_test_config_file(self, test_file): + """Read the specified test configuration file.""" + self.replace_official_config_file(test_file) + try: + cfg.read() + finally: + self.restore_official_config_file() + + def rename_official_config_file(self): + """Rename the official config file so that it can't be read.""" + shutil.move(CfgTestCase.config_file, + os.path.join(CfgTestCase.directory, SAVED_CONFIG_FILE)) + + def replace_official_config_file(self, test_file): + """Replace plinth.config with the specified test config file.""" + self.rename_official_config_file() + test_data_directory = os.path.join(os.path.dirname( + os.path.realpath(__file__)), 'data') + shutil.copy2(os.path.join(test_data_directory, test_file), + CfgTestCase.config_file) + + def restore_official_config_file(self): + """Restore the official plinth.config file.""" + if os.path.isfile(CfgTestCase.config_file): + os.remove(CfgTestCase.config_file) + shutil.move(os.path.join(CfgTestCase.directory, SAVED_CONFIG_FILE), + CfgTestCase.config_file) + + +if __name__ == '__main__': + unittest.main() diff --git a/plinth/tests/test_context_processors.py b/plinth/tests/test_context_processors.py new file mode 100644 index 000000000..ededea2eb --- /dev/null +++ b/plinth/tests/test_context_processors.py @@ -0,0 +1,49 @@ +#!/usr/bin/python3 +# -*- mode: python; mode: auto-fill; fill-column: 80 -*- +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.http import HttpRequest +import unittest + +from plinth import context_processors as cp + + +class ContextProcessorsTestCase(unittest.TestCase): + """Verify behavior of the context_processors module.""" + + def test_common(self): + """Verify that the 'common' function returns the correct values.""" + request = HttpRequest() + request.path = '/aaa/bbb/ccc/' + response = cp.common(request) + self.assertIsNotNone(response) + + config = response['cfg'] + self.assertIsNotNone(config) + self.assertEqual('Plinth', config.product_name) + self.assertEqual('FreedomBox', config.box_name) + + submenu = response['submenu'] + self.assertIsNone(submenu) + + urls = response['active_menu_urls'] + self.assertIsNotNone(urls) + self.assertEqual(['/', '/aaa/', '/aaa/bbb/', '/aaa/bbb/ccc/'], urls) + + +if __name__ == '__main__': + unittest.main() diff --git a/plinth/tests/test_menu.py b/plinth/tests/test_menu.py new file mode 100644 index 000000000..8f73ca3f2 --- /dev/null +++ b/plinth/tests/test_menu.py @@ -0,0 +1,139 @@ +#!/usr/bin/python3 +# -*- mode: python; mode: auto-fill; fill-column: 80 -*- +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.http import HttpRequest +import random +import unittest + +from plinth.menu import Menu + + +URL_TEMPLATE = '/a{}/b{}/c{}/' + + +class MenuTestCase(unittest.TestCase): + """Verify the behavior of the Plinth Menu class.""" + + # Test methods + + def test_menu_creation_without_arguments(self): + """Verify the Menu state without initialization parameters.""" + menu = Menu() + self.assertEqual('', menu.label) + self.assertEqual('', menu.icon) + self.assertEqual('#', menu.url) + self.assertEqual(50, menu.order) + self.assertEqual(0, len(menu.items)) + + def test_menu_creation_with_arguments(self): + """Verify the Menu state with initialization parameters.""" + expected_label = 'Label' + expected_icon = 'Icon' + expected_url = '/aaa/bbb/ccc/' + expected_order = 42 + menu = Menu(expected_label, expected_icon, expected_url, + expected_order) + + self.assertEqual(expected_label, menu.label) + self.assertEqual(expected_icon, menu.icon) + self.assertEqual(expected_url, menu.url) + self.assertEqual(expected_order, menu.order) + self.assertEqual(0, len(menu.items)) + + @unittest.skip('requires configuring Django beforehand') + def test_get(self): + """Verify that a menu item can be correctly retrieved.""" + expected_label = 'Label2' + expected_icon = 'Icon2' + expected_url = '/ddd/eee/fff/' + expected_order = 2 + menu = Menu() + menu.add_item(expected_label, expected_icon, expected_url, + expected_order) + actual_item = menu.get(expected_url) + + self.assertIsNotNone(actual_item) + self.assertEqual(expected_label, actual_item.label) + self.assertEqual(expected_icon, actual_item.icon) + self.assertEqual(expected_url, actual_item.url) + self.assertEqual(expected_order, actual_item.order) + self.assertEqual(0, len(actual_item.items)) + + def test_sort_items(self): + """Verify that menu items are sorted correctly.""" + menu = self.build_menu() + + # Verify that the order of every item is equal to or greater + # than the order of the item preceding it + for i in range(1, 5): + self.assertTrue(menu.items[i].order >= menu.items[i-1].order) + + @unittest.skip('requires configuring Django beforehand') + def test_add_urlname(self): + """Verify that a named URL can be added to a menu correctly.""" + + def test_add_item(self): + """Verify that a menu item can be correctly added.""" + expected_label = 'Label3' + expected_icon = 'Icon3' + expected_url = '/ggg/hhh/iii/' + expected_order = 3 + menu = Menu() + actual_item = menu.add_item(expected_label, expected_icon, + expected_url, expected_order) + + self.assertIsNotNone(actual_item) + self.assertEqual(expected_label, actual_item.label) + self.assertEqual(expected_icon, actual_item.icon) + self.assertEqual(expected_url, actual_item.url) + self.assertEqual(expected_order, actual_item.order) + self.assertEqual(0, len(actual_item.items)) + + def test_active_item(self): + """Verify that an active menu item can be correctly retrieved.""" + menu = self.build_menu() + + for i in range(1, 8): + request = HttpRequest() + request.path = URL_TEMPLATE.format(i, i, i) + item = menu.active_item(request) + if i <= 5: + self.assertEqual('Item' + str(i), item.label) + self.assertEqual(request.path, item.url) + else: + self.assertIsNone(item) + + # Helper methods + + def build_menu(self, size=5): + """Build a menu with the specified number of items.""" + random.seed() + item_data = [] + for i in range(1, size+1): + item_data.append(['Item' + str(i), + 'Icon' + str(i), + URL_TEMPLATE.format(i, i, i), + random.randint(0, 100)]) + menu = Menu() + for data in item_data: + menu.add_item(data[0], data[1], data[2], data[3]) + return menu + + +if __name__ == '__main__': + unittest.main()