2025-02-14 23:37:41 +01:00
|
|
|
defmodule PhilomenaWeb.ReactivationControllerTest do
|
|
|
|
use PhilomenaWeb.ConnCase, async: true
|
|
|
|
|
2025-02-16 02:29:23 +01:00
|
|
|
alias Philomena.Repo
|
|
|
|
alias Philomena.Users.UserToken
|
2025-02-14 23:37:41 +01:00
|
|
|
alias Philomena.Users
|
2025-02-16 02:29:23 +01:00
|
|
|
import Philomena.UsersFixtures
|
2025-02-14 23:37:41 +01:00
|
|
|
|
2025-02-16 02:29:23 +01:00
|
|
|
setup do
|
|
|
|
%{user: deactivated_user_fixture()}
|
|
|
|
end
|
2025-02-14 23:37:41 +01:00
|
|
|
|
2025-02-15 01:03:53 +01:00
|
|
|
@host PhilomenaWeb.Endpoint.config(:url)[:host]
|
|
|
|
@port PhilomenaWeb.Endpoint.config(:http)[:port]
|
|
|
|
|
2025-02-14 23:37:41 +01:00
|
|
|
describe "GET /reactivations/:id" do
|
2025-02-14 23:45:30 +01:00
|
|
|
test "renders the reactivate account page", %{conn: conn} do
|
2025-02-14 23:37:41 +01:00
|
|
|
conn = get(conn, ~p"/reactivations/pinkie-pie-is-best-pony")
|
|
|
|
response = html_response(conn, 200)
|
|
|
|
assert response =~ "<h1>Reactivate Your Account</h1>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-02-15 01:03:53 +01:00
|
|
|
describe "POST /reactivations/" do
|
2025-02-14 23:37:41 +01:00
|
|
|
test "reactivate account page works", %{conn: conn, user: user} do
|
2025-02-16 01:58:45 +01:00
|
|
|
{:ok, email} =
|
|
|
|
Users.deliver_user_reactivation_instructions(user, &url(~p"/reactivations/#{&1}"))
|
|
|
|
|
2025-02-16 02:29:23 +01:00
|
|
|
assert UserToken.user_and_contexts_query(user, ["reactivate"]) |> Repo.exists?()
|
|
|
|
|
2025-02-16 01:58:19 +01:00
|
|
|
{token, url} = extract_reactivation_link_from_email(email)
|
2025-02-15 00:05:53 +01:00
|
|
|
|
2025-02-15 01:03:53 +01:00
|
|
|
assert token != nil
|
|
|
|
assert url != nil
|
|
|
|
|
|
|
|
conn = post(conn, url, %{"token" => token})
|
2025-02-14 23:37:41 +01:00
|
|
|
assert redirected_to(conn) == ~p"/"
|
|
|
|
|
2025-02-16 01:58:19 +01:00
|
|
|
user = Users.get_user!(user.id)
|
|
|
|
assert user.deleted_by_user_id == nil
|
2025-02-16 02:29:23 +01:00
|
|
|
|
|
|
|
assert not(UserToken.user_and_contexts_query(user, ["reactivate"]) |> Repo.exists?())
|
2025-02-14 23:37:41 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp extract_reactivation_link_from_email(email = %Swoosh.Email{}) do
|
2025-02-15 01:03:53 +01:00
|
|
|
%{"token" => token, "url" => url} =
|
|
|
|
Regex.named_captures(
|
|
|
|
~r/(?<url>http:\/\/#{@host}:#{@port}\/reactivations)\/(?<token>.*)/,
|
|
|
|
email.text_body
|
|
|
|
)
|
|
|
|
|
|
|
|
{token, url}
|
2025-02-14 23:37:41 +01:00
|
|
|
end
|
|
|
|
end
|