Replies: 1 comment 1 reply
-
Redirects work as long as they can be resolved via simple http: from starlette.responses import RedirectResponse
from nicegui import ui
from nicegui.testing import User
pytest_plugins = ['nicegui.testing.plugin']
def create_pages() -> None:
@ui.page("/redirect")
async def redirect() -> RedirectResponse:
return RedirectResponse("/")
@ui.page("/")
async def main_page():
ui.label("main page")
async def test_redirect(user: User):
create_pages()
await user.open("/redirect")
await user.should_see("main page")
if __name__ in {"__main__", "__mp_main__"}:
create_pages()
ui.run(port=8888) I think the default wait time is just not long enough. After simplifying your code a bit and increasing retries from default 3 to 4 it works for me: from starlette.responses import RedirectResponse
from nicegui import app, ui
from nicegui.testing import User
pytest_plugins = ['nicegui.testing.plugin']
def create_pages() -> None:
@ui.page("/login")
async def login() -> RedirectResponse:
print("redirect login")
return RedirectResponse("/mock-login")
@ui.page("/logout")
async def logout() -> Response:
del app.storage.user["username"]
print("redirect logout")
return RedirectResponse(url="/")
@ui.page("/")
async def main_page():
print("main page", app.storage.user.get("username", ""))
if app.storage.user.get("username", ""):
ui.link("logout", target="/logout")
else:
ui.link("login", target="/login")
@app.get("/mock-login")
async def mock_login() -> Response:
app.storage.user["username"] = "dummy user"
print("mock login")
return RedirectResponse("/")
async def test_login(user: User):
create_pages()
await user.open("/")
user.find("login").click()
await user.should_see("logout", retries=4)
if __name__ in {"__main__", "__mp_main__"}:
create_pages()
ui.run(port=8888, storage_secret="foobar") The user fixture won't work if you redirect to external websites which require user interaction. For such scenarios we suggest to user the Screen fixture. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Question
Hello,
i just wanted to test my login function, but the user fixture does not seem to work with redirects or I am doing something else wrong.
Is it possible to achieve something like this with the user fixture?
The production code contains a redirect to a keycloak server (in "/login") and setting the user and going back to "/" on a "/auth" page, but i did not include this and would be skipping this via some env variable in my tests. It would just be nice to include the login stuff as most of my page is basically read-only for unauthenticated users (i.e. never write to the database).
Beta Was this translation helpful? Give feedback.
All reactions