towerops/test/towerops_web/live/admin/user_live_test.exs
Graham McIntire 83d47d1502
perf(tests): remove/reduce Process.sleep calls for faster tests
- Remove all 25ms and 50ms sleeps from agent_channel_test
- Add poll_until helper for async DB operations (early exit on success)
- Reduce timeout test sleeps from 100ms to 60ms
- Reduce polling delays from 10ms to 5ms in multiple tests
- Remove unnecessary timestamp and background task sleeps
- Reduce circuit breaker recovery sleep from 150ms to 110ms

Performance: 30.3s → 28.3s (6.6% faster)
Total improvement from baseline: 52s → 28.3s (45.6% faster)
2026-03-06 07:57:30 -06:00

102 lines
3.2 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 is in the assigns
users = :sys.get_state(view.pid).socket.assigns.users
test_user = Enum.find(users, &(&1.id == user.id))
assert test_user.device_count == 3
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 in the assigns
users = :sys.get_state(view.pid).socket.assigns.users
test_user = Enum.find(users, &(&1.id == user.id))
assert test_user.device_count == 0
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")
# Get all users from the assigns
users = :sys.get_state(view.pid).socket.assigns.users
# Find user1 and verify device count
user1_with_count = Enum.find(users, &(&1.id == user1.id))
assert user1_with_count.device_count == 2
# Find user2 and verify device count
user2_with_count = Enum.find(users, &(&1.id == user2.id))
assert user2_with_count.device_count == 1
end
end
end