From 11c2c7cbd5cfab474819dcec246eaa2abef2c1e7 Mon Sep 17 00:00:00 2001 From: Andy Petralli <6251451+andresp@users.noreply.github.com> Date: Sun, 29 Oct 2023 10:19:30 -0700 Subject: [PATCH] Fixtestrunningbug (#25) * Fix minor test bug * Add unittests --- .vscode/launch.json | 2 +- .vscode/settings.json | 9 +++++--- setup.cfg | 8 +++++-- src/docsismodem/exceptions.py | 9 ++++++++ src/docsismodem/modems/technicolor_xb7.py | 19 +++++++++++++++-- tests/{mocks.py => test_mocks.py} | 10 +++++---- tests/test_motorolamb8600m.py | 2 +- tests/test_netgearcm2000.py | 2 +- tests/test_technicolorxb7.py | 26 ++++++++++++++++++++++- tests/test_touchstonetg3492upcch.py | 2 +- 10 files changed, 73 insertions(+), 16 deletions(-) create mode 100644 src/docsismodem/exceptions.py rename tests/{mocks.py => test_mocks.py} (55%) diff --git a/.vscode/launch.json b/.vscode/launch.json index 6ebbffa..f1e93da 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "program": "src/docsismodem/retriever.py", "console": "integratedTerminal", - "justMyCode": true + "justMyCode": false } ] } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index eced3d9..f557e9f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,10 +1,13 @@ { "python.linting.enabled": true, "[python]": { - "editor.defaultFormatter": "ms-python.black-formatter" + "editor.defaultFormatter": "ms-python.python" }, "python.formatting.provider": "none", - "python.testing.pytestArgs": [], + "python.testing.pytestArgs": [ + "tests" + ], "python.testing.unittestEnabled": false, - "python.testing.pytestEnabled": true + "python.testing.pytestEnabled": true, + } \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 702cbfd..6deff74 100644 --- a/setup.cfg +++ b/setup.cfg @@ -34,7 +34,7 @@ install_requires = python-dateutil==2.8.2 python-logging-loki==0.3.1 pytz==2022.7 - requests==2.28.1 + requests==2.31.0 selenium==4.7.2 rfc3339==6.2 schedule==1.1.0 @@ -48,4 +48,8 @@ install_requires = dev = build==1.0.3 flake8==6.1.0 - pytest==7.4.2 \ No newline at end of file + pytest==7.4.2 + responses==0.23.3 + flake8-pytest-style==1.7.2 + flask==2.0.0 + werkzeug==2.0.0 diff --git a/src/docsismodem/exceptions.py b/src/docsismodem/exceptions.py new file mode 100644 index 0000000..7beff45 --- /dev/null +++ b/src/docsismodem/exceptions.py @@ -0,0 +1,9 @@ +class ModemConnectionError(Exception): + def __init__(self, message="An error occurred loging into the modem."): + self.message = message + super().__init__(self.message) + +class ModemCredentialsError(Exception): + def __init__(self, message="An error occurred loging into the modem."): + self.message = message + super().__init__(self.message) \ No newline at end of file diff --git a/src/docsismodem/modems/technicolor_xb7.py b/src/docsismodem/modems/technicolor_xb7.py index 53f4efd..ba652cc 100644 --- a/src/docsismodem/modems/technicolor_xb7.py +++ b/src/docsismodem/modems/technicolor_xb7.py @@ -1,3 +1,4 @@ +from docsismodem.exceptions import ModemConnectionError, ModemCredentialsError from .observablemodem import ObservableModem from bs4 import BeautifulSoup from datetime import datetime @@ -90,8 +91,22 @@ def login(self): 'password': self.config['Modem']['Password'] } loginUrl = "/check.jst" - - self.session.post(self.baseUrl + loginUrl, data=modemAuthentication) + + try: + response = self.session.post(self.baseUrl + loginUrl, data=modemAuthentication) + if response.status_code == 200: + # 200 indicates a login failure + msg = "Invalid login credentials" + logging.error(msg) + raise ModemCredentialsError(msg) + except requests.ConnectionError as e: + msg = 'Could not connect to modem.' + logging.error(msg) + raise ModemConnectionError(msg) + except requests.exceptions.Timeout: + msg = 'Connection to modem timed out.' + logging.error(msg) + raise ModemConnectionError(msg) def collectStatus(self): self.logger.info("Getting modem status") diff --git a/tests/mocks.py b/tests/test_mocks.py similarity index 55% rename from tests/mocks.py rename to tests/test_mocks.py index 6e66507..5bf04a1 100644 --- a/tests/mocks.py +++ b/tests/test_mocks.py @@ -3,15 +3,17 @@ 'Bucket': 'testBucket', 'Org': 'org', 'Host': 'localhost', - 'Port': '443', - 'UseTls': True, + 'Port': '5000', + 'UseTls': False, 'Token': 'token' }, 'General': { 'HostTimezone': 'Pacific/Los_Angeles' }, 'Modem': { - 'Host': '10.0.0.1', - 'LogTimezone': 'Pacific/Los_Angeles' + 'Host': 'localhost:5000', + 'LogTimezone': 'Pacific/Los_Angeles', + 'Username': 'admin', + 'Password': 'password' } } \ No newline at end of file diff --git a/tests/test_motorolamb8600m.py b/tests/test_motorolamb8600m.py index 49ec7f4..9eb21ec 100644 --- a/tests/test_motorolamb8600m.py +++ b/tests/test_motorolamb8600m.py @@ -3,7 +3,7 @@ from docsismodem.modems.observablemodem import ObservableModem from docsismodem.modems.observablemodemfactory import ObservableModemFactory -from tests.mocks import config +from tests.test_mocks import config class TestMotorolaMB8600: diff --git a/tests/test_netgearcm2000.py b/tests/test_netgearcm2000.py index 46f4fc0..18de18b 100644 --- a/tests/test_netgearcm2000.py +++ b/tests/test_netgearcm2000.py @@ -3,7 +3,7 @@ from docsismodem.modems.observablemodem import ObservableModem from docsismodem.modems.observablemodemfactory import ObservableModemFactory -from tests.mocks import config +from tests.test_mocks import config class TestNetgear2000: diff --git a/tests/test_technicolorxb7.py b/tests/test_technicolorxb7.py index 3c248a6..8f627d0 100644 --- a/tests/test_technicolorxb7.py +++ b/tests/test_technicolorxb7.py @@ -1,9 +1,13 @@ import logging + +import pytest +import responses +from docsismodem.exceptions import ModemConnectionError, ModemCredentialsError from docsismodem.modems.observablemodem import ObservableModem from docsismodem.modems.observablemodemfactory import ObservableModemFactory from docsismodem.modems.technicolor_xb7 import TechnicolorXB7 -from tests.mocks import config +from tests.test_mocks import config class TestTechnicolorXB7: @@ -13,3 +17,23 @@ def test_init(self): assert isinstance(instance, ObservableModem) assert type(instance) is TechnicolorXB7 + @responses.activate + def test_succcessful_login(self): + + responses.add(responses.POST, 'http://localhost:5000/check.jst', json={}, status=302) + + instance = ObservableModemFactory.get("TechnicolorXB7", config, logging.getLogger(None)) + instance.login() + + @responses.activate + def test_invalid_login(self): + responses.add(responses.POST, 'http://localhost:5000/check.jst', json={}, status=200) + + instance = ObservableModemFactory.get("TechnicolorXB7", config, logging.getLogger(None)) + with pytest.raises(ModemCredentialsError): + instance.login() + + def test_login_unreachable_modem(self): + instance = ObservableModemFactory.get("TechnicolorXB7", config, logging.getLogger(None)) + with pytest.raises(ModemConnectionError): + instance.login() \ No newline at end of file diff --git a/tests/test_touchstonetg3492upcch.py b/tests/test_touchstonetg3492upcch.py index 0484437..0558a21 100644 --- a/tests/test_touchstonetg3492upcch.py +++ b/tests/test_touchstonetg3492upcch.py @@ -3,7 +3,7 @@ from docsismodem.modems.observablemodemfactory import ObservableModemFactory from docsismodem.modems.touchstone_tg3492_upc_ch import TouchstoneTG3492UPCCH -from tests.mocks import config +from tests.test_mocks import config class TestTouchstoneTG3492UPCCH: