Skip to content

Commit

Permalink
tests: add sendgrid channel test
Browse files Browse the repository at this point in the history
  • Loading branch information
sbgap committed Jun 11, 2024
1 parent 4e11dfe commit fcc1360
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
20 changes: 14 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,21 @@ jobs:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/alerta
run: |
pytest --cov=alerta tests/*.py
- name: Test Twilio with pytest
id: twilio
- name: Test Channel with pytest
id: channel
if: github.event_name == 'pull_request'
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/alerta
TWILIO_SID: ${{ secrets.TWILIO_SID }}
TWILIO_TOKEN: ${{ secrets.TWILIO_TOKEN }}
TWILIO_SENDER: ${{ secrets.TWILIO_SENDER }}
TWILIO_RECEIVER: ${{ secrets.TWILIO_RECEIVER }}
SENDGRID_TOKEN: ${{ secrets.SENDGRID_TOKEN }}
SENDGRID_SENDER: ${{ secrets.SENDGRID_SENDER }}
SENDGRID_RECEIVER: ${{ secrets.SENDGRID_RECEIVER }}
run: |
pytest --cov=alerta tests/test_notification_rule.py::NotificationRuleTestCase::test_twilio_sms_channel
pytest --cov=alerta tests/test_notification_rule.py::NotificationRuleTestCase::test_twilio_sms_channel \
tests/test_notification_rule.py::NotificationRuleTestCase::test_sendgrid_channel
- uses: act10ns/slack@v2
with:
status: ${{ job.status }}
Expand Down Expand Up @@ -110,17 +114,21 @@ jobs:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/alerta
run: |
pytest --cov=alerta tests/*.py
- name: Test Twilio with pytest
id: twilio
- name: Test Channel with pytest
id: channel
if: github.event_name == 'pull_request'
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/alerta
TWILIO_SID: ${{ secrets.TWILIO_SID }}
TWILIO_TOKEN: ${{ secrets.TWILIO_TOKEN }}
TWILIO_SENDER: ${{ secrets.TWILIO_SENDER }}
TWILIO_RECEIVER: ${{ secrets.TWILIO_RECEIVER }}
SENDGRID_TOKEN: ${{ secrets.SENDGRID_TOKEN }}
SENDGRID_SENDER: ${{ secrets.SENDGRID_SENDER }}
SENDGRID_RECEIVER: ${{ secrets.SENDGRID_RECEIVER }}
run: |
pytest --cov=alerta tests/test_notification_rule.py::NotificationRuleTestCase::test_twilio_sms_channel
pytest --cov=alerta tests/test_notification_rule.py::NotificationRuleTestCase::test_twilio_sms_channel \
tests/test_notification_rule.py::NotificationRuleTestCase::test_sendgrid_channel
- uses: act10ns/slack@v2
with:
status: ${{ job.status }}
Expand Down
45 changes: 45 additions & 0 deletions tests/test_notification_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,3 +1102,48 @@ def test_twilio_sms_channel(self):
map(get_history_id, history)
)
self.assertTrue(history[0]['sent'])

def test_sendgrid_channel(self):
try:
sendgrid_config = {
'token': os.environ['SENDGRID_TOKEN'],
'sender': os.environ['SENDGRID_SENDER'],
'receiver': os.environ['SENDGRID_RECEIVER']
}
except KeyError:
self.skipTest('Missing required twilio environment')
notification_rule = {
'environment': 'Production',
'channelId': 'sms',
'service': ['Core'],
'receivers': [sendgrid_config['receiver']],
}

channel = {
'id': 'sms',
'sender': sendgrid_config['sender'],
'type': 'sendgrid',
'apiToken': sendgrid_config['token'],
}

self.create_api_obj('/notificationchannels', channel, self.headers)
data = self.create_api_obj('/notificationrules', notification_rule, self.headers)
notification_rule_id = data['id']

data = self.create_api_obj('/alert', self.prod_alert, self.headers)
alert_id = data['id']
active_notification_rules = self.create_api_obj('/notificationrules/active', data['alert'], self.headers, 200)['notificationRules']
self.assertIn(
notification_rule_id,
map(get_id, active_notification_rules),
)
history = self.get_api_obj('notificationhistory', self.headers)['notificationHistory']

while len(history) == 0:
history = self.get_api_obj('notificationhistory', self.headers)['notificationHistory']

self.assertIn(
{'rule_id': notification_rule_id, 'alert_id': alert_id},
map(get_history_id, history)
)
self.assertTrue(history[0]['sent'])

0 comments on commit fcc1360

Please sign in to comment.