towerops/test/towerops_web/live/mobile_qr_live_test.exs

65 lines
2.1 KiB
Elixir

defmodule ToweropsWeb.MobileQRLiveTest do
use ToweropsWeb.ConnCase, async: true
import Phoenix.LiveViewTest
setup :register_and_log_in_user
describe "mount" do
test "renders QR code display", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/mobile/qr-login")
assert html =~ "Link Mobile App"
end
test "shows QR code image", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/mobile/qr-login")
assert has_element?(view, "img")
end
test "shows waiting message", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/mobile/qr-login")
assert html =~ "Waiting for mobile app to scan"
end
test "shows how-to instructions", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/mobile/qr-login")
assert html =~ "How to link your phone"
end
end
describe "handle_info" do
test "catch-all handles unexpected messages without crashing", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/mobile/qr-login")
send(view.pid, :unexpected_message)
assert render(view) =~ "Link Mobile App"
end
test ":check_completion still pending — does not crash and re-arms timer", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/mobile/qr-login")
send(view.pid, :check_completion)
# Trigger a render to verify view is still alive after the handler runs
assert render(view) =~ "Link Mobile App"
end
test ":check_completion completes login when matching mobile session exists", %{
conn: conn
} do
{:ok, view, _html} = live(conn, ~p"/mobile/qr-login")
# Read the live view's current qr_token and complete it externally
qr_token = :sys.get_state(view.pid).socket.assigns.qr_token
{:ok, _mobile_session} =
Towerops.MobileSessions.complete_qr_login(qr_token.token, %{
device_name: "iPhone Test",
os: "iOS",
os_version: "17.0",
ip_address: "127.0.0.1",
user_agent: "Test Agent"
})
send(view.pid, :check_completion)
assert render(view) =~ "authenticated successfully"
end
end
end