|
1 | 1 | import time |
2 | 2 | import pytest |
3 | 3 |
|
| 4 | +from slackeventsapi.server import SlackEventAdapterException |
| 5 | + |
4 | 6 | def test_event_emission(adapter): |
5 | 7 | test_event_emission.event_handled = False |
6 | 8 |
|
@@ -29,3 +31,62 @@ def event_handler(event_data): |
29 | 31 | assert res.status_code == 200 |
30 | 32 |
|
31 | 33 | assert test_event_emission.event_handled |
| 34 | + |
| 35 | +def test_error_timestamp(adapter): |
| 36 | + test_error_timestamp.event_handled = False |
| 37 | + |
| 38 | + # Error should trigger an event |
| 39 | + @adapter.on('error') |
| 40 | + def event_handler(exception): |
| 41 | + test_error_timestamp.event_handled = True |
| 42 | + assert isinstance(exception, SlackEventAdapterException) |
| 43 | + assert str(exception) == 'Invalid request timestamp' |
| 44 | + |
| 45 | + data = pytest.reaction_event_fixture |
| 46 | + |
| 47 | + # Set timestamp to Thu Jan 01 00:00:00 1970 UTC (Epoch 0) |
| 48 | + timestamp = 0 |
| 49 | + |
| 50 | + signature = pytest.create_signature(adapter.signing_secret, timestamp, data) |
| 51 | + |
| 52 | + with adapter.server.test_client() as client: |
| 53 | + res = client.post( |
| 54 | + '/slack/events', |
| 55 | + data=data, |
| 56 | + content_type='application/json', |
| 57 | + headers={ |
| 58 | + 'X-Slack-Request-Timestamp': timestamp, |
| 59 | + 'X-Slack-Signature': signature |
| 60 | + } |
| 61 | + ) |
| 62 | + assert res.status_code == 403 |
| 63 | + |
| 64 | + assert test_error_timestamp.event_handled |
| 65 | + |
| 66 | +def test_error_signature(adapter): |
| 67 | + test_error_signature.event_handled = False |
| 68 | + |
| 69 | + # Error should trigger an event |
| 70 | + @adapter.on('error') |
| 71 | + def event_handler(exception): |
| 72 | + test_error_signature.event_handled = True |
| 73 | + assert isinstance(exception, SlackEventAdapterException) |
| 74 | + assert str(exception) == 'Invalid request signature' |
| 75 | + |
| 76 | + data = pytest.reaction_event_fixture |
| 77 | + timestamp = int(time.time()) |
| 78 | + signature = pytest.create_signature('INVALID SIGNATURE', timestamp, data) |
| 79 | + |
| 80 | + with adapter.server.test_client() as client: |
| 81 | + res = client.post( |
| 82 | + '/slack/events', |
| 83 | + data=data, |
| 84 | + content_type='application/json', |
| 85 | + headers={ |
| 86 | + 'X-Slack-Request-Timestamp': timestamp, |
| 87 | + 'X-Slack-Signature': signature |
| 88 | + } |
| 89 | + ) |
| 90 | + assert res.status_code == 403 |
| 91 | + |
| 92 | + assert test_error_signature.event_handled |
0 commit comments