From 5e1a97fcb385dfd1420f88929c8b0ceda221b078 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Sun, 11 Jan 2026 14:58:55 -0600 Subject: [PATCH] fix tests --- config/test.exs | 3 ++- lib/towerops/agents.ex | 18 ++++++++++-------- .../controllers/api/agent_controller.ex | 6 +----- lib/towerops_web/plugs/agent_auth.ex | 8 ++++++++ 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/config/test.exs b/config/test.exs index c8b9ea28..9790da73 100644 --- a/config/test.exs +++ b/config/test.exs @@ -47,4 +47,5 @@ config :towerops, ToweropsWeb.Endpoint, config :towerops, ping_module: Towerops.Monitoring.PingMock, poller_module: Towerops.Snmp.PollerMock, - snmp_adapter: Towerops.Snmp.SnmpMock + snmp_adapter: Towerops.Snmp.SnmpMock, + sql_sandbox: true diff --git a/lib/towerops/agents.ex b/lib/towerops/agents.ex index 05f240a2..52f82ac7 100644 --- a/lib/towerops/agents.ex +++ b/lib/towerops/agents.ex @@ -83,18 +83,20 @@ defmodule Towerops.Agents do {1, nil} """ - def update_agent_token_heartbeat(agent_token_id, ip_address, metadata \\ %{}) do + def update_agent_token_heartbeat(agent_token_id, ip_address, metadata \\ nil) do now = DateTime.truncate(DateTime.utc_now(), :second) + # Only update metadata if explicitly provided (not nil and not empty) + updates = + case metadata do + nil -> [last_seen_at: now, last_ip: ip_address] + meta when map_size(meta) > 0 -> [last_seen_at: now, last_ip: ip_address, metadata: meta] + _ -> [last_seen_at: now, last_ip: ip_address] + end + AgentToken |> where([t], t.id == ^agent_token_id) - |> Repo.update_all( - set: [ - last_seen_at: now, - last_ip: ip_address, - metadata: metadata - ] - ) + |> Repo.update_all(set: updates) end @doc """ diff --git a/lib/towerops_web/controllers/api/agent_controller.ex b/lib/towerops_web/controllers/api/agent_controller.ex index 67274106..058b75b0 100644 --- a/lib/towerops_web/controllers/api/agent_controller.ex +++ b/lib/towerops_web/controllers/api/agent_controller.ex @@ -77,11 +77,7 @@ defmodule ToweropsWeb.Api.AgentController do } # Update synchronously in the heartbeat endpoint - fn -> - Agents.update_agent_token_heartbeat(agent_token.id, ip, metadata) - end - |> Task.async() - |> Task.await() + Agents.update_agent_token_heartbeat(agent_token.id, ip, metadata) json(conn, %{status: "ok"}) end diff --git a/lib/towerops_web/plugs/agent_auth.ex b/lib/towerops_web/plugs/agent_auth.ex index adcedc50..2e654027 100644 --- a/lib/towerops_web/plugs/agent_auth.ex +++ b/lib/towerops_web/plugs/agent_auth.ex @@ -9,6 +9,7 @@ defmodule ToweropsWeb.Plugs.AgentAuth do import Phoenix.Controller import Plug.Conn + alias Ecto.Adapters.SQL.Sandbox alias Towerops.Agents @doc """ @@ -43,7 +44,14 @@ defmodule ToweropsWeb.Plugs.AgentAuth do defp update_heartbeat(conn, agent_token) do ip = to_string(:inet_parse.ntoa(conn.remote_ip)) + parent = self() + Task.start(fn -> + # Allow this task to use the test database sandbox + if Application.get_env(:towerops, :sql_sandbox) do + Sandbox.allow(Towerops.Repo, parent, self()) + end + Agents.update_agent_token_heartbeat(agent_token.id, ip) end)