aprs.me/test/aprsme_web/live/api_docs_live_test.exs

111 lines
3.4 KiB
Elixir

defmodule AprsmeWeb.ApiDocsLiveTest do
use AprsmeWeb.ConnCase
import Aprsme.PacketsFixtures
import Phoenix.LiveViewTest
describe "ApiDocsLive" do
test "renders the API documentation page", %{conn: conn} do
{:ok, _lv, html} = live(conn, ~p"/api", on_error: :warn)
assert html =~ "API Documentation"
assert html =~ "/api/v1/callsign"
assert html =~ "/api/v1/weather/nearby"
end
test "update_callsign event updates the callsign input", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/api", on_error: :warn)
rendered =
lv
|> element("#test_callsign")
|> render_change(%{"callsign" => "k1abc"})
assert rendered =~ "k1abc"
end
test "submitting test_api with empty callsign shows an error", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/api", on_error: :warn)
rendered =
lv
|> form("form[phx-submit=\"test_api\"]", %{"callsign" => " "})
|> render_submit()
assert rendered =~ "Please enter a callsign"
end
test "invalid callsign format returns an error response", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/api", on_error: :warn)
# Seed the callsign via the phx-change so the form submits properly.
lv
|> element("#test_callsign")
|> render_change(%{"callsign" => "BAD!"})
rendered =
lv
|> form("form[phx-submit=\"test_api\"]", %{"callsign" => "BAD!"})
|> render_submit()
# The handler sends itself a :call_api message; after it returns the
# result assign should contain the invalid-format error JSON.
render(lv)
assert rendered =~ "Test API" or rendered =~ "Testing..."
# Wait for the async message handler to complete.
:ok = Process.sleep(50)
assert render(lv) =~ "Invalid callsign format"
end
test "valid callsign with no packets returns 'No packets found'", %{conn: conn} do
{:ok, lv, _html} = live(conn, ~p"/api", on_error: :warn)
lv
|> element("#test_callsign")
|> render_change(%{"callsign" => "N0CALL-9"})
lv
|> form("form[phx-submit=\"test_api\"]", %{"callsign" => "N0CALL-9"})
|> render_submit()
# Let the async :call_api message run.
:ok = Process.sleep(100)
rendered = render(lv)
# Either "No packets found" (most common in a clean test DB) or a
# packet result. Both exercise the happy path of make_http_request.
assert rendered =~ "No packets found" or rendered =~ "data"
end
test "valid callsign with existing packet returns JSON data", %{conn: conn} do
# Create a packet in the DB so the test_api event actually returns data.
_packet =
packet_fixture(%{
sender: "K5ABC-9",
base_callsign: "K5ABC",
ssid: "9",
received_at: DateTime.utc_now(),
lat: Decimal.new("33.0"),
lon: Decimal.new("-96.5"),
has_position: true,
comment: "API test packet",
temperature: 72.5,
humidity: 50
})
{:ok, lv, _html} = live(conn, ~p"/api", on_error: :warn)
lv
|> element("#test_callsign")
|> render_change(%{"callsign" => "K5ABC-9"})
lv
|> form("form[phx-submit=\"test_api\"]", %{"callsign" => "K5ABC-9"})
|> render_submit()
:ok = Process.sleep(150)
rendered = render(lv)
assert rendered =~ "K5ABC"
assert rendered =~ "data"
end
end
end