From 36cc1dc7ab0c4cb9e5e4a78fab94300e67962e4f Mon Sep 17 00:00:00 2001 From: Iain Learmonth Date: Thu, 30 Mar 2023 14:11:09 +0100 Subject: [PATCH] tests: switch from nose to pytest --- .gitlab-ci.yml | 6 +-- scripts/quicktest.sh | 2 +- tests/list/test_obfuscating_encoder.py | 75 +++++++++++++------------- tests/proxy/test_proxy_automation.py | 56 +++++++++++++++++++ 4 files changed, 96 insertions(+), 43 deletions(-) create mode 100644 tests/proxy/test_proxy_automation.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 406da9a..75f103b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -41,13 +41,13 @@ test:mypy: - pip install -r requirements-types.txt --quiet - mypy app -test:nose: +test:pytest: script: - cp config.yaml.example config.yaml - apt update && apt install build-essential - pip install -r requirements.txt --quiet - - pip install nose - - DISABLE_METRICS=true nosetests tests + - pip install pytest + - DISABLE_METRICS=true pytest tests test:pylint: script: diff --git a/scripts/quicktest.sh b/scripts/quicktest.sh index ddb1df0..983474b 100755 --- a/scripts/quicktest.sh +++ b/scripts/quicktest.sh @@ -5,5 +5,5 @@ set -e bandit -r app flake8 app mypy app -nosetests tests +pytest tests diff --git a/tests/list/test_obfuscating_encoder.py b/tests/list/test_obfuscating_encoder.py index cf753bf..d7140c3 100644 --- a/tests/list/test_obfuscating_encoder.py +++ b/tests/list/test_obfuscating_encoder.py @@ -1,47 +1,44 @@ import json - -from nose.tools import assert_equal, assert_not_in +import unittest from app.terraform.list import obfuscator -def test_obfuscating_string(): - data = "hello" - obfs = obfuscator(data) - print(f"Obfuscated string: {obfs}") - j = json.dumps(obfs).replace("!AAA!", "\\u") - loaded = json.loads(j) - assert_equal(data, loaded) +class TestObfuscatingEncoder(unittest.TestCase): + def test_obfuscating_string(self): + data = "hello" + obfs = obfuscator(data) + print(f"Obfuscated string: {obfs}") + j = json.dumps(obfs).replace("!AAA!", "\\u") + loaded = json.loads(j) + self.assertEqual(data, loaded) + def test_obfuscating_simple_dict(self): + data = {"hello": "world"} + obfs = obfuscator(data) + print(f"Obfuscated string: {obfs}") + j = json.dumps(obfs).replace("!AAA!", "\\u") + self.assertNotIn("hello", obfs) + self.assertNotIn("world", obfs) + loaded = json.loads(j) + self.assertEqual(data, loaded) -def test_obfuscating_simple_dict(): - data = {"hello": "world"} - obfs = obfuscator(data) - print(f"Obfuscated string: {obfs}") - j = json.dumps(obfs).replace("!AAA!", "\\u") - assert_not_in("hello", obfs) - assert_not_in("world", obfs) - loaded = json.loads(j) - assert_equal(data, loaded) + def test_obfuscating_simple_list(self): + data = ["hello", "world"] + obfs = obfuscator(data) + print(f"Obfuscated string: {obfs}") + j = json.dumps(obfs).replace("!AAA!", "\\u") + self.assertNotIn("hello", obfs) + self.assertNotIn("world", obfs) + loaded = json.loads(j) + self.assertEqual(data, loaded) - -def test_obfuscating_simple_list(): - data = ["hello", "world"] - obfs = obfuscator(data) - print(f"Obfuscated string: {obfs}") - j = json.dumps(obfs).replace("!AAA!", "\\u") - assert_not_in("hello", obfs) - assert_not_in("world", obfs) - loaded = json.loads(j) - assert_equal(data, loaded) - - -def test_obfuscating_for_real(): - data = json.load(open("tests/list/mirrorSites.json")) - obfs = obfuscator(data) - j = json.dumps(obfs).replace("!AAA!", "\\u") - print(f"Obfuscated string: {obfs}") - for a in range(17, 27): - assert_not_in(chr(a), j) - loaded = json.loads(j) - assert_equal(data, loaded) + def test_obfuscating_for_real(self): + data = json.load(open("tests/list/mirrorSites.json")) + obfs = obfuscator(data) + j = json.dumps(obfs).replace("!AAA!", "\\u") + print(f"Obfuscated string: {obfs}") + for a in range(17, 27): + self.assertNotIn(chr(a), j) + loaded = json.loads(j) + self.assertEqual(data, loaded) diff --git a/tests/proxy/test_proxy_automation.py b/tests/proxy/test_proxy_automation.py new file mode 100644 index 0000000..9fa1efc --- /dev/null +++ b/tests/proxy/test_proxy_automation.py @@ -0,0 +1,56 @@ +import unittest +from unittest.mock import MagicMock, patch +from app.terraform.proxy import ProxyAutomation, update_smart_proxy_instance +from app import app + + +class TestUpdateSmartProxyInstance(unittest.TestCase): + def setUp(self): + self.group_id = 1 + self.provider = 'test_provider' + self.region = 'test_region' + self.instance_id = 'test_instance_id' + app.config['TESTING'] = True + self.app = app.test_client() + + @patch('app.terraform.proxy.SmartProxy') + @patch('app.terraform.proxy.db') + def test_update_smart_proxy_instance_new(self, mock_db, mock_smart_proxy): + # Configure the mocked SmartProxy query to return None + mock_smart_proxy.query.filter.return_value.first.return_value = None + + # Call the function + update_smart_proxy_instance(self.group_id, self.provider, self.region, self.instance_id) + + # Assert that a new SmartProxy instance was created and added to the session + mock_smart_proxy.assert_called_once() + mock_db.session.add.assert_called_once() + + @patch('app.terraform.proxy.SmartProxy') + @patch('app.terraform.proxy.db') + def test_update_smart_proxy_instance_existing(self, mock_db, mock_smart_proxy): + # Configure the mocked SmartProxy query to return an existing instance + mock_instance = MagicMock() + mock_smart_proxy.query.filter.return_value.first.return_value = mock_instance + + # Call the function + update_smart_proxy_instance(self.group_id, self.provider, self.region, self.instance_id) + + # Assert that the existing SmartProxy instance was updated + self.assertEqual(mock_instance.instance_id, self.instance_id) + + +class TestProxyAutomation(unittest.TestCase): + def setUp(self): + app.config['TESTING'] = True + self.app = app.test_client() + + def test_proxy_automation_abstract_methods(self): + # Test NotImplementedError for import_state + with self.assertRaises(NotImplementedError): + proxy_automation = ProxyAutomation() + proxy_automation.import_state(None) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file