From c9878d84799889cb7dd168e9f8890d65096cbafd Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Sat, 9 May 2026 09:37:10 -0500 Subject: [PATCH] test: cn_maestro/preseem client transport-error and forbidden branches --- test/towerops/cn_maestro/client_test.exs | 33 +++++++++++++++ test/towerops/preseem/client_test.exs | 53 ++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/test/towerops/cn_maestro/client_test.exs b/test/towerops/cn_maestro/client_test.exs index d30d647b..08b4e38f 100644 --- a/test/towerops/cn_maestro/client_test.exs +++ b/test/towerops/cn_maestro/client_test.exs @@ -181,6 +181,39 @@ defmodule Towerops.CnMaestro.ClientTest do assert {:error, {:unexpected_status, 500}} = Client.list_devices("http://test", "tok") end + + test "returns transport error on Req failure" do + Req.Test.stub(Client, fn conn -> Req.Test.transport_error(conn, :econnrefused) end) + assert {:error, _} = Client.list_devices("http://test", "tok") + end + end + + describe "list_networks/2 — transport error" do + test "returns transport error" do + Req.Test.stub(Client, fn conn -> Req.Test.transport_error(conn, :timeout) end) + assert {:error, _} = Client.list_networks("http://test", "tok") + end + end + + describe "get_device_statistics/3 — transport error" do + test "returns transport error" do + Req.Test.stub(Client, fn conn -> Req.Test.transport_error(conn, :timeout) end) + assert {:error, _} = Client.get_device_statistics("http://test", "tok", "AA:BB") + end + end + + describe "get_device_performance/3 — transport error" do + test "returns transport error" do + Req.Test.stub(Client, fn conn -> Req.Test.transport_error(conn, :timeout) end) + assert {:error, _} = Client.get_device_performance("http://test", "tok", "AA:BB") + end + end + + describe "get_token/3 — transport error" do + test "returns transport error from underlying Req call" do + Req.Test.stub(Client, fn conn -> Req.Test.transport_error(conn, :econnrefused) end) + assert {:error, _} = Client.get_token("http://test", "id", "secret") + end end describe "list_networks/2 — error paths" do diff --git a/test/towerops/preseem/client_test.exs b/test/towerops/preseem/client_test.exs index bfb24caf..19d9b036 100644 --- a/test/towerops/preseem/client_test.exs +++ b/test/towerops/preseem/client_test.exs @@ -130,5 +130,58 @@ defmodule Towerops.Preseem.ClientTest do assert {:error, _} = Client.list_access_points_basic("key") end + + test "follows pagination across multiple pages" do + counter = :counters.new(1, [:atomics]) + + Req.Test.stub(Client, fn conn -> + :counters.add(counter, 1, 1) + + case :counters.get(counter, 1) do + 1 -> + Req.Test.json(conn, %{ + data: [%{id: 1, name: "AP-1"}, %{id: 2, name: "AP-2"}], + paginator: %{page: 1, page_count: 2} + }) + + _ -> + Req.Test.json(conn, %{ + data: [%{id: 3, name: "AP-3"}], + paginator: %{page: 2, page_count: 2} + }) + end + end) + + assert {:ok, all} = Client.list_access_points_basic("key") + assert length(all) == 3 + assert Enum.map(all, & &1["id"]) == [1, 2, 3] + end + + test "returns :forbidden on 403" do + Req.Test.stub(Client, fn conn -> + conn |> Plug.Conn.put_status(403) |> Req.Test.json(%{}) + end) + + assert {:error, :forbidden} = Client.list_access_points_basic("key") + end + + test "returns unexpected_status tuple on other non-2xx" do + Req.Test.stub(Client, fn conn -> + conn |> Plug.Conn.put_status(503) |> Req.Test.json(%{"error" => "down"}) + end) + + assert {:error, {:unexpected_status, 503, _body}} = + Client.list_access_points_basic("key") + end + end + + describe "list_access_points/1 — additional error branches" do + test "returns :forbidden on 403" do + Req.Test.stub(Client, fn conn -> + conn |> Plug.Conn.put_status(403) |> Req.Test.json(%{}) + end) + + assert {:error, :forbidden} = Client.list_access_points("key") + end end end