コード例 #1
0
    def test_commands(self):
        app = App(
            client=self.web_client,
            signing_secret=self.signing_secret,
        )

        def command_handler(ack):
            ack()

        app.command("/hello-world")(command_handler)

        app_handler = SlackRequestHandler(app)

        input = (
            "token=verification_token"
            "&team_id=T111"
            "&team_domain=test-domain"
            "&channel_id=C111"
            "&channel_name=random"
            "&user_id=W111"
            "&user_name=primary-owner"
            "&command=%2Fhello-world"
            "&text=Hi"
            "&enterprise_id=E111"
            "&enterprise_name=Org+Name"
            "&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxxxx"
            "&trigger_id=111.111.xxx")
        timestamp, body = str(int(time())), input

        async def endpoint(req: Request):
            return await app_handler.handle(req)

        api = Starlette(
            debug=True,
            routes=[
                Route("/slack/events", endpoint=endpoint, methods=["POST"])
            ],
        )
        client = TestClient(api)
        response = client.post(
            "/slack/events",
            data=body,
            headers=self.build_headers(timestamp, body),
        )
        assert response.status_code == 200
        assert_auth_test_count(self, 1)
コード例 #2
0
    def test_commands(self):
        app = App(client=self.web_client, signing_secret=self.signing_secret,)

        def command_handler(ack):
            ack()

        app.command("/hello-world")(command_handler)

        input = (
            "token=verification_token"
            "&team_id=T111"
            "&team_domain=test-domain"
            "&channel_id=C111"
            "&channel_name=random"
            "&user_id=W111"
            "&user_name=primary-owner"
            "&command=%2Fhello-world"
            "&text=Hi"
            "&enterprise_id=E111"
            "&enterprise_name=Org+Name"
            "&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxxxx"
            "&trigger_id=111.111.xxx"
        )
        timestamp, body = str(int(time())), input
        event = {
            "body": body,
            "queryStringParameters": {},
            "headers": self.build_headers(timestamp, body),
            "requestContext": {"http": {"method": "POST"}},
            "isBase64Encoded": False,
        }
        response = SlackRequestHandler(app).handle(event, self.context)
        assert response["statusCode"] == 200
        assert self.mock_received_requests["/auth.test"] == 1

        event = {
            "body": body,
            "queryStringParameters": {},
            "headers": self.build_headers(timestamp, body),
            "requestContext": {"httpMethod": "POST"},
            "isBase64Encoded": False,
        }
        response = SlackRequestHandler(app).handle(event, self.context)
        assert response["statusCode"] == 200
        assert self.mock_received_requests["/auth.test"] == 1
コード例 #3
0
ファイル: test_flask.py プロジェクト: seratch/bolt-python
    def test_commands(self):
        app = App(
            client=self.web_client,
            signing_secret=self.signing_secret,
        )

        def command_handler(ack):
            ack()

        app.command("/hello-world")(command_handler)

        input = (
            "token=verification_token"
            "&team_id=T111"
            "&team_domain=test-domain"
            "&channel_id=C111"
            "&channel_name=random"
            "&user_id=W111"
            "&user_name=primary-owner"
            "&command=%2Fhello-world"
            "&text=Hi"
            "&enterprise_id=E111"
            "&enterprise_name=Org+Name"
            "&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxxxx"
            "&trigger_id=111.111.xxx"
        )
        timestamp, body = str(int(time())), input

        flask_app = Flask(__name__)

        @flask_app.route("/slack/events", methods=["POST"])
        def endpoint():
            return SlackRequestHandler(app).handle(request)

        with flask_app.test_client() as client:
            rv = client.post(
                "/slack/events",
                data=body,
                headers=self.build_headers(timestamp, body),
            )
            assert rv.status_code == 200
            assert_auth_test_count(self, 1)
