show error page puzzle link only for premium users

This commit is contained in:
2026-11-06 15:47:53 +01:00
parent 785e403370
commit bf176ebd42
9 changed files with 53 additions and 13 deletions
@@ -1,11 +1,13 @@
defmodule ChesstrainerWeb.ErrorHTMLTest do
use ExUnit.Case, async: true
alias Chesstrainer.Accounts.Scope
alias Chesstrainer.Accounts.User
alias Chesstrainer.Branding
alias ChesstrainerWeb.ErrorHTML
defp render(template) do
Phoenix.Template.render_to_string(ErrorHTML, template, "html", %{current_scope: nil})
defp render(template, scope \\ nil) do
Phoenix.Template.render_to_string(ErrorHTML, template, "html", %{current_scope: scope})
end
test "404 renders a complete document with the site chrome" do
@@ -15,7 +17,6 @@ defmodule ChesstrainerWeb.ErrorHTMLTest do
assert html =~ "<title>#{Branding.title("Page not found")}</title>"
assert html =~ ~s(class="error-code">404</span>)
assert html =~ "The page you were looking for moved"
assert html =~ "Browse puzzles"
assert html =~ ~s(class="site-footer")
assert html =~ "/assets/js/marketing.js"
refute html =~ ~s(name="csrf-token")
@@ -31,6 +32,18 @@ defmodule ChesstrainerWeb.ErrorHTMLTest do
assert html =~ ~s(class="site-footer")
end
test "hides the puzzles link from guests and free users" do
refute render("404") =~ "Browse puzzles"
refute render("404", Scope.for_user(%User{plan: :free})) =~ "Browse puzzles"
end
test "shows the puzzles link to premium users" do
html = render("404", Scope.for_user(%User{plan: :premium}))
assert html =~ "Browse puzzles"
assert html =~ ~s(href="/endgames")
end
test "unknown statuses fall back to the plain status message" do
assert ErrorHTML.render("503.html", %{}) == "Service Unavailable"
end
@@ -271,9 +271,25 @@ defmodule ChesstrainerWeb.PageControllerTest do
assert LazyHTML.text(LazyHTML.query(document, "title")) == Branding.title("Page not found")
assert LazyHTML.text(LazyHTML.query(document, ".error-code")) == "404"
assert html =~ "The page you were looking for moved"
assert html =~ "Browse puzzles"
refute html =~ "Browse puzzles"
assert html =~ ~s(class="site-footer")
assert html =~ "/assets/js/marketing.js"
assert html =~ ~s(name="robots")
end
test "premium users see the puzzles link on the 404 page", %{conn: conn} do
{:ok, user} =
user_fixture()
|> Ecto.Changeset.change(plan: :premium)
|> Chesstrainer.Repo.update()
html =
conn
|> log_in_user(user)
|> get("/this-page-does-not-exist")
|> html_response(404)
assert html =~ "Browse puzzles"
assert html =~ ~s(href="/endgames")
end
end