示例#1
0
 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,
             )
示例#2
0
 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
示例#3
0
    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)
示例#4
0
def failure(args: FailureArgs) -> BoltResponse:
    return BoltResponse(status=args.suggested_status_code, body=args.reason)
示例#5
0
 def failure(args: FailureArgs) -> BoltResponse:
     assert args.request is not None
     assert args.reason is not None
     return BoltResponse(status=502, body="customized")
示例#6
0
 def success(args: SuccessArgs) -> BoltResponse:
     assert args.request is not None
     return BoltResponse(status=200, body="customized")
示例#7
0
def listener_middleware_returning_response():
    return BoltResponse(status=200, body="listener middleware")
示例#8
0
 def handle_errors(error):
     assert isinstance(error, BoltUnhandledRequestError)
     return BoltResponse(status=404, body="TODO")
示例#9
0
 def this_should_be_skipped():
     return BoltResponse(status=500, body="failed")