towerops/test/towerops_web/live/agent_live/show_test.exs
Graham McIntire 2269f38fc5 test: lift coverage 78.42% → 79.59% with focused unit + integration tests
Adds tests across previously-uncovered or under-covered modules:

- Mix tasks: backfill_checks, copy_mibs, import_mibs (new test files)
- Status pages: StatusIncident changeset (new file)
- Webhook signature verification on AgentReleaseWebhookController
- CloudflareBanWorker: HTTP-stubbed success / 5xx / transport-error paths
- FirmwareVersionFetcherWorker: stubbed perform/0 success + non-200 + tx error
- Geoip.Import: production --production code path (HTTP-stubbed)
- AgentLive.Show: superuser restart_agent + update_agent + non-superuser paths
- NetworkMapLive: node_clicked, deep-link node, topology_updated PubSub
- PreseemInsightsLive: toggle_select, deselect_all, filter, dismiss-twice
- AlertQuery: 1-arity defaults variants
- ProfileWatcher: yaml + reload-trigger event passthrough
- MobileAuthController: verify_qr_token success + get/revoke session
- HealthController: redis-configured branch
- RedisHealthCheck: un-tag :integration tests now that Redis is local
- FourOhFourTracker: un-skip module (Redis available)
- PingExecutor: un-tag local-only tests + KeyError surface + clamp branch
- CheckExecutorWorker: dispatch tcp/dns/ssl/ping branches
- UserResetPasswordController: drop dead edit/update + their template

Also removes dead code: edit/update actions on UserResetPasswordController
and the unused edit.html.heex template — both routed via LiveView.
2026-05-07 18:40:43 -05:00

299 lines
9.5 KiB
Elixir

