From bf176ebd4259b5001bcecf53249a57cdd42ca45e Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 6 Nov 2026 15:47:53 +0100 Subject: [PATCH] show error page puzzle link only for premium users --- lib/chesstrainer/accounts/scope.ex | 9 +++++++++ lib/chesstrainer_web/components/layouts.ex | 5 ++--- .../controllers/error_html.ex | 6 ++++-- .../controllers/error_html/404.html.heex | 1 + .../controllers/error_html/500.html.heex | 1 + .../controllers/page_html/not_found.html.heex | 1 + .../live/endgame_live/train.ex | 6 ++---- .../controllers/error_html_test.exs | 19 ++++++++++++++++--- .../controllers/page_controller_test.exs | 18 +++++++++++++++++- 9 files changed, 53 insertions(+), 13 deletions(-) diff --git a/lib/chesstrainer/accounts/scope.ex b/lib/chesstrainer/accounts/scope.ex index 4970abe..9cd0a09 100644 --- a/lib/chesstrainer/accounts/scope.ex +++ b/lib/chesstrainer/accounts/scope.ex @@ -30,4 +30,13 @@ defmodule Chesstrainer.Accounts.Scope do end def for_user(nil), do: nil + + @doc """ + True when the scope belongs to a premium user. + + `for_user/1` returns `nil` for anonymous visitors, so the catch-all covers + both nil scopes and free plans. + """ + def premium?(%__MODULE__{user: %User{plan: :premium}}), do: true + def premium?(_scope), do: false end diff --git a/lib/chesstrainer_web/components/layouts.ex b/lib/chesstrainer_web/components/layouts.ex index c17cbec..3d797c8 100644 --- a/lib/chesstrainer_web/components/layouts.ex +++ b/lib/chesstrainer_web/components/layouts.ex @@ -6,6 +6,7 @@ defmodule ChesstrainerWeb.Layouts do use ChesstrainerWeb, :html alias Chesstrainer.Accounts.Role + alias Chesstrainer.Accounts.Scope # Embed all files in layouts/* within this module. # The default root.html.heex file contains the HTML @@ -110,7 +111,7 @@ defmodule ChesstrainerWeb.Layouts do defp nav_links(scope) do marketing = - if premium?(scope) do + if Scope.premium?(scope) do [ %{label: "Puzzle Search", href: "/endgames"} ] @@ -147,8 +148,6 @@ defmodule ChesstrainerWeb.Layouts do defp admin?(%{user: %{role: role}}), do: Role.admin?(role) defp admin?(_scope), do: false - defp premium?(scope), do: match?(%{user: %{plan: :premium}}, scope) - # Signed-in users start a puzzle session; visitors browse the public list. defp start_href(%{user: %{}}), do: ~p"/train" defp start_href(_scope), do: ~p"/guest-train" diff --git a/lib/chesstrainer_web/controllers/error_html.ex b/lib/chesstrainer_web/controllers/error_html.ex index 055f452..513ad9e 100644 --- a/lib/chesstrainer_web/controllers/error_html.ex +++ b/lib/chesstrainer_web/controllers/error_html.ex @@ -10,6 +10,8 @@ defmodule ChesstrainerWeb.ErrorHTML do """ use ChesstrainerWeb, :html + alias Chesstrainer.Accounts.Scope + embed_templates "error_html/*" @doc """ @@ -22,7 +24,7 @@ defmodule ChesstrainerWeb.ErrorHTML do attr :title, :string, required: true attr :message, :string, required: true attr :hint, :string, default: nil - attr :puzzles, :boolean, default: true + attr :current_scope, :map, default: nil def error_page(assigns) do ~H""" @@ -36,7 +38,7 @@ defmodule ChesstrainerWeb.ErrorHTML do <.pill_button variant={:primary} href={~p"/"}> Back to home <.icon_arrow /> - <.pill_button :if={@puzzles} variant={:light} href={~p"/endgames"}> + <.pill_button :if={Scope.premium?(@current_scope)} variant={:light} href={~p"/endgames"}> Browse puzzles diff --git a/lib/chesstrainer_web/controllers/error_html/404.html.heex b/lib/chesstrainer_web/controllers/error_html/404.html.heex index d337bea..ec522f4 100644 --- a/lib/chesstrainer_web/controllers/error_html/404.html.heex +++ b/lib/chesstrainer_web/controllers/error_html/404.html.heex @@ -33,6 +33,7 @@ status={404} title="This page isn't on the board" message="The page you were looking for moved, or never existed. Check the address, or head back to the trainer." + current_scope={assigns[:current_scope]} /> diff --git a/lib/chesstrainer_web/controllers/error_html/500.html.heex b/lib/chesstrainer_web/controllers/error_html/500.html.heex index 8a15e9b..74d942f 100644 --- a/lib/chesstrainer_web/controllers/error_html/500.html.heex +++ b/lib/chesstrainer_web/controllers/error_html/500.html.heex @@ -34,6 +34,7 @@ title="Something went wrong" message="An unexpected error stopped this page from loading. It's on us, not you — please try again in a moment." hint={"If it keeps happening, email #{Branding.support_email()}."} + current_scope={assigns[:current_scope]} /> diff --git a/lib/chesstrainer_web/controllers/page_html/not_found.html.heex b/lib/chesstrainer_web/controllers/page_html/not_found.html.heex index 558d065..cc96c1c 100644 --- a/lib/chesstrainer_web/controllers/page_html/not_found.html.heex +++ b/lib/chesstrainer_web/controllers/page_html/not_found.html.heex @@ -4,4 +4,5 @@ status={404} title="This page isn't on the board" message="The page you were looking for moved, or never existed. Check the address, or head back to the trainer." + current_scope={@current_scope} /> diff --git a/lib/chesstrainer_web/live/endgame_live/train.ex b/lib/chesstrainer_web/live/endgame_live/train.ex index 7826511..594a165 100644 --- a/lib/chesstrainer_web/live/endgame_live/train.ex +++ b/lib/chesstrainer_web/live/endgame_live/train.ex @@ -9,6 +9,7 @@ defmodule ChesstrainerWeb.EndgameLive.Train do use ChesstrainerWeb, :live_view + alias Chesstrainer.Accounts.Scope alias Chesstrainer.Puzzles alias ChesstrainerWeb.EndgameLive.PlayCore alias ChesstrainerWeb.PlayComponents @@ -31,7 +32,7 @@ defmodule ChesstrainerWeb.EndgameLive.Train do <.icon name="hero-x-mark" /> Stop session <.button - :if={premium?(@current_scope)} + :if={Scope.premium?(@current_scope)} id="puzzle-search" navigate={~p"/endgames"} > @@ -310,9 +311,6 @@ defmodule ChesstrainerWeb.EndgameLive.Train do %{user | rating: socket.assigns.user_rating || user.rating} end - defp premium?(%{user: %{plan: :premium}}), do: true - defp premium?(_scope), do: false - defp summarise(session, rating) do %{ difficulty: session.difficulty, diff --git a/test/chesstrainer_web/controllers/error_html_test.exs b/test/chesstrainer_web/controllers/error_html_test.exs index 60f20e8..c221af6 100644 --- a/test/chesstrainer_web/controllers/error_html_test.exs +++ b/test/chesstrainer_web/controllers/error_html_test.exs @@ -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 =~ "#{Branding.title("Page not found")}" assert html =~ ~s(class="error-code">404) 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 diff --git a/test/chesstrainer_web/controllers/page_controller_test.exs b/test/chesstrainer_web/controllers/page_controller_test.exs index a859886..48f6d00 100644 --- a/test/chesstrainer_web/controllers/page_controller_test.exs +++ b/test/chesstrainer_web/controllers/page_controller_test.exs @@ -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