|
| 1 | +import base64 |
1 | 2 | import json |
2 | 3 | from time import time |
3 | 4 | from urllib.parse import quote |
@@ -70,6 +71,55 @@ def test_not_found(self): |
70 | 71 | response = not_found() |
71 | 72 | assert response["statusCode"] == 404 |
72 | 73 |
|
| 74 | + def test_null_fields_in_api_gateway_event(self): |
| 75 | + app = App(client=self.web_client, signing_secret=self.signing_secret) |
| 76 | + event = { |
| 77 | + "httpMethod": "POST", |
| 78 | + "requestContext": {"httpMethod": "POST"}, |
| 79 | + "headers": None, |
| 80 | + "multiValueHeaders": None, |
| 81 | + "queryStringParameters": None, |
| 82 | + "body": "{}", |
| 83 | + "isBase64Encoded": False, |
| 84 | + } |
| 85 | + response = SlackRequestHandler(app).handle(event, self.context) |
| 86 | + assert response["statusCode"] == 401 |
| 87 | + assert event["headers"] is None |
| 88 | + |
| 89 | + def test_missing_is_base64_encoded_and_null_body(self): |
| 90 | + app = App(client=self.web_client, signing_secret=self.signing_secret) |
| 91 | + event = {"requestContext": {"http": {"method": "POST"}}, "headers": {}, "body": "{}"} |
| 92 | + assert SlackRequestHandler(app).handle(event, self.context)["statusCode"] == 401 |
| 93 | + event = {"requestContext": {"http": {"method": "POST"}}, "headers": {}, "body": None, "isBase64Encoded": True} |
| 94 | + assert SlackRequestHandler(app).handle(event, self.context)["statusCode"] == 401 |
| 95 | + |
| 96 | + def test_base64_encoded_body(self): |
| 97 | + app = App(client=self.web_client, signing_secret=self.signing_secret) |
| 98 | + |
| 99 | + def event_handler(): |
| 100 | + pass |
| 101 | + |
| 102 | + app.event("app_mention")(event_handler) |
| 103 | + input = { |
| 104 | + "token": "verification_token", |
| 105 | + "team_id": "T111", |
| 106 | + "api_app_id": "A111", |
| 107 | + "event": {"type": "app_mention", "text": "<@W111> Hi", "user": "W222", "team": "T111", "channel": "C111"}, |
| 108 | + "type": "event_callback", |
| 109 | + "event_id": "Ev111", |
| 110 | + "event_time": 1595926230, |
| 111 | + } |
| 112 | + timestamp, body = str(int(time())), json.dumps(input) |
| 113 | + event = { |
| 114 | + "body": base64.b64encode(body.encode("utf-8")).decode("ascii"), |
| 115 | + "queryStringParameters": None, |
| 116 | + "headers": self.build_headers(timestamp, body), |
| 117 | + "requestContext": {"http": {"method": "POST"}}, |
| 118 | + "isBase64Encoded": True, |
| 119 | + } |
| 120 | + response = SlackRequestHandler(app).handle(event, self.context) |
| 121 | + assert response["statusCode"] == 200 |
| 122 | + |
73 | 123 | def test_first_value(self): |
74 | 124 | assert _first_value({"foo": [1, 2, 3]}, "foo") == 1 |
75 | 125 | assert _first_value({"foo": []}, "foo") is None |
|
0 commit comments