defmodule ToweropsWeb.AgentLive.ShowTest do
use ToweropsWeb.ConnCase
import Phoenix.LiveViewTest
alias Towerops.AgentsFixtures
setup %{conn: conn} do
user = Towerops.AccountsFixtures.user_fixture(enable_totp: true)
{:ok, organization} = Towerops.Organizations.create_organization(%{name: "Test Org"}, user.id)
conn =
conn
|> log_in_user(user)
|> put_session(:current_organization_id, organization.id)
%{conn: conn, user: user, organization: organization}
end
defp create_agent(organization_id, name \\ nil) do
{:ok, agent_token, _raw_token} = AgentsFixtures.agent_token_fixture(organization_id, name)
agent_token
end
describe "mount and display" do
test "renders agent details page with agent name", %{conn: conn, organization: org} do
agent = create_agent(org.id, "My Test Agent")
{:ok, _view, html} = live(conn, ~p"/agents/#{agent.id}")
assert html =~ "My Test Agent"
assert html =~ "Agent Details"
end
test "displays status information", %{conn: conn, organization: org} do
agent = create_agent(org.id)
{:ok, _view, html} = live(conn, ~p"/agents/#{agent.id}")
assert html =~ "Status"
# Agent has nil last_seen_at, so should show "Never connected"
assert html =~ "Never connected"
end
test "displays device count section", %{conn: conn, organization: org} do
agent = create_agent(org.id)
{:ok, _view, html} = live(conn, ~p"/agents/#{agent.id}")
assert html =~ "Device"
# No direct assignments by default
assert html =~ "0 direct"
end
test "displays polling targets section with no devices message", %{conn: conn, organization: org} do
agent = create_agent(org.id)
{:ok, _view, html} = live(conn, ~p"/agents/#{agent.id}")
assert html =~ "Polling Targets"
assert html =~ "No devices assigned"
end
test "displays edit link", %{conn: conn, organization: org} do
agent = create_agent(org.id)
{:ok, _view, html} = live(conn, ~p"/agents/#{agent.id}")
assert html =~ "Edit Agent"
assert html =~ ~p"/agents/#{agent.id}/edit"
end
test "displays back to agents link", %{conn: conn, organization: org} do
agent = create_agent(org.id)
{:ok, _view, html} = live(conn, ~p"/agents/#{agent.id}")
assert html =~ "Back to Agents"
end
test "displays timestamps section", %{conn: conn, organization: org} do
agent = create_agent(org.id)
{:ok, _view, html} = live(conn, ~p"/agents/#{agent.id}")
assert html =~ "Timestamps"
assert html =~ "Created"
assert html =~ "Last Updated"
end
end
describe "tab switching" do
test "switches to debug tab for superuser", %{conn: _conn, organization: _org} do
user = Towerops.AccountsFixtures.user_fixture(enable_totp: true)
user
|> Ecto.Changeset.change(%{is_superuser: true})
|> Towerops.Repo.update!()
{:ok, super_org} = Towerops.Organizations.create_organization(%{name: "Super Org"}, user.id)
super_agent = create_agent(super_org.id)
super_conn =
build_conn()
|> log_in_user(user)
|> put_session(:current_organization_id, super_org.id)
{:ok, view, html} = live(super_conn, ~p"/agents/#{super_agent.id}")
# Default tab should be overview
assert html =~ "Overview"
assert html =~ "Debug Logs"
# Switch to debug tab
html = view |> element("button", "Debug Logs") |> render_click()
assert html =~ "Debug Status"
assert html =~ "View Debug Logs"
end
test "switches back to overview tab", %{conn: _conn, organization: _org} do
user = Towerops.AccountsFixtures.user_fixture(enable_totp: true)
user
|> Ecto.Changeset.change(%{is_superuser: true})
|> Towerops.Repo.update!()
{:ok, super_org} = Towerops.Organizations.create_organization(%{name: "Super Org"}, user.id)
super_agent = create_agent(super_org.id)
super_conn =
build_conn()
|> log_in_user(user)
|> put_session(:current_organization_id, super_org.id)
{:ok, view, _html} = live(super_conn, ~p"/agents/#{super_agent.id}")
# Switch to debug tab then back to overview
view |> element("button", "Debug Logs") |> render_click()
html = view |> element("button", "Overview") |> render_click()
assert html =~ "Polling Targets"
end
test "non-superuser does not see tab navigation", %{conn: conn, organization: org} do
agent = create_agent(org.id)
{:ok, _view, html} = live(conn, ~p"/agents/#{agent.id}")
refute html =~ "Debug Logs"
end
end
describe "authentication" do
test "redirects to login when not authenticated" do
conn = build_conn()
assert {:error, {:redirect, %{to: to}}} = live(conn, ~p"/agents/#{Ecto.UUID.generate()}")
assert to =~ "/users/log-in"
end
end
describe "authorization" do
test "raises when accessing agent from another organization", %{conn: conn} do
# Create a second user with their own org and agent
other_user = Towerops.AccountsFixtures.user_fixture(enable_totp: true)
{:ok, other_org} = Towerops.Organizations.create_organization(%{name: "Other Org"}, other_user.id)
other_agent = create_agent(other_org.id, "Other Org Agent")
# The current user should not be able to access this agent
assert_raise Ecto.NoResultsError, fn ->
live(conn, ~p"/agents/#{other_agent.id}")
end
end
test "superuser can access agent from any organization" do
superuser = Towerops.AccountsFixtures.user_fixture(enable_totp: true)
superuser
|> Ecto.Changeset.change(%{is_superuser: true})
|> Towerops.Repo.update!()
{:ok, super_org} = Towerops.Organizations.create_organization(%{name: "Super Org"}, superuser.id)
# Create agent in a different org
other_user = Towerops.AccountsFixtures.user_fixture(enable_totp: true)
{:ok, other_org} = Towerops.Organizations.create_organization(%{name: "Other Org"}, other_user.id)
other_agent = create_agent(other_org.id, "Cross-Org Agent")
super_conn =
build_conn()
|> log_in_user(superuser)
|> put_session(:current_organization_id, super_org.id)
{:ok, _view, html} = live(super_conn, ~p"/agents/#{other_agent.id}")
assert html =~ "Cross-Org Agent"
end
end
describe "superuser actions" do
test "restart button is visible for superuser" do
superuser = Towerops.AccountsFixtures.user_fixture(enable_totp: true)
superuser
|> Ecto.Changeset.change(%{is_superuser: true})
|> Towerops.Repo.update!()
{:ok, super_org} = Towerops.Organizations.create_organization(%{name: "Super Org"}, superuser.id)
agent = create_agent(super_org.id)
super_conn =
build_conn()
|> log_in_user(superuser)
|> put_session(:current_organization_id, super_org.id)
{:ok, _view, html} = live(super_conn, ~p"/agents/#{agent.id}")
assert html =~ "Restart"
assert html =~ "Update"
end
test "restart and update buttons are not visible for regular user", %{conn: conn, organization: org} do
agent = create_agent(org.id)
{:ok, _view, html} = live(conn, ~p"/agents/#{agent.id}")
refute html =~ "restart_agent"
refute html =~ "update_agent"
end
test "restart_agent event flashes success for superuser" do
superuser = Towerops.AccountsFixtures.user_fixture(enable_totp: true)
superuser
|> Ecto.Changeset.change(%{is_superuser: true})
|> Towerops.Repo.update!()
{:ok, super_org} = Towerops.Organizations.create_organization(%{name: "Super Org"}, superuser.id)
agent = create_agent(super_org.id)
super_conn =
build_conn()
|> log_in_user(superuser)
|> put_session(:current_organization_id, super_org.id)
{:ok, view, _html} = live(super_conn, ~p"/agents/#{agent.id}")
html = render_click(view, "restart_agent", %{})
assert html =~ "Restart command sent"
end
test "update_agent shows error when no release available for superuser" do
superuser = Towerops.AccountsFixtures.user_fixture(enable_totp: true)
superuser
|> Ecto.Changeset.change(%{is_superuser: true})
|> Towerops.Repo.update!()
{:ok, super_org} = Towerops.Organizations.create_organization(%{name: "Super Org"}, superuser.id)
agent = create_agent(super_org.id)
super_conn =
build_conn()
|> log_in_user(superuser)
|> put_session(:current_organization_id, super_org.id)
{:ok, view, _html} = live(super_conn, ~p"/agents/#{agent.id}")
Req.Test.stub(Towerops.Agents.ReleaseChecker, fn conn ->
Plug.Conn.send_resp(conn, 503, "{}")
end)
html = render_click(view, "update_agent", %{})
# Either matches no-asset or release-fetch-failure flash
assert html =~ "Update command sent" or html =~ "Failed" or
html =~ "No binary"
end
test "restart_agent event no-ops for non-superuser", %{conn: conn, organization: org} do
agent = create_agent(org.id)
{:ok, view, _html} = live(conn, ~p"/agents/#{agent.id}")
html = render_click(view, "restart_agent", %{})
refute html =~ "Restart command sent"
end
test "update_agent event no-ops for non-superuser", %{conn: conn, organization: org} do
agent = create_agent(org.id)
{:ok, view, _html} = live(conn, ~p"/agents/#{agent.id}")
html = render_click(view, "update_agent", %{})
refute html =~ "Update command sent"
end
end
end