Skip to content

Commit

Permalink
Fixtestrunningbug (#25)
Browse files Browse the repository at this point in the history
* Fix minor test bug

* Add unittests
  • Loading branch information
andresp authored Oct 29, 2023
1 parent ff5a536 commit 11c2c7c
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"program": "src/docsismodem/retriever.py",
"console": "integratedTerminal",
"justMyCode": true
"justMyCode": false
}
]
}
9 changes: 6 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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,

}
8 changes: 6 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,4 +48,8 @@ install_requires =
dev =
build==1.0.3
flake8==6.1.0
pytest==7.4.2
pytest==7.4.2
responses==0.23.3
flake8-pytest-style==1.7.2
flask==2.0.0
werkzeug==2.0.0
9 changes: 9 additions & 0 deletions src/docsismodem/exceptions.py
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 17 additions & 2 deletions src/docsismodem/modems/technicolor_xb7.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from docsismodem.exceptions import ModemConnectionError, ModemCredentialsError
from .observablemodem import ObservableModem
from bs4 import BeautifulSoup
from datetime import datetime
Expand Down Expand Up @@ -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")
Expand Down
10 changes: 6 additions & 4 deletions tests/mocks.py → tests/test_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
2 changes: 1 addition & 1 deletion tests/test_motorolamb8600m.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion tests/test_netgearcm2000.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
26 changes: 25 additions & 1 deletion tests/test_technicolorxb7.py
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -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()
2 changes: 1 addition & 1 deletion tests/test_touchstonetg3492upcch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down

0 comments on commit 11c2c7c

Please sign in to comment.