def _write_response(self, bolt_resp: BoltResponse, resp: Response): resp.body = bolt_resp.body status = HTTPStatus(bolt_resp.status) resp.status = str(f"{status.value} {status.phrase}") resp.set_headers(bolt_resp.first_headers_without_set_cookie()) for cookie in bolt_resp.cookies(): for name, c in cookie.items(): expire_value = c.get("expires") expire = (datetime.strptime(expire_value, "%a, %d %b %Y %H:%M:%S %Z") if expire_value else None) resp.set_cookie( name=name, value=c.value, expires=expire, max_age=c.get("max-age"), domain=c.get("domain"), path=c.get("path"), secure=True, http_only=True, )
def test_all_values_from_context(self): req = BoltRequest(body="", headers={}) req.context["foo"] = "FOO" req.context["bar"] = 123 required_args = ["foo", "bar", "ack"] arg_params: dict = build_required_kwargs( logger=logging.getLogger(__name__), required_arg_names=required_args, request=req, response=BoltResponse(status=200), next_func=next, ) assert arg_params["foo"] == "FOO" assert arg_params["bar"] == 123 assert arg_params["ack"] is not None
def test_block_action(self): body = { "type": "block_actions", "actions": [{ "type": "button", "action_id": "valid_action_id", "block_id": "b", "action_ts": "111.222", "value": "v", }], } raw_body = f"payload={quote(json.dumps(body))}" headers = {"Content-Type": "application/x-www-form-urlencoded"} req = BoltRequest(body=raw_body, headers=headers) resp = BoltResponse(status=404) assert block_action("valid_action_id").matches(req, resp) is True assert block_action("invalid_action_id").matches(req, resp) is False assert block_action(re.compile("valid_.+")).matches(req, resp) is True assert block_action(re.compile("invalid_.+")).matches(req, resp) is False assert action("valid_action_id").matches(req, resp) is True assert action("invalid_action_id").matches(req, resp) is False assert action(re.compile("valid_.+")).matches(req, resp) is True assert action(re.compile("invalid_.+")).matches(req, resp) is False assert action({ "action_id": "valid_action_id" }).matches(req, resp) is True assert action({ "action_id": "invalid_action_id" }).matches(req, resp) is False assert action({ "action_id": re.compile("valid_.+") }).matches(req, resp) is True assert (action({ "action_id": re.compile("invalid_.+") }).matches(req, resp) is False) # block_id + action_id assert (action({ "action_id": "valid_action_id", "block_id": "b" }).matches(req, resp) is True) assert (action({ "action_id": "invalid_action_id", "block_id": "b" }).matches(req, resp) is False) assert (action({ "action_id": re.compile("valid_.+"), "block_id": "b" }).matches(req, resp) is True) assert (action({ "action_id": re.compile("invalid_.+"), "block_id": "b" }).matches(req, resp) is False) assert (action({ "action_id": "valid_action_id", "block_id": "bbb" }).matches(req, resp) is False) assert (action({ "action_id": "invalid_action_id", "block_id": "bbb" }).matches(req, resp) is False) assert (action({ "action_id": re.compile("valid_.+"), "block_id": "bbb" }).matches(req, resp) is False) assert (action({ "action_id": re.compile("invalid_.+"), "block_id": "bbb" }).matches(req, resp) is False) # with type assert (action({ "action_id": "valid_action_id", "type": "block_actions" }).matches(req, resp) is True) assert (action({ "callback_id": "valid_action_id", "type": "interactive_message" }).matches(req, resp) is False) assert (action({ "callback_id": "valid_action_id", "type": "workflow_step_edit" }).matches(req, resp) is False)
def failure(args: FailureArgs) -> BoltResponse: return BoltResponse(status=args.suggested_status_code, body=args.reason)
def failure(args: FailureArgs) -> BoltResponse: assert args.request is not None assert args.reason is not None return BoltResponse(status=502, body="customized")
def success(args: SuccessArgs) -> BoltResponse: assert args.request is not None return BoltResponse(status=200, body="customized")
def listener_middleware_returning_response(): return BoltResponse(status=200, body="listener middleware")
def handle_errors(error): assert isinstance(error, BoltUnhandledRequestError) return BoltResponse(status=404, body="TODO")
def this_should_be_skipped(): return BoltResponse(status=500, body="failed")