Skip to content

Commit 58de183

Browse files
authored
Expand testing of update endpoints (#217)
1 parent 872a167 commit 58de183

16 files changed

Lines changed: 336 additions & 102 deletions

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
PROJECT=alertaclient
2-
COMPOSE_PROJECT_NAME=sdk
2+
COMPOSE_PROJECT_NAME=client

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ COPY . /app
1212
WORKDIR /app
1313

1414
RUN pip install -r requirements.txt
15-
RUN pip install -r requirements-dev.txt
15+
RUN pip install pytest
1616
RUN pip install .

alertaclient/api.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,22 @@ def get_blackouts(self, query=None):
189189
r = self.http.get('/blackouts', query)
190190
return [Blackout.parse(b) for b in r['blackouts']]
191191

192-
def update_blackout(self, id, blackout):
193-
self.http.put('/blackout/%s' % id, blackout)
192+
def update_blackout(self, id, **kwargs):
193+
data = {
194+
'customer': kwargs.get('customer'),
195+
'environment': kwargs.get('environment'),
196+
'service': kwargs.get('service'),
197+
'resource': kwargs.get('resource'),
198+
'event': kwargs.get('event'),
199+
'group': kwargs.get('group'),
200+
'tags': kwargs.get('tags'),
201+
'startTime': kwargs.get('startTime'),
202+
'endTime': kwargs.get('endTime'),
203+
'text': kwargs.get('text'),
204+
}
205+
206+
r = self.http.put('/blackout/{}'.format(id), data)
207+
return Blackout.parse(r['blackout'])
194208

195209
def delete_blackout(self, id):
196210
return self.http.delete('/blackout/%s' % id)
@@ -211,8 +225,13 @@ def get_customers(self, query=None):
211225
r = self.http.get('/customers', query)
212226
return [Customer.parse(c) for c in r['customers']]
213227

214-
def update_customer(self, id, customer):
215-
self.http.put('/customer/%s' % id, customer)
228+
def update_customer(self, id, **kwargs):
229+
data = {
230+
'match': kwargs.get('match'),
231+
'customer': kwargs.get('customer')
232+
}
233+
r = self.http.put('/customer/{}'.format(id), data)
234+
return Customer.parse(r['customer'])
216235

217236
def delete_customer(self, id):
218237
return self.http.delete('/customer/%s' % id)
@@ -267,7 +286,8 @@ def update_key(self, id, **kwargs):
267286
'expireTime': kwargs.get('expireTime'),
268287
'customer': kwargs.get('customer')
269288
}
270-
return self.http.put('/key/{}'.format(id), data)
289+
r = self.http.put('/key/{}'.format(id), data)
290+
return ApiKey.parse(r['key'])
271291

272292
def delete_key(self, id):
273293
return self.http.delete('/key/%s' % id)
@@ -293,7 +313,8 @@ def update_perm(self, id, **kwargs):
293313
'match': kwargs.get('match'), # role
294314
'scopes': kwargs.get('scopes')
295315
}
296-
return self.http.put('/perm/{}'.format(id), data)
316+
r = self.http.put('/perm/{}'.format(id), data)
317+
return Permission.parse(r['permission'])
297318

298319
def delete_perm(self, id):
299320
return self.http.delete('/perm/%s' % id)
@@ -356,7 +377,8 @@ def update_user(self, id, **kwargs):
356377
'text': kwargs.get('text'),
357378
'email_verified': kwargs.get('email_verified')
358379
}
359-
return self.http.put('/user/{}'.format(id), data)
380+
r = self.http.put('/user/{}'.format(id), data)
381+
return User.parse(r['user'])
360382

361383
def update_me(self, **kwargs):
362384
data = {
@@ -367,7 +389,8 @@ def update_me(self, **kwargs):
367389
'attributes': kwargs.get('attributes', None) or dict(),
368390
'text': kwargs.get('text')
369391
}
370-
return self.http.put('/user/me', data)
392+
r = self.http.put('/user/me', data)
393+
return User.parse(r['user'])
371394

