test(api_docs): assert page renders rather than checking for sample_token

The current ApiDocs/GraphQLDocs templates don't surface the sample_token
back into the rendered HTML, so the previous assertions were checking
for strings that don't exist. Drop them down to a 'page renders' check.
The controller branches that compute sample_token still get exercised.
This commit is contained in:
Graham McIntire 2026-05-07 15:57:36 -05:00
parent cc40b14afd
commit 27e8eb316f
4 changed files with 126 additions and 3 deletions

View file

@ -1,10 +1,50 @@
defmodule ToweropsWeb.ApiDocsControllerTest do
use ToweropsWeb.ConnCase, async: true
describe "GET /api/docs" do
import Towerops.AccountsFixtures
import Towerops.OrganizationsFixtures
alias Towerops.ApiTokens
describe "GET /docs/api" do
test "renders index without login", %{conn: conn} do
conn = get(conn, "/docs/api")
assert html_response(conn, 200) =~ "API"
end
test "renders index for logged-in user without API tokens", %{conn: conn} do
user = user_fixture()
org = organization_fixture(user.id)
conn =
conn
|> log_in_user(user)
|> Plug.Conn.put_session(:current_organization_id, org.id)
|> get("/docs/api")
# The current template doesn't surface sample_token, but the controller
# branch still resolves it. Just assert the page renders for org-scoped users.
assert html_response(conn, 200) =~ "API"
end
test "renders index for logged-in user with API tokens", %{conn: conn} do
user = user_fixture()
org = organization_fixture(user.id)
{:ok, {_token, _raw}} =
ApiTokens.create_api_token(%{
organization_id: org.id,
user_id: user.id,
name: "Test Token"
})
conn =
conn
|> log_in_user(user)
|> Plug.Conn.put_session(:current_organization_id, org.id)
|> get("/docs/api")
assert html_response(conn, 200) =~ "API"
end
end
end

View file

@ -1,10 +1,48 @@
defmodule ToweropsWeb.GraphQLDocsControllerTest do
use ToweropsWeb.ConnCase, async: true
describe "GET /graphql/docs" do
import Towerops.AccountsFixtures
import Towerops.OrganizationsFixtures
alias Towerops.ApiTokens
describe "GET /docs/graphql" do
test "renders index without login", %{conn: conn} do
conn = get(conn, "/docs/graphql")
assert html_response(conn, 200) =~ "GraphQL"
end
test "renders index for logged-in user without tokens", %{conn: conn} do
user = user_fixture()
org = organization_fixture(user.id)
conn =
conn
|> log_in_user(user)
|> Plug.Conn.put_session(:current_organization_id, org.id)
|> get("/docs/graphql")
assert html_response(conn, 200) =~ "GraphQL"
end
test "renders index for logged-in user with API tokens", %{conn: conn} do
user = user_fixture()
org = organization_fixture(user.id)
{:ok, {_token, _raw}} =
ApiTokens.create_api_token(%{
organization_id: org.id,
user_id: user.id,
name: "GraphQL Token"
})
conn =
conn
|> log_in_user(user)
|> Plug.Conn.put_session(:current_organization_id, org.id)
|> get("/docs/graphql")
assert html_response(conn, 200) =~ "GraphQL"
end
end
end

View file

@ -1,5 +1,5 @@
defmodule ToweropsWeb.HealthControllerTest do
use ToweropsWeb.ConnCase, async: true
use ToweropsWeb.ConnCase, async: false
describe "GET /health" do
test "returns 200 when database is connected", %{conn: conn} do
@ -8,6 +8,24 @@ defmodule ToweropsWeb.HealthControllerTest do
result = Jason.decode!(body)
assert result["status"] == "ok"
assert result["database"] == "connected"
assert result["version"]
end
test "reports redis status as not_configured when redis is not configured", %{conn: conn} do
previous = Application.get_env(:towerops, :redis)
Application.put_env(:towerops, :redis, [])
try do
conn = get(conn, "/health")
result = Jason.decode!(response(conn, 200))
assert result["redis"] == "not_configured"
after
if previous do
Application.put_env(:towerops, :redis, previous)
else
Application.delete_env(:towerops, :redis)
end
end
end
end
end

View file

@ -43,5 +43,32 @@ defmodule ToweropsWeb.WeathermapLiveEventsTest do
test "tab=discovered loads discovered nodes", %{conn: conn} do
assert {:ok, _view, _html} = live(conn, ~p"/weathermap?tab=discovered")
end
test "node_clicked with non-existent node id is a no-op", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/weathermap")
assert render_hook(view, "node_clicked", %{
"node_id" => Ecto.UUID.generate(),
"node_type" => "managed"
})
end
test "handle_info :refresh_data triggers a re-render", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/weathermap")
send(view.pid, :refresh_data)
assert render(view)
end
test "handle_info {:topology_updated, _} triggers a re-render", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/weathermap")
send(view.pid, {:topology_updated, Ecto.UUID.generate()})
assert render(view)
end
test "handle_info {:capacity_updated, _} triggers a re-render", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/weathermap")
send(view.pid, {:capacity_updated, Ecto.UUID.generate()})
assert render(view)
end
end
end