Files
chess-endgame-trainer/test/chesstrainer_web/controllers/user_session_controller_test.exs
T
owenrees 624ddab644 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.
2026-11-11 21:27:59 +01:00

262 lines
8.1 KiB
Elixir

defmodule ChesstrainerWeb.UserSessionControllerTest do
use ChesstrainerWeb.ConnCase, async: false
@moduletag :capture_log
import Chesstrainer.AccountsFixtures
import ExUnit.CaptureLog
import Swoosh.TestAssertions
alias ChesstrainerWeb.LoginThrottle
setup do
LoginThrottle.reset()
%{user: user_fixture()}
end
describe "POST /users/log-in - email or username and password" do
test "logs the user in with their email", %{conn: conn, user: user} do
conn =
post(conn, ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => valid_user_password()}
})
assert get_session(conn, :user_token)
assert redirected_to(conn) == ~p"/"
# Now do a logged in request and assert on the menu
conn = get(conn, ~p"/")
response = html_response(conn, 200)
assert response =~ user.username
assert response =~ ~p"/users/profile"
assert response =~ ~p"/users/log-out"
end
test "logs the user in with their username", %{conn: conn, user: user} do
conn =
post(conn, ~p"/users/log-in", %{
"user" => %{"email" => user.username, "password" => valid_user_password()}
})
assert get_session(conn, :user_token)
assert redirected_to(conn) == ~p"/"
end
test "logs the user in with remember me", %{conn: conn, user: user} do
conn =
post(conn, ~p"/users/log-in", %{
"user" => %{
"email" => user.email,
"password" => valid_user_password(),
"remember_me" => "true"
}
})
assert conn.resp_cookies["_chesstrainer_web_user_remember_me"]
assert redirected_to(conn) == ~p"/"
end
test "logs the user in with return to", %{conn: conn, user: user} do
conn =
conn
|> init_test_session(user_return_to: "/foo/bar")
|> post(~p"/users/log-in", %{
"user" => %{
"email" => user.email,
"password" => valid_user_password()
}
})
assert redirected_to(conn) == "/foo/bar"
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Welcome back!"
end
test "redirects to login page with invalid credentials", %{conn: conn, user: user} do
conn =
post(conn, ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => "invalid_password"}
})
assert Phoenix.Flash.get(conn.assigns.flash, :error) ==
"Invalid email, username or password"
assert redirected_to(conn) == ~p"/users/log-in"
end
test "does not log in an unconfirmed user and resends confirmation instructions once", %{
conn: conn
} do
user = unconfirmed_user_fixture()
conn =
post(conn, ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => valid_user_password()}
})
refute get_session(conn, :user_token)
assert Phoenix.Flash.get(conn.assigns.flash, :error) ==
"Invalid email, username or password"
assert redirected_to(conn) == ~p"/users/log-in"
assert_email_sent(subject: "Confirmation instructions")
second =
post(build_conn(), ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => valid_user_password()}
})
refute get_session(second, :user_token)
refute_email_sent()
end
test "blocks further attempts after the login limit is reached", %{
conn: conn,
user: user
} do
for _ <- 1..LoginThrottle.max_per_login() do
blocked =
post(conn, ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => "wrong password"}
})
assert Phoenix.Flash.get(blocked.assigns.flash, :error) ==
"Invalid email, username or password"
end
conn =
post(conn, ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => valid_user_password()}
})
refute get_session(conn, :user_token)
assert Phoenix.Flash.get(conn.assigns.flash, :error) ==
"Too many attempts. Try again in 15 minutes."
assert redirected_to(conn) == ~p"/users/log-in"
end
test "the address bucket blocks spraying many logins", %{conn: conn, user: user} do
for index <- 1..LoginThrottle.max_per_ip() do
blocked =
post(conn, ~p"/users/log-in", %{
"user" => %{
"email" => "unknown#{index}@example.com",
"password" => "wrong password"
}
})
assert Phoenix.Flash.get(blocked.assigns.flash, :error) ==
"Invalid email, username or password"
end
conn =
post(conn, ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => valid_user_password()}
})
refute get_session(conn, :user_token)
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "Too many attempts."
end
test "a successful login clears the login bucket", %{conn: conn, user: user} do
for _ <- 1..(LoginThrottle.max_per_login() - 1) do
post(conn, ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => "wrong password"}
})
end
conn =
post(conn, ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => valid_user_password()}
})
assert get_session(conn, :user_token)
post(build_conn(), ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => "wrong password"}
})
conn =
post(build_conn(), ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => valid_user_password()}
})
assert get_session(conn, :user_token)
end
test "the admin login uses the same throttle", %{conn: conn, user: user} do
for _ <- 1..LoginThrottle.max_per_login() do
post(conn, ~p"/admin/log-in", %{
"user" => %{"email" => user.email, "password" => "wrong password"}
})
end
conn =
post(conn, ~p"/admin/log-in", %{
"user" => %{"email" => user.email, "password" => valid_user_password()}
})
refute get_session(conn, :user_token)
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "Too many attempts."
end
test "log events contain no raw address or password", %{conn: conn, user: user} do
log =
capture_log(
[format: "$message $metadata\n", metadata: [:ip, :reason]],
fn ->
post(conn, ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => "secret-wrong-password"}
})
end
)
assert log =~ "auth.login.failed"
assert log =~ "invalid_credentials"
refute log =~ "127.0.0.1"
refute log =~ "secret-wrong-password"
refute log =~ user.email
end
test "a throttled request logs auth.login.throttled", %{conn: conn, user: user} do
for _ <- 1..LoginThrottle.max_per_login() do
post(conn, ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => "wrong password"}
})
end
log =
capture_log(
[format: "$message $metadata\n", metadata: [:ip, :reason]],
fn ->
post(conn, ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => valid_user_password()}
})
end
)
assert log =~ "auth.login.throttled"
assert log =~ "login"
refute log =~ "127.0.0.1"
end
end
describe "DELETE /users/log-out" do
test "logs the user out", %{conn: conn, user: user} do
conn = conn |> log_in_user(user) |> delete(~p"/users/log-out")
assert redirected_to(conn) == ~p"/"
refute get_session(conn, :user_token)
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Logged out successfully"
end
test "succeeds even if the user is not logged in", %{conn: conn} do
conn = delete(conn, ~p"/users/log-out")
assert redirected_to(conn) == ~p"/"
refute get_session(conn, :user_token)
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "Logged out successfully"
end
end
end