towerops/test/towerops_web/controllers/api/mobile_controller_test.exs
Graham McIntire 3ca0834ef0 tests: raise coverage to 70% via helper promotion + new unit/property tests
Promoted pure presentation and utility helpers from `defp` to `def @doc false`
across ~20 LiveViews, Oban workers, and sync modules so they're reachable from
unit tests. Refactored several `cond` blocks into idiomatic function heads with
guards. Added ~250 new test cases in new files under test/towerops and
test/towerops_web, including DB-backed tests for CnMaestro.Sync and
AlertNotificationWorker, and removed dead LiveView tab components and
CapacityLive (no callers anywhere in lib/test).

Configured mix.exs test_coverage.ignore_modules to exclude vendored third-party
code (SnmpKit, protobuf-generated Towerops.Agent.*, Absinthe GraphQL types,
Phoenix HTML modules, Inspect protocol impls) from coverage calculations —
these are not our project code.

Coverage: 66.93% → 70.09%. Full suite: 10,127 tests, 0 failures.
2026-04-24 09:49:06 -05:00

129 lines
4.2 KiB
Elixir

defmodule ToweropsWeb.Api.MobileControllerTest do
use ToweropsWeb.ConnCase, async: true
import Towerops.AccountsFixtures
alias Towerops.MobileSessions
alias Towerops.Organizations
setup do
user = user_fixture()
{:ok, organization} = Organizations.create_organization(%{name: "Test Org"}, user.id)
{:ok, session} =
MobileSessions.create_mobile_session(%{
user_id: user.id,
device_name: "Test iPhone"
})
conn = put_req_header(build_conn(), "authorization", "Bearer #{session.raw_token}")
%{conn: conn, user: user, organization: organization, session: session}
end
describe "list_organizations" do
test "returns user organizations", %{conn: conn} do
conn = get(conn, ~p"/api/v1/mobile/organizations")
assert %{"organizations" => orgs} = json_response(conn, 200)
assert orgs != []
assert hd(orgs)["name"] == "Test Org"
end
end
describe "list_sites" do
test "returns sites for organization", %{conn: conn, organization: org} do
{:ok, _site} =
Towerops.Sites.create_site(%{
name: "Test Site",
organization_id: org.id
})
conn = get(conn, ~p"/api/v1/mobile/organizations/#{org.id}/sites")
assert %{"sites" => sites} = json_response(conn, 200)
assert length(sites) == 1
assert hd(sites)["name"] == "Test Site"
end
test "returns forbidden for non-member organization", %{conn: conn} do
other_user = user_fixture()
{:ok, other_org} = Organizations.create_organization(%{name: "Other Org"}, other_user.id)
conn = get(conn, ~p"/api/v1/mobile/organizations/#{other_org.id}/sites")
assert json_response(conn, 403)["error"] =~ "Access denied"
end
end
describe "list_alerts" do
test "returns alerts for organization", %{conn: conn, organization: org} do
conn = get(conn, ~p"/api/v1/mobile/organizations/#{org.id}/alerts")
assert %{"alerts" => _alerts} = json_response(conn, 200)
end
test "handles malformed limit parameter without crashing", %{conn: conn, organization: org} do
# Non-numeric limit should default to 50 instead of crashing
conn = get(conn, ~p"/api/v1/mobile/organizations/#{org.id}/alerts?limit=abc")
assert %{"alerts" => _alerts} = json_response(conn, 200)
end
test "respects limit parameter boundaries", %{conn: conn, organization: org} do
# Limit above 200 should be capped at 200
conn = get(conn, ~p"/api/v1/mobile/organizations/#{org.id}/alerts?limit=500")
assert %{"alerts" => _alerts} = json_response(conn, 200)
end
test "handles negative limit parameter", %{conn: conn, organization: org} do
# Negative limit should default to 50
conn = get(conn, ~p"/api/v1/mobile/organizations/#{org.id}/alerts?limit=-10")
assert %{"alerts" => _alerts} = json_response(conn, 200)
end
end
describe "format_alert_status/1" do
alias ToweropsWeb.Api.MobileController
test "resolved wins over acknowledged" do
alert = %{resolved_at: ~U[2026-01-01 00:00:00Z], acknowledged_at: ~U[2026-01-01 00:00:00Z]}
assert "resolved" == MobileController.format_alert_status(alert)
end
test "acknowledged when not resolved" do
alert = %{resolved_at: nil, acknowledged_at: ~U[2026-01-01 00:00:00Z]}
assert "acknowledged" == MobileController.format_alert_status(alert)
end
test "active when neither" do
alert = %{resolved_at: nil, acknowledged_at: nil}
assert "active" == MobileController.format_alert_status(alert)
end
end
describe "timeticks_to_string/1" do
alias ToweropsWeb.Api.MobileController
test "days when over a day" do
timeticks = (2 * 86_400 + 3 * 3600) * 100
assert "2d 3h" == MobileController.timeticks_to_string(timeticks)
end
test "hours when less than a day" do
timeticks = (5 * 3600 + 30 * 60) * 100
assert "5h 30m" == MobileController.timeticks_to_string(timeticks)
end
test "minutes when less than an hour" do
timeticks = 45 * 60 * 100
assert "45m" == MobileController.timeticks_to_string(timeticks)
end
test "zero timeticks = 0m" do
assert "0m" == MobileController.timeticks_to_string(0)
end
end
end