Skip to content

Commit

Permalink
tests: redirects from event handlers: add tests for response classes
Browse files Browse the repository at this point in the history
Previously, this test only used response dicts, but did not test using
respones classes.

Signed-off-by: Florian Scherf <[email protected]>
  • Loading branch information
fscherf committed Feb 10, 2023
1 parent 078dec7 commit 694a43d
Showing 1 changed file with 77 additions and 50 deletions.
127 changes: 77 additions & 50 deletions tests/test_redirects_from_event_handlers.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 694a43d

Please sign in to comment.