mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
Admin panel, login throttling, and Cloudflare edge hardening (#150)
* Document the remote shell for production user management * add sitemap to footer links * remove border under theme selection * Add admin theme and user management (#144) Add /admin/themes CRUD and /admin/users list/detail with role and plan editing, guarded by a shared Policy module. Include last-super-admin and self-role protections plus structured id-only admin logs. Also add admin invitations, confirmation resends and password-reset emails, which work in development through the Swoosh mailbox before SES is configured. * Rate-limit password logins and emit auth events (#142) Add a two-bucket login throttle (5 failures per login, 10 per IP in a 15-minute window) and a shared client-address helper for controllers and LiveViews. Blocked attempts skip the database and bcrypt. Log auth.login.failed and auth.login.throttled warnings with boot-scoped HMAC addresses only, and stop repeated confirmation emails on unconfirmed login attempts. * Harden the production login edge for Cloudflare Document the layered login defence: the app throttle is primary, Caddy must pass Cloudflare's CF-Connecting-IP to the app, and Cloudflare edge rate limiting covers request floods. Update the reference Caddyfile to accept only Cloudflare ranges and reject direct-to-origin traffic, and document the exact Cloudflare dashboard rules. * Document 10-second Cloudflare rate-limit window Cloudflare may only offer a 10-second period. Keep the edge rule at 10 login POSTs per 10 seconds per IP and rely on the app's 15-minute login/address throttle for credential guessing. * Document Cloudflare limits for LiveView registration Registration submits over the shared LiveView socket, so a Cloudflare rule on POST /users/register will not count account creations. Note the app-level throttle/Turnstile options instead. * Document current Cloudflare free-plan edge controls Record the production setup: Cloudflare-only Caddy origins, Bot Fight Mode, and the 10-second login flood rule (10 POSTs per 10 seconds, 10-second block). Clarify that the application's 15-minute throttle remains the real credential lockout. * Auto-refresh Cloudflare CIDRs for Caddy Move the origin allowlist into a generated cloudflare-ranges.caddy imported by Caddy. Add an hourly systemd timer and script that fetch Cloudflare's v4/v6 lists, reject malformed responses, validate the candidate Caddyfile, and reload Caddy only when the ranges change. Restore the previous file on validation or reload failure. * Document Cloudflare CIDR refresher setup and operations Record the installed paths, permissions, timer checks, manual run, update procedure, and the direct-on-VPS setup fallback when the deploy directory is absent.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
defmodule ChesstrainerWeb.Admin.ThemeLive.FormTest do
|
||||
use ChesstrainerWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Chesstrainer.ThemesFixtures
|
||||
|
||||
alias Chesstrainer.Themes
|
||||
|
||||
setup :register_and_log_in_admin
|
||||
|
||||
test "creates a theme", %{conn: conn} do
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/themes/new")
|
||||
|
||||
view
|
||||
|> form("#theme-form", theme: %{name: " Lucena ", category: "endgame"})
|
||||
|> render_submit()
|
||||
|
||||
assert_redirect(view, ~p"/admin/themes")
|
||||
|
||||
assert [%{name: "Lucena", category: :endgame}] = Themes.search("Lucena", "endgame")
|
||||
end
|
||||
|
||||
test "renames and recategorises a theme", %{conn: conn} do
|
||||
theme = theme_fixture(%{name: "Lucena", category: :endgame})
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/themes/#{theme.id}/edit")
|
||||
|
||||
view
|
||||
|> form("#theme-form", theme: %{name: "Lucena", category: "opening"})
|
||||
|> render_submit()
|
||||
|
||||
assert_redirect(view, ~p"/admin/themes")
|
||||
|
||||
updated = Themes.get_theme!(theme.id)
|
||||
assert updated.name == "Lucena"
|
||||
assert updated.category == :opening
|
||||
end
|
||||
|
||||
test "shows the existing duplicate-name validation error", %{conn: conn} do
|
||||
theme_fixture(%{name: "Lucena", category: :endgame})
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/themes/new")
|
||||
|
||||
html =
|
||||
view
|
||||
|> form("#theme-form", theme: %{name: "lucena", category: "endgame"})
|
||||
|> render_submit()
|
||||
|
||||
assert html =~ "already exists"
|
||||
assert length(Themes.list_themes()) == 1
|
||||
end
|
||||
|
||||
test "validates a blank name", %{conn: conn} do
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/themes/new")
|
||||
|
||||
html =
|
||||
view
|
||||
|> form("#theme-form", theme: %{name: "", category: "endgame"})
|
||||
|> render_submit()
|
||||
|
||||
assert html =~ "can't be blank"
|
||||
end
|
||||
|
||||
test "shows the admin sub-navigation", %{conn: conn} do
|
||||
{:ok, _view, html} = live(conn, ~p"/admin/themes/new")
|
||||
|
||||
assert html =~ ~p"/admin/endgames"
|
||||
assert html =~ ~p"/admin/themes"
|
||||
assert html =~ ~p"/admin/users"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,72 @@
|
||||
defmodule ChesstrainerWeb.Admin.ThemeLive.IndexTest do
|
||||
use ChesstrainerWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Chesstrainer.EndgamesFixtures
|
||||
import Chesstrainer.ThemesFixtures
|
||||
|
||||
alias Chesstrainer.Endgames
|
||||
alias Chesstrainer.Repo
|
||||
alias Chesstrainer.Themes
|
||||
|
||||
setup :register_and_log_in_admin
|
||||
|
||||
test "lists themes with their usage count", %{conn: conn} do
|
||||
used_theme = theme_fixture(%{name: "Lucena"})
|
||||
unused_theme = theme_fixture(%{name: "Philidor"})
|
||||
endgame = endgame_fixture()
|
||||
{:ok, _} = Endgames.update_endgame(endgame, %{}, [used_theme])
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/themes")
|
||||
|
||||
assert has_element?(view, "#theme-#{used_theme.id}")
|
||||
assert has_element?(view, "#theme-usage-#{used_theme.id}", "1 endgame")
|
||||
assert has_element?(view, "#theme-usage-#{unused_theme.id}", "0 endgames")
|
||||
end
|
||||
|
||||
test "filters by name and category", %{conn: conn} do
|
||||
theme_fixture(%{name: "Lucena", category: :endgame})
|
||||
theme_fixture(%{name: "Philidor", category: :endgame})
|
||||
theme_fixture(%{name: "Pin", category: :tactic})
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/themes")
|
||||
|
||||
view |> form("#theme-admin-filter-form", %{search: "Luc"}) |> render_change()
|
||||
|
||||
assert has_element?(view, "td", "Lucena")
|
||||
refute has_element?(view, "td", "Philidor")
|
||||
refute has_element?(view, "td", "Pin")
|
||||
|
||||
view |> form("#theme-admin-filter-form", %{search: "", category: "tactic"}) |> render_change()
|
||||
|
||||
assert has_element?(view, "td", "Pin")
|
||||
refute has_element?(view, "td", "Lucena")
|
||||
end
|
||||
|
||||
test "shows an empty state that explains what themes do", %{conn: conn} do
|
||||
{:ok, _view, html} = live(conn, ~p"/admin/themes")
|
||||
|
||||
assert html =~ "No themes yet"
|
||||
assert html =~ "Themes power endgame filtering"
|
||||
end
|
||||
|
||||
test "deleting a theme detaches it from endgames and keeps the endgame", %{conn: conn} do
|
||||
theme = theme_fixture(%{name: "Lucena"})
|
||||
endgame = endgame_fixture()
|
||||
{:ok, _} = Endgames.update_endgame(endgame, %{}, [theme])
|
||||
|
||||
{:ok, view, html} = live(conn, ~p"/admin/themes")
|
||||
|
||||
assert html =~ "Used by 1 endgame"
|
||||
assert html =~ "The endgames will be kept"
|
||||
|
||||
view |> element("#theme-#{theme.id} a", "Delete") |> render_click()
|
||||
|
||||
assert_raise Ecto.NoResultsError, fn -> Themes.get_theme!(theme.id) end
|
||||
|
||||
assert Endgames.get_endgame!(endgame.id)
|
||||
|
||||
endgame = Endgames.get_endgame!(endgame.id) |> Repo.preload(:themes)
|
||||
assert endgame.themes == []
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,77 @@
|
||||
defmodule ChesstrainerWeb.Admin.UserLive.IndexTest do
|
||||
use ChesstrainerWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Chesstrainer.AccountsFixtures
|
||||
|
||||
alias Chesstrainer.Accounts
|
||||
|
||||
setup :register_and_log_in_admin
|
||||
|
||||
test "lists the users an admin may manage", %{conn: conn} do
|
||||
member = user_fixture()
|
||||
moderator = user_with_role(:moderator)
|
||||
other_admin = user_with_role(:admin)
|
||||
super_admin = user_with_role(:super_admin)
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/users")
|
||||
|
||||
assert has_element?(view, "#user-" <> member.id)
|
||||
assert has_element?(view, "#user-" <> moderator.id)
|
||||
refute has_element?(view, "#user-" <> other_admin.id)
|
||||
refute has_element?(view, "#user-" <> super_admin.id)
|
||||
end
|
||||
|
||||
test "searches by email or username", %{conn: conn} do
|
||||
target = user_fixture(%{email: "FindMe@Example.com", username: "searchable_name"})
|
||||
user_fixture()
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/users")
|
||||
|
||||
view |> form("#admin-user-search-form", %{search: "findme"}) |> render_change()
|
||||
|
||||
assert has_element?(view, "#user-" <> target.id)
|
||||
assert has_element?(view, "td", target.email)
|
||||
|
||||
view |> form("#admin-user-search-form", %{search: "searchable"}) |> render_change()
|
||||
assert has_element?(view, "#user-" <> target.id)
|
||||
|
||||
view |> form("#admin-user-search-form", %{search: "nobody"}) |> render_change()
|
||||
assert render(view) =~ "No users match"
|
||||
end
|
||||
|
||||
test "paginates 25 users per page", %{conn: conn} do
|
||||
for _ <- 1..26 do
|
||||
user_fixture()
|
||||
end
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/users")
|
||||
|
||||
assert row_count(view) == 25
|
||||
assert render(view) =~ "Page 1 of 2"
|
||||
|
||||
view |> element("#next-page") |> render_click()
|
||||
|
||||
assert row_count(view) == 1
|
||||
assert render(view) =~ "Page 2 of 2"
|
||||
end
|
||||
|
||||
test "shows the admin navigation and invite action", %{conn: conn} do
|
||||
{:ok, view, html} = live(conn, ~p"/admin/users")
|
||||
|
||||
assert html =~ ~p"/admin/endgames"
|
||||
assert html =~ ~p"/admin/themes"
|
||||
assert html =~ ~p"/admin/users/new"
|
||||
assert has_element?(view, "#admin-user-search-form")
|
||||
end
|
||||
|
||||
defp row_count(view) do
|
||||
document = LazyHTML.from_fragment(render(view))
|
||||
document |> LazyHTML.query("#admin-users tr") |> LazyHTML.to_tree() |> length()
|
||||
end
|
||||
|
||||
defp user_with_role(role) do
|
||||
{:ok, user} = Accounts.update_user_role(user_fixture(), role)
|
||||
user
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,87 @@
|
||||
defmodule ChesstrainerWeb.Admin.UserLive.NewTest do
|
||||
use ChesstrainerWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Chesstrainer.AccountsFixtures
|
||||
import Swoosh.TestAssertions
|
||||
|
||||
alias Chesstrainer.Accounts
|
||||
|
||||
setup :register_and_log_in_admin
|
||||
|
||||
test "invites a new member", %{conn: conn} do
|
||||
email = unique_user_email()
|
||||
username = unique_username()
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/users/new")
|
||||
|
||||
{:ok, _view, html} =
|
||||
view
|
||||
|> form("#invite-user-form", user: %{email: email, username: username, role: "member"})
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, ~p"/admin/users")
|
||||
|
||||
assert html =~ "Invitation sent to #{email}."
|
||||
|
||||
invited = Accounts.get_user_by_email(email)
|
||||
assert invited.username == username
|
||||
assert invited.confirmed_at
|
||||
|
||||
assert_email_sent(to: email, subject: "You're invited to Inner Square Chess")
|
||||
end
|
||||
|
||||
test "does not offer admin roles to an admin", %{conn: conn} do
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/users/new")
|
||||
|
||||
html = render(view)
|
||||
|
||||
assert html =~ "Moderator"
|
||||
refute html =~ ~s(value="admin")
|
||||
refute html =~ ~s(value="super_admin")
|
||||
end
|
||||
|
||||
test "rejects a direct attempt to invite an admin", %{conn: conn} do
|
||||
email = unique_user_email()
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/users/new")
|
||||
|
||||
html =
|
||||
render_click(view, "save", %{
|
||||
"user" => %{
|
||||
"email" => email,
|
||||
"username" => unique_username(),
|
||||
"role" => "admin"
|
||||
}
|
||||
})
|
||||
|
||||
assert html =~ "You are not authorized to assign that role."
|
||||
refute Accounts.get_user_by_email(email)
|
||||
end
|
||||
|
||||
test "shows duplicate email errors", %{conn: conn} do
|
||||
existing = user_fixture()
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/users/new")
|
||||
|
||||
html =
|
||||
view
|
||||
|> form("#invite-user-form",
|
||||
user: %{
|
||||
email: existing.email,
|
||||
username: unique_username(),
|
||||
role: "member"
|
||||
}
|
||||
)
|
||||
|> render_submit()
|
||||
|
||||
assert html =~ "has already been taken"
|
||||
end
|
||||
|
||||
test "shows the admin sub-navigation", %{conn: conn} do
|
||||
{:ok, _view, html} = live(conn, ~p"/admin/users/new")
|
||||
|
||||
assert html =~ ~p"/admin/endgames"
|
||||
assert html =~ ~p"/admin/themes"
|
||||
assert html =~ ~p"/admin/users"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,126 @@
|
||||
defmodule ChesstrainerWeb.Admin.UserLive.ShowTest do
|
||||
use ChesstrainerWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Chesstrainer.AccountsFixtures
|
||||
import Swoosh.TestAssertions
|
||||
|
||||
alias Chesstrainer.Accounts
|
||||
|
||||
setup :register_and_log_in_admin
|
||||
|
||||
test "shows the user's identity and editable fields", %{conn: conn} do
|
||||
member = user_fixture()
|
||||
|
||||
{:ok, view, html} = live(conn, ~p"/admin/users/#{member.id}")
|
||||
|
||||
assert html =~ member.email
|
||||
assert html =~ member.username
|
||||
assert has_element?(view, "#admin-user-form")
|
||||
assert has_element?(view, "#send-password-reset")
|
||||
refute has_element?(view, "#resend-confirmation")
|
||||
end
|
||||
|
||||
test "an admin can change a member's role and plan", %{conn: conn} do
|
||||
member = user_fixture()
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/users/#{member.id}")
|
||||
|
||||
html =
|
||||
view
|
||||
|> form("#admin-user-form", user: %{role: "moderator", plan: "premium"})
|
||||
|> render_submit()
|
||||
|
||||
assert html =~ "User updated successfully"
|
||||
assert Accounts.get_user!(member.id).role == :moderator
|
||||
assert Accounts.get_user!(member.id).plan == :premium
|
||||
end
|
||||
|
||||
test "an admin cannot promote a member through a direct event", %{conn: conn} do
|
||||
member = user_fixture()
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/users/#{member.id}")
|
||||
|
||||
html = render_click(view, "save", %{"user" => %{"role" => "admin"}})
|
||||
|
||||
assert html =~ "You are not authorized to modify this user."
|
||||
assert Accounts.get_user!(member.id).role == :member
|
||||
end
|
||||
|
||||
test "an admin can view themselves read-only but has no editable form", %{
|
||||
conn: conn,
|
||||
user: admin
|
||||
} do
|
||||
{:ok, view, html} = live(conn, ~p"/admin/users/#{admin.id}")
|
||||
|
||||
assert html =~ "You can view this account but not change its role or plan."
|
||||
refute has_element?(view, "#admin-user-form")
|
||||
end
|
||||
|
||||
test "an admin is redirected away from another admin's detail page", %{conn: conn} do
|
||||
other_admin = user_with_role(:admin)
|
||||
|
||||
{:ok, _view, html} =
|
||||
live(conn, ~p"/admin/users/#{other_admin.id}")
|
||||
|> follow_redirect(conn, ~p"/admin/users")
|
||||
|
||||
assert html =~ "You are not authorized to view that user."
|
||||
end
|
||||
|
||||
test "a super admin can change an admin's role and plan" do
|
||||
super_admin = user_with_role(:super_admin)
|
||||
admin = user_with_role(:admin)
|
||||
|
||||
conn = build_conn() |> log_in_user(super_admin)
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/users/#{admin.id}")
|
||||
|
||||
html =
|
||||
view
|
||||
|> form("#admin-user-form", user: %{role: "moderator", plan: "premium"})
|
||||
|> render_submit()
|
||||
|
||||
assert html =~ "User updated successfully"
|
||||
assert Accounts.get_user!(admin.id).role == :moderator
|
||||
assert Accounts.get_user!(admin.id).plan == :premium
|
||||
end
|
||||
|
||||
test "a super admin cannot change their own role, even through a direct event" do
|
||||
super_admin = user_with_role(:super_admin)
|
||||
conn = build_conn() |> log_in_user(super_admin)
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/users/#{super_admin.id}")
|
||||
|
||||
html = render_click(view, "save", %{"user" => %{"role" => "admin"}})
|
||||
|
||||
assert html =~ "You cannot change your own role."
|
||||
assert Accounts.get_user!(super_admin.id).role == :super_admin
|
||||
end
|
||||
|
||||
test "resends a confirmation email to an unconfirmed member", %{conn: conn} do
|
||||
member = unconfirmed_user_fixture()
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/users/#{member.id}")
|
||||
|
||||
html = view |> element("#resend-confirmation") |> render_click()
|
||||
|
||||
assert html =~ "Confirmation email sent."
|
||||
assert_email_sent(to: member.email, subject: "Confirmation instructions")
|
||||
end
|
||||
|
||||
test "sends a password reset email to a member", %{conn: conn} do
|
||||
member = user_fixture()
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/users/#{member.id}")
|
||||
|
||||
html = view |> element("#send-password-reset") |> render_click()
|
||||
|
||||
assert html =~ "Password reset email sent."
|
||||
assert_email_sent(to: member.email, subject: "Reset password instructions")
|
||||
end
|
||||
|
||||
defp user_with_role(role) do
|
||||
{:ok, user} = Accounts.update_user_role(user_fixture(), role)
|
||||
user
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule ChesstrainerWeb.UserLive.LoginTest do
|
||||
use ChesstrainerWeb.ConnCase, async: true
|
||||
|
||||
@moduletag :capture_log
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Chesstrainer.AccountsFixtures
|
||||
|
||||
|
||||
Reference in New Issue
Block a user