Skip to content

Commit 15add5e

Browse files
committed
Added more tests.
1 parent 4779344 commit 15add5e

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

tests/mockserver.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""A simple mock NUT server for testing the Python client."""
22

33
class MockServer(object):
4-
def __init__(self, host=None, port=None, broken=False, ok=True):
4+
def __init__(self, host=None, port=None, broken=True, ok=True,
5+
broken_username=False):
56
self.valid = "test"
67
self.broken = broken
78
self.ok = ok
9+
self.broken_username = broken_username
810

911
def write(self, text):
1012
self.command = text
@@ -17,7 +19,9 @@ def close(self):
1719
pass
1820

1921
def run_command(self):
20-
if self.broken == True:
22+
if self.broken and not self.broken_username and self.command == "USERNAME %s\n" % self.valid:
23+
return 'OK\n'
24+
elif self.broken:
2125
return 'ERR\n'
2226
elif self.command == "HELP\n":
2327
return 'Commands: HELP VER GET LIST SET INSTCMD LOGIN LOGOUT USERNAME PASSWORD STARTTLS\n'

tests/testclient.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ class TestClient(unittest.TestCase):
1212

1313
def setUp(self):
1414
self.client = PyNUTClient(connect=False, debug=True)
15-
self.client._srv_handler = MockServer()
15+
self.client._srv_handler = MockServer(broken=False)
1616
self.broken_client = PyNUTClient(connect=False, debug=True)
1717
self.broken_client._srv_handler = MockServer(broken=True)
1818
self.not_ok_client = PyNUTClient(connect=False, debug=True)
19-
self.not_ok_client._srv_handler = MockServer(ok=False)
19+
self.not_ok_client._srv_handler = MockServer(ok=False,
20+
broken=False)
2021
self.valid = "test"
2122
self.invalid = "does_not_exist"
2223
self.valid_ups_name = "Test UPS 1"
@@ -46,8 +47,13 @@ def test_connect_debug(self):
4647
except Exception:
4748
assert(False)
4849

50+
def test_connect_broken(self):
51+
telnetlib.Telnet = MockServer
52+
client = PyNUTClient(login=self.valid, password=self.valid,
53+
connect=False)
54+
self.assertRaises(PyNUTError, client._connect)
55+
4956
def test_connect_credentials(self):
50-
# FIXME: Mock the __gettem__ call, so this can be fully tested.
5157
try:
5258
PyNUTClient(login=self.valid, password=self.valid)
5359
except TypeError:
@@ -57,6 +63,18 @@ def test_connect_credentials(self):
5763
except Exception:
5864
assert(False)
5965

66+
def test_connect_credentials_username_ok(self):
67+
try:
68+
telnetlib.Telnet = MockServer
69+
PyNUTClient(login=self.valid, password=self.valid,
70+
debug=True)
71+
except TypeError:
72+
pass
73+
except PyNUTError:
74+
pass
75+
except Exception:
76+
assert(False)
77+
6078
def test_get_ups_list(self):
6179
ups_list = self.client.list_ups()
6280
self.assertEquals(type(ups_list), dict)

0 commit comments

Comments
 (0)