-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_gateway.py
275 lines (229 loc) · 8.78 KB
/
test_gateway.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import gateway
import testdevices
import pytest
import gevent
import json
import base64
import werkzeug.security as wsecurity
import os
import time
app = gateway.app
gateway.api_users['TEST_USER'] = wsecurity.generate_password_hash('XXX_TEST_PASSWORD')
def basic_auth(u, p):
s = "{}:{}".format(u, p)
n = base64.b64encode(s.encode('utf8'))
return b"Basic " + n
def authed(**kwargs):
username = kwargs.get('username', 'TEST_USER')
password = kwargs.get('password', 'XXX_TEST_PASSWORD')
if kwargs.get('username') is not None:
del kwargs['username']
if kwargs.get('password') is not None:
del kwargs['password']
headers = kwargs.get('headers', {})
headers['Authorization'] = basic_auth(username, password)
kwargs['headers'] = headers
return kwargs
@pytest.mark.parametrize("verb,action", [
("GET", "/state"),
("POST", "/unlock"),
("POST", "/lock"),
])
def test_unknown_door_404(verb, action):
doorid = "unknown-door-id-666"
with app.test_client() as client:
r = getattr(client, verb.lower())("doors/{}{}".format(doorid, action), **authed())
body = r.data.decode('utf8')
assert r.status_code == 404
assert 'text/plain' in r.content_type
assert 'Unknown' in body
assert 'unknown-door-id-666' in body
# TODO: parametrize
def test_missing_credentials_401(devices):
with app.test_client() as c:
r = c.post("doors/virtual-1/unlock?timeout=1.0")
body = r.data.decode('utf8')
assert 'text/plain' in r.content_type
assert r.status_code == 401
def test_wrong_password_403(devices):
with app.test_client() as c:
r = c.post("doors/virtual-1/unlock?timeout=1.0", **authed(password='wrong'))
body = r.data.decode('utf8')
assert 'text/plain' in r.content_type
assert r.status_code == 403
def test_empty_password_403(devices):
with app.test_client() as c:
r = c.post("doors/virtual-1/unlock?timeout=1.0", **authed(password=''))
body = r.data.decode('utf8')
assert r.status_code == 403
def test_invalid_user_403(devices):
with app.test_client() as c:
r = c.post("doors/virtual-1/unlock?timeout=1.0", **authed(username='invalid'))
body = r.data.decode('utf8')
assert r.status_code == 403
def test_invalid_user_emptypass_403(devices):
with app.test_client() as c:
r = c.post("doors/virtual-1/unlock?timeout=1.0", **authed(username='invalid', password=''))
body = r.data.decode('utf8')
assert r.status_code == 403
@pytest.fixture(scope="module")
def devices():
gateway.mqtt_client = gateway.setup_mqtt_client()
gevent.sleep(1) # ensure we are connected/subcribed before discovery
testdevices.run()
gevent.sleep(1) # let the devices spin up
@pytest.fixture(scope="module")
def mqtt_test_client():
broker_url = os.environ.get('MSGFLO_BROKER', 'mqtt://localhost')
mqtt_client, host, port = gateway.create_mqtt_client(broker_url)
timeout = 5
mqtt_client.connect(host, port, timeout)
return mqtt_client
# POST /door/id/unlock
def test_unlock_successful(devices):
with app.test_client() as c:
r = c.post("doors/virtual-1/unlock?timeout=1.0", **authed())
body = r.data.decode('utf8')
assert r.status_code == 200
assert 'unlocked' in body
def test_unlock_errors(devices):
with app.test_client() as c:
r = c.post("doors/erroring-1/unlock", **authed())
body = r.data.decode('utf8')
assert r.status_code == 502
assert 'error' in body
assert 'fails always' in body
def test_unlock_timeout(devices):
with app.test_client() as c:
r = c.post("doors/notresponding-1/unlock?timeout=0.5", **authed())
body = r.data.decode('utf8')
assert r.status_code == 504
def test_unlock_with_duration(devices):
with app.test_client() as c:
r = c.post("doors/virtual-1/unlock?duration=1", **authed())
body = r.data.decode('utf8')
assert r.status_code == 200
gevent.sleep(2) # ensure is locked again at end of test
def test_xss_doorid(devices):
with app.test_client() as c:
r = c.post("doors/%3Cinput%20onfocus%3Dalert%281%29%20autofocus%3E/lock", **authed())
body = r.data.decode('utf8')
assert r.status_code != 200
assert '<input ' not in body
# POST /door/id/lock
def test_lock_successful(devices):
with app.test_client() as c:
r = c.post("doors/virtual-1/lock", **authed())
body = r.data.decode('utf8')
assert r.status_code == 200
assert 'text/plain' in r.content_type
assert ' locked' in body
def test_lock_timeout(devices):
with app.test_client() as c:
r = c.post("doors/notresponding-1/lock?timeout=0.5", **authed())
body = r.data.decode('utf8')
assert 'text/plain' in r.content_type
assert r.status_code == 504
def test_lock_errors(devices):
with app.test_client() as c:
r = c.post("doors/erroring-1/lock", **authed())
body = r.data.decode('utf8')
assert r.status_code == 502
assert 'text/plain' in r.content_type
assert 'error' in body
assert 'fails always' in body
# GET /status
def test_status_missing_device_503(devices):
with app.test_client() as c:
r = c.get("status", **authed())
body = r.data.decode('utf8')
assert r.status_code == 503
assert r.content_type == 'application/json'
details = json.loads(body)
assert details['doors']['sorenga-1']['status'] == 503
assert details['doors']['virtual-1']['status'] == 200
assert details['doors']['virtual-1']['last_seen'] >= 1512050000
def test_status_no_bolt_sensor(devices):
door_id = 'virtual-1' # door without bolt sensor
assert gateway.doors[door_id]
with app.test_client() as c:
r = c.get("status", **authed())
details = json.loads(r.data.decode('utf8'))
assert 'bolt' not in details['doors'][door_id], 'bolt information should not be present'
def test_status_bolt_sensor_changes(devices):
door_id = 'virtual-2'
gpio_number = 22
def set_bolt_present(state : bool):
device = 'doors/'+door_id
testdevices.set_fake_gpio(device, gpio_number, state)
gevent.sleep(0.5)
with app.test_client() as c:
test_start = time.time()
set_bolt_present(False)
set_bolt_present(True)
r = c.get("status", **authed())
details = json.loads(r.data.decode('utf8'))
assert 'bolt' in details['doors'][door_id]
bolt = details['doors'][door_id]['bolt']
assert bolt['present']
assert bolt['last_updated'] > test_start
update_time = time.time()
set_bolt_present(False)
r = c.get("status", **authed())
details = json.loads(r.data.decode('utf8'))
assert 'bolt' in details['doors'][door_id]
bolt = details['doors'][door_id]['bolt']
assert not bolt['present']
assert bolt['last_updated'] > update_time
# TODO: keep this info in database
not_running = [
'notresponding-1',
'sorenga-1',
'fubiak-1',
'dev-0',
'origo-1',
'origo-2',
'deichman-toyen',
'lindeberg-1',
'deichman-majorstuen',
'loren-1',
'deichman-stovner',
'unused-4',
'unused-12',
'unused-13',
]
ignore = '&'.join('ignore={}'.format(d) for d in not_running)
def test_status_all_devices_ok(devices):
with app.test_client() as c:
r = c.get("status?" + ignore, **authed())
body = r.data.decode('utf8')
assert r.content_type == 'application/json'
assert r.status_code == 200, body
details = json.loads(body)
statuses = [ d['status'] for d in details['doors'].values() ]
assert statuses == [200] * len(statuses)
assert 'sorenga-2' not in details['doors'].keys()
def test_status_seen_but_too_long_ago(devices):
with app.test_client() as c:
r = c.get("status?"+ignore+"&timeperiod=1", **authed())
body = r.data.decode('utf8')
assert r.status_code == 503, body
details = json.loads(body)
assert details['doors']['virtual-1']['status'] == 503
def test_invalid_fbp_message(devices, mqtt_test_client):
'''should not influence future messages'''
with app.test_client() as c:
status_url = "status?"+ignore
r = c.get(status_url, **authed())
body = r.data.decode('utf8')
assert r.content_type == 'application/json'
assert r.status_code == 200, body
# Send invalid message
mqtt_test_client.publish('fbp', '{}')
gevent.sleep(0.5)
# check still works
r = c.post("doors/virtual-1/unlock?duration=1", **authed())
body = r.data.decode('utf8')
assert r.status_code == 200
# ensure is locked again at end of test
gevent.sleep(1.1)