test: cn_maestro/preseem client transport-error and forbidden branches
This commit is contained in:
parent
59483f3b81
commit
c9878d8479
2 changed files with 86 additions and 0 deletions
|
|
@ -181,6 +181,39 @@ defmodule Towerops.CnMaestro.ClientTest do
|
||||||
|
|
||||||
assert {:error, {:unexpected_status, 500}} = Client.list_devices("http://test", "tok")
|
assert {:error, {:unexpected_status, 500}} = Client.list_devices("http://test", "tok")
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe "list_networks/2 — error paths" do
|
describe "list_networks/2 — error paths" do
|
||||||
|
|
|
||||||
|
|
@ -130,5 +130,58 @@ defmodule Towerops.Preseem.ClientTest do
|
||||||
|
|
||||||
assert {:error, _} = Client.list_access_points_basic("key")
|
assert {:error, _} = Client.list_access_points_basic("key")
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue