2.9 KiB · text 5af55d9
defmodule GitGud.ProfileRepoTest do
use GitGud.DataCase, async: false
alias GitGud.Accounts
alias GitGud.Organizations
alias GitGud.Repositories
alias GitGud.Repositories.Repository
describe "ensure_profile_repo on registration" do
test "register_user auto-creates the same-name repo" do
{:ok, user} = Accounts.register_user(%{email: "carol@profile.test"})
assert %Repository{
owner_id: uid,
organization_id: nil,
name: "carol",
visibility: "public"
} = Repo.get_by(Repository, owner_id: user.id, name: user.handle)
assert uid == user.id
end
test "calling ensure_profile_repo twice is idempotent" do
{:ok, user} = Accounts.register_user(%{email: "dora@profile.test"})
{:ok, repo1} = Repositories.ensure_profile_repo(user)
{:ok, repo2} = Repositories.ensure_profile_repo(user)
assert repo1.id == repo2.id
end
test "register doesn't fail when profile-repo creation fails" do
# Stuff a tombstone repo with the same name AFTER the user is created
# so the second ensure call sees it as already existing.
{:ok, user} = Accounts.register_user(%{email: "ed@profile.test"})
assert Repo.get_by(Repository, owner_id: user.id, name: user.handle)
end
end
describe "ensure_profile_repo on org creation" do
test "create_organization auto-creates the same-name org repo" do
{:ok, founder} = Accounts.register_user(%{email: "fred@profile.test"})
{:ok, org} = Organizations.create_organization(founder, %{"handle" => "newcoacme"})
assert %Repository{
organization_id: oid,
name: "newcoacme",
visibility: "public"
} = Repo.get_by(Repository, organization_id: org.id, name: org.handle)
assert oid == org.id
end
end
describe "get_profile_readme/2" do
test "returns nil when there's no profile repo" do
{:ok, user} = Accounts.register_user(%{email: "ghost@profile.test"})
# Wipe their auto-created profile repo to simulate the no-repo case.
Repo.delete_all(from r in Repository, where: r.owner_id == ^user.id)
assert Repositories.get_profile_readme(user) == nil
end
test "newly registered users get a seeded README rendered on their profile" do
{:ok, user} = Accounts.register_user(%{email: "harriet@profile.test"})
assert {:ok, html} = Repositories.get_profile_readme(user)
assert html =~ "Hi, I"
assert html =~ "@harriet"
assert html =~ "Profile README"
end
test "newly created orgs get a seeded README" do
{:ok, founder} = Accounts.register_user(%{email: "ivan@profile.test"})
{:ok, org} = Organizations.create_organization(founder, %{"handle" => "ivan-corp"})
assert {:ok, html} = Repositories.get_profile_readme(org)
assert html =~ "ivan-corp"
assert html =~ "Org README"
end
end
end