Updates controllers and tests to be not hardcoded and use show convention.

This commit is contained in:
May Tusek 2025-02-14 16:03:53 -08:00
parent 4ce618f3a8
commit b3492b4459
2 changed files with 18 additions and 7 deletions

View file

@ -3,11 +3,11 @@ defmodule PhilomenaWeb.ReactivationController do
alias Philomena.Users.{User} alias Philomena.Users.{User}
alias Philomena.Users alias Philomena.Users
def show(conn, %{"token" => _}) do def show(conn, %{"id" => _}) do
render(conn, "show.html", title: "Reactivate Your Account") render(conn, "show.html", title: "Reactivate Your Account")
end end
def post(conn, %{"token" => token}) do def create(conn, %{"token" => token}) do
with user = %User{} <- Users.get_user_by_reactivation_token(token) do with user = %User{} <- Users.get_user_by_reactivation_token(token) do
Users.reactivate_user(user) Users.reactivate_user(user)
else else

View file

@ -6,6 +6,9 @@ defmodule PhilomenaWeb.ReactivationControllerTest do
setup :register_and_log_in_user setup :register_and_log_in_user
@host PhilomenaWeb.Endpoint.config(:url)[:host]
@port PhilomenaWeb.Endpoint.config(:http)[:port]
describe "GET /reactivations/:id" do describe "GET /reactivations/:id" do
test "renders the reactivate account page", %{conn: conn} do test "renders the reactivate account page", %{conn: conn} do
conn = delete(conn, ~p"/deactivations") conn = delete(conn, ~p"/deactivations")
@ -15,17 +18,19 @@ defmodule PhilomenaWeb.ReactivationControllerTest do
end end
end end
describe "POST /reactivations/:id" do describe "POST /reactivations/" do
test "reactivate account page works", %{conn: conn, user: user} do test "reactivate account page works", %{conn: conn, user: user} do
conn = delete(conn, ~p"/deactivations") conn = delete(conn, ~p"/deactivations")
reactivation_link = {token, url} =
Memory.all() Memory.all()
|> Enum.find(&(&1.subject == "Reactivation instructions for your account")) |> Enum.find(&(&1.subject == "Reactivation instructions for your account"))
|> extract_reactivation_link_from_email() |> extract_reactivation_link_from_email()
assert reactivation_link != nil assert token != nil
conn = post(conn, reactivation_link) assert url != nil
conn = post(conn, url, %{"token" => token})
assert redirected_to(conn) == ~p"/" assert redirected_to(conn) == ~p"/"
user = Users.get_user!(user.id) user = Users.get_user!(user.id)
@ -34,6 +39,12 @@ defmodule PhilomenaWeb.ReactivationControllerTest do
end end
defp extract_reactivation_link_from_email(email = %Swoosh.Email{}) do defp extract_reactivation_link_from_email(email = %Swoosh.Email{}) do
Regex.scan(~r/http:\/\/localhost:4002\/reactivations\/.*/, email.text_body) |> hd |> hd %{"token" => token, "url" => url} =
Regex.named_captures(
~r/(?<url>http:\/\/#{@host}:#{@port}\/reactivations)\/(?<token>.*)/,
email.text_body
)
{token, url}
end end
end end