def test_request_context_match() -> None: app = Quart(__name__) url_adapter = Mock() rule = Rule('/', ['GET'], 'index') url_adapter.match.return_value = (rule, {'arg': 'value'}) app.create_url_adapter = lambda *_: url_adapter # type: ignore request = Request('GET', 'http', '/', CIMultiDict()) RequestContext(app, request) assert request.url_rule == rule assert request.view_args == {'arg': 'value'} @pytest.mark.parametrize( 'exception_type, exception_instance', [ (MethodNotAllowed, MethodNotAllowed(['GET'])), (NotFound, NotFound()), (RedirectRequired, RedirectRequired('/')), ], ) def test_request_context_matching_error( exception_type: Exception, exception_instance: Exception, ) -> None: app = Quart(__name__) url_adapter = Mock() url_adapter.match.side_effect = exception_instance app.create_url_adapter = lambda *_: url_adapter # type: ignore request = Request('GET', 'http', '/', CIMultiDict()) RequestContext(app, request) assert isinstance(request.routing_exception,
url_adapter = Mock() rule = Rule("/", {"GET"}, "index") url_adapter.match.return_value = (rule, {"arg": "value"}) app.create_url_adapter = lambda *_: url_adapter # type: ignore request = Request( "GET", "http", "/", b"", CIMultiDict(), "", "1.1", send_push_promise=no_op_push ) RequestContext(app, request) assert request.url_rule == rule assert request.view_args == {"arg": "value"} @pytest.mark.parametrize( "exception_type, exception_instance", [ (MethodNotAllowed, MethodNotAllowed(["GET"])), (NotFound, NotFound()), (RedirectRequired, RedirectRequired("/")), ], ) def test_request_context_matching_error( exception_type: Exception, exception_instance: Exception ) -> None: app = Quart(__name__) url_adapter = Mock() url_adapter.match.side_effect = exception_instance app.create_url_adapter = lambda *_: url_adapter # type: ignore request = Request( "GET", "http", "/", b"", CIMultiDict(), "", "1.1", send_push_promise=no_op_push ) RequestContext(app, request)
async def discord_oauth_callback(): """ Callback endpoint for finished discord authorization flow. GET -> Only used in DEBUG mode. Gets code from querystring. POST -> Gets code from request data. """ if request.method == "GET": if not current_app.debug: # A GET request to this endpoint should only be used in testing. raise MethodNotAllowed(("POST", )) qs = parse_qs(request.query_string.decode()) code = qs.get("code") if code is not None: code = code[0] callback = request.scheme + "://" + request.host + "/auth/discord/callback" elif request.method == "POST": data = await request.json code = data.get("code") callback = data.get("callback", "") else: raise RuntimeWarning("Unexpected request method. (%s)" % request.method) if code is None: return ( jsonify({ "error": "Bad Request", "message": "Missing code in %s." % "querystring arguments" if request.method == "GET" else "JSON data", }), 400, ) if not is_valid_url(callback): return ( jsonify({ "error": "Bad Request", "message": "Not a well formed redirect URL." }), 400, ) access_data, status_code = await exchange_code(code=code, scope=format_scopes(SCOPES), redirect_uri=callback) if access_data.get("error", False): if status_code == 400: return ( jsonify({ "error": "Bad Request", "message": "Discord returned 400 status.", "data": access_data, }), 400, ) raise RuntimeWarning("Unpredicted status_code.\n%s\n%s" % (str(access_data), status_code)) expires_at = datetime.utcnow() + timedelta( seconds=access_data["expires_in"]) expires_at.replace(microsecond=0) user_data = await get_user(access_token=access_data["access_token"]) user_data["id"] = uid = int(user_data["id"]) user = await User.fetch(id=uid) if user is None: user = await User.create( id=user_data["id"], username=user_data["username"], discriminator=user_data["discriminator"], avatar=user_data["avatar"], ) await Token( user_id=user.id, data=access_data, expires_at=expires_at, token=access_data["access_token"], ).update() token = jwt.encode( { "uid": user.id, "exp": expires_at, "iat": datetime.utcnow() }, key=os.environ["SECRET_KEY"], ) return jsonify(token=token, exp=expires_at)
def test_method_not_allowed() -> None: error = MethodNotAllowed(['GET', 'POST']) assert 'GET, POST' == error.get_headers()['Allow']
def test_method_not_allowed() -> None: error = MethodNotAllowed(["GET", "POST"]) assert "GET, POST" == error.get_headers()["Allow"]