Beispiel #1
0
    def execute(self, environment, event):
        path = event["input"]["path"]
        http_method = event["input"]["requestContext"]["httpMethod"]
        response = ApiGatewayResponse()
        if http_method == "GET":
            if path == "/oauth/authorization_grant":
                query_strings = event["input"][
                    "multiValueQueryStringParameters"]
                code = query_strings.get("code", None)
                state = query_strings.get("state", None)
                if (code is not None and state is not None and len(code) == 1
                        and len(state) == 1):
                    response = UserInstall(
                        client_id=environment["CLIENT_ID"],
                        client_secret=environment["CLIENT_SECRET"],
                        redirect_uri=environment["REDIRECT_URI"],
                        user_token_store=StubUserTokenStore(),
                    ).execute(code[0], state[0])
                else:
                    logging.debug("missing code or state")
                    response.not_found()
            else:
                response.not_found()
        else:
            request = ApiGatewayRequest(event)
            response = HandleEvent(
                environment=environment,
                headers=request.headers(),
                raw_body=request.raw_body(),
            ).execute()

        return response.present()
    def test_ok(self):
        response = ApiGatewayResponse().ok()

        assert response.present() == {
            "statusCode": 204,
            "headers": {},
            "body": None,
        }
    def test_not_found(self):
        response = ApiGatewayResponse().not_found()

        assert response.present() == {
            "statusCode": 404,
            "headers": {},
            "body": None,
        }
    def test_auth_error(self):
        response = ApiGatewayResponse().auth_error()

        assert response.present() == {
            "statusCode": 401,
            "headers": {},
            "body": None,
        }
    def test_redirect(self):
        url = "https://example.com"
        response = ApiGatewayResponse().redirect(url=url)

        assert response.present() == {
            "statusCode": 307,
            "headers": {
                "Location": url
            },
            "body": None,
        }
    def test_ok_with_html_body(self):
        body = "<html>"
        response = ApiGatewayResponse().ok_html(body)

        assert response.present() == {
            "statusCode": 200,
            "headers": {
                "Content-Type": "text/html"
            },
            "body": body,
        }
    def test_ok_with_body(self):
        body = dict(test="testing")
        response = ApiGatewayResponse().ok(body)

        assert response.present() == {
            "statusCode": 200,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": '{"test": "testing"}',
        }
    def test_throw_exception_if_response_not_set(self):
        response = ApiGatewayResponse()

        with pytest.raises(NoResponseSetError):
            response.present()