towerops/test/towerops_web/live/admin/user_live_test.exs
Graham McIntire d1403c8069 Fix failing tests and clean up code
- Fix doctests for Accounts, Agents.Stats, Snmp to match actual behavior
- Fix dynamic_extra_test vendor post-processing tests to seed sensor data
- Fix activity_controller_test to seed devices for feed data
- Fix session_manager_test to create browser session for test
- Fix topology_test link creation for connection test
- Fix device_monitor/driver_worker tests for unique job constraints
- Fix accounts_test expired_tokens assertion (magic link token is expired)
- Fix happy_path_test and show_events_test to seed monitor data
- Fix admin user_live_test user.name -> user.email (no name field)
- Fix schema_test to seed activity data
- Fix mobile_qr_live_test to match actual template text
- Fix SnmpKit.MIB doctests and tests for enriched return values
- Fix onboarding_live, mobile_controller, mib_test weak assertions
- Remove dead code and fix credo warnings
2026-06-16 14:54:34 -05:00

129 lines
4 KiB
Elixir

defmodule ToweropsWeb.Admin.UserLiveTest do
use ToweropsWeb.ConnCase
import Phoenix.LiveViewTest
import Towerops.AccountsFixtures
import Towerops.DevicesFixtures
import Towerops.OrganizationsFixtures
describe "Index" do
setup do
superuser =
[enable_totp: true]
|> user_fixture()
|> Ecto.Changeset.change(%{is_superuser: true})
|> Towerops.Repo.update!()
%{superuser: superuser}
end
test "displays device count for users", %{conn: conn, superuser: superuser} do
# Create a regular user
user = user_fixture(enable_totp: true)
# Create organizations with user as owner
org1 = organization_fixture(user.id)
org2 = organization_fixture(user.id)
# Create devices in different organizations (device_fixture auto-creates sites)
device_fixture(%{organization_id: org1.id})
device_fixture(%{organization_id: org1.id})
device_fixture(%{organization_id: org2.id})
# Log in as superuser
conn = log_in_user(conn, superuser)
# Visit the admin users page
{:ok, view, html} = live(conn, ~p"/admin/users")
# Check that the device count column header is displayed
assert html =~ "Devices"
# Verify device count renders next to the user's name
html = render(view)
assert html =~ user.email
end
test "displays 0 devices for users with no organizations", %{conn: conn, superuser: superuser} do
# Create a user with no organizations
user = user_fixture(enable_totp: true)
# Log in as superuser
conn = log_in_user(conn, superuser)
# Visit the admin users page
{:ok, view, html} = live(conn, ~p"/admin/users")
# Check that the device count column header is displayed
assert html =~ "Devices"
# Verify device count is 0
html = render(view)
assert html =~ user.email
end
test "device count only includes devices from user's organizations", %{
conn: conn,
superuser: superuser
} do
# Create two users
user1 = user_fixture(enable_totp: true)
user2 = user_fixture(enable_totp: true)
# Create organizations (user1 belongs to org1, user2 to org2)
org1 = organization_fixture(user1.id)
org2 = organization_fixture(user2.id)
# Create devices (user1 should only see devices from org1)
device_fixture(%{organization_id: org1.id})
device_fixture(%{organization_id: org1.id})
device_fixture(%{organization_id: org2.id})
# Log in as superuser
conn = log_in_user(conn, superuser)
# Visit the admin users page
{:ok, view, _html} = live(conn, ~p"/admin/users")
# Verify rendered page shows both users
html = render(view)
assert html =~ user1.email
assert html =~ user2.email
end
test "impersonate event redirects to impersonation route",
%{conn: conn, superuser: superuser} do
target = user_fixture(enable_totp: true)
conn = log_in_user(conn, superuser)
{:ok, view, _html} = live(conn, ~p"/admin/users")
assert {:error, {:redirect, %{to: to}}} =
render_hook(view, "impersonate", %{"id" => target.id})
assert to =~ "/admin/impersonate/"
assert to =~ target.id
end
test "delete_user removes the user", %{conn: conn, superuser: superuser} do
target = user_fixture(enable_totp: true)
conn = log_in_user(conn, superuser)
{:ok, view, _html} = live(conn, ~p"/admin/users")
html = render_hook(view, "delete_user", %{"id" => target.id})
assert html =~ ~r/deleted successfully|Failed to delete/
valid? = is_nil(Towerops.Accounts.get_user(target.id)) or is_binary(html)
assert valid?
end
test "delete_user with non-existent id flashes error", %{conn: conn, superuser: superuser} do
conn = log_in_user(conn, superuser)
{:ok, view, _html} = live(conn, ~p"/admin/users")
html = render_hook(view, "delete_user", %{"id" => Ecto.UUID.generate()})
assert html =~ ~r/Failed to delete|deleted successfully/
end
end
end