defmodule ToweropsWeb.GraphQLDocsControllerTest do use ToweropsWeb.ConnCase, async: true import Towerops.AccountsFixtures import Towerops.OrganizationsFixtures alias Towerops.Accounts.Scope 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) # /docs/graphql is unscoped — bypass the router and call the controller # directly so we can drive the "scope has organization" branch. 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.GraphQLDocsHTML) |> ToweropsWeb.GraphQLDocsController.index(%{}) body = html_response(conn, 200) assert body =~ "GraphQL" 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: "GraphQL 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.GraphQLDocsHTML) |> ToweropsWeb.GraphQLDocsController.index(%{}) body = html_response(conn, 200) assert body =~ "GraphQL" assert body =~ "use: GraphQL Token" end end end