diff --git a/test/towerops_web/controllers/api/agent_controller_test.exs b/test/towerops_web/controllers/api/agent_controller_test.exs index 73814c7e..1a549135 100644 --- a/test/towerops_web/controllers/api/agent_controller_test.exs +++ b/test/towerops_web/controllers/api/agent_controller_test.exs @@ -43,6 +43,48 @@ defmodule ToweropsWeb.Api.AgentControllerTest do } = json_response(conn, 200) end + test "returns config with equipment without SNMP device", %{ + conn: conn, + token: token, + organization: org, + agent_token: agent_token + } do + {:ok, site} = + Towerops.Sites.create_site(%{ + name: "Test Site", + organization_id: org.id + }) + + {:ok, equipment} = + Towerops.Equipment.create_equipment(%{ + name: "Test Router", + ip_address: "192.168.1.1", + snmp_enabled: true, + snmp_version: "2c", + snmp_community: "public", + site_id: site.id + }) + + {:ok, _assignment} = Agents.assign_equipment_to_agent(agent_token.id, equipment.id) + + conn = + conn + |> put_req_header("authorization", "Bearer #{token}") + |> get(~p"/api/v1/agent/config") + + response = json_response(conn, 200) + + assert %{ + "version" => "1.0", + "poll_interval_seconds" => 60, + "equipment" => [equipment_config] + } = response + + assert equipment_config["id"] == equipment.id + assert equipment_config["sensors"] == [] + assert equipment_config["interfaces"] == [] + end + test "returns config with assigned equipment", %{ conn: conn, token: token, @@ -207,6 +249,80 @@ defmodule ToweropsWeb.Api.AgentControllerTest do assert %{"status" => "accepted", "received" => 2} = json_response(conn, 200) end + + test "handles protobuf metrics with unknown types", %{conn: conn, token: token} do + # Create metric with unknown type (by using empty metric_type) + batch = %MetricBatch{ + metrics: [ + %Metric{metric_type: nil} + ] + } + + encoded = MetricBatch.encode(batch) + + conn = + conn + |> put_req_header("authorization", "Bearer #{token}") + |> put_req_header("content-type", "application/x-protobuf") + |> post(~p"/api/v1/agent/metrics", encoded) + + # Unknown types are filtered out, so received count is 0 + assert %{"status" => "accepted", "received" => 0} = json_response(conn, 200) + end + + test "handles JSON metrics with unknown types", %{conn: conn, token: token} do + metrics = [ + %{ + "type" => "unknown_type", + "some_field" => "value" + } + ] + + conn = + conn + |> put_req_header("authorization", "Bearer #{token}") + |> post(~p"/api/v1/agent/metrics", %{"metrics" => metrics}) + + assert %{"status" => "accepted", "received" => 1} = json_response(conn, 200) + end + + test "handles metrics with nil timestamp", %{conn: conn, token: token} do + metrics = [ + %{ + "type" => "sensor_reading", + "sensor_id" => Ecto.UUID.generate(), + "value" => 45.5, + "status" => "ok", + "timestamp" => nil + } + ] + + conn = + conn + |> put_req_header("authorization", "Bearer #{token}") + |> post(~p"/api/v1/agent/metrics", %{"metrics" => metrics}) + + assert %{"status" => "accepted", "received" => 1} = json_response(conn, 200) + end + + test "handles metrics with invalid ISO8601 timestamp", %{conn: conn, token: token} do + metrics = [ + %{ + "type" => "sensor_reading", + "sensor_id" => Ecto.UUID.generate(), + "value" => 45.5, + "status" => "ok", + "timestamp" => "invalid-date-string" + } + ] + + conn = + conn + |> put_req_header("authorization", "Bearer #{token}") + |> post(~p"/api/v1/agent/metrics", %{"metrics" => metrics}) + + assert %{"status" => "accepted", "received" => 1} = json_response(conn, 200) + end end describe "POST /api/v1/agent/heartbeat" do