-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.py
338 lines (306 loc) · 14.7 KB
/
tests.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import unittest, time, json, datetime, os
import jsonschema
from notify import Emailer, NotifyError
from app import Feed, JsonSettings, Validator
class EmailerTests(unittest.TestCase):
def setUp(self):
options = {
"test": True,
"host": "localhost",
"port": 25,
"sender": "[email protected]"
}
self.emailer = Emailer(options)
def test_recipient_format(self):
# Test that we require at least one of "to", "cc" or "bcc" to send an email.
recipients = {"fail": "[email protected]"}
self.assertRaises(NotifyError, self.emailer.format, recipients, u"This is the subject!", u"This is the body!")
def test_recipient_dict_format(self):
# Test that recipients must be a dict.
recipients = "fail"
self.assertRaises(NotifyError, self.emailer.format, recipients, u"This is the subject!", u"This is the body!")
def test_basic_format(self):
# Test the basic case of formatting the email correctly.
recipients = {"to": "[email protected]"}
recipient_addrs, message = self.emailer.format(recipients, u"This is the subject!", u"This is the body!")
self.assertEqual(recipient_addrs, ['[email protected]'])
expected_msg = u"From: [email protected]\r\nTo: [email protected]\r\nSubject: This is the subject!\r\n\r\nThis is the body!"
self.assertEqual(message, expected_msg)
def test_advanced_format(self):
# Make sure we can handle all of the input formats we accept.
recipients = {
"to": "[email protected]",
"cc": ["[email protected]", "[email protected]"],
}
recipient_addrs, message = self.emailer.format(recipients, u"This is the subject!", u"This is the body!")
expected_recipient_addrs = ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']
self.assertEqual(recipient_addrs, expected_recipient_addrs)
expected_msg = u"From: [email protected]\r\nCc: [email protected], [email protected]\r\nTo: [email protected]\r\nSubject: This is the subject!\r\n\r\nThis is the body!"
self.assertEqual(message, expected_msg)
class FeedTests(unittest.TestCase):
def test_raw_next_try_minutes(self):
# Test that the feed correctly recognises a next_try value of 10m as being valid
f = Feed('name', 'endpoint', 'username', 'password', '10m', False, {})
self.assertEqual(f.next_try, datetime.timedelta(minutes = 10))
def test_raw_next_try_hours(self):
# Test that the feed correctly recognises a next_try value of 10h as being valid
f = Feed('name', 'endpoint', 'username', 'password', '10h', False, {})
self.assertEqual(f.next_try, datetime.timedelta(hours = 10))
def test_raw_next_try_days(self):
# Test that the feed correctly recognises a next_try value of 10d as being valid
f = Feed('name', 'endpoint', 'username', 'password', '10d', False, {})
self.assertEqual(f.next_try, datetime.timedelta(days = 10))
def test_raw_next_try_invalid_duration(self):
# Test that the feed correctly recognises a next_try value of 1.2m as being invalid
self.assertRaises(ValueError, Feed, 'name', 'endpoint', 'username', 'password', '1.2m', False, {})
def test_raw_next_try_invalid_period(self):
# Test that the feed correctly recognises a next_try value of 10s as being invalid
self.assertRaises(ValueError, Feed, 'name', 'endpoint', 'username', 'password', '10s', False, {})
class JsonSettingsTests(unittest.TestCase):
test_settings_file_path = 'test_settings.json'
invalid_settings_file_path = 'invalid_settings.json'
def setUp(self):
with open(self.test_settings_file_path, 'w') as test_settings_file:
json.dump(
{
"feeds": [
{
"name": "flmv",
"endpoint": "http://flm.foxpico.com/FLM/",
"username": "isdcf",
"password": "isdcf",
"next_try": "1m",
"ignore_warnings": False,
"failure_email": {
"to": ["[email protected]"]
}
}
],
"validator": {
"endpoint": "http://flm.foxpico.com/validator",
"username": "isdcf",
"password": "isdcf"
},
"email": {
"host": "<SMTP host>",
"port": 25,
"ssl": {
"enabled": False,
"key": "<path to key file>",
"cert": "<path to cert file>"
},
"sender": "[email protected]"
}
},
test_settings_file,
indent=4,
separators=(',', ': '),
sort_keys=True
)
with open(self.invalid_settings_file_path, 'w') as invalid_settings_file:
# This json is missing an endpoint for the validator
json.dump(
{
"feeds": [
{
"name": "flmv",
"endpoint": "http://flm.foxpico.com/FLM/",
"username": "isdcf",
"password": "isdcf",
"next_try": "1m",
"failure_email": {
"to": ["[email protected]"],
}
}
],
"validator": {
"username": "isdcf",
"password": "isdcf"
},
"email": {
"host": "<SMTP host>",
"port": 25,
"ssl": {
"enabled": False,
"key": "<path to key file>",
"cert": "<path to cert file>"
},
"sender": "[email protected]"
}
},
invalid_settings_file,
indent=4,
separators=(',', ': '),
sort_keys=True
)
def tearDown(self):
os.remove(self.test_settings_file_path)
os.remove(self.invalid_settings_file_path)
def test_settings_file_not_present(self):
# Test that an exception is thrown when attempting to load a file that isn't present
self.assertRaises(IOError, JsonSettings, "this/file/is/not/here.json")
def test_settings_file_present_and_valid(self):
# Test that a valid json file can be loaded
JsonSettings(self.test_settings_file_path)
def test_settings_file_present_and_not_valid(self):
# Test that a invalid json file cannot be loaded
self.assertRaises(jsonschema.ValidationError, JsonSettings, self.invalid_settings_file_path)
class ValidatorTests(unittest.TestCase):
populated_json_errors_and_warnings = {
"test-duration": 2219,
"test-time": 0,
"total-issue-count": 12,
"url": "https://dc.artsalliancemedia.com/fort_nocs/flm",
"validation-results": {
"errors": [
"An error line item",
"An error line item",
"An error line item",
"An error line item",
"An error line item",
"An error line item",
"An error line item",
"An error line item",
],
"warnings": [
"A warning line item",
"A warning line item",
"A warning line item",
"A warning line item",
]
},
"validation-type": "all-data"
}
populated_json_warnings = {
"test-duration": 2219,
"test-time": 0,
"total-issue-count": 4,
"url": "https://dc.artsalliancemedia.com/fort_nocs/flm",
"validation-results": {
"warnings": [
"A warning line item",
"A warning line item",
"A warning line item",
"A warning line item",
]
},
"validation-type": "all-data"
}
populated_json_errors = {
"test-duration": 2219,
"test-time": 0,
"total-issue-count": 8,
"url": "https://dc.artsalliancemedia.com/fort_nocs/flm",
"validation-results": {
"errors": [
"An error line item",
"An error line item",
"An error line item",
"An error line item",
"An error line item",
"An error line item",
"An error line item",
"An error line item",
]
},
"validation-type": "all-data"
}
success_json = {
"test-time" : 0,
"validation-results" : {
"warnings" : [],
"errors" : []
},
"total-issue-count" : 0,
"validation-type" : "all-data",
"url" : "http://redacted/FLM/",
"test-duration" : 7
}
failure_json = {
"test-time" : 0,
"validation-results" : {
"warnings" : [
"a warning",
"another warning"
],
"errors" : [
"an error"
]
},
"total-issue-count" : 3,
"validation-type" : "all-data",
"url" : "http://redacted/FLM/",
"test-duration" : 7
}
invalid_json = {
"test-time" : 0,
"validation-results" : {
"warnings" : [
"a warning",
"another warning"
],
"errors" : [
"an error"
]
},
"validation-type" : "all-data",
"url" : "http://redacted/FLM/",
"test-duration" : 7
}
validator = Validator("endpoint", "username", "password")
def test_ignore_warnings(self):
feed = Feed('name', 'endpoint', 'username', 'password', '10m', True, {})
feed.validation_start_time = datetime.datetime.now() - datetime.timedelta(minutes=10)
self.populated_json_errors_and_warnings["test-time"] = int(time.time())
validation_finished, total_issues, response_json = self.validator.handle_results_response(feed, json.dumps(self.populated_json_errors_and_warnings))
self.assertEqual(total_issues, 8)
feed.validation_start_time = datetime.datetime.now() - datetime.timedelta(minutes=10)
self.populated_json_errors["test-time"] = int(time.time())
validation_finished, total_issues, response_json = self.validator.handle_results_response(feed, json.dumps(self.populated_json_errors))
self.assertEqual(total_issues, 8)
feed.validation_start_time = datetime.datetime.now() - datetime.timedelta(minutes=10)
self.populated_json_warnings["test-time"] = int(time.time())
validation_finished, total_issues, response_json = self.validator.handle_results_response(feed, json.dumps(self.populated_json_warnings))
self.assertEqual(total_issues, 0)
def test_include_warnings(self):
feed = Feed('name', 'endpoint', 'username', 'password', '10m', False, {})
feed.validation_start_time = datetime.datetime.now() - datetime.timedelta(minutes=10)
self.populated_json_errors_and_warnings["test-time"] = int(time.time())
validation_finished, total_issues, response_json = self.validator.handle_results_response(feed, json.dumps(self.populated_json_errors_and_warnings))
self.assertEqual(total_issues, 12)
feed.validation_start_time = datetime.datetime.now() - datetime.timedelta(minutes=10)
self.populated_json_errors["test-time"] = int(time.time())
validation_finished, total_issues, response_json = self.validator.handle_results_response(feed, json.dumps(self.populated_json_errors))
self.assertEqual(total_issues, 8)
feed.validation_start_time = datetime.datetime.now() - datetime.timedelta(minutes=10)
self.populated_json_warnings["test-time"] = int(time.time())
validation_finished, total_issues, response_json = self.validator.handle_results_response(feed, json.dumps(self.populated_json_warnings))
self.assertEqual(total_issues, 4)
def test_process_not_finished(self):
feed = Feed('name', 'endpoint', 'username', 'password', '10m', False, {})
self.success_json["test-time"] = int(time.time()) - 5000
feed.validation_start_time = datetime.datetime.now()
validation_finished, total_issues, response_json = self.validator.handle_results_response(feed, json.dumps(self.success_json))
self.assertEqual(validation_finished, False)
def test_process_success_response(self):
feed = Feed('name', 'endpoint', 'username', 'password', '10m', False, {})
feed.validation_start_time = datetime.datetime.now() - datetime.timedelta(minutes=10)
self.success_json["test-time"] = int(time.time())
validation_finished, total_issues, response_json = self.validator.handle_results_response(feed, json.dumps(self.success_json))
self.assertEqual(validation_finished, True)
self.assertEqual(total_issues, 0)
def test_process_failure_response(self):
feed = Feed('name', 'endpoint', 'username', 'password', '10m', False, {})
feed.validation_start_time = datetime.datetime.now() - datetime.timedelta(minutes=10)
self.failure_json["test-time"] = int(time.time())
validation_finished, total_issues, response_json = self.validator.handle_results_response(feed, json.dumps(self.failure_json))
self.assertEqual(validation_finished, True)
self.assertEqual(total_issues, 3)
def test_process_invalid_response(self):
feed = Feed('name', 'endpoint', 'username', 'password', '10m', False, {})
feed.validation_start_time = datetime.datetime.now() - datetime.timedelta(minutes=10)
self.invalid_json["test-time"] = int(time.time())
self.assertRaises(jsonschema.ValidationError, self.validator.handle_results_response, feed, json.dumps(self.invalid_json))
if __name__ == '__main__':
unittest.main()