From b5e3b4259519f1174e790365020d87d2103c939d Mon Sep 17 00:00:00 2001 From: Joshua Rose Date: Mon, 10 Apr 2023 12:35:02 +1000 Subject: [PATCH 1/4] ref: lower case naming convention --- Tests/Config/test_config.py | 35 ----------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 Tests/Config/test_config.py diff --git a/Tests/Config/test_config.py b/Tests/Config/test_config.py deleted file mode 100644 index 221e533..0000000 --- a/Tests/Config/test_config.py +++ /dev/null @@ -1,35 +0,0 @@ -import os -import unittest -from configparser import ConfigParser - -class TestConfig(unittest.TestCase): - def setUp(self): - """ Setup is the first method run in the test.""" - self.path = os.path.join('Config/logger.ini') - - if not os.path.isdir('Logs'): - os.mkdir('Logs') - if not os.path.exists(os.path.join('Logs', 'traceback.log')): - with open(os.path.join('Logs', 'traceback.log'), 'w') as fp: - fp.write("Created traceback.log as part of tests.") - fp.close() - - def test_config_exists(self): - self.assertTrue(self.path) - - def test_config_is_valid(self): - parser = ConfigParser() - if self.test_config_exists: - parser.read(self.path) - - # Check if the required sections are present in the config - self.assertIn('formatters', parser.sections()) - self.assertIn('handler_console', parser.sections()) - - # Check if the required keys are present in the sections - self.assertIn('keys', parser['formatters']) - self.assertIn('level', parser['handler_console']) - - -if __name__ == "__main__": - unittest.main() From d4b8670ecc8115c1988f8ae6254dee6b0490c08e Mon Sep 17 00:00:00 2001 From: Joshua Rose Date: Mon, 10 Apr 2023 12:42:31 +1000 Subject: [PATCH 2/4] feat: impelemented test project structure for #40 --- tests/Common/test_breach_checker.py | 0 tests/Common/test_utils.py | 0 tests/Config/test_check_config_validity.py | 0 tests/Config/test_config.py | 35 +++++++++++++++++++ tests/Config/test_formatter.py | 0 tests/Email/test_email_reputation_checker.py | 0 tests/IP/test_ip_reputation_checker.py | 0 .../test_username_reputation_checker.py | 0 8 files changed, 35 insertions(+) create mode 100644 tests/Common/test_breach_checker.py create mode 100644 tests/Common/test_utils.py create mode 100644 tests/Config/test_check_config_validity.py create mode 100644 tests/Config/test_config.py create mode 100644 tests/Config/test_formatter.py create mode 100644 tests/Email/test_email_reputation_checker.py create mode 100644 tests/IP/test_ip_reputation_checker.py create mode 100644 tests/Username/test_username_reputation_checker.py diff --git a/tests/Common/test_breach_checker.py b/tests/Common/test_breach_checker.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/Common/test_utils.py b/tests/Common/test_utils.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/Config/test_check_config_validity.py b/tests/Config/test_check_config_validity.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/Config/test_config.py b/tests/Config/test_config.py new file mode 100644 index 0000000..221e533 --- /dev/null +++ b/tests/Config/test_config.py @@ -0,0 +1,35 @@ +import os +import unittest +from configparser import ConfigParser + +class TestConfig(unittest.TestCase): + def setUp(self): + """ Setup is the first method run in the test.""" + self.path = os.path.join('Config/logger.ini') + + if not os.path.isdir('Logs'): + os.mkdir('Logs') + if not os.path.exists(os.path.join('Logs', 'traceback.log')): + with open(os.path.join('Logs', 'traceback.log'), 'w') as fp: + fp.write("Created traceback.log as part of tests.") + fp.close() + + def test_config_exists(self): + self.assertTrue(self.path) + + def test_config_is_valid(self): + parser = ConfigParser() + if self.test_config_exists: + parser.read(self.path) + + # Check if the required sections are present in the config + self.assertIn('formatters', parser.sections()) + self.assertIn('handler_console', parser.sections()) + + # Check if the required keys are present in the sections + self.assertIn('keys', parser['formatters']) + self.assertIn('level', parser['handler_console']) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/Config/test_formatter.py b/tests/Config/test_formatter.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/Email/test_email_reputation_checker.py b/tests/Email/test_email_reputation_checker.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/IP/test_ip_reputation_checker.py b/tests/IP/test_ip_reputation_checker.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/Username/test_username_reputation_checker.py b/tests/Username/test_username_reputation_checker.py new file mode 100644 index 0000000..e69de29 From af8d568840492d3643c7d879516b5bada2a1e39f Mon Sep 17 00:00:00 2001 From: Joshua Rose Date: Mon, 10 Apr 2023 13:32:45 +1000 Subject: [PATCH 3/4] feat: tests for config and formatter Addresses several bullet points in #38 --- Config/config.py | 2 +- tests/Config/test_config.py | 22 +++++++++++++++++ tests/Config/test_formatter.py | 45 ++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/Config/config.py b/Config/config.py index 7bced48..97e4be2 100644 --- a/Config/config.py +++ b/Config/config.py @@ -39,7 +39,7 @@ def get_logging_config(file_location="Config/logger.ini"): open(file_location, "r") except FileNotFoundError: logging.critical(f"File location invalid: {file_location}") - sys.exit(1) + raise FileNotFoundError config = get_config('Config/logger.ini') return config diff --git a/tests/Config/test_config.py b/tests/Config/test_config.py index 221e533..73355d0 100644 --- a/tests/Config/test_config.py +++ b/tests/Config/test_config.py @@ -1,5 +1,12 @@ import os import unittest + +import sys + +sys.path.insert(0, os.getcwd()) + +from requests.api import get +from Config.config import configure_logging, get_config, get_logging_config from configparser import ConfigParser class TestConfig(unittest.TestCase): @@ -30,6 +37,21 @@ def test_config_is_valid(self): self.assertIn('keys', parser['formatters']) self.assertIn('level', parser['handler_console']) + def test_directory_structure(self): + """ Check that config module is where it's supposed to be """ + + self.assertTrue(os.path.exists(os.path.join("Config", "config.py"))) + + def test_get_config(self): + self.assertIn("property", get_logging_config.__builtins__.keys()) + + def test_get_logging_config(self): + with self.assertRaises(FileNotFoundError) as _: + get_logging_config("random_path.txt") + self.assertIsInstance(get_logging_config(), ConfigParser) + + def test_configure_logging(self): + self.assertIsInstance(configure_logging(), ConfigParser) if __name__ == "__main__": unittest.main() diff --git a/tests/Config/test_formatter.py b/tests/Config/test_formatter.py index e69de29..5bb2760 100644 --- a/tests/Config/test_formatter.py +++ b/tests/Config/test_formatter.py @@ -0,0 +1,45 @@ +import os +import sys +import unittest +import logging +sys.path.insert(0, os.getcwd()) +from Config.formatter import Formatter +from Config.config import configure_logging + + +class TestFormatter(unittest.TestCase): + def setUp(self): + # Ensure file paths are all set up + if not os.path.split(os.path.basename(os.getcwd()))[1] == "HackAlert": + print("Pathname invalid, ensure you're in the base directory.") + sys.exit(1) + if not os.path.isdir(os.path.join("logs")) or not os.path.isdir(os.path.join("logs", "tests")): + os.makedirs(os.path.join('logs', 'tests')) + files = [os.path.join("logs/tests/test_critical.log"), + os.path.join("logs/tests/test_error.log"), + os.path.join("logs/tests/test_warning.log"), + os.path.join("logs/tests/test_debug.log"), + os.path.join("logs/tests/test_info.log")] + for file_ in files: + if not os.path.exists(file_): + with open(file_, "w") as file: + file.write(f"Created {file_} for tests.") + file.close() + + configure_logging() + self.formatter = Formatter() + self.logger = logging.getLogger() + + def test_records(self): + self.records = { + "critical": self.logger.makeRecord( "test_critical", logging.CRITICAL, "logs/tests/test_critical.log", 50, "[test] critical message", (), None,), + "error": self.logger.makeRecord( "test_error", logging.ERROR, "logs/tests/test_error.log", 40, "[test] error message", (), None,), + "warning": self.logger.makeRecord( "test_warning", logging.WARNING, "logs/tests/test_warning.log", 30, "[test] warning message", (), None,), + "debug": self.logger.makeRecord( "test_debug", logging.DEBUG, "logs/tests/test_debug.log", 20, "[test] debug message", (), None,), + "info": self.logger.makeRecord( "test_info", logging.INFO, "logs/tests/test_info.log", 10, "[test] info message", (), None,), + } + + for record in self.records.keys(): + if self.assertIsInstance(self.records[record], logging.LogRecord) is not False: + self.assertIn(record, self.records[record].msg) + From c24b8d7beba13d6674f2202ef62de47accd168c5 Mon Sep 17 00:00:00 2001 From: Joshua Rose Date: Tue, 11 Apr 2023 21:28:23 +1000 Subject: [PATCH 4/4] feat(tests): Tests for checking validity of config --- Config/check_config_validity.py | 8 +++++++- tests/Config/test_check_config_validity.py | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Config/check_config_validity.py b/Config/check_config_validity.py index 957fa1a..0718e8d 100644 --- a/Config/check_config_validity.py +++ b/Config/check_config_validity.py @@ -1,7 +1,11 @@ import configparser config = configparser.ConfigParser() -config.read('Config/logger.ini') + +def read_config(): + return config.read('Config/logger.ini') + + # check if the required sections are present if 'loggers' not in config.sections() or 'handlers' not in config.sections(): @@ -21,3 +25,5 @@ if not isinstance(config.getint('loggers', 'keys.suspicious.level'), int): raise ValueError('Invalid value for keys.suspicious.level in config.ini') +if __name__ == "__main__": + read_config() diff --git a/tests/Config/test_check_config_validity.py b/tests/Config/test_check_config_validity.py index e69de29..7904908 100644 --- a/tests/Config/test_check_config_validity.py +++ b/tests/Config/test_check_config_validity.py @@ -0,0 +1,22 @@ +import configparser +import unittest +import sys +import os +sys.path.insert(0, os.getcwd()) +from Config import check_config_validity + + +class TestConfigValid(unittest.TestCase): + def setUp(self): + check_config_validity.read_config() + self.config = configparser.ConfigParser() + + def test_key_value_format(self): + self.assertIsInstance(self.config.getint('handlers', 'console.level'), int) + self.assertIsInstance(self.config.getint('loggers', 'keys.suspicious.level'), int) + + def test_keys_present_in_handlers(self): + assert self.config['loggers'] or 'suspicious' in self.config ['loggers'] + + def test_sections_present(self): + assert 'loggers' in self.config.sections() or 'handlers' in self.config.sections()