Exemple #1
0
 def test_build(self):
     required_args = [
         "logger",
         "client",
         "req",
         "resp",
         "context",
         "body",
         "payload",
         "ack",
         "say",
         "respond",
         "next",
     ]
     arg_params: dict = build_required_kwargs(
         logger=logging.getLogger(__name__),
         required_arg_names=required_args,
         request=BoltRequest(body="", headers={}),
         response=BoltResponse(status=200),
         next_func=next,
     )
     args = Args(**arg_params)
     assert args.logger is not None
     assert args.request is not None
     assert args.response is not None
     assert args.client is not None
Exemple #2
0
def handle_no_karma_op(
    body: dict,
    next: Callable  # pylint: disable=redefined-builtin
) -> Union[Callable, BoltResponse]:  # pylint: disable=unsubscriptable-object
    """Middleware which enables KarmaChameleon to immediately and gracefully handle events
    which do not contain any karma operations or slash-commands.

    Unfortunately the Slack Bolt API requires that the next middleware method be referred
    to as "next", which is a built-in.  You can't win them all I suppose...

    Arguments:
    body -- a dict containing the entire Slack Bolt API event
    next -- callable reference to the next middleware listener.

    Returns:
    If the incoming event contains a karma operation, then the next middleware listener
    is returned and invoked by the caller of this middleware.  If there is no karma
    operation contained in the event, and the event is not a command, then a
    BoltResponse(200) is returned as we have nothing further to do.
    """
    if body.get("command"):
        return next()

    if body["event"]["type"] == "message" and "text" in body["event"]:
        msg = body["event"]["text"]
        if app.inc_regex.match(msg) or app.dec_regex.match(msg):
            return next()
    # This is too chatty to be left enabled, but it may be useful for debug in the future.
    # logger.debug("Ignoring event with no karma operation")
    return BoltResponse(status=200,
                        body="Ignoring event with no karma operation")
Exemple #3
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
Exemple #4
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)
def failure(args: FailureArgs) -> BoltResponse:
    return BoltResponse(status=args.suggested_status_code, body=args.reason)
Exemple #6
0
 def failure(args: FailureArgs) -> BoltResponse:
     assert args.request is not None
     assert args.reason is not None
     return BoltResponse(status=502, body="customized")
Exemple #7
0
 def success(args: SuccessArgs) -> BoltResponse:
     assert args.request is not None
     return BoltResponse(status=200, body="customized")
Exemple #8
0
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")
Exemple #10
0
 def this_should_be_skipped():
     return BoltResponse(status=500, body="failed")