mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
Add user profile hub with account, settings and data export (#103)
This commit is contained in:
@@ -161,6 +161,22 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
|
||||
assert Enum.any?(endgames, &(&1.id == second.id))
|
||||
end
|
||||
|
||||
test "keeps next puzzle available while reviewing", %{conn: conn} do
|
||||
{view, endgames} = start_medium_session(conn, 2)
|
||||
first = :sys.get_state(view.pid).socket.assigns.endgame
|
||||
|
||||
solve_current(view)
|
||||
view |> element("button", "Review") |> render_click()
|
||||
|
||||
assert has_element?(view, "#next-puzzle-button")
|
||||
view |> element("#next-puzzle-button") |> render_click()
|
||||
|
||||
second = :sys.get_state(view.pid).socket.assigns.endgame
|
||||
assert second.id != first.id
|
||||
assert Enum.any?(endgames, &(&1.id == second.id))
|
||||
refute :sys.get_state(view.pid).socket.assigns.reviewing
|
||||
end
|
||||
|
||||
test "a retry does not count toward the average again", %{conn: conn} do
|
||||
{view, [_endgame]} = start_medium_session(conn, 1)
|
||||
|
||||
|
||||
+76
-20
@@ -1,23 +1,79 @@
|
||||
defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
defmodule ChesstrainerWeb.UserLive.ProfileTest do
|
||||
use ChesstrainerWeb.ConnCase, async: true
|
||||
|
||||
alias Chesstrainer.Accounts
|
||||
alias Chesstrainer.Accounts.Scope
|
||||
alias Chesstrainer.Attempts
|
||||
import Phoenix.LiveViewTest
|
||||
import Chesstrainer.AccountsFixtures
|
||||
import Chesstrainer.EndgamesFixtures
|
||||
|
||||
describe "Settings page" do
|
||||
test "renders settings page", %{conn: conn} do
|
||||
describe "profile sections" do
|
||||
setup %{conn: conn} do
|
||||
user = user_fixture()
|
||||
%{conn: log_in_user(conn, user), user: user}
|
||||
end
|
||||
|
||||
test "links to the account, settings and data tabs", %{conn: conn} do
|
||||
{:ok, view, _html} = live(conn, ~p"/users/profile")
|
||||
|
||||
assert has_element?(view, "#profile-tabs")
|
||||
assert has_element?(view, ~s{a#profile-tab-account[href="/users/profile"]})
|
||||
assert has_element?(view, ~s{a#profile-tab-settings[href="/users/profile/settings"]})
|
||||
assert has_element?(view, ~s{a#profile-tab-data[href="/users/profile/data"]})
|
||||
end
|
||||
|
||||
test "settings tab shows the current plan", %{conn: conn} do
|
||||
{:ok, view, html} = live(conn, ~p"/users/profile/settings")
|
||||
|
||||
assert has_element?(view, "#membership-settings")
|
||||
assert html =~ "Free"
|
||||
end
|
||||
|
||||
test "data tab shows progress, export links and recent attempts", %{conn: conn, user: user} do
|
||||
endgame = endgame_fixture()
|
||||
{:ok, _attempt} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved)
|
||||
|
||||
{:ok, view, html} = live(conn, ~p"/users/profile/data")
|
||||
|
||||
assert has_element?(view, "#training-stats")
|
||||
assert html =~ "100%"
|
||||
assert html =~ endgame.key
|
||||
assert has_element?(view, ~s{a#export-csv[href="/users/profile/export/csv"]})
|
||||
assert has_element?(view, ~s{a#export-json[href="/users/profile/export/json"]})
|
||||
end
|
||||
|
||||
test "data tab shows an empty state without attempts", %{conn: conn} do
|
||||
{:ok, view, _html} = live(conn, ~p"/users/profile/data")
|
||||
|
||||
assert render(view) =~ "No attempts yet"
|
||||
end
|
||||
|
||||
test "data tab does not require sudo mode", %{conn: conn, user: user} do
|
||||
conn =
|
||||
log_in_user(conn, user,
|
||||
token_authenticated_at: DateTime.add(DateTime.utc_now(:second), -11, :minute)
|
||||
)
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/users/profile/data")
|
||||
|
||||
assert has_element?(view, "#training-stats")
|
||||
end
|
||||
end
|
||||
|
||||
describe "Account tab" do
|
||||
test "renders the account forms", %{conn: conn} do
|
||||
{:ok, _lv, html} =
|
||||
conn
|
||||
|> log_in_user(user_fixture())
|
||||
|> live(~p"/users/settings")
|
||||
|> live(~p"/users/profile")
|
||||
|
||||
assert html =~ "Change Email"
|
||||
assert html =~ "Save Password"
|
||||
end
|
||||
|
||||
test "redirects if user is not logged in", %{conn: conn} do
|
||||
assert {:error, redirect} = live(conn, ~p"/users/settings")
|
||||
assert {:error, redirect} = live(conn, ~p"/users/profile")
|
||||
|
||||
assert {:redirect, %{to: path, flash: flash}} = redirect
|
||||
assert path == ~p"/users/log-in"
|
||||
@@ -30,7 +86,7 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
|> log_in_user(user_fixture(),
|
||||
token_authenticated_at: DateTime.add(DateTime.utc_now(:second), -11, :minute)
|
||||
)
|
||||
|> live(~p"/users/settings")
|
||||
|> live(~p"/users/profile")
|
||||
|> follow_redirect(conn, ~p"/users/log-in")
|
||||
|
||||
assert conn.resp_body =~ "You must re-authenticate to access this page."
|
||||
@@ -46,7 +102,7 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
test "updates the user email", %{conn: conn, user: user} do
|
||||
new_email = unique_user_email()
|
||||
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/settings")
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/profile")
|
||||
|
||||
result =
|
||||
lv
|
||||
@@ -60,7 +116,7 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
end
|
||||
|
||||
test "renders errors with invalid data (phx-change)", %{conn: conn} do
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/settings")
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/profile")
|
||||
|
||||
result =
|
||||
lv
|
||||
@@ -75,7 +131,7 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
end
|
||||
|
||||
test "renders errors with invalid data (phx-submit)", %{conn: conn, user: user} do
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/settings")
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/profile")
|
||||
|
||||
result =
|
||||
lv
|
||||
@@ -98,7 +154,7 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
test "updates the user password", %{conn: conn, user: user} do
|
||||
new_password = valid_user_password()
|
||||
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/settings")
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/profile")
|
||||
|
||||
form =
|
||||
form(lv, "#password_form", %{
|
||||
@@ -113,7 +169,7 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
|
||||
new_password_conn = follow_trigger_action(form, conn)
|
||||
|
||||
assert redirected_to(new_password_conn) == ~p"/users/settings"
|
||||
assert redirected_to(new_password_conn) == ~p"/users/profile"
|
||||
|
||||
assert get_session(new_password_conn, :user_token) != get_session(conn, :user_token)
|
||||
|
||||
@@ -124,7 +180,7 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
end
|
||||
|
||||
test "renders errors with invalid data (phx-change)", %{conn: conn} do
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/settings")
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/profile")
|
||||
|
||||
result =
|
||||
lv
|
||||
@@ -142,7 +198,7 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
end
|
||||
|
||||
test "renders errors with invalid data (phx-submit)", %{conn: conn} do
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/settings")
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/profile")
|
||||
|
||||
result =
|
||||
lv
|
||||
@@ -167,7 +223,7 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
end
|
||||
|
||||
test "renders the permanent deletion warning", %{conn: conn} do
|
||||
{:ok, _lv, html} = live(conn, ~p"/users/settings")
|
||||
{:ok, _lv, html} = live(conn, ~p"/users/profile")
|
||||
|
||||
assert html =~ "Delete account"
|
||||
assert html =~ "permanent and cannot be undone"
|
||||
@@ -175,7 +231,7 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
end
|
||||
|
||||
test "deletes the user and redirects home", %{conn: conn, user: user} do
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/settings")
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/profile")
|
||||
|
||||
assert {:error, {:redirect, %{to: "/"}}} =
|
||||
result = lv |> element("#delete_account_button") |> render_click()
|
||||
@@ -187,11 +243,11 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
end
|
||||
|
||||
test "logs the deleted user out", %{conn: conn, user: user} do
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/settings")
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/profile")
|
||||
|
||||
lv |> element("#delete_account_button") |> render_click()
|
||||
|
||||
assert {:error, redirect} = live(conn, ~p"/users/settings")
|
||||
assert {:error, redirect} = live(conn, ~p"/users/profile")
|
||||
assert {:redirect, %{to: path}} = redirect
|
||||
assert path == ~p"/users/log-in"
|
||||
refute Accounts.get_user_by_email(user.email)
|
||||
@@ -215,7 +271,7 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
{:error, redirect} = live(conn, ~p"/users/settings/confirm-email/#{token}")
|
||||
|
||||
assert {:live_redirect, %{to: path, flash: flash}} = redirect
|
||||
assert path == ~p"/users/settings"
|
||||
assert path == ~p"/users/profile"
|
||||
assert %{"info" => message} = flash
|
||||
assert message == "Email changed successfully."
|
||||
refute Accounts.get_user_by_email(user.email)
|
||||
@@ -224,7 +280,7 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
# use confirm token again
|
||||
{:error, redirect} = live(conn, ~p"/users/settings/confirm-email/#{token}")
|
||||
assert {:live_redirect, %{to: path, flash: flash}} = redirect
|
||||
assert path == ~p"/users/settings"
|
||||
assert path == ~p"/users/profile"
|
||||
assert %{"error" => message} = flash
|
||||
assert message == "Email change link is invalid or it has expired."
|
||||
end
|
||||
@@ -232,7 +288,7 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
||||
test "does not update email with invalid token", %{conn: conn, user: user} do
|
||||
{:error, redirect} = live(conn, ~p"/users/settings/confirm-email/oops")
|
||||
assert {:live_redirect, %{to: path, flash: flash}} = redirect
|
||||
assert path == ~p"/users/settings"
|
||||
assert path == ~p"/users/profile"
|
||||
assert %{"error" => message} = flash
|
||||
assert message == "Email change link is invalid or it has expired."
|
||||
assert Accounts.get_user_by_email(user.email)
|
||||
Reference in New Issue
Block a user