prelaunch env var prevents the site from being operational

This commit is contained in:
2026-11-09 20:49:25 +01:00
parent 9d3dd426db
commit 79782dc778
23 changed files with 510 additions and 98 deletions
@@ -1,5 +1,5 @@
defmodule ChesstrainerWeb.MailingListControllerTest do
use ChesstrainerWeb.ConnCase, async: true
use ChesstrainerWeb.ConnCase, async: false
alias Chesstrainer.MailingList
alias Chesstrainer.MailingList.Subscriber
@@ -169,6 +169,22 @@ defmodule ChesstrainerWeb.MailingListControllerTest do
assert redirected_to(conn) == "/#mailing-list"
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "valid email address"
end
test "rejects signups when the mailing list is disabled", %{conn: conn} do
previous = Application.get_env(:chesstrainer, :mailing_list_enabled)
on_exit(fn -> restore_env(:mailing_list_enabled, previous) end)
Application.put_env(:chesstrainer, :mailing_list_enabled, false)
conn =
post(conn, ~p"/mailing-list",
subscriber: %{email: "player@example.com", form_token: form_token()}
)
assert redirected_to(conn) == "/#mailing-list"
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "isn't open yet"
refute_received {:email, _email}
assert Repo.aggregate(Subscriber, :count) == 0
end
end
describe "GET /mailing-list/confirm/:token" do
@@ -227,4 +243,7 @@ defmodule ChesstrainerWeb.MailingListControllerTest do
defp form_token do
MailingListFormToken.sign(System.system_time(:second) - 5)
end
defp restore_env(key, nil), do: Application.delete_env(:chesstrainer, key)
defp restore_env(key, value), do: Application.put_env(:chesstrainer, key, value)
end
@@ -180,6 +180,29 @@ defmodule ChesstrainerWeb.PageControllerTest do
assert LazyHTML.text(LazyHTML.query(document, ".rating-note strong")) == "0"
end
test "renders the coming soon page while prelaunch is on", %{conn: conn} do
previous = Application.get_env(:chesstrainer, :prelaunch)
on_exit(fn -> restore_env(:prelaunch, previous) end)
Application.put_env(:chesstrainer, :prelaunch, true)
html = conn |> get(~p"/") |> html_response(200)
assert html =~ "coming soon"
assert html =~ "Master the"
refute html =~ "Try today"
end
test "hides the mailing list when it is disabled", %{conn: conn} do
previous = Application.get_env(:chesstrainer, :mailing_list_enabled)
on_exit(fn -> restore_env(:mailing_list_enabled, previous) end)
Application.put_env(:chesstrainer, :mailing_list_enabled, false)
html = conn |> get(~p"/") |> html_response(200)
refute html =~ ~s(id="mailing-list-form")
refute html =~ "Join the list"
end
test "renders the mailing list signup form", %{conn: conn} do
html = conn |> get(~p"/") |> html_response(200)
document = LazyHTML.from_document(html)
@@ -305,4 +328,7 @@ defmodule ChesstrainerWeb.PageControllerTest do
assert html =~ "Browse puzzles"
assert html =~ ~s(href="/endgames")
end
defp restore_env(key, nil), do: Application.delete_env(:chesstrainer, key)
defp restore_env(key, value), do: Application.put_env(:chesstrainer, key, value)
end
@@ -1,5 +1,5 @@
defmodule ChesstrainerWeb.RobotsControllerTest do
use ChesstrainerWeb.ConnCase, async: true
use ChesstrainerWeb.ConnCase, async: false
alias ChesstrainerWeb.Endpoint
@@ -18,5 +18,20 @@ defmodule ChesstrainerWeb.RobotsControllerTest do
assert [content_type] = get_resp_header(conn, "content-type")
assert content_type =~ "text/plain"
end
test "disallows crawlers while prelaunch is on", %{conn: conn} do
previous = Application.get_env(:chesstrainer, :prelaunch)
on_exit(fn -> restore_env(:prelaunch, previous) end)
Application.put_env(:chesstrainer, :prelaunch, true)
body = conn |> get(~p"/robots.txt") |> response(200)
assert body =~ "User-agent: *"
assert body =~ "Disallow: /"
refute body =~ "Allow: /"
end
end
defp restore_env(key, nil), do: Application.delete_env(:chesstrainer, key)
defp restore_env(key, value), do: Application.put_env(:chesstrainer, key, value)
end
@@ -0,0 +1,85 @@
defmodule ChesstrainerWeb.Plugs.PrelaunchTest do
use ChesstrainerWeb.ConnCase, async: false
import Chesstrainer.AccountsFixtures
import Phoenix.LiveViewTest
alias Chesstrainer.Accounts.User
alias Chesstrainer.Repo
setup do
previous = Application.get_env(:chesstrainer, :prelaunch)
on_exit(fn -> restore_env(:prelaunch, previous) end)
Application.put_env(:chesstrainer, :prelaunch, true)
:ok
end
test "allows the public pages, legal pages, health, and admin login", %{conn: conn} do
assert conn |> get(~p"/") |> html_response(200)
assert conn |> get(~p"/privacy") |> html_response(200)
assert conn |> get(~p"/terms-conditions") |> html_response(200)
assert conn |> get(~p"/health") |> response(200)
assert conn |> get(~p"/admin/log-in") |> html_response(200)
end
test "hides public auth links and start training", %{conn: conn} do
html = conn |> get(~p"/") |> html_response(200)
refute html =~ ">Log in<"
refute html =~ ">Register<"
refute html =~ "Start training"
end
test "the hidden admin login uses the admin form action", %{conn: conn} do
html = conn |> get(~p"/admin/log-in") |> html_response(200)
assert html =~ ~s(action="/admin/log-in")
refute html =~ "Sign up"
end
test "pauses the public login route", %{conn: conn} do
assert conn |> get(~p"/users/log-in") |> redirected_to() == "/"
end
test "admin login redirects to the admin area", %{conn: conn} do
user =
user_fixture()
|> Ecto.Changeset.change(role: :admin)
|> Repo.update!()
conn =
post(conn, ~p"/admin/log-in",
user: %{email: user.email, password: "hello world!", remember_me: "true"}
)
assert redirected_to(conn) == "/admin/endgames"
end
test "redirects blocked routes to the home page", %{conn: conn} do
conn = get(conn, ~p"/train")
assert redirected_to(conn) == "/"
assert get_resp_header(conn, "x-robots-tag") == ["noindex, nofollow"]
end
test "redirects registration and unknown pages", %{conn: conn} do
assert conn |> get(~p"/users/register") |> redirected_to() == "/"
assert conn |> get("/definitely-not-a-page") |> redirected_to() == "/"
end
test "blocks direct registration LiveView requests", %{conn: conn} do
assert {:error, {:redirect, %{to: "/"}}} = live(conn, ~p"/users/register")
end
test "blocks registration reached through LiveView navigation", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/admin/log-in")
assert {:error, {:redirect, %{to: "/"}}} =
live_redirect(view, to: ~p"/users/register")
assert Repo.aggregate(User, :count) == 0
end
defp restore_env(key, nil), do: Application.delete_env(:chesstrainer, key)
defp restore_env(key, value), do: Application.put_env(:chesstrainer, key, value)
end