Skip to content

Commit

Permalink
[staus pages][m]: on_incident and subscribe_user methods + tests - fixes
Browse files Browse the repository at this point in the history
 #1 (#2)

* [staus pages][m]: on_incident and subscribe_user methods + tests - fixes #1

* [code][s]: avoid need on json.dumps

* v0.1.0
  • Loading branch information
zelima committed Nov 2, 2018
1 parent ffa6cfe commit bcbcae2
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 8 deletions.
2 changes: 1 addition & 1 deletion datahub_emails/VERSION
@@ -1 +1 @@
0.0.1
0.1.0
71 changes: 69 additions & 2 deletions datahub_emails/api.py
@@ -1,9 +1,10 @@
import logging
import requests
import json

from .config import mch_user, mch_secret, mch_list_id
from .config import statuspage_id, statuspage_api_key

statuspage_headers={'Authorization': 'OAuth %s' % statuspage_api_key}

def on_new_user(user_info):
errors = []
Expand All @@ -15,7 +16,7 @@ def on_new_user(user_info):
ret = requests.post(
'https://us12.api.mailchimp.com/3.0/lists/{}/members/'.format(mch_list_id),
auth=(mch_user, mch_secret),
data=json.dumps(mch_info))
json=mch_info)
resp = ret.json()
if ret.status_code > 205:
errors.append(resp.get('detail'))
Expand All @@ -26,3 +27,69 @@ def on_new_user(user_info):
logging.error(errors[0])
return False
return True


def on_incident(incident, publisher, errors='', status='identified'):
if isinstance(errors, list):
errors = '\n'.join(errors)
component_id = _get_component_id(publisher)
logging.error(component_id)
if component_id is None:
logging.info('Component with name %s Not Found' % publisher)
return

payload = {
"incident": {
"name": incident,
"body": errors,
"status": status,
"component_ids": [component_id]
}
}
resp = requests.post(
'https://api.statuspage.io/v1/pages/{page_id}/incidents'.format(
page_id=statuspage_id),
headers=statuspage_headers,
json=payload
)
if resp.status_code > 205:
logging.info('Not able to connect to https://api.statuspage.io')
return
return resp.json()


def subscribe_user(username, email):
component_id = _get_component_id(username)
if component_id is None:
logging.info('Component with name %s Not Found' % username)
return

payload = {
"email": email,
"component_ids": [component_id]
}
resp = requests.post(
'https://api.statuspage.io/v1/pages/{page_id}/subscribers'.format(page_id=statuspage_id),
headers=statuspage_headers,
json=payload
)
if resp.status_code > 205:
logging.info('Not able to connect to https://api.statuspage.io')
return
return resp.json()


def _get_component_id(component_name):
resp = requests.get(
'https://api.statuspage.io/v1/pages/{page_id}/components'.format(page_id=statuspage_id),
headers=statuspage_headers
)
if resp.status_code > 205:
logging.info('Not able to connect to https://api.statuspage.io')
return
components = resp.json()
component_id = None
for component in components:
if component.get('name') == component_name:
component_id = component.get('id')
return component_id
3 changes: 3 additions & 0 deletions datahub_emails/config.py
Expand Up @@ -3,3 +3,6 @@
mch_user = os.environ.get('MAILCHIMP_USER')
mch_secret = os.environ.get('MAILCHIMP_SECRET')
mch_list_id = os.environ.get('MAILCHIMP_LIST_ID', '97878f666d')

statuspage_id = os.environ.get('STATUSPAGE_ID')
statuspage_api_key = os.environ.get('STATUSPAGE_API_KEY')
1 change: 1 addition & 0 deletions requirements.txt.dev
@@ -0,0 +1 @@
requests-mock
35 changes: 32 additions & 3 deletions tests/test_api.py
@@ -1,5 +1,34 @@
import os
import unittest
import requests_mock

class TestApi(unittest.TestCase):
def test_work(self):
assert 1 == 1
from datahub_emails import api


class TestStatusPage(unittest.TestCase):

@requests_mock.mock()
def test_on_incident_returns_none_if_comonent_not_exists(self, m):
m.get('https://api.statuspage.io/v1/pages/test/components', json={})
res = api.on_incident('Test Incident', 'testing')
self.assertIsNone(res)

@requests_mock.mock()
def test_on_incident_works(self, m):
m.get('https://api.statuspage.io/v1/pages/test/components', json=[{'name': 'test', 'id': 'test'}])
m.post('https://api.statuspage.io/v1/pages/test/incidents', json={'success': True})
res = api.on_incident('Test Incident', 'test', 'errors')
self.assertDictEqual(res, {'success': True})

@requests_mock.mock()
def test_subscribe_user_returns_none_if_comonent_not_exists(self, m):
m.get('https://api.statuspage.io/v1/pages/test/components', json={})
res = api.subscribe_user('test', 'testing@mail.com')
self.assertIsNone(res)

@requests_mock.mock()
def test_subscribe_user_works(self, m):
m.get('https://api.statuspage.io/v1/pages/test/components', json=[{'name': 'test', 'id': 'test'}])
m.post('https://api.statuspage.io/v1/pages/test/subscribers', json={'success': True})
res = api.subscribe_user('test', 'testing@mail.com')
self.assertDictEqual(res, {'success': True})
7 changes: 5 additions & 2 deletions tox.ini
@@ -1,5 +1,5 @@
[tox]
package=emails
package=datahub_emails
skip_missing_interpreters=true
envlist=
py36
Expand All @@ -11,13 +11,16 @@ deps=
pytest-cov
coveralls
coverage
requests-mock
passenv=
CI
TRAVIS
TRAVIS_JOB_ID
TRAVIS_BRANCH
commands=
python -m pytest tests -sv --cov=emails
python -m pytest tests -sv --cov=datahub_emails
setenv =
MAILCHIMP_USER=test
MAILCHIMP_PASS=test
STATUSPAGE_ID=test
STATUSPAGE_API_KEY=test-key

0 comments on commit bcbcae2

Please sign in to comment.