Adds new tests.

This commit is contained in:
May Tusek 2025-02-14 14:37:41 -08:00
parent c4f483e5ee
commit eaf54d4126
3 changed files with 74 additions and 0 deletions

View file

@ -0,0 +1,31 @@
defmodule PhilomenaWeb.DeactivationControllerTest do
use PhilomenaWeb.ConnCase, async: true
alias Swoosh.Adapters.Local.Storage.Memory
alias Philomena.Users
alias Philomena.Repo
import Philomena.UsersFixtures
setup :register_and_log_in_user
describe "GET /deactivations" do
test "renders the deactive account page", %{conn: conn, user: user} do
conn = get(conn, ~p"/deactivations")
response = html_response(conn, 200)
assert response =~ "<h1>Deactivate Account</h1>"
end
end
describe "DELETE /deactivations" do
test "causes the user to be deactivated", %{conn: conn, user: user} do
conn = delete(conn, ~p"/deactivations")
assert redirected_to(conn) == ~p"/"
conn = get(conn, ~p"/registrations/edit")
assert redirected_to(conn) == ~p"/sessions/new"
assert Memory.all() |> Enum.count() == 1
user = Users.get_user!(user.id)
assert user.deleted_by_user_id == user.id
end
end
end

View file

@ -0,0 +1,37 @@
defmodule PhilomenaWeb.ReactivationControllerTest do
use PhilomenaWeb.ConnCase, async: true
alias Swoosh.Adapters.Local.Storage.Memory
alias Philomena.Users
alias Philomena.Repo
alias Phoenix.Flash
import Philomena.UsersFixtures
setup :register_and_log_in_user
describe "GET /reactivations/:id" do
test "renders the reactivate account page", %{conn: conn, user: user} do
conn = delete(conn, ~p"/deactivations")
conn = get(conn, ~p"/reactivations/pinkie-pie-is-best-pony")
response = html_response(conn, 200)
assert response =~ "<h1>Reactivate Your Account</h1>"
end
end
describe "POST /reactivations/:id" do
test "reactivate account page works", %{conn: conn, user: user} do
conn = delete(conn, ~p"/deactivations")
reactivation_link = Memory.all() |> hd |> extract_reactivation_link_from_email
conn = post(conn, reactivation_link)
assert redirected_to(conn) == ~p"/"
user = Users.get_user!(user.id)
assert user.deleted_by_user_id == nil
end
end
defp extract_reactivation_link_from_email(email = %Swoosh.Email{}) do
Regex.scan(~r/http:\/\/localhost:4002\/reactivations\/.*/, email.text_body) |> hd |> hd
end
end

View file

@ -55,6 +55,12 @@ defmodule PhilomenaWeb.RegistrationControllerTest do
assert response =~ "Settings" assert response =~ "Settings"
end end
test "renders the deactivation section of the settings page", %{conn: conn, user: user} do
conn = get(conn, ~p"/registrations/edit")
response = html_response(conn, 200)
assert response =~ "<h3>Deactivate Account</h3>"
end
test "redirects if user is not logged in" do test "redirects if user is not logged in" do
conn = build_conn() conn = build_conn()
conn = get(conn, ~p"/registrations/edit") conn = get(conn, ~p"/registrations/edit")