Apply Testing LiveView course patterns (live/2, has_element?, render_click, form/3). Add tests for MobileQRLive, WeathermapLive, StatusPageLive, controllers, plugs, and pure function modules. Expand proto decode coverage to 85.24%.
150 lines
4.6 KiB
Elixir
150 lines
4.6 KiB
Elixir
defmodule Towerops.Uisp.ClientTest do
|
|
use ExUnit.Case, async: true
|
|
|
|
alias Towerops.Uisp.Client
|
|
|
|
describe "test_connection/2" do
|
|
test "returns {:ok, body} on 200" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
Req.Test.json(conn, %{id: 1, name: "Site-A"})
|
|
end)
|
|
|
|
assert {:ok, _} = Client.test_connection("https://uisp.example.com", "key")
|
|
end
|
|
|
|
test "returns error on 401" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
conn |> Plug.Conn.put_status(401) |> Req.Test.json(%{})
|
|
end)
|
|
|
|
assert {:error, :unauthorized} = Client.test_connection("https://uisp.example.com", "bad-key")
|
|
end
|
|
|
|
test "returns error on 403" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
conn |> Plug.Conn.put_status(403) |> Req.Test.json(%{})
|
|
end)
|
|
|
|
assert {:error, :forbidden} = Client.test_connection("https://uisp.example.com", "bad-key")
|
|
end
|
|
|
|
test "returns error on unexpected status" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
conn |> Plug.Conn.put_status(500) |> Req.Test.json(%{error: "internal"})
|
|
end)
|
|
|
|
assert {:error, {:unexpected_status, 500}} = Client.test_connection("https://uisp.example.com", "bad-key")
|
|
end
|
|
|
|
test "returns error on transport failure" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
Req.Test.transport_error(conn, :econnrefused)
|
|
end)
|
|
|
|
assert {:error, _} = Client.test_connection("https://uisp.example.com", "bad-key")
|
|
end
|
|
end
|
|
|
|
describe "list_sites/2" do
|
|
test "returns {:ok, list} with direct array" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
Req.Test.json(conn, [%{id: 1, name: "Site-A"}])
|
|
end)
|
|
|
|
assert {:ok, [%{"id" => 1}]} = Client.list_sites("https://uisp.example.com", "key")
|
|
end
|
|
|
|
test "returns {:ok, list} with items wrapper" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
Req.Test.json(conn, %{items: [%{id: 1, name: "Site-A"}]})
|
|
end)
|
|
|
|
assert {:ok, [%{"id" => 1}]} = Client.list_sites("https://uisp.example.com", "key")
|
|
end
|
|
|
|
test "returns {:ok, []} for unexpected body shape" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
Req.Test.json(conn, %{unexpected: true})
|
|
end)
|
|
|
|
assert {:ok, []} = Client.list_sites("https://uisp.example.com", "key")
|
|
end
|
|
|
|
test "returns error on 401" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
conn |> Plug.Conn.put_status(401) |> Req.Test.json(%{})
|
|
end)
|
|
|
|
assert {:error, :unauthorized} = Client.list_sites("https://uisp.example.com", "key")
|
|
end
|
|
|
|
test "returns error on unexpected status" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
conn |> Plug.Conn.put_status(500) |> Req.Test.json(%{error: "internal"})
|
|
end)
|
|
|
|
assert {:error, {:unexpected_status, 500, %{"error" => "internal"}}} =
|
|
Client.list_sites("https://uisp.example.com", "key")
|
|
end
|
|
|
|
test "returns error on transport failure" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
Req.Test.transport_error(conn, :econnrefused)
|
|
end)
|
|
|
|
assert {:error, _} = Client.list_sites("https://uisp.example.com", "key")
|
|
end
|
|
end
|
|
|
|
describe "list_devices/2" do
|
|
test "returns {:ok, list}" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
Req.Test.json(conn, [%{id: 1, name: "Device-A"}])
|
|
end)
|
|
|
|
assert {:ok, [%{"id" => 1}]} = Client.list_devices("https://uisp.example.com", "key")
|
|
end
|
|
end
|
|
|
|
describe "request_raw/3" do
|
|
test "returns {:ok, body} on 200" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
Req.Test.json(conn, %{status: "ok"})
|
|
end)
|
|
|
|
assert {:ok, %{"status" => "ok"}} = Client.request_raw(:get, "https://uisp.example.com/sites", "key")
|
|
end
|
|
|
|
test "returns error on 401" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
conn |> Plug.Conn.put_status(401) |> Req.Test.json(%{})
|
|
end)
|
|
|
|
assert {:error, :unauthorized} = Client.request_raw(:get, "https://uisp.example.com/sites", "key")
|
|
end
|
|
|
|
test "returns error on 404" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
conn |> Plug.Conn.put_status(404) |> Req.Test.json(%{})
|
|
end)
|
|
|
|
assert {:error, :not_found} = Client.request_raw(:get, "https://uisp.example.com/sites", "key")
|
|
end
|
|
|
|
test "returns error on unexpected status" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
conn |> Plug.Conn.put_status(500) |> Req.Test.json(%{})
|
|
end)
|
|
|
|
assert {:error, {:unexpected_status, 500, %{}}} = Client.request_raw(:get, "https://uisp.example.com/sites", "key")
|
|
end
|
|
|
|
test "returns error on transport failure" do
|
|
Req.Test.stub(Client, fn conn ->
|
|
Req.Test.transport_error(conn, :econnrefused)
|
|
end)
|
|
|
|
assert {:error, _} = Client.request_raw(:get, "https://uisp.example.com/sites", "key")
|
|
end
|
|
end
|
|
end
|