3.6 KiB · text 5af55d9
defmodule GitGud.ProfileFieldsTest do
use GitGud.DataCase, async: false
alias GitGud.Accounts
alias GitGud.Accounts.User
alias GitGud.Organizations
alias GitGud.Organizations.Organization
import GitGud.AccountsFixtures
describe "user handle" do
test "register seeds handle from email local-part" do
{:ok, u} = Accounts.register_user(%{email: "alice@example.com"})
assert u.handle == "alice"
end
test "duplicate local-part gets a _N suffix" do
{:ok, u1} = Accounts.register_user(%{email: "alice@a.example"})
{:ok, u2} = Accounts.register_user(%{email: "alice@b.example"})
{:ok, u3} = Accounts.register_user(%{email: "alice@c.example"})
assert u1.handle == "alice"
assert u2.handle == "alice_1"
assert u3.handle == "alice_2"
end
test "handles are case-insensitive (citext)" do
{:ok, _} = Accounts.register_user(%{email: "Bob@example.com"})
assert %User{handle: "bob"} = Accounts.get_user_by_handle("BOB")
end
test "get_user_by_handle returns nil when missing" do
refute Accounts.get_user_by_handle("nobody-here")
end
end
describe "profile_changeset" do
test "casts the profile fields, ignores other keys" do
user = user_fixture()
attrs = %{
"display_name" => "Alice Liddell",
"bio" => "Curious",
"url" => "https://alice.example",
"location" => "Wonderland",
"company" => "Q.E. II",
"public_email" => "publicalice@example",
"timezone" => "Europe/London",
"socials" => %{"items" => [%{"platform" => "Fediverse", "url" => "https://fedi/alice"}]},
# Ignored — not in cast list.
"email" => "evil@example"
}
{:ok, updated} = Accounts.update_user_profile(user, attrs)
assert updated.display_name == "Alice Liddell"
assert updated.bio == "Curious"
assert updated.url == "https://alice.example"
assert updated.location == "Wonderland"
assert updated.company == "Q.E. II"
assert updated.public_email == "publicalice@example"
assert updated.timezone == "Europe/London"
assert %{"items" => [%{"platform" => "Fediverse"}]} = updated.socials
assert updated.email == user.email
end
test "rejects fields over length caps" do
user = user_fixture()
attrs = %{"display_name" => String.duplicate("x", 200)}
{:error, cs} = Accounts.update_user_profile(user, attrs)
assert %{display_name: [_]} = errors_on(cs)
end
end
describe "User.display/1" do
test "prefers display_name → handle → email-local" do
assert User.display(%User{display_name: "X", handle: "h", email: "e@e"}) == "X"
assert User.display(%User{handle: "h", email: "e@e"}) == "h"
assert User.display(%User{email: "e@e"}) == "e"
end
end
describe "org profile_changeset" do
test "casts + updates the same fields" do
user = user_fixture()
{:ok, org} = Organizations.create_organization(user, %{"handle" => "acme"})
{:ok, org2} =
Organizations.update_organization_profile(org, %{
"display_name" => "ACME Inc",
"bio" => "We make anvils.",
"location" => "Tucson"
})
assert org2.display_name == "ACME Inc"
assert org2.bio == "We make anvils."
assert org2.location == "Tucson"
end
test "Organization.display/1 falls back chain" do
assert Organization.display(%Organization{display_name: "D", handle: "h"}) == "D"
assert Organization.display(%Organization{handle: "h", name: "N"}) == "N"
assert Organization.display(%Organization{handle: "h"}) == "h"
end
end
end