forked from alerta/alerta
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_customers.py
More file actions
431 lines (352 loc) · 17 KB
/
test_customers.py
File metadata and controls
431 lines (352 loc) · 17 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import json
import unittest
from flask import g
from alerta.app import create_app, db
from alerta.exceptions import ApiError
from alerta.models.enums import Scope
from alerta.models.key import ApiKey
from alerta.utils.api import assign_customer
class CustomersTestCase(unittest.TestCase):
def setUp(self):
test_config = {
'TESTING': True,
'AUTH_REQUIRED': True,
'CUSTOMER_VIEWS': True,
'ADMIN_USERS': ['[email protected]'],
'ALLOWED_EMAIL_DOMAINS': ['alerta.io', 'foo.com', 'bar.com']
}
self.app = create_app(test_config)
self.client = self.app.test_client()
self.foo_alert = {
'event': 'foo1',
'resource': 'foo1',
'environment': 'Production',
'service': ['Web']
}
self.bar_alert = {
'event': 'bar1',
'resource': 'bar1',
'environment': 'Production',
'service': ['Web']
}
with self.app.test_request_context('/'):
self.app.preprocess_request()
self.api_key = ApiKey(
scopes=[Scope.admin, Scope.read, Scope.write],
text='admin-key'
)
self.api_key.create()
self.admin_headers = {
'Authorization': 'Key %s' % self.api_key.key,
'Content-type': 'application/json'
}
def tearDown(self):
db.destroy()
def test_customers(self):
# add customer mappings
payload = {
'customer': 'Bar Corp',
'match': 'bar.com'
}
response = self.client.post('/customer', data=json.dumps(payload),
content_type='application/json', headers=self.admin_headers)
self.assertEqual(response.status_code, 201)
payload = {
'customer': 'Foo Bar Corp',
'match': '[email protected]'
}
response = self.client.post('/customer', data=json.dumps(payload),
content_type='application/json', headers=self.admin_headers)
self.assertEqual(response.status_code, 201)
response = self.client.get('/customers', headers=self.admin_headers)
self.assertEqual(response.status_code, 200)
# create users
payload = {
'name': 'Bar User',
'email': '[email protected]',
'password': 'b8rb8r',
'text': ''
}
response = self.client.post('/auth/signup', data=json.dumps(payload),
content_type='application/json', headers=self.admin_headers)
self.assertEqual(response.status_code, 200, response.data)
data = json.loads(response.data.decode('utf-8'))
self.assertIsNotNone(data, 'Failed to create user')
self.bar_bearer_headers = {
'Authorization': 'Bearer %s' % data['token'],
'Content-type': 'application/json'
}
payload = {
'name': 'Foo Bar User',
'email': '[email protected]',
'password': 'f00b8r',
'text': ''
}
response = self.client.post('/auth/signup', data=json.dumps(payload),
content_type='application/json', headers=self.admin_headers)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertIsNotNone(data, 'Failed to create user')
self.foobar_bearer_headers = {
'Authorization': 'Bearer %s' % data['token'],
'Content-type': 'application/json'
}
# create API key for [email protected]
payload = {
'user': '[email protected]',
'scopes': ['read', 'write'],
'text': ''
}
response = self.client.post('/key', data=json.dumps(payload),
content_type='application/json', headers=self.bar_bearer_headers)
self.assertEqual(response.status_code, 201, response.data)
data = json.loads(response.data.decode('utf-8'))
self.assertIsNotNone(data['key'], 'Failed to create read-write key')
self.bar_api_key_headers = {
'Authorization': 'Key %s' % data['key'],
'Content-type': 'application/json'
}
# create API keys for [email protected]
payload = {
'user': '[email protected]',
'scopes': ['read', 'write'],
'text': '',
'customer': 'Foo Bar Corp'
}
response = self.client.post('/key', data=json.dumps(payload),
content_type='application/json', headers=self.foobar_bearer_headers)
self.assertEqual(response.status_code, 201, response.data)
data = json.loads(response.data.decode('utf-8'))
self.assertIsNotNone(data['key'], 'Failed to create read-write key')
self.foobar_api_key_headers = {
'Authorization': 'Key %s' % data['key'],
'Content-type': 'application/json'
}
payload = {
'user': '[email protected]',
'scopes': ['read', 'write'],
'text': '',
'customer': 'Bar Corp'
}
response = self.client.post('/key', data=json.dumps(payload),
content_type='application/json', headers=self.foobar_bearer_headers)
self.assertEqual(response.status_code, 201, response.data)
data = json.loads(response.data.decode('utf-8'))
self.assertIsNotNone(data['key'], 'Failed to create read-write key')
self.foobar_bar_only_api_key_headers = {
'Authorization': 'Key %s' % data['key'],
'Content-type': 'application/json'
}
# get list of customers for users
response = self.client.get('/customers', headers=self.bar_api_key_headers)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual([c['customer'] for c in data['customers']], ['Bar Corp'])
response = self.client.get('/customers', headers=self.foobar_api_key_headers)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual([c['customer'] for c in data['customers']], ['Foo Bar Corp'])
# create alerts using API keys
response = self.client.post('/alert', data=json.dumps(self.foo_alert), headers=self.bar_api_key_headers)
self.assertEqual(response.status_code, 201)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['alert']['customer'], 'Bar Corp')
response = self.client.post('/alert', data=json.dumps(self.foo_alert), headers=self.foobar_api_key_headers)
self.assertEqual(response.status_code, 201)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['alert']['customer'], 'Foo Bar Corp')
response = self.client.post('/alert', data=json.dumps(self.foo_alert),
headers=self.foobar_bar_only_api_key_headers)
self.assertEqual(response.status_code, 201)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['alert']['customer'], 'Bar Corp')
response = self.client.post('/alert', data=json.dumps(self.foo_alert),
headers=self.foobar_bar_only_api_key_headers)
self.assertEqual(response.status_code, 201)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['alert']['customer'], 'Bar Corp')
# create alerts using Bearer tokens
response = self.client.post('/alert', data=json.dumps(self.foo_alert), headers=self.bar_bearer_headers)
self.assertEqual(response.status_code, 201, response.data)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['alert']['customer'], 'Bar Corp')
self.foo_alert['customer'] = 'Foo Bar Corp'
response = self.client.post('/alert', data=json.dumps(self.foo_alert), headers=self.foobar_bearer_headers)
self.assertEqual(response.status_code, 201, response.data)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['alert']['customer'], 'Foo Bar Corp')
def test_blackouts(self):
# add customer mappings
payload = {
'customer': 'Foo Corp',
'match': 'foo.com'
}
response = self.client.post('/customer', data=json.dumps(payload),
content_type='application/json', headers=self.admin_headers)
self.assertEqual(response.status_code, 201)
payload = {
'customer': 'Bar Corp',
'match': 'bar.com'
}
response = self.client.post('/customer', data=json.dumps(payload),
content_type='application/json', headers=self.admin_headers)
self.assertEqual(response.status_code, 201)
# create users
payload = {
'name': 'Foo User',
'email': '[email protected]',
'password': 'f00f00',
'text': ''
}
response = self.client.post('/auth/signup', data=json.dumps(payload),
content_type='application/json', headers=self.admin_headers)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertIsNotNone(data, 'Failed to create user')
foo_user_headers = {
'Authorization': 'Bearer %s' % data['token'],
'Content-type': 'application/json'
}
payload = {
'name': 'Bar User',
'email': '[email protected]',
'password': 'b8rb8r',
'text': ''
}
response = self.client.post('/auth/signup', data=json.dumps(payload),
content_type='application/json', headers=self.admin_headers)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertIsNotNone(data, 'Failed to create user')
bar_user_headers = {
'Authorization': 'Bearer %s' % data['token'],
'Content-type': 'application/json'
}
# create customer blackout by foo user
response = self.client.post(
'/blackout', data=json.dumps({'environment': 'Production'}), headers=foo_user_headers)
self.assertEqual(response.status_code, 201)
data = json.loads(response.data.decode('utf-8'))
blackout_id = data['id']
# new alert by foo user should be suppressed
response = self.client.post('/alert', data=json.dumps(self.foo_alert), headers=foo_user_headers)
self.assertEqual(response.status_code, 202)
# new alert by bar user should not be suppressed
response = self.client.post('/alert', data=json.dumps(self.bar_alert), headers=bar_user_headers)
self.assertEqual(response.status_code, 201)
# delete blackout by id
response = self.client.delete('/blackout/' + blackout_id, headers=self.admin_headers)
self.assertEqual(response.status_code, 200)
# create global blackout by admin user
response = self.client.post(
'/blackout', data=json.dumps({'environment': 'Production'}), headers=self.admin_headers)
self.assertEqual(response.status_code, 201)
data = json.loads(response.data.decode('utf-8'))
blackout_id = data['id']
# new alert by foo user should be suppressed
response = self.client.post('/alert', data=json.dumps(self.foo_alert), headers=foo_user_headers)
self.assertEqual(response.status_code, 202)
# new alert by bar user should be suppressed
response = self.client.post('/alert', data=json.dumps(self.bar_alert), headers=bar_user_headers)
self.assertEqual(response.status_code, 202)
# delete blackout by id
response = self.client.delete('/blackout/' + blackout_id, headers=self.admin_headers)
self.assertEqual(response.status_code, 200)
def test_assign_customer(self):
with self.app.test_request_context('/'):
self.app.preprocess_request()
# nothing wanted, assign one
g.customers = ['Customer1']
g.scopes = []
self.assertEqual(assign_customer(wanted=None), 'Customer1')
# nothing wanted, but too many, throw error
g.customers = ['Customer1', 'Customer2']
g.scopes = []
with self.assertRaises(ApiError) as e:
assign_customer(wanted=None)
exc = e.exception
self.assertEqual(str(exc), 'must define customer as more than one possibility')
# customer wanted, matches so allow
g.customers = ['Customer1']
g.scopes = []
self.assertEqual(assign_customer(wanted='Customer1'), 'Customer1')
# customer wanted, in list so allow
g.customers = ['Customer1', 'Customer2']
g.scopes = []
self.assertEqual(assign_customer(wanted='Customer2'), 'Customer2')
# customer wanted not in list, throw exception
g.customers = ['Customer1', 'Customer2']
g.scopes = []
with self.assertRaises(ApiError) as e:
assign_customer(wanted='Customer3')
exc = e.exception
self.assertEqual(str(exc), "not allowed to set customer to 'Customer3'")
# no customers, admin scope so allow
g.customers = []
g.scopes = ['admin']
self.assertEqual(assign_customer(wanted=None), None)
self.assertEqual(assign_customer(wanted='Customer1'), 'Customer1')
g.customers = ['Customer1', 'Customer2']
g.scopes = ['admin']
with self.assertRaises(ApiError) as e:
assign_customer(wanted=None)
exc = e.exception
self.assertEqual(str(exc), 'must define customer as more than one possibility')
self.assertEqual(assign_customer(wanted='Customer3'), 'Customer3')
# wrong scope
g.customers = ['Customer1']
g.scopes = ['read:keys', 'write:keys']
with self.assertRaises(ApiError) as e:
assign_customer(wanted='Customer2', permission=Scope.admin_keys)
exc = e.exception
self.assertEqual(str(exc), "not allowed to set customer to 'Customer2'")
# right scope
g.customers = ['Customer1']
g.scopes = ['admin:keys', 'read:keys', 'write:keys']
self.assertEqual(assign_customer(wanted='Customer2', permission=Scope.admin_keys), 'Customer2')
def test_invalid_customer(self):
self.foo_alert['customer'] = ''
response = self.client.post('/alert', data=json.dumps(self.foo_alert), headers=self.admin_headers)
self.assertEqual(response.status_code, 400)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['message'], 'customer must not be an empty string')
def test_edit_customer(self):
# add customer mappings
payload = {
'customer': 'Foo Corp',
'match': 'foo.com'
}
response = self.client.post('/customer', data=json.dumps(payload),
content_type='application/json', headers=self.admin_headers)
self.assertEqual(response.status_code, 201)
data = json.loads(response.data.decode('utf-8'))
customer_id = data['id']
# change customer name
update = {
'customer': 'Bar Corp'
}
response = self.client.put('/customer/' + customer_id, data=json.dumps(update), headers=self.admin_headers)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['status'], 'ok')
# check updates worked and didn't change anything else
response = self.client.get('/customer/' + customer_id, headers=self.admin_headers)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['customer']['customer'], 'Bar Corp')
self.assertEqual(data['customer']['match'], 'foo.com')
# change customer lookup
update = {
'match': 'bar.com'
}
response = self.client.put('/customer/' + customer_id, data=json.dumps(update), headers=self.admin_headers)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['status'], 'ok')
# check updates worked and didn't change anything else
response = self.client.get('/customer/' + customer_id, headers=self.admin_headers)
self.assertEqual(response.status_code, 200)
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['customer']['customer'], 'Bar Corp')
self.assertEqual(data['customer']['match'], 'bar.com')