Skip to content

Commit 804e3b2

Browse files
committed
Add tests for error emitters
1 parent 16961ab commit 804e3b2

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

tests/test_events.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import time
22
import pytest
33

4+
from slackeventsapi.server import SlackEventAdapterException
5+
46
def test_event_emission(adapter):
57
test_event_emission.event_handled = False
68

@@ -29,3 +31,62 @@ def event_handler(event_data):
2931
assert res.status_code == 200
3032

3133
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

Comments
 (0)