Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def loadable_app(monkeypatch: MonkeyPatch) -> Mock:
app = Mock(spec=Quart)
app.cli = AppGroup()
module = Mock()
module.app = app
monkeypatch.setattr(quart.cli, "import_module", lambda _: module)
return app
async def test_propagation(debug: bool, testing: bool, raises: bool) -> None:
app = Quart(__name__)
@app.route("/")
async def exception() -> ResponseReturnValue:
raise SimpleException()
app.debug = debug
app.testing = testing
test_client = app.test_client()
if raises:
with pytest.raises(SimpleException):
await test_client.get("/")
else:
response = await test_client.get("/")
assert response.status_code == 500
async def test_has_request_context() -> None:
app = Quart(__name__)
headers, path, query_string = make_test_headers_path_and_query_string(app, "/")
request = Request(
"GET", "http", path, query_string, headers, "", "1.1", send_push_promise=no_op_push
)
async with RequestContext(Quart(__name__), request):
assert has_request_context() is True
assert has_app_context() is True
assert has_request_context() is False
assert has_app_context() is False
async def test_overlapping_websocket_ctx() -> None:
app = Quart(__name__)
websocket = Websocket("/", b"", "ws", CIMultiDict(), "", "1.1", [], None, None, None)
ctx1 = app.websocket_context(websocket)
await ctx1.__aenter__()
ctx2 = app.websocket_context(websocket)
await ctx2.__aenter__()
await ctx1.__aexit__(None, None, None)
assert has_app_context() # Ensure the app context still exists for ctx2
await ctx2.__aexit__(None, None, None)
async def _save_session(session: SecureCookieSession) -> Response:
interface = SecureCookieSessionInterface()
app = Quart(__name__)
app.secret_key = "secret"
response = Response("")
await interface.save_session(app, session, response)
return response
async def test_ascii_dumps(as_ascii: bool, expected: str) -> None:
app = Quart(__name__)
async with app.app_context():
app.config["JSON_AS_ASCII"] = as_ascii
assert dumps("🎊") == expected
def test_endpoint_overwrite() -> None:
app = Quart(__name__)
def route() -> str:
return ""
def route2() -> str:
return ""
async def route3() -> str:
return ""
app.add_url_rule("/a", "index", route, ["GET"])
app.add_url_rule("/a/a", "index", route, ["GET"]) # Should not assert, as same view func
with pytest.raises(AssertionError):
app.add_url_rule("/a/b", "index", route2, ["GET"])
app.add_url_rule("/b", "async", route3, ["GET"])
app.add_url_rule("/b/a", "async", route3, ["GET"]) # Should not assert, as same view func
async def test_secure_cookie_session_interface_save_session() -> None:
session = SecureCookieSession()
session["something"] = "else"
interface = SecureCookieSessionInterface()
app = Quart(__name__)
app.secret_key = "secret"
response = Response("")
await interface.save_session(app, session, response)
cookies: SimpleCookie = SimpleCookie()
cookies.load(response.headers["Set-Cookie"])
cookie = cookies[app.session_cookie_name]
assert cookie["path"] == interface.get_cookie_path(app)
assert cookie["httponly"] == "" if not interface.get_cookie_httponly(app) else True
assert cookie["secure"] == "" if not interface.get_cookie_secure(app) else True
if version_info >= (3, 8):
assert cookie["samesite"] == (interface.get_cookie_samesite(app) or "")
assert cookie["domain"] == (interface.get_cookie_domain(app) or "")
assert cookie["expires"] == (interface.get_expiration_time(app, session) or "")
assert response.headers["Vary"] == "Cookie"
def _session_app() -> Quart:
app = Quart(__name__)
app.session_interface = AsyncMock(spec=SessionInterface)
app.session_interface.open_session.return_value = SecureCookieSession() # type: ignore
app.session_interface.is_null_session.return_value = False # type: ignore
@app.route("/")
async def route() -> str:
session["a"] = "b"
return ""
@app.websocket("/ws/")
async def ws() -> None:
session["a"] = "b"
await websocket.accept()
await websocket.send("")
@app.websocket("/ws_return/")
async def test_has_app_context() -> None:
async with AppContext(Quart(__name__)):
assert has_app_context() is True
assert has_app_context() is False