prop/test/microwaveprop_web/live/user_management_live_test.exs
Graham McIntire da6b312213
Add live_table dep and convert /users to use it
Wire up gurujada/live_table 0.4.1 as a dependency (alongside its
transitive sutra_ui and optional igniter requirement) with the
:live_table config pointing at our Repo and PubSub. Forced oban_web
override so the path-pinned vendored copy still wins over live_table's
hex constraint.

Convert the admin /users page to use LiveTable.LiveResource with
sortable, searchable columns for callsign, name, and email. Custom
renderers preserve the daisyUI admin badge and the pending/confirmed
date cell. Hidden id field stays in the query so the edit and delete
actions can target a record by id.

Updated the delete test to push the delete event directly with an id
payload; the stream dom_ids under live_table are random UUIDs so
tr#users-<id> selectors no longer work.
2026-04-12 17:07:07 -05:00

73 lines
2.1 KiB
Elixir

defmodule MicrowavepropWeb.UserManagementLiveTest do
use MicrowavepropWeb.ConnCase
import Microwaveprop.AccountsFixtures
import Phoenix.LiveViewTest
alias Microwaveprop.Accounts
defp admin_user_fixture do
user = user_fixture()
{:ok, user} = Accounts.admin_update_user(user, %{is_admin: true})
user
end
describe "Index" do
test "redirects non-admin to /", %{conn: conn} do
conn = log_in_user(conn, user_fixture())
assert {:error, {:redirect, %{to: "/"}}} = live(conn, ~p"/users")
end
test "redirects anonymous user to log in", %{conn: conn} do
assert {:error, {:redirect, %{to: "/users/log-in"}}} = live(conn, ~p"/users")
end
test "admin sees the user list", %{conn: conn} do
admin = admin_user_fixture()
other = user_fixture(callsign: "W5TX")
conn = log_in_user(conn, admin)
{:ok, _view, html} = live(conn, ~p"/users")
assert html =~ admin.callsign
assert html =~ other.callsign
end
test "admin can delete another user", %{conn: conn} do
admin = admin_user_fixture()
target = user_fixture(callsign: "W5TX")
conn = log_in_user(conn, admin)
{:ok, view, _} = live(conn, ~p"/users")
view
|> render_click("delete", %{"id" => target.id})
refute Accounts.get_user_by_email(target.email)
end
end
describe "Edit" do
test "admin can promote a user to admin", %{conn: conn} do
admin = admin_user_fixture()
target = user_fixture(callsign: "W5TX")
conn = log_in_user(conn, admin)
{:ok, form_live, _} = live(conn, ~p"/users/#{target.id}/edit")
assert {:ok, _view, _html} =
form_live
|> form("#user-form",
user: %{
callsign: target.callsign,
name: target.name,
email: target.email,
is_admin: true
}
)
|> render_submit()
|> follow_redirect(conn, ~p"/users")
assert Accounts.get_user!(target.id).is_admin
end
end
end