ash_hq/test/live/settings_test.exs
2023-01-12 23:52:58 -05:00

122 lines
3.7 KiB
Elixir

defmodule AshHq.SettingsTest do
use AshHqWeb.ConnCase
import Phoenix.ConnTest
import Phoenix.LiveViewTest
@endpoint AshHqWeb.Endpoint
setup :register_and_log_in_user
describe "change email form" do
test "renders", %{conn: conn} do
{:ok, _view, html} = live(conn, "/users/settings")
assert html =~ "Update Email"
end
test "submission shows a flash message", %{conn: conn} do
{:ok, view, _html} = live(conn, "/users/settings")
assert {:ok, _view, html} =
view
|> element("form#update_email")
|> render_submit(%{
update_email: %{email: "new_email@example.com", current_password: "password123"}
})
|> follow_redirect(conn)
assert html =~ "Check your email"
end
test "submission sends an email but does not change the email", %{conn: conn, user: user} do
{:ok, view, _html} = live(conn, "/users/settings")
view
|> element("form#update_email")
|> render_submit(%{
update_email: %{email: "new_email@example.com", current_password: "password123"}
})
assert_received {:email, email}
assert email.subject == "Confirm Your Email Change"
new_user = AshHq.Accounts.reload!(user, authorize?: false)
assert new_user.email == user.email
end
test "following the link in the email changes the email", %{conn: conn, user: user} do
{:ok, view, _html} = live(conn, "/users/settings")
view
|> element("form#update_email")
|> render_submit(%{
update_email: %{email: "new_email@example.com", current_password: "password123"}
})
assert_received {:email, email}
assert %{"url" => url} = Regex.named_captures(~r/(?<url>http[^\s\"]*)/, email.html_body)
uri = URI.parse(url)
path = %{uri | authority: nil, host: nil, scheme: nil, port: nil} |> to_string()
assert {:ok, _conn} = conn |> live(path) |> follow_redirect(conn, "/")
new_user = AshHq.Accounts.reload!(user, authorize?: false)
assert to_string(new_user.email) == "new_email@example.com"
end
end
describe "change_password" do
test "renders", %{conn: conn} do
{:ok, _view, html} = live(conn, "/users/settings")
assert html =~ "Change Password"
end
test "submission shows a flash message", %{conn: conn} do
{:ok, view, _html} = live(conn, "/users/settings")
assert {:ok, _view, html} =
view
|> element("form#change_password")
|> render_submit(%{
change_password: %{
password: "hello world2!",
password_confirmation: "hello world2!",
current_password: "password123"
}
})
|> follow_redirect(conn)
assert html =~ "Password has been successfully changed"
end
test "submission changes the password", %{conn: conn, user: user} do
{:ok, view, _html} = live(conn, "/users/settings")
assert {:ok, _view, html} =
view
|> element("form#change_password")
|> render_submit(%{
change_password: %{
password: "hello world2!",
password_confirmation: "hello world2!",
current_password: "password123"
}
})
|> follow_redirect(conn)
assert html =~ "Password has been successfully changed"
assert AshHq.Accounts.User
|> Ash.Query.for_read(:sign_in_with_password, %{
email: user.email,
password: "hello world2!"
})
|> AshHq.Accounts.read_one!(authorize?: false)
end
end
end