|
| 1 | +import pytest |
| 2 | +from playwright.sync_api import expect |
| 3 | + |
| 4 | +from tests.factories import WebAuthnCredentialFactory |
| 5 | + |
| 6 | + |
| 7 | +def test_webauthn_signals_triggered( |
| 8 | + live_server, |
| 9 | + django_db_serialized_rollback, |
| 10 | + page, |
| 11 | + playwright_force_login, |
| 12 | + playwright_manipulate_session, |
| 13 | + user, |
| 14 | + wait_for_console_message, |
| 15 | +): |
| 16 | + """Verify the ``PublicKeyCredential.signalCurrentUserDetails`` and |
| 17 | + ``PublicKeyCredential.signalAllAcceptedCredentials`` browser signals are |
| 18 | + triggered. |
| 19 | + """ |
| 20 | + |
| 21 | + playwright_force_login(user) |
| 22 | + playwright_manipulate_session( |
| 23 | + lambda session: session.update({"otp_webauthn_sync_needed": True}) |
| 24 | + ) |
| 25 | + |
| 26 | + # Create some credentials for the user |
| 27 | + WebAuthnCredentialFactory(user=user) |
| 28 | + WebAuthnCredentialFactory(user=user) |
| 29 | + |
| 30 | + # Set up signal waiters |
| 31 | + await_signal_accepted_credentials = wait_for_console_message( |
| 32 | + r"Signaled accepted credentials to the browser." |
| 33 | + ) |
| 34 | + await_signal_user_details = wait_for_console_message( |
| 35 | + r"Signaled current user details to the browser." |
| 36 | + ) |
| 37 | + |
| 38 | + page.goto(live_server.url) |
| 39 | + |
| 40 | + # Double check that PublicKeyCredential.signalCurrentUserDetails and |
| 41 | + # PublicKeyCredential.signalAllAcceptedCredentials apis are available in this browser |
| 42 | + # otherwise the result won't be meaningful. |
| 43 | + if not page.evaluate("() => 'signalCurrentUserDetails' in PublicKeyCredential"): |
| 44 | + pytest.skip( |
| 45 | + "PublicKeyCredential.signalCurrentUserDetails does not exist in this browser, cannot test this feature!", |
| 46 | + ) |
| 47 | + if not page.evaluate("() => 'signalAllAcceptedCredentials' in PublicKeyCredential"): |
| 48 | + pytest.skip( |
| 49 | + "PublicKeyCredential.signalAllAcceptedCredentials does not exist in this browser, cannot test this feature!", |
| 50 | + ) |
| 51 | + |
| 52 | + # Wait for the signals to be sent |
| 53 | + try: |
| 54 | + await_signal_accepted_credentials() |
| 55 | + except TimeoutError: |
| 56 | + pytest.fail( |
| 57 | + "sync_signals.ts did not post console message indicating PublicKeyCredential.signalAllAcceptedCredentials was called." |
| 58 | + ) |
| 59 | + try: |
| 60 | + await_signal_user_details() |
| 61 | + except TimeoutError: |
| 62 | + pytest.fail( |
| 63 | + "sync_signals.ts did not post console message indicating PublicKeyCredential.signalCurrentUserDetails was called." |
| 64 | + ) |
| 65 | + |
| 66 | + # Verify that the config script tag has been removed from the DOM |
| 67 | + expect(page.locator("script[id='otp_webauthn_sync_signals_config']")).to_have_count( |
| 68 | + 0 |
| 69 | + ) |
0 commit comments