-
-
Notifications
You must be signed in to change notification settings - Fork 37.2k
Switch mailgun webhooks to the new Mailgun webhook api #17919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
balloob
merged 5 commits into
home-assistant:dev
from
rohankapoorcom:new-mailgun-webhook-api
Oct 30, 2018
+252
−26
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
87c0f37
Switch mailgun webhooks to the webhook api
rohankapoorcom 1f31359
Change mailgun strings to indicate application/json is in use
rohankapoorcom e290792
Lint
rohankapoorcom 719a765
Revert Changes to .translations.
rohankapoorcom e75b08c
Don't fail if the API key isn't set
rohankapoorcom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,231 @@ | ||
| """Test the init file of Mailgun.""" | ||
| from unittest.mock import patch | ||
| import hashlib | ||
| import hmac | ||
| from unittest.mock import Mock | ||
|
|
||
| from homeassistant import data_entry_flow | ||
| from homeassistant.components import mailgun | ||
| import pytest | ||
|
|
||
| from homeassistant import data_entry_flow | ||
| from homeassistant.components import mailgun, webhook | ||
| from homeassistant.const import CONF_API_KEY, CONF_DOMAIN | ||
| from homeassistant.core import callback | ||
| from homeassistant.setup import async_setup_component | ||
|
|
||
| API_KEY = 'abc123' | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def http_client(hass, aiohttp_client): | ||
| """Initialize a Home Assistant Server for testing this module.""" | ||
| await async_setup_component(hass, webhook.DOMAIN, {}) | ||
| return await aiohttp_client(hass.http.app) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def webhook_id_with_api_key(hass): | ||
| """Initialize the Mailgun component and get the webhook_id.""" | ||
| await async_setup_component(hass, mailgun.DOMAIN, { | ||
| mailgun.DOMAIN: { | ||
| CONF_API_KEY: API_KEY, | ||
| CONF_DOMAIN: 'example.com' | ||
| }, | ||
| }) | ||
|
|
||
| hass.config.api = Mock(base_url='http://example.com') | ||
| result = await hass.config_entries.flow.async_init('mailgun', context={ | ||
| 'source': 'user' | ||
| }) | ||
| assert result['type'] == data_entry_flow.RESULT_TYPE_FORM, result | ||
|
|
||
| result = await hass.config_entries.flow.async_configure( | ||
| result['flow_id'], {}) | ||
| assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY | ||
|
|
||
| return result['result'].data['webhook_id'] | ||
|
|
||
| async def test_config_flow_registers_webhook(hass, aiohttp_client): | ||
| """Test setting up Mailgun and sending webhook.""" | ||
| with patch('homeassistant.util.get_local_ip', return_value='example.com'): | ||
| result = await hass.config_entries.flow.async_init('mailgun', context={ | ||
| 'source': 'user' | ||
| }) | ||
|
|
||
| @pytest.fixture | ||
| async def webhook_id_without_api_key(hass): | ||
| """Initialize the Mailgun component and get the webhook_id w/o API key.""" | ||
| await async_setup_component(hass, mailgun.DOMAIN, {}) | ||
|
|
||
| hass.config.api = Mock(base_url='http://example.com') | ||
| result = await hass.config_entries.flow.async_init('mailgun', context={ | ||
| 'source': 'user' | ||
| }) | ||
| assert result['type'] == data_entry_flow.RESULT_TYPE_FORM, result | ||
|
|
||
| result = await hass.config_entries.flow.async_configure( | ||
| result['flow_id'], {}) | ||
| assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY | ||
| webhook_id = result['result'].data['webhook_id'] | ||
|
|
||
| mailgun_events = [] | ||
| return result['result'].data['webhook_id'] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def mailgun_events(hass): | ||
| """Return a list of mailgun_events triggered.""" | ||
| events = [] | ||
|
|
||
| @callback | ||
| def handle_event(event): | ||
| """Handle Mailgun event.""" | ||
| mailgun_events.append(event) | ||
| events.append(event) | ||
|
|
||
| hass.bus.async_listen(mailgun.MESSAGE_RECEIVED, handle_event) | ||
|
|
||
| client = await aiohttp_client(hass.http.app) | ||
| await client.post('/api/webhook/{}'.format(webhook_id), data={ | ||
| 'hello': 'mailgun' | ||
| }) | ||
| return events | ||
|
|
||
|
|
||
| async def test_mailgun_webhook_with_missing_signature( | ||
| http_client, | ||
| webhook_id_with_api_key, | ||
| mailgun_events | ||
| ): | ||
| """Test that webhook doesn't trigger an event without a signature.""" | ||
| event_count = len(mailgun_events) | ||
|
|
||
| await http_client.post( | ||
| '/api/webhook/{}'.format(webhook_id_with_api_key), | ||
| json={ | ||
| 'hello': 'mailgun', | ||
| 'signature': {} | ||
| } | ||
| ) | ||
|
|
||
| assert len(mailgun_events) == event_count | ||
|
|
||
| await http_client.post( | ||
| '/api/webhook/{}'.format(webhook_id_with_api_key), | ||
| json={ | ||
| 'hello': 'mailgun', | ||
| } | ||
| ) | ||
|
|
||
| assert len(mailgun_events) == event_count | ||
|
|
||
|
|
||
| async def test_mailgun_webhook_with_different_api_key( | ||
| http_client, | ||
| webhook_id_with_api_key, | ||
| mailgun_events | ||
| ): | ||
| """Test that webhook doesn't trigger an event with a wrong signature.""" | ||
| timestamp = '1529006854' | ||
| token = 'a8ce0edb2dd8301dee6c2405235584e45aa91d1e9f979f3de0' | ||
|
|
||
| event_count = len(mailgun_events) | ||
|
|
||
| await http_client.post( | ||
| '/api/webhook/{}'.format(webhook_id_with_api_key), | ||
| json={ | ||
| 'hello': 'mailgun', | ||
| 'signature': { | ||
| 'signature': hmac.new( | ||
| key=b'random_api_key', | ||
| msg=bytes('{}{}'.format(timestamp, token), 'utf-8'), | ||
| digestmod=hashlib.sha256 | ||
| ).hexdigest(), | ||
| 'timestamp': timestamp, | ||
| 'token': token | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| assert len(mailgun_events) == event_count | ||
|
|
||
|
|
||
| async def test_mailgun_webhook_event_with_correct_api_key( | ||
| http_client, | ||
| webhook_id_with_api_key, | ||
| mailgun_events | ||
| ): | ||
| """Test that webhook triggers an event after validating a signature.""" | ||
| timestamp = '1529006854' | ||
| token = 'a8ce0edb2dd8301dee6c2405235584e45aa91d1e9f979f3de0' | ||
|
|
||
| event_count = len(mailgun_events) | ||
|
|
||
| await http_client.post( | ||
| '/api/webhook/{}'.format(webhook_id_with_api_key), | ||
| json={ | ||
| 'hello': 'mailgun', | ||
| 'signature': { | ||
| 'signature': hmac.new( | ||
| key=bytes(API_KEY, 'utf-8'), | ||
| msg=bytes('{}{}'.format(timestamp, token), 'utf-8'), | ||
| digestmod=hashlib.sha256 | ||
| ).hexdigest(), | ||
| 'timestamp': timestamp, | ||
| 'token': token | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| assert len(mailgun_events) == event_count + 1 | ||
| assert mailgun_events[-1].data['webhook_id'] == webhook_id_with_api_key | ||
| assert mailgun_events[-1].data['hello'] == 'mailgun' | ||
|
|
||
|
|
||
| async def test_mailgun_webhook_with_missing_signature_without_api_key( | ||
| http_client, | ||
| webhook_id_without_api_key, | ||
| mailgun_events | ||
| ): | ||
| """Test that webhook triggers an event without a signature w/o API key.""" | ||
| event_count = len(mailgun_events) | ||
|
|
||
| await http_client.post( | ||
| '/api/webhook/{}'.format(webhook_id_without_api_key), | ||
| json={ | ||
| 'hello': 'mailgun', | ||
| 'signature': {} | ||
| } | ||
| ) | ||
|
|
||
| assert len(mailgun_events) == event_count + 1 | ||
| assert mailgun_events[-1].data['webhook_id'] == webhook_id_without_api_key | ||
| assert mailgun_events[-1].data['hello'] == 'mailgun' | ||
|
|
||
| await http_client.post( | ||
| '/api/webhook/{}'.format(webhook_id_without_api_key), | ||
| json={ | ||
| 'hello': 'mailgun', | ||
| } | ||
| ) | ||
|
|
||
| assert len(mailgun_events) == event_count + 1 | ||
| assert mailgun_events[-1].data['webhook_id'] == webhook_id_without_api_key | ||
| assert mailgun_events[-1].data['hello'] == 'mailgun' | ||
|
|
||
|
|
||
| async def test_mailgun_webhook_event_without_an_api_key( | ||
| http_client, | ||
| webhook_id_without_api_key, | ||
| mailgun_events | ||
| ): | ||
| """Test that webhook triggers an event if there is no api key.""" | ||
| timestamp = '1529006854' | ||
| token = 'a8ce0edb2dd8301dee6c2405235584e45aa91d1e9f979f3de0' | ||
|
|
||
| event_count = len(mailgun_events) | ||
|
|
||
| await http_client.post( | ||
| '/api/webhook/{}'.format(webhook_id_without_api_key), | ||
| json={ | ||
| 'hello': 'mailgun', | ||
| 'signature': { | ||
| 'signature': hmac.new( | ||
| key=bytes(API_KEY, 'utf-8'), | ||
| msg=bytes('{}{}'.format(timestamp, token), 'utf-8'), | ||
| digestmod=hashlib.sha256 | ||
| ).hexdigest(), | ||
| 'timestamp': timestamp, | ||
| 'token': token | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| assert len(mailgun_events) == 1 | ||
| assert mailgun_events[0].data['webhook_id'] == webhook_id | ||
| assert mailgun_events[0].data['hello'] == 'mailgun' | ||
| assert len(mailgun_events) == event_count + 1 | ||
| assert mailgun_events[-1].data['webhook_id'] == webhook_id_without_api_key | ||
| assert mailgun_events[-1].data['hello'] == 'mailgun' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.