コード例 #4
0
    def test_lazy_listeners(self):
        app = App(client=self.web_client, signing_secret=self.signing_secret,)

        def command_handler(ack):
            ack()

        def say_it(say):
            say("Done!")

        app.command("/hello-world")(ack=command_handler, lazy=[say_it])

        input = (
            "token=verification_token"
            "&team_id=T111"
            "&team_domain=test-domain"
            "&channel_id=C111"
            "&channel_name=random"
            "&user_id=W111"
            "&user_name=primary-owner"
            "&command=%2Fhello-world"
            "&text=Hi"
            "&enterprise_id=E111"
            "&enterprise_name=Org+Name"
            "&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxxxx"
            "&trigger_id=111.111.xxx"
        )
        timestamp, body = str(int(time())), input
        headers = self.build_headers(timestamp, body)
        headers["x-slack-bolt-lazy-only"] = "1"
        headers["x-slack-bolt-lazy-function-name"] = "say_it"
        event = {
            "body": body,
            "queryStringParameters": {},
            "headers": headers,
            "requestContext": {"http": {"method": "NONE"}},
            "isBase64Encoded": False,
        }
        response = SlackRequestHandler(app).handle(event, self.context)
        assert response["statusCode"] == 200
        assert self.mock_received_requests["/auth.test"] == 1
        assert self.mock_received_requests["/chat.postMessage"] == 1
コード例 #5
0
    def test_message_meuh(self, mocker):

        mocker.patch("fip_slack_bot.main.get_live_on_meuh",
                     return_value=resp_get_live_on_meuh)

        app = App(
            client=self.web_client,
            signing_secret=self.signing_secret,
        )

        app.command("/meuh")(message_meuh)

        request = self.build_custom_valid_request(meuh_command_body)

        response = app.dispatch(request)
        # might be asynchronous
        sleep(0.1)
        assert response.status == 200
        assert self.mock_received_requests["/chat.postMessage"] == 1
        assert (self.mock_received_requests_body["/chat.postMessage"][-1] ==
                meuh_result_body)
コード例 #6
0
ファイル: test_django.py プロジェクト: seratch/bolt-python
    def test_commands_lazy(self):
        app = App(
            client=self.web_client,
            signing_secret=self.signing_secret,
        )

        def command_handler(ack):
            ack()

        def lazy_handler():
            pass

        app.command("/hello-world")(ack=command_handler, lazy=[lazy_handler])

        input = (
            "token=verification_token"
            "&team_id=T111"
            "&team_domain=test-domain"
            "&channel_id=C111"
            "&channel_name=random"
            "&user_id=W111"
            "&user_name=primary-owner"
            "&command=%2Fhello-world"
            "&text=Hi"
            "&enterprise_id=E111"
            "&enterprise_name=Org+Name"
            "&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxxxx"
            "&trigger_id=111.111.xxx")
        timestamp, body = str(int(time())), input

        request = self.rf.post(
            "/slack/events",
            data=body,
            content_type="application/x-www-form-urlencoded",
        )
        request.headers = self.build_headers(timestamp, body)

        response = SlackRequestHandler(app).handle(request)
        assert response.status_code == 200
        assert_auth_test_count(self, 1)
コード例 #7
0
    def test_commands(self):
        app = App(
            client=self.web_client,
            signing_secret=self.signing_secret,
        )

        def command_handler(ack):
            ack()

        app.command("/hello-world")(command_handler)

        input = (
            "token=verification_token"
            "&team_id=T111"
            "&team_domain=test-domain"
            "&channel_id=C111"
            "&channel_name=random"
            "&user_id=W111"
            "&user_name=primary-owner"
            "&command=%2Fhello-world"
            "&text=Hi"
            "&enterprise_id=E111"
            "&enterprise_name=Org+Name"
            "&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxxxx"
            "&trigger_id=111.111.xxx")
        timestamp, body = str(int(time())), input

        api = falcon.API()
        resource = SlackAppResource(app)
        api.add_route("/slack/events", resource)

        client = testing.TestClient(api)
        response = client.simulate_post(
            "/slack/events",
            body=body,
            headers=self.build_headers(timestamp, body),
        )
        assert response.status_code == 200
        assert self.mock_received_requests["/auth.test"] == 1
コード例 #8
0
    def setup_server(cls):
        cls.old_os_env = remove_os_env_temporarily()
        setup_mock_web_api_server(cls)

        signing_secret = "secret"
        valid_token = "xoxb-valid"
        mock_api_server_base_url = "http://localhost:8888"
        web_client = WebClient(
            token=valid_token,
            base_url=mock_api_server_base_url,
        )
        app = App(
            client=web_client,
            signing_secret=signing_secret,
        )

        def event_handler():
            pass

        def shortcut_handler(ack):
            ack()

        def command_handler(ack):
            ack()

        app.event("app_mention")(event_handler)
        app.shortcut("test-shortcut")(shortcut_handler)
        app.command("/hello-world")(command_handler)

        handler = SlackRequestHandler(app)

        class SlackApp(object):
            @cherrypy.expose
            @cherrypy.tools.slack_in()
            def events(self, **kwargs):
                return handler.handle()

        cherrypy.tree.mount(SlackApp(), "/slack")
