From 694a43d14422c8d45ecef90176a5dcb6036acd0a Mon Sep 17 00:00:00 2001 From: Florian Scherf Date: Fri, 10 Feb 2023 11:45:52 +0100 Subject: [PATCH] tests: redirects from event handlers: add tests for response classes Previously, this test only used response dicts, but did not test using respones classes. Signed-off-by: Florian Scherf --- tests/test_redirects_from_event_handlers.py | 127 ++++++++++++-------- 1 file changed, 77 insertions(+), 50 deletions(-) diff --git a/tests/test_redirects_from_event_handlers.py b/tests/test_redirects_from_event_handlers.py index 09284495..7ac2ae8e 100644 --- a/tests/test_redirects_from_event_handlers.py +++ b/tests/test_redirects_from_event_handlers.py @@ -1,53 +1,80 @@ +import pytest + +from lona import RedirectResponse, View from lona.html import Button -from lona import View - - -def setup_app(app): - class RedirectButton(Button): - def handle_input_event(self, input_event): - return { - 'redirect': '/', - } - - @app.route('/') - class Index(View): - def handle_request(self, request): - return 'SUCCESS' - - @app.route('/redirect-from-handle-input-event-root/') - class RedirectFromHandleInputEventRootView(View): - def handle_request(self, request): - return RedirectButton() - - def handle_input_event_root(self, input_event): - return { - 'redirect': '/', - } - - @app.route('/redirect-from-handle-input-event/') - class RedirectFromHandleInputEventView(View): - def handle_request(self, request): - return Button() - - def handle_input_event(self, input_event): - return { - 'redirect': '/', - } - - @app.route('/redirect-from-button/') - class RedirectFromButtonView(View): - def handle_request(self, request): - return RedirectButton() - - @app.route('/redirect-from-on-view-event/') - class RedirectFromOnViewEvent(View): - def on_view_event(self, view_event): - return { - 'redirect': '/', - } - - -async def test_redirects_from_event_handlers(lona_app_context): + + +def get_setup_app_callback(response_format): + def setup_app(app): + class RedirectButton(Button): + def handle_input_event(self, input_event): + if response_format == 'response-dict': + return { + 'redirect': '/', + } + + elif response_format == 'response-class': + return RedirectResponse('/') + + @app.route('/') + class Index(View): + def handle_request(self, request): + return 'SUCCESS' + + @app.route('/redirect-from-handle-input-event-root/') + class RedirectFromHandleInputEventRootView(View): + def handle_request(self, request): + return RedirectButton() + + def handle_input_event_root(self, input_event): + if response_format == 'response-dict': + return { + 'redirect': '/', + } + + elif response_format == 'response-class': + return RedirectResponse('/') + + @app.route('/redirect-from-handle-input-event/') + class RedirectFromHandleInputEventView(View): + def handle_request(self, request): + return Button() + + def handle_input_event(self, input_event): + if response_format == 'response-dict': + return { + 'redirect': '/', + } + + elif response_format == 'response-class': + return RedirectResponse('/') + + @app.route('/redirect-from-button/') + class RedirectFromButtonView(View): + def handle_request(self, request): + return RedirectButton() + + @app.route('/redirect-from-on-view-event/') + class RedirectFromOnViewEvent(View): + def handle_request(self, request): + return 'REDIRECT FROM ON VIEW EVENT' + + def on_view_event(self, view_event): + if response_format == 'response-dict': + return { + 'redirect': '/', + } + + elif response_format == 'response-class': + return RedirectResponse('/') + + return setup_app + + +@pytest.mark.parametrize( + 'response_format', ['response-dict', 'response-class'], +) +async def test_redirects_from_event_handlers(response_format, lona_app_context): """ This test tests redirects from @@ -58,7 +85,7 @@ async def test_redirects_from_event_handlers(lona_app_context): from playwright.async_api import async_playwright - context = await lona_app_context(setup_app) + context = await lona_app_context(get_setup_app_callback(response_format)) async with async_playwright() as p: browser = await p.chromium.launch()