372395
def update_user_attributes(self, id, attributes):
373396
data = {
@@ -435,7 +458,8 @@ def update_group(self, id, **kwargs):
435458
'name': kwargs.get('name'),
436459
'text': kwargs.get('text')
437460
}
438-
return self.http.put('/group/{}'.format(id), data)
461+
r = self.http.put('/group/{}'.format(id), data)
462+
return Group.parse(r['group'])
439463

440464
def add_user_to_group(self, group_id, user_id):
441465
return self.http.put('/group/{}/user/{}'.format(group_id, user_id))

alertaclient/models/enums.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,38 @@ def from_str(action: str, resource: str = None):
5757
return Scope('{}:{}'.format(action, resource))
5858
else:
5959
return Scope(action)
60+
61+
62+
ADMIN_SCOPES = [Scope.admin, Scope.read, Scope.write]
63+
64+
65+
class ChangeType(str, Enum):
66+
67+
open = 'open'
68+
assign = 'assign'
69+
ack = 'ack'
70+
unack = 'unack'
71+
shelve = 'shelve'
72+
unshelve = 'unshelve'
73+
close = 'close'
74+
75+
new = 'new'
76+
action = 'action'
77+
status = 'status'
78+
value = 'value'
79+
severity = 'severity'
80+
note = 'note'
81+
timeout = 'timeout'
82+
expired = 'expired'
83+
84+
85+
class NoteType(str, Enum):
86+
87+
alert = 'alert'
88+
blackout = 'blackout'
89+
customer = 'customer'
90+
group = 'group'
91+
heartbeat = 'heartbeat'
92+
key = 'api-key'
93+
perm = 'permission'
94+
user = 'user'

docker-compose.ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ services:
3737
build: .
3838
depends_on:
3939
- api
40-
command: ["./wait-for-it.sh", "api:8080", "-t", "60", "--", "pytest", "tests/integration/test_sdk.py"]
40+
command: ["./wait-for-it.sh", "api:8080", "-t", "60", "--", "pytest", "tests/integration/"]
4141
networks:
4242
net:
4343
aliases:

tests/integration/test_alerts.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import unittest
2+
3+
from alertaclient.api import Client
4+
5+
6+
class AlertTestCase(unittest.TestCase):
7+
8+
def setUp(self):
9+
self.client = Client(endpoint='http://api:8080', key='demo-key')
10+
11+
def test_alert(self):
12+
id, alert, message = self.client.send_alert(
13+
environment='Production', resource='web01', event='node_down', correlated=['node_up', 'node_down'],
14+
service=['Web', 'App'], severity='critical', tags=['london', 'linux'], value=4
15+
)
16+
self.assertEqual(alert.value, '4') # values cast to string
17+
self.assertEqual(alert.timeout, 86400) # timeout returned as int
18+
self.assertIn('london', alert.tags)
19+
20+
def test_alert_notes(self):
21+
alert_id, alert, message = self.client.send_alert(
22+
environment='Production', resource='web02', event='node_down', correlated=['node_up', 'node_down'],
23+
service=['Web', 'App'], severity='critical', tags=['london', 'linux'], value=4
24+
)
25+
note = self.client.alert_note(alert_id, text='this is a test note')
26+
self.assertEqual(note.text, 'this is a test note')
27+
28+
notes = self.client.get_alert_notes(alert_id)
29+
self.assertEqual(notes[0].text, 'this is a test note')
30+
self.assertEqual(notes[0].user, 'admin@alerta.io')
31+
32+
note = self.client.update_alert_note(alert_id, notes[0].id, text='updated note text')
33+
self.assertEqual(note.text, 'updated note text')
34+
35+
self.client.delete_alert_note(alert_id, notes[0].id)
36+
37+
notes = self.client.get_alert_notes(alert_id)
38+
self.assertEqual(notes, [])
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import unittest
2+
3+
from alertaclient.api import Client
4+
5+
6+
class AlertTestCase(unittest.TestCase):
7+
8+
def setUp(self):
9+
self.client = Client(endpoint='http://api:8080', key='demo-key')
10+
11+
def test_blackout(self):
12+
blackout = self.client.create_blackout(
13+
environment='Production', service=['Web', 'App'], resource='web01', event='node_down', group='Network', tags=['london', 'linux']
14+
)
15+
blackout_id = blackout.id
16+
17+
self.assertEqual(blackout.environment, 'Production')
18+
self.assertEqual(blackout.service, ['Web', 'App'])
19+
self.assertIn('london', blackout.tags)
20+
self.assertIn('linux', blackout.tags)
21+
22+
blackout = self.client.update_blackout(blackout_id, environment='Development', group='Network', text='updated blackout')
23+
self.assertEqual(blackout.environment, 'Development')
24+
self.assertEqual(blackout.group, 'Network')
25+
self.assertEqual(blackout.text, 'updated blackout')
26+
27+
blackout = self.client.create_blackout(
28+
environment='Production', service=['Core'], group='Network'
29+
)
30+
31+
blackouts = self.client.get_blackouts()
32+
self.assertEqual(len(blackouts), 2)
33+
34+
self.client.delete_blackout(blackout_id)
35+
36+
blackouts = self.client.get_blackouts()
37+
self.assertEqual(len(blackouts), 1)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import unittest
2+
3+
from alertaclient.api import Client
4+
5+
6+
class AlertTestCase(unittest.TestCase):
7+
8+
def setUp(self):
9+
self.client = Client(endpoint='http://api:8080', key='demo-key')
10+
11+
def test_customer(self):
12+
customer = self.client.create_customer(customer='ACME Corp.', match='example.com')
13+
14+
customer_id = customer.id
15+
16+
self.assertEqual(customer.customer, 'ACME Corp.')
17+
self.assertEqual(customer.match, 'example.com')
18+
19+
customer = self.client.update_customer(customer_id, customer='Foo Corp.', match='foo.com')
20+
21+
self.assertEqual(customer.customer, 'Foo Corp.')
22+
self.assertEqual(customer.match, 'foo.com')
23+
24+
customer = self.client.create_customer(customer='Quetzal Inc.', match='quetzal.io')
25+
26+
customers = self.client.get_customers()
27+
self.assertEqual(len(customers), 2)
28+
29+
self.client.delete_customer(customer_id)
30+
31+
customers = self.client.get_customers()
32+
self.assertEqual(len(customers), 1)

tests/integration/test_groups.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import unittest
2+
3+
from alertaclient.api import Client
4+
5+
6+
class AlertTestCase(unittest.TestCase):
7+
8+
def setUp(self):
9+
self.client = Client(endpoint='http://api:8080', key='demo-key')
10+
11+
def test_group(self):
12+
group = self.client.create_group(name='myGroup', text='test group')
13+
14+
group_id = group.id
15+
16+
self.assertEqual(group.name, 'myGroup')
17+
self.assertEqual(group.text, 'test group')
18+
19+
group = self.client.update_group(group_id, name='newGroup', text='updated group text')
20+
self.assertEqual(group.name, 'newGroup')
21+
self.assertEqual(group.text, 'updated group text')
22+
23+
group = self.client.create_group(name='myGroup2', text='test group2')
24+
25+
groups = self.client.get_users_groups()
26+
self.assertEqual(len(groups), 2, groups)
27+
28+
self.client.delete_group(group_id)
29+
30+
groups = self.client.get_users_groups()
31+
self.assertEqual(len(groups), 1)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import unittest
2+
3+
from alertaclient.api import Client
4+
5+
6+
class AlertTestCase(unittest.TestCase):
7+
8+
def setUp(self):
9+
self.client = Client(endpoint='http://api:8080', key='demo-key')
10+
11+
def test_heartbeat(self):
12+
hb = self.client.heartbeat(origin='app/web01', timeout=10, tags=['london', 'linux'])
13+
self.assertEqual(hb.origin, 'app/web01')
14+
self.assertEqual(hb.event_type, 'Heartbeat')
15+
self.assertEqual(hb.timeout, 10)
16+
self.assertIn('linux', hb.tags)

0 commit comments

Comments
 (0)