Esempio n. 1
0
    def test_handle_callback(self):
        oauth_flow = OAuthFlow(
            client=WebClient(base_url=self.mock_api_server_base_url),
            settings=OAuthSettings(
                client_id="111.222",
                client_secret="xxx",
                scopes=["chat:write", "commands"],
                installation_store=FileInstallationStore(),
                state_store=FileOAuthStateStore(expiration_seconds=120),
                success_url="https://www.example.com/completion",
                failure_url="https://www.example.com/failure",
            ),
        )
        state = oauth_flow.issue_new_state(None)
        req = BoltRequest(
            body="",
            query=f"code=foo&state={state}",
            headers={
                "cookie": [f"{oauth_flow.settings.state_cookie_name}={state}"]
            },
        )
        resp = oauth_flow.handle_callback(req)
        assert resp.status == 200
        assert "https://www.example.com/completion" in resp.body

        app = App(signing_secret="signing_secret", oauth_flow=oauth_flow)
        global_shortcut_body = {
            "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",
        }
        body = f"payload={quote(json.dumps(global_shortcut_body))}"
        timestamp = str(int(time()))
        signature_verifier = SignatureVerifier("signing_secret")
        headers = {
            "content-type": ["application/x-www-form-urlencoded"],
            "x-slack-signature": [
                signature_verifier.generate_signature(body=body,
                                                      timestamp=timestamp)
            ],
            "x-slack-request-timestamp": [timestamp],
        }
        request = BoltRequest(body=body, headers=headers)
        response = app.dispatch(request)
        assert response.status == 200
        assert self.mock_received_requests["/auth.test"] == 1
Esempio n. 2
0
def test_verified():
    app.dependency_overrides[verify_signature] = verify_signature
    client = TestClient(app)
    timestamp = int(datetime.now().timestamp())
    data = {'type': 'url_verification', 'token': 'token'}
    verifier = SignatureVerifier(settings.SLACK_SIGNING_SECRET)
    signature = verifier.generate_signature(timestamp=timestamp,
                                            body=json.dumps(data))
    headers = {
        'x-slack-request-timestamp': str(timestamp),
        'x-slack-signature': signature
    }
    res = client.post('/v1/events/', json=data, headers=headers)
    assert res.status_code == HTTPStatus.OK
Esempio n. 3
0
 def test_generate_signature_body_as_bytes(self):
     verifier = SignatureVerifier(self.signing_secret)
     signature = verifier.generate_signature(
         timestamp=self.timestamp, body=self.body.encode("utf-8")
     )
     self.assertEqual(self.valid_signature, signature)
Esempio n. 4
0
 def test_generate_signature(self):
     verifier = SignatureVerifier(self.signing_secret)
     signature = verifier.generate_signature(
         timestamp=self.timestamp, body=self.body
     )
     self.assertEqual(self.valid_signature, signature)