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:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user