async def incoming_event(request): slack = request.app.plugins["slack"] payload = await request.json() LOG.log(5, "Incoming event payload: %s", payload) if payload.get("type") == "url_verification": if slack.signing_secret: try: raw_payload = await request.read() validate_request_signature(raw_payload.decode("utf-8"), request.headers, slack.signing_secret) return Response(body=payload["challenge"]) except (InvalidSlackSignature, InvalidTimestamp): return Response(status=500) elif payload["token"] == slack.verify: return Response(body=payload["challenge"]) else: return Response(status=500) try: verification_token = await _validate_request(request, slack) event = Event.from_http(payload, verification_token=verification_token) except (FailedVerification, InvalidSlackSignature, InvalidTimestamp): return Response(status=401) if event["type"] == "message": return await _incoming_message(event, request) else: futures = list(_dispatch(slack.routers["event"], event, request.app)) if futures: return await _wait_and_check_result(futures) return Response(status=200)
def test_no_dispatch(self, message_router, message): def handler(): pass msg = Event.from_http(message) message_router.register("xxx", handler) for h in message_router.dispatch(msg): assert False
def test_no_dispatch(self, event, event_router): def handler(): pass ev = Event.from_http(event) event_router.register("xxx", handler) for h in event_router.dispatch(ev): assert False
def test_response_thread_default(self, message): msg = Event.from_http(message) rep = msg.response() if "thread_ts" in msg or "thread_ts" in msg.get("message", {}): assert rep == { "channel": "C00000A00", "thread_ts": "123456789.000001" } else: assert rep == {"channel": "C00000A00"}
def test_prepare_request_body_message_force_json(self, token, message): msg = Event.from_http(message) _, body, headers = sansio.prepare_request(methods.AUTH_REVOKE, msg, {}, {}, token, as_json=True) assert isinstance(body, str) assert "Authorization" in headers assert "Content-type" in headers assert headers["Content-type"] == "application/json; charset=utf-8"
def test_prepare_request_message_hook(self, token, message): msg = Event.from_http(message) _, body, headers = sansio.prepare_request( "https://hooks.slack.com/abcdefg", msg, {}, {}, token) assert isinstance(body, str) data = json.loads(body) assert isinstance(data.get("attachments", []), list) assert "Authorization" in headers assert "Content-type" in headers assert headers["Content-type"] == "application/json; charset=utf-8"
def test_dispatch_subtype(self, message_router, message): def handler(): pass msg = Event.from_http(message) message_router.register(".*", handler, subtype="channel_topic") handlers = list() for h in message_router.dispatch(msg): handlers.append(h) assert len(handlers) == 1 assert handlers[0] is handler
def test_dispatch_channel(self, message_router, message): def handler(): pass msg = Event.from_http(message) message_router.register("hello", handler, channel="C00000A00") handlers = list() for h in message_router.dispatch(msg): handlers.append(h) assert len(handlers) == 1 assert handlers[0] is handler
def test_dispatch_details(self, event, event_router): def handler(): pass ev = Event.from_http(event) handlers = list() event_router.register("channel_deleted", handler, channel="C00000A00") event_router.register("pin_added", handler, channel="C00000A00") event_router.register("reaction_added", handler, reaction="sirbot") event_router.register("message", handler, text=None) for h in event_router.dispatch(ev): handlers.append(h) assert len(handlers) == 1 assert handlers[0] is handler
def test_dispatch(self, event, event_router): def handler(): pass ev = Event.from_http(event) handlers = list() event_router.register("channel_deleted", handler) event_router.register("pin_added", handler) event_router.register("reaction_added", handler) event_router.register("message", handler) for h in event_router.dispatch(ev): handlers.append(h) assert len(handlers) == 1 assert handlers[0] is handler
def test_prepare_request_body_message(self, token, message): msg = Event.from_http(message) _, body, headers = sansio.prepare_request(methods.AUTH_TEST, msg, {}, {}, token) assert isinstance(body, str) assert "Authorization" in headers assert "Content-type" in headers assert headers["Content-type"] == "application/json; charset=utf-8" _, body, headers = sansio.prepare_request(methods.AUTH_REVOKE, msg, {}, {}, token) assert isinstance(body, dict) assert isinstance(body.get("attachments", ""), str) assert body["token"] == token
def test_multiple_dispatch_pattern(self, message_router, message): def handler(): pass def handler_bis(): pass msg = Event.from_http(message) message_router.register("hello", handler) message_router.register("hello", handler_bis) handlers = list() for h in message_router.dispatch(msg): handlers.append(h) assert len(handlers) == 2 assert handlers[0] is handler assert handlers[1] is handler_bis
async def incoming_event(request): slack = request.app.plugins["slack"] payload = await request.json() LOG.log(5, "Incoming event payload: %s", payload) if "challenge" in payload: if payload["token"] == slack.verify: return Response(body=payload["challenge"]) else: return Response(status=500) try: event = Event.from_http(payload, verification_token=slack.verify) except FailedVerification: return Response(status=401) if event["type"] == "message": return await _incoming_message(event, request) else: futures = list(_dispatch(slack.routers["event"], event, request.app)) if futures: return await _wait_and_check_result(futures) return Response(status=200)
async def incoming_event(request): slack = request.app.plugins['slack'] payload = await request.json() LOG.log(5, 'Incoming event payload: %s', payload) if 'challenge' in payload: if payload['token'] == slack.verify: return Response(body=payload['challenge']) else: return Response(status=500) try: event = Event.from_http(payload, verification_token=slack.verify) except FailedVerification: return Response(status=401) if event['type'] == 'message': return await _incoming_message(event, request) else: futures = list(_dispatch(slack.routers['event'], event, request.app)) if futures: return await _wait_and_check_result(futures) return Response(status=200)
def test_no_need_reconnect(self, event): ev = Event.from_http(event) assert sansio.need_reconnect(ev) is False
def test_not_discard_event(self, event): ev = Event.from_http(event) assert sansio.discard_event(ev, "B0AAA0A01") is False
def test_mapping_set(self, event): ev = Event.from_http(event) assert ev["user"] == "U000AA000" ev["user"] = "******" assert ev["user"] == "foo"
def test_modify_clone(self, event): ev = Event.from_http(event) clone = ev.clone() clone["text"] = "aaaaa" assert clone != ev
def test_response(self, message): msg = Event.from_http(message) rep = msg.response() assert isinstance(rep, slack.events.Message) assert rep["channel"] == "C00000A00"
def message(request): return Event.from_http(raw_event(request))
def event(request): return Event.from_http(raw_event(request))
def test_response_not_in_thread(self, message): msg = Event.from_http(message) rep = msg.response(in_thread=False) assert rep == {"channel": "C00000A00"}
def test_mapping_delete(self, event): ev = Event.from_http(event) assert ev["user"] == "U000AA000" del ev["user"] with pytest.raises(KeyError): print(ev["user"])
def test_mapping_access(self, event): ev = Event.from_http(event) assert ev["user"] == "U000AA000"
def test_response_in_thread(self, message): msg = Event.from_http(message) rep = msg.response(in_thread=True) assert rep == {"channel": "C00000A00", "thread_ts": "123456789.000001"}
def test_clone_event(self, event): ev = Event.from_http(event) clone = ev.clone() assert clone == ev