defmodule ToweropsWeb.ApiDocsControllerTest do use ToweropsWeb.ConnCase, async: true import Towerops.AccountsFixtures import Towerops.OrganizationsFixtures alias Towerops.Accounts.Scope 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) # /docs/api is unscoped (no org slug in path), so the browser pipeline # never populates `scope.organization`. Bypass the router and dispatch # the controller action directly with a scope that has organization set. scope = user |> Scope.for_user() |> Scope.put_organization(org) conn = conn |> Phoenix.ConnTest.bypass_through(ToweropsWeb.Router, [:browser]) |> get("/") |> Plug.Conn.assign(:current_scope, scope) |> Phoenix.Controller.put_view(ToweropsWeb.ApiDocsHTML) |> ToweropsWeb.ApiDocsController.index(%{}) body = html_response(conn, 200) assert body =~ "API" assert body =~ "create one in your organization settings" 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" }) scope = user |> Scope.for_user() |> Scope.put_organization(org) conn = conn |> Phoenix.ConnTest.bypass_through(ToweropsWeb.Router, [:browser]) |> get("/") |> Plug.Conn.assign(:current_scope, scope) |> Phoenix.Controller.put_view(ToweropsWeb.ApiDocsHTML) |> ToweropsWeb.ApiDocsController.index(%{}) body = html_response(conn, 200) assert body =~ "API" assert body =~ "use: Test Token" end end end