75 lines
2.3 KiB
Elixir
75 lines
2.3 KiB
Elixir
defmodule ToweropsWeb.MobileQRLiveTest do
|
|
use ToweropsWeb.ConnCase, async: true
|
|
|
|
import Ecto.Query
|
|
import Phoenix.LiveViewTest
|
|
|
|
alias Towerops.MobileSessions.QRLoginToken
|
|
|
|
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,
|
|
user: user
|
|
} do
|
|
{:ok, view, _html} = live(conn, ~p"/mobile/qr-login")
|
|
|
|
# Query the QR token that was just created during mount
|
|
qr_token =
|
|
Repo.one!(
|
|
from q in QRLoginToken,
|
|
where: q.user_id == ^user.id,
|
|
order_by: [desc: q.inserted_at],
|
|
limit: 1
|
|
)
|
|
|
|
{: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
|