mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
Add user profile hub with account, settings and data export (#103)
This commit is contained in:
@@ -59,6 +59,65 @@ defmodule Chesstrainer.AttemptsTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "stats_for_user/1" do
|
||||
test "summarises attempts, outcomes and distinct puzzles" do
|
||||
user = user_fixture()
|
||||
first = endgame_fixture()
|
||||
second = endgame_fixture()
|
||||
|
||||
{:ok, _} = Attempts.record_attempt(Scope.for_user(user), first, :solved)
|
||||
{:ok, _} = Attempts.record_attempt(Scope.for_user(user), first, :failed)
|
||||
{:ok, _} = Attempts.record_attempt(Scope.for_user(user), second, :solved)
|
||||
|
||||
assert %{attempts: 3, solved: 2, failed: 1, endgames: 2} =
|
||||
Attempts.stats_for_user(user.id)
|
||||
end
|
||||
|
||||
test "returns zeroes for a user with no attempts" do
|
||||
user = user_fixture()
|
||||
|
||||
assert %{attempts: 0, solved: 0, failed: 0, endgames: 0} =
|
||||
Attempts.stats_for_user(user.id)
|
||||
end
|
||||
end
|
||||
|
||||
describe "list_attempts_for_user/2" do
|
||||
test "returns the user's attempts newest first with endgames preloaded" do
|
||||
user = user_fixture()
|
||||
first = endgame_fixture(fen: "8/8/3k4/8/8/3K1Q2/8/8 w - - 0 1")
|
||||
second = endgame_fixture(fen: "8/8/3k4/8/8/3K1R2/8/8 w - - 0 1")
|
||||
{:ok, first_attempt} = Attempts.record_attempt(Scope.for_user(user), first, :solved)
|
||||
{:ok, second_attempt} = Attempts.record_attempt(Scope.for_user(user), second, :failed)
|
||||
|
||||
# Attempts within the same second would otherwise tie on `inserted_at`.
|
||||
Repo.update_all(from(a in Attempt, where: a.id == ^first_attempt.id),
|
||||
set: [inserted_at: ~N[2026-01-01 00:00:00]]
|
||||
)
|
||||
|
||||
Repo.update_all(from(a in Attempt, where: a.id == ^second_attempt.id),
|
||||
set: [inserted_at: ~N[2026-01-02 00:00:00]]
|
||||
)
|
||||
|
||||
attempts = Attempts.list_attempts_for_user(user.id)
|
||||
|
||||
assert Enum.map(attempts, & &1.id) == [second_attempt.id, first_attempt.id]
|
||||
assert Enum.all?(attempts, &match?(%Chesstrainer.Endgames.Endgame{}, &1.endgame))
|
||||
end
|
||||
|
||||
test "excludes other users and honours the limit" do
|
||||
user = user_fixture()
|
||||
other = user_fixture()
|
||||
endgame = endgame_fixture()
|
||||
|
||||
{:ok, _} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved)
|
||||
{:ok, _} = Attempts.record_attempt(Scope.for_user(user), endgame, :failed)
|
||||
{:ok, _} = Attempts.record_attempt(Scope.for_user(other), endgame, :solved)
|
||||
|
||||
assert length(Attempts.list_attempts_for_user(user.id)) == 2
|
||||
assert length(Attempts.list_attempts_for_user(user.id, limit: 1)) == 1
|
||||
end
|
||||
end
|
||||
|
||||
describe "cascade" do
|
||||
test "deleting a user removes their attempts" do
|
||||
user = user_fixture()
|
||||
|
||||
@@ -3,8 +3,33 @@ defmodule ChesstrainerWeb.LayoutsTest do
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
|
||||
alias Chesstrainer.Accounts.Scope
|
||||
alias Chesstrainer.Accounts.User
|
||||
alias ChesstrainerWeb.Layouts
|
||||
|
||||
describe "navbar/1" do
|
||||
test "signed-in users get a profile menu" do
|
||||
scope = Scope.for_user(%User{username: "alice"})
|
||||
|
||||
html = render_component(&Layouts.navbar/1, %{current_scope: scope})
|
||||
|
||||
assert html =~ "alice"
|
||||
assert html =~ ~s(class="nav-user-menu")
|
||||
assert html =~ ~p"/users/profile"
|
||||
assert html =~ ~p"/users/profile/settings"
|
||||
assert html =~ ~p"/users/profile/data"
|
||||
assert html =~ ~p"/users/log-out"
|
||||
end
|
||||
|
||||
test "guests get log in and register links" do
|
||||
html = render_component(&Layouts.navbar/1, %{current_scope: nil})
|
||||
|
||||
assert html =~ ~p"/users/log-in"
|
||||
assert html =~ ~p"/users/register"
|
||||
refute html =~ ~s(class="nav-user-menu")
|
||||
end
|
||||
end
|
||||
|
||||
describe "colour_mode_toggle/1" do
|
||||
test "renders all three modes as a labelled radiogroup" do
|
||||
html = render_component(&Layouts.colour_mode_toggle/1, %{})
|
||||
|
||||
@@ -174,17 +174,17 @@ defmodule ChesstrainerWeb.PageControllerTest do
|
||||
|
||||
assert html =~ ~p"/users/log-in"
|
||||
assert html =~ ~p"/users/register"
|
||||
refute html =~ ~p"/users/settings"
|
||||
refute html =~ ~p"/users/profile"
|
||||
end
|
||||
|
||||
test "signed-in navbar links to settings and log out", %{conn: conn} do
|
||||
test "signed-in navbar links to the profile and log out", %{conn: conn} do
|
||||
html =
|
||||
conn
|
||||
|> log_in_user(user_fixture())
|
||||
|> get(~p"/")
|
||||
|> html_response(200)
|
||||
|
||||
assert html =~ ~p"/users/settings"
|
||||
assert html =~ ~p"/users/profile"
|
||||
assert html =~ ~p"/users/log-out"
|
||||
refute html =~ ~p"/users/register"
|
||||
end
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
defmodule ChesstrainerWeb.UserDataControllerTest do
|
||||
use ChesstrainerWeb.ConnCase, async: true
|
||||
|
||||
import Chesstrainer.AccountsFixtures
|
||||
import Chesstrainer.EndgamesFixtures
|
||||
|
||||
alias Chesstrainer.Accounts.Scope
|
||||
alias Chesstrainer.Attempts
|
||||
|
||||
setup %{conn: conn} do
|
||||
user = user_fixture()
|
||||
endgame = endgame_fixture()
|
||||
{:ok, _attempt} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved)
|
||||
|
||||
%{conn: log_in_user(conn, user), user: user, endgame: endgame}
|
||||
end
|
||||
|
||||
test "redirects anonymous visitors to log in" do
|
||||
conn = build_conn() |> get(~p"/users/profile/export/csv")
|
||||
|
||||
assert redirected_to(conn) == ~p"/users/log-in"
|
||||
end
|
||||
|
||||
test "downloads the user details and attempts as CSV", %{
|
||||
conn: conn,
|
||||
user: user,
|
||||
endgame: endgame
|
||||
} do
|
||||
conn = get(conn, ~p"/users/profile/export/csv")
|
||||
|
||||
assert response(conn, 200) =~
|
||||
"user_username,user_email,user_rating,user_rating_deviation,user_plan,user_joined_at," <>
|
||||
"played_at,puzzle,public_code,color,result,outcome,rated_at"
|
||||
|
||||
assert response(conn, 200) =~ user.username
|
||||
assert response(conn, 200) =~ user.email
|
||||
assert response(conn, 200) =~ to_string(user.rating)
|
||||
assert response(conn, 200) =~ endgame.key
|
||||
assert response(conn, 200) =~ "solved"
|
||||
|
||||
assert get_resp_header(conn, "content-type") == ["text/csv; charset=utf-8"]
|
||||
|
||||
assert get_resp_header(conn, "content-disposition") == [
|
||||
~s(attachment; filename="lucid-chess-data-#{Date.utc_today()}.csv")
|
||||
]
|
||||
end
|
||||
|
||||
test "downloads the user details and attempts as JSON", %{
|
||||
conn: conn,
|
||||
user: user,
|
||||
endgame: endgame
|
||||
} do
|
||||
conn = get(conn, ~p"/users/profile/export/json")
|
||||
|
||||
body = Jason.decode!(response(conn, 200))
|
||||
|
||||
assert %{
|
||||
"user" => %{
|
||||
"username" => username,
|
||||
"email" => email,
|
||||
"rating" => 1500.0,
|
||||
"rating_deviation" => 350.0,
|
||||
"plan" => "free",
|
||||
"joined_at" => joined_at
|
||||
},
|
||||
"attempts" => [
|
||||
%{
|
||||
"puzzle" => puzzle,
|
||||
"public_code" => public_code,
|
||||
"color" => "white",
|
||||
"result" => "win",
|
||||
"outcome" => "solved",
|
||||
"rated_at" => nil
|
||||
}
|
||||
]
|
||||
} = body
|
||||
|
||||
assert username == user.username
|
||||
assert email == user.email
|
||||
assert is_binary(joined_at)
|
||||
assert puzzle == endgame.key
|
||||
assert public_code == endgame.public_code
|
||||
|
||||
assert get_resp_header(conn, "content-type") == ["application/json; charset=utf-8"]
|
||||
end
|
||||
|
||||
test "includes user details when there are no attempts", %{conn: conn} do
|
||||
user = user_fixture()
|
||||
conn = log_in_user(conn, user) |> get(~p"/users/profile/export/csv")
|
||||
|
||||
[header, row] =
|
||||
conn
|
||||
|> response(200)
|
||||
|> String.trim_trailing("\n")
|
||||
|> String.split("\n")
|
||||
|
||||
assert header =~ "user_username"
|
||||
assert row =~ user.username
|
||||
assert row =~ user.email
|
||||
end
|
||||
|
||||
test "only exports the signed-in user's attempts", %{conn: conn} do
|
||||
other = user_fixture()
|
||||
other_endgame = endgame_fixture(fen: "8/8/3k4/8/8/3K1R2/8/8 w - - 0 1")
|
||||
{:ok, _} = Attempts.record_attempt(Scope.for_user(other), other_endgame, :failed)
|
||||
|
||||
body = conn |> get(~p"/users/profile/export/json") |> response(200)
|
||||
|
||||
refute body =~ other_endgame.public_code
|
||||
end
|
||||
end
|
||||
@@ -22,7 +22,7 @@ defmodule ChesstrainerWeb.UserSessionControllerTest do
|
||||
conn = get(conn, ~p"/")
|
||||
response = html_response(conn, 200)
|
||||
assert response =~ user.username
|
||||
assert response =~ ~p"/users/settings"
|
||||
assert response =~ ~p"/users/profile"
|
||||
assert response =~ ~p"/users/log-out"
|
||||
end
|
||||
|
||||
|
||||
@@ -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)
|
||||
@@ -74,13 +74,13 @@ defmodule ChesstrainerWeb.UserAuthTest do
|
||||
assert max_age == @remember_me_cookie_max_age
|
||||
end
|
||||
|
||||
test "redirects to settings when user is already logged in", %{conn: conn, user: user} do
|
||||
test "redirects to the profile when user is already logged in", %{conn: conn, user: user} do
|
||||
conn =
|
||||
conn
|
||||
|> assign(:current_scope, Scope.for_user(user))
|
||||
|> UserAuth.log_in_user(user)
|
||||
|
||||
assert redirected_to(conn) == ~p"/users/settings"
|
||||
assert redirected_to(conn) == ~p"/users/profile"
|
||||
end
|
||||
|
||||
test "writes a cookie if remember_me was set in previous session", %{conn: conn, user: user} do
|
||||
|
||||
Reference in New Issue
Block a user