towerops/test/towerops_web/controllers/api/mobile_controller_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

298 lines
9.8 KiB
Elixir

defmodule ToweropsWeb.Api.MobileControllerTest do
use ToweropsWeb.ConnCase, async: true
import Towerops.AccountsFixtures
alias Towerops.MobileSessions
alias Towerops.Organizations
alias Towerops.Snmp.Device
alias Towerops.Snmp.Interface
alias ToweropsWeb.Api.MobileController
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
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
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
describe "list_devices and get_device" do
test "list_devices returns 200 with empty list when no devices exist", %{conn: conn, organization: org} do
conn = get(conn, ~p"/api/v1/mobile/organizations/#{org.id}/devices")
assert %{"device" => []} = json_response(conn, 200)
end
test "list_devices forbidden for non-member", %{conn: conn} do
other_user = user_fixture()
{:ok, other_org} = Organizations.create_organization(%{name: "Other"}, other_user.id)
conn = get(conn, ~p"/api/v1/mobile/organizations/#{other_org.id}/devices")
assert json_response(conn, 403)["error"] =~ "Access denied"
end
test "get_device handler 404s on unknown id (direct call)" do
user = user_fixture()
conn = Plug.Conn.assign(build_conn(), :current_user, user)
conn = MobileController.get_device(conn, %{"id" => Ecto.UUID.generate()})
assert conn.status == 404
end
test "get_device handler returns 403 for cross-org device (direct call)" do
user = user_fixture()
other_user = user_fixture()
{:ok, other_org} = Organizations.create_organization(%{name: "Other2"}, other_user.id)
{:ok, other_site} = Towerops.Sites.create_site(%{name: "Site Other", organization_id: other_org.id})
{:ok, device} =
Towerops.Devices.create_device(%{
name: "Forbidden",
ip_address: "10.99.0.1",
site_id: other_site.id,
organization_id: other_org.id
})
conn = Plug.Conn.assign(build_conn(), :current_user, user)
conn = MobileController.get_device(conn, %{"id" => device.id})
assert conn.status == 403
end
test "get_device direct call formats sensors and interfaces from SNMP device",
%{user: user, organization: org} do
{:ok, site} =
Towerops.Sites.create_site(%{name: "Detail Site", organization_id: org.id})
{:ok, device} =
Towerops.Devices.create_device(%{
name: "Core",
ip_address: "10.50.50.50",
site_id: site.id,
organization_id: org.id,
snmp_enabled: true
})
{:ok, snmp_dev} =
Towerops.Repo.insert(%Device{
device_id: device.id,
sys_uptime: (5 * 3600 + 30 * 60) * 100,
sys_descr: "test"
})
{:ok, _iface} =
Towerops.Repo.insert(%Interface{
snmp_device_id: snmp_dev.id,
if_index: 1,
if_name: "eth0",
if_speed: 1_000_000_000,
if_oper_status: "up"
})
{:ok, _sensor} =
Towerops.Repo.insert(%Towerops.Snmp.Sensor{
snmp_device_id: snmp_dev.id,
sensor_type: "temperature",
sensor_index: "1",
sensor_descr: "CPU Temperature",
sensor_oid: ".1.3.6.1.4.1.9.9.13.1.3.1.3.1"
})
conn = Plug.Conn.assign(build_conn(), :current_user, user)
conn = MobileController.get_device(conn, %{"id" => device.id})
assert conn.status == 200
body = Jason.decode!(conn.resp_body)
assert body["id"] == device.id
assert body["name"] == "Core"
assert body["site"]["id"] == site.id
assert body["site"]["name"] == "Detail Site"
assert body["interfaces"] != []
assert body["sensors"] == []
assert body["uptime"] =~ "h"
end
test "get_device direct call maps interfaces when SNMP device has them",
%{user: user, organization: org} do
{:ok, site} =
Towerops.Sites.create_site(%{name: "Iface Site", organization_id: org.id})
{:ok, device} =
Towerops.Devices.create_device(%{
name: "WithIfaces",
ip_address: "10.55.55.55",
site_id: site.id,
organization_id: org.id,
snmp_enabled: true
})
{:ok, snmp_dev} =
Towerops.Repo.insert(%Device{
device_id: device.id,
sys_uptime: 5_000,
sys_descr: "test"
})
{:ok, _iface} =
Towerops.Repo.insert(%Interface{
snmp_device_id: snmp_dev.id,
if_index: 1,
if_name: "ge-0/0/0",
if_alias: "uplink",
if_oper_status: "up",
if_admin_status: "up",
if_speed: 1_000_000_000,
if_phys_address: "aa:bb:cc:dd:ee:ff"
})
conn = Plug.Conn.assign(build_conn(), :current_user, user)
conn = MobileController.get_device(conn, %{"id" => device.id})
assert conn.status == 200
body = Jason.decode!(conn.resp_body)
assert [iface] = body["interfaces"]
assert iface["name"] == "ge-0/0/0"
assert iface["alias"] == "uplink"
assert iface["status"] == "up"
assert iface["mac_address"] == "aa:bb:cc:dd:ee:ff"
end
end
describe "list_alerts — formatting" do
test "returns formatted alert with status mapping", %{conn: conn, organization: org} do
{:ok, site} = Towerops.Sites.create_site(%{name: "Alert Site", organization_id: org.id})
{:ok, device} =
Towerops.Devices.create_device(%{
name: "Alert-Dev",
ip_address: "10.20.30.40",
site_id: site.id,
organization_id: org.id
})
{:ok, _alert} =
Towerops.Alerts.create_alert(%{
device_id: device.id,
alert_type: "device_down",
triggered_at: ~U[2026-01-01 00:00:00Z],
message: "down"
})
response = get(conn, ~p"/api/v1/mobile/organizations/#{org.id}/alerts")
%{"alerts" => alerts} = json_response(response, 200)
[a] = alerts
assert a["status"] == "active"
assert a["message"] == "down"
assert a["equipment_name"] == "Alert-Dev"
assert a["device_id"] == device.id
end
end
end