def test_oauth(self): app = App( client=self.web_client, signing_secret=self.signing_secret, oauth_settings=OAuthSettings( client_id="111.111", client_secret="xxx", scopes=["chat:write", "commands"], ), ) request = self.rf.get("/slack/install") response = SlackRequestHandler(app).handle(request) assert response.status_code == 200 assert response.get("content-type") == "text/html; charset=utf-8" assert "https://slack.com/oauth/v2/authorize?state=" in response.content.decode( "utf-8")
def test_shortcuts(self): app = App( client=self.web_client, signing_secret=self.signing_secret, ) def shortcut_handler(ack): ack() app.shortcut("test-shortcut")(shortcut_handler) 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 = 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)
def test_events(self): app = App( client=self.web_client, signing_secret=self.signing_secret, ) def event_handler(): pass app.event("app_mention")(event_handler) 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 = self.rf.post("/slack/events", data=body, content_type="application/json") request.headers = self.build_headers(timestamp, body) response = SlackRequestHandler(app).handle(request) assert response.status_code == 200 assert_auth_test_count(self, 1)
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)
"""Views for the Slack App.""" from django.http import HttpRequest from django.views.decorators.csrf import csrf_exempt from slack_bolt.adapter.django import SlackRequestHandler from .utils import app handler = SlackRequestHandler(app=app) @csrf_exempt def events(request: HttpRequest): """View to direct to the Bolt event handler.""" return handler.handle(request)
from django.shortcuts import redirect from django.http import HttpRequest, HttpResponse from django.views.decorators.csrf import csrf_exempt from slack_bolt.adapter.django import SlackRequestHandler from graphene_django.views import GraphQLView from .slack import ROBOT def index(request: HttpRequest): return redirect("./graphql") def healthz(request: HttpRequest): return HttpResponse("ok") graphql = csrf_exempt(GraphQLView.as_view(graphiql=True)) __slack_handler = SlackRequestHandler(app=ROBOT) @csrf_exempt def slack(request: HttpRequest): return __slack_handler.handle(request)