defmodule ToweropsWeb.Plugs.GraphQLIntrospectionTest do # NOTE: async: false because the "production" describe block mutates the # global `Application.put_env(:towerops, :env, :prod)`. Other async tests # (notably the vendor SNMP profile tests) check this env to decide whether # to short-circuit SNMP calls, so running this concurrently caused # intermittent vendor test failures. See `Towerops.Snmp.Client.phoenix_snmp_disabled/0`. use ToweropsWeb.ConnCase, async: false alias ToweropsWeb.Plugs.GraphQLIntrospection describe "call/2 in production" do setup do Application.put_env(:towerops, :env, :prod) on_exit(fn -> Application.put_env(:towerops, :env, :test) end) :ok end test "blocks introspection queries containing __schema", %{conn: conn} do conn = conn |> Map.put(:body_params, %{"query" => "{ __schema { types { name } } }"}) |> GraphQLIntrospection.call([]) assert conn.halted assert conn.status == 400 assert conn.resp_body =~ "Introspection is disabled" end test "blocks introspection queries containing __type", %{conn: conn} do conn = conn |> Map.put(:body_params, %{"query" => "{ __type(name: \"User\") { name } }"}) |> GraphQLIntrospection.call([]) assert conn.halted assert conn.status == 400 end test "allows normal queries", %{conn: conn} do conn = conn |> Map.put(:body_params, %{"query" => "{ devices { id name } }"}) |> GraphQLIntrospection.call([]) refute conn.halted refute conn.status == 400 end test "allows requests with no body_params", %{conn: conn} do conn = GraphQLIntrospection.call(conn, []) refute conn.halted end test "allows requests with empty query", %{conn: conn} do conn = conn |> Map.put(:body_params, %{"query" => ""}) |> GraphQLIntrospection.call([]) refute conn.halted end end describe "call/2 in non-production" do test "allows introspection queries in test env", %{conn: conn} do conn = conn |> Map.put(:body_params, %{"query" => "{ __schema { types { name } } }"}) |> GraphQLIntrospection.call([]) refute conn.halted end end describe "init/1" do test "passes through opts unchanged" do assert GraphQLIntrospection.init(:some_opts) == :some_opts end end end