コード例 #9
0
class TestTornado(AsyncHTTPTestCase):
    signature_verifier = SignatureVerifier(signing_secret)

    def setUp(self):
        self.old_os_env = remove_os_env_temporarily()
        setup_mock_web_api_server(self)

        web_client = WebClient(
            token=valid_token,
            base_url=mock_api_server_base_url,
        )
        self.app = App(
            client=web_client,
            signing_secret=signing_secret,
        )
        self.app.event("app_mention")(event_handler)
        self.app.shortcut("test-shortcut")(shortcut_handler)
        self.app.command("/hello-world")(command_handler)

        AsyncHTTPTestCase.setUp(self)

    def tearDown(self):
        AsyncHTTPTestCase.tearDown(self)
        cleanup_mock_web_api_server(self)
        restore_os_env(self.old_os_env)

    def get_app(self):
        return Application([("/slack/events", SlackEventsHandler,
                             dict(app=self.app))])

    def generate_signature(self, body: str, timestamp: str):
        return self.signature_verifier.generate_signature(
            body=body,
            timestamp=timestamp,
        )

    def build_headers(self, timestamp: str, body: str):
        content_type = ("application/json" if body.startswith("{") else
                        "application/x-www-form-urlencoded")
        return {
            "content-type": content_type,
            "x-slack-signature": self.generate_signature(body, timestamp),
            "x-slack-request-timestamp": timestamp,
        }

    @gen_test
    async def test_events(self):
        input = {
            "token": "verification_token",
            "team_id": "T111",
            "enterprise_id": "E111",
            "api_app_id": "A111",
            "event": {
                "client_msg_id": "9cbd4c5b-7ddf-4ede-b479-ad21fca66d63",
                "type": "app_mention",
                "text": "<@W111> Hi there!",
                "user": "******",
                "ts": "1595926230.009600",
                "team": "T111",
                "channel": "C111",
                "event_ts": "1595926230.009600",
            },
            "type": "event_callback",
            "event_id": "Ev111",
            "event_time": 1595926230,
            "authed_users": ["W111"],
        }
        timestamp, body = str(int(time())), json.dumps(input)

        request = HTTPRequest(
            url=self.get_url("/slack/events"),
            method="POST",
            body=body,
            headers=self.build_headers(timestamp, body),
        )
        response: HTTPResponse = await self.http_client.fetch(request)
        assert response.code == 200
        assert_auth_test_count(self, 1)

    @gen_test
    async def test_shortcuts(self):
        input = {
            "type": "shortcut",
            "token": "verification_token",
            "action_ts": "111.111",
            "team": {
                "id": "T111",
                "domain": "workspace-domain",
                "enterprise_id": "E111",
                "enterprise_name": "Org Name",
            },
            "user": {
                "id": "W111",
                "username": "******",
                "team_id": "T111"
            },
            "callback_id": "test-shortcut",
            "trigger_id": "111.111.xxxxxx",
        }

        timestamp, body = str(int(
            time())), f"payload={quote(json.dumps(input))}"

        request = HTTPRequest(
            url=self.get_url("/slack/events"),
            method="POST",
            body=body,
            headers=self.build_headers(timestamp, body),
        )
        response: HTTPResponse = await self.http_client.fetch(request)
        assert response.code == 200
        assert_auth_test_count(self, 1)

    @gen_test
    async def test_commands(self):
        input = (
            "token=verification_token"
            "&team_id=T111"
            "&team_domain=test-domain"
            "&channel_id=C111"
            "&channel_name=random"
            "&user_id=W111"
            "&user_name=primary-owner"
            "&command=%2Fhello-world"
            "&text=Hi"
            "&enterprise_id=E111"
            "&enterprise_name=Org+Name"
            "&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxxxx"
            "&trigger_id=111.111.xxx")
        timestamp, body = str(int(time())), input

        request = HTTPRequest(
            url=self.get_url("/slack/events"),
            method="POST",
            body=body,
            headers=self.build_headers(timestamp, body),
        )
        response: HTTPResponse = await self.http_client.fetch(request)
        assert response.code == 200
        assert_auth_test_count(self, 1)