diff --git a/test/mv_web/live/join_live_test.exs b/test/mv_web/live/join_live_test.exs index bee6cf0..4772e48 100644 --- a/test/mv_web/live/join_live_test.exs +++ b/test/mv_web/live/join_live_test.exs @@ -9,12 +9,11 @@ defmodule MvWeb.JoinLiveTest do Honeypot: form param `"website"` (legit-sounding name per best practice; not "honeypot"). Field is hidden via CSS class in app.css (off-screen, no inline styles), type="text". """ - # async: false so LiveView and test share sandbox (submit creates JoinRequest in LiveView process). + # async: false → shared sandbox; all processes (including LiveView) share the DB connection. use MvWeb.ConnCase, async: false import Phoenix.LiveViewTest import Ecto.Query - alias Ecto.Adapters.SQL.Sandbox alias Mv.Membership alias Mv.Repo @@ -35,20 +34,18 @@ defmodule MvWeb.JoinLiveTest do end describe "submit join form" do - setup :enable_join_form_for_test + setup context do + reset_rate_limiter() + enable_join_form_for_test(context) + end @tag role: :unauthenticated test "submit with valid allowlist data creates one JoinRequest and shows success copy", %{ conn: conn } do - # Re-apply allowlist so this test is robust when run in parallel with others (Settings singleton). - enable_join_form_for_test(%{}) count_before = count_join_requests() {:ok, view, _html} = live(conn, "/join") - # Allow LiveView process to use the same Ecto sandbox so the test sees the created JoinRequest. - Sandbox.allow(Repo, conn.private[:ecto_sandbox], view.pid) - view |> form("#join-form", %{ "email" => "newuser#{System.unique_integer([:positive])}@example.com", @@ -92,23 +89,6 @@ defmodule MvWeb.JoinLiveTest do test "after rate limit exceeded submit returns 429 or error and no new JoinRequest", %{ conn: conn } do - # Reset rate limit state so this test is independent of others (same key in test) - try do - :ets.delete_all_objects(MvWeb.JoinRateLimit) - rescue - ArgumentError -> :ok - end - - enable_join_form(true) - # Set allowlist so form has email, first_name, last_name - {:ok, settings} = Membership.get_settings() - - Membership.update_settings(settings, %{ - join_form_field_ids: ["email", "first_name", "last_name"], - join_form_field_required: %{"email" => true, "first_name" => false, "last_name" => false} - }) - - # Rely on test config: join rate limit low (e.g. 2 per window) base_email = "ratelimit#{System.unique_integer([:positive])}@example.com" count_before = count_join_requests() sandbox = conn.private[:ecto_sandbox] @@ -176,4 +156,10 @@ defmodule MvWeb.JoinLiveTest do defp count_join_requests do Repo.one(from j in "join_requests", select: count(j.id)) || 0 end + + defp reset_rate_limiter do + :ets.delete_all_objects(MvWeb.JoinRateLimit) + rescue + ArgumentError -> :ok + end end