-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathtest_request_verification.py
More file actions
88 lines (74 loc) · 3.42 KB
/
Copy pathtest_request_verification.py
File metadata and controls
88 lines (74 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from time import time
import pytest
from slack_sdk.signature import SignatureVerifier
from slack_bolt.middleware.request_verification.async_request_verification import (
AsyncRequestVerification,
)
from slack_bolt.request.async_request import AsyncBoltRequest
from slack_bolt.response import BoltResponse
async def next():
return BoltResponse(status=200, body="next")
class TestAsyncRequestVerification:
signing_secret = "secret"
signature_verifier = SignatureVerifier(signing_secret)
def generate_signature(self, body: str, timestamp: str):
return self.signature_verifier.generate_signature(
body=body,
timestamp=timestamp,
)
def build_headers(self, timestamp: str, body: str):
return {
"content-type": ["application/x-www-form-urlencoded"],
"x-slack-signature": [self.generate_signature(body, timestamp)],
"x-slack-request-timestamp": [timestamp],
}
@pytest.mark.asyncio
async def test_valid(self):
middleware = AsyncRequestVerification(signing_secret="secret")
timestamp = str(int(time()))
raw_body = "payload={}"
req = AsyncBoltRequest(body=raw_body, headers=self.build_headers(timestamp, raw_body))
resp = BoltResponse(status=404)
resp = await middleware.async_process(req=req, resp=resp, next=next)
assert resp.status == 200
assert resp.body == "next"
@pytest.mark.asyncio
async def test_invalid(self):
middleware = AsyncRequestVerification(signing_secret="secret")
req = AsyncBoltRequest(body="payload={}", headers={})
resp = BoltResponse(status=404)
resp = await middleware.async_process(req=req, resp=resp, next=next)
assert resp.status == 401
assert resp.body == """{"error": "invalid request"}"""
@pytest.mark.asyncio
async def test_ssl_check_param_requires_valid_signature(self):
middleware = AsyncRequestVerification(signing_secret="secret")
req = AsyncBoltRequest(
body="token=random&ssl_check=1",
headers={
"content-type": ["application/x-www-form-urlencoded"],
"x-slack-signature": ["v0=invalid"],
"x-slack-request-timestamp": ["0"],
},
)
resp = BoltResponse(status=404)
resp = await middleware.async_process(req=req, resp=resp, next=next)
assert resp.status == 401
assert resp.body == """{"error": "invalid request"}"""
def test_empty_signing_secret_does_not_raise_on_init(self):
AsyncRequestVerification(signing_secret="")
@pytest.mark.asyncio
async def test_socket_mode_request_skips_verification_without_signing_secret(self):
middleware = AsyncRequestVerification(signing_secret="")
req = AsyncBoltRequest(mode="socket_mode", body="payload={}", headers={})
resp = BoltResponse(status=404, body="default")
resp = await middleware.async_process(req=req, resp=resp, next=next)
assert resp.status == 200
assert resp.body == "next"
@pytest.mark.asyncio
async def test_http_request_with_empty_signing_secret_raises(self):
middleware = AsyncRequestVerification(signing_secret="")
req = AsyncBoltRequest(body="payload={}", headers={})
resp = BoltResponse(status=404)
with pytest.raises(ValueError):
await middleware.async_process(req=req, resp=resp, next=next)