defmodule CartosnapApp.TripsTest do
use CartosnapApp.DataCase, async: true
import CartosnapApp.AccountsFixtures
import CartosnapApp.PhotosFixtures
alias CartosnapApp.Photos.Photo
alias CartosnapApp.Trips
alias CartosnapApp.Trips.TripInvite
setup do
%{scope: user_scope_fixture(), friend: user_scope_fixture(), stranger: user_scope_fixture()}
end
defp join_trip(scope, owner, trip) do
{:ok, invite} = Trips.create_invite(owner, trip)
{:ok, _} = Trips.accept_invite(scope, invite.token)
end
describe "trips" do
test "create_trip/2 sets the owner and checks the dates", %{scope: scope} do
assert {:ok, trip} =
Trips.create_trip(scope, %{
name: "Maine",
date_start: ~D[2025-07-01],
date_end: ~D[2025-07-04]
})
assert trip.user == scope.user.id
assert {:error, changeset} =
Trips.create_trip(scope, %{
name: "Backwards",
date_start: ~D[2025-07-04],
date_end: ~D[2025-07-01]
})
assert "must be on or after the start" in errors_on(changeset).date_end
end
test "the owner can't be set through attrs", %{scope: scope, friend: friend} do
{:ok, trip} =
Trips.create_trip(scope, %{
name: "Mine",
date_start: ~D[2025-07-01],
date_end: ~D[2025-07-01],
user: friend.user.id
})
assert trip.user == scope.user.id
end
test "are seen by their owner and members only", %{
scope: scope,
friend: friend,
stranger: stranger
} do
trip = trip_fixture(scope)
assert Trips.list_trips(friend) == []
assert_raise Ecto.NoResultsError, fn -> Trips.get_trip!(friend, trip.id) end
join_trip(friend, scope, trip)
assert [%{id: id}] = Trips.list_trips(friend)
assert id == trip.id
assert Trips.member?(friend, trip)
refute Trips.owner?(friend, trip)
refute Trips.member?(stranger, trip)
end
test "only the owner changes or deletes one", %{scope: scope, friend: friend} do
trip = trip_fixture(scope)
join_trip(friend, scope, trip)
assert_raise FunctionClauseError, fn -> Trips.update_trip(friend, trip, %{name: "x"}) end
assert_raise FunctionClauseError, fn -> Trips.delete_trip(friend, trip) end
end
end
describe "the album" do
test "is everyone's photos, filed by their owners", %{scope: scope, friend: friend} do
trip = trip_fixture(scope)
join_trip(friend, scope, trip)
mine = photo_fixture(scope, date: ~U[2025-06-01 00:00:00Z])
theirs = photo_fixture(friend, date: ~U[2025-06-02 00:00:00Z])
Trips.subscribe(trip)
assert {:ok, 1} = Trips.add_photos(scope, trip.id, [mine.id])
assert {:ok, 1} = Trips.add_photos(friend, trip.id, [theirs.id])
assert_receive :album_changed
assert Enum.map(Trips.list_album(trip), & &1.id) == [theirs.id, mine.id]
assert Trips.trip_summaries([trip])[trip.id].count == 2
end
test "you can only file your own photos, once, under trips you can see",
%{scope: scope, friend: friend, stranger: stranger} do
trip = trip_fixture(scope)
join_trip(friend, scope, trip)
mine = photo_fixture(scope)
strangers = photo_fixture(stranger)
assert {:ok, 0} = Trips.add_photos(friend, trip.id, [mine.id])
assert {:ok, 1} = Trips.add_photos(scope, trip.id, [mine.id, strangers.id])
assert {:ok, 0} = Trips.add_photos(scope, trip.id, [mine.id])
assert_raise Ecto.NoResultsError, fn ->
Trips.add_photos(stranger, trip.id, [strangers.id])
end
end
test "a photo can be in several trips", %{scope: scope} do
photo = photo_fixture(scope)
one = trip_fixture(scope)
two = trip_fixture(scope)
{:ok, 1} = Trips.add_photos(scope, one.id, [photo.id])
{:ok, 1} = Trips.add_photos(scope, two.id, [photo.id])
assert Trips.photo_trips(scope, photo) |> Enum.map(& &1.id) |> Enum.sort() ==
Enum.sort([one.id, two.id])
end
test "a photo comes out by its owner's hand or the trip owner's", %{
scope: scope,
friend: friend
} do
trip = trip_fixture(scope)
join_trip(friend, scope, trip)
a = photo_fixture(friend)
b = photo_fixture(friend)
{:ok, 2} = Trips.add_photos(friend, trip.id, [a.id, b.id])
mine = photo_fixture(scope)
{:ok, 1} = Trips.add_photos(scope, trip.id, [mine.id])
assert :ok = Trips.remove_photo(friend, trip, a)
assert :ok = Trips.remove_photo(scope, trip, b)
assert {:error, :unauthorized} = Trips.remove_photo(friend, trip, mine)
assert Enum.map(Trips.list_album(trip), & &1.id) == [mine.id]
end
test "photo changes reach the trip's topic", %{scope: scope} do
trip = trip_fixture(scope)
photo = photo_fixture(scope)
{:ok, 1} = Trips.add_photos(scope, trip.id, [photo.id])
Trips.subscribe(trip)
{:ok, _} = CartosnapApp.Photos.toggle_promoted(scope, photo)
assert_receive {:photo_updated, %Photo{promoted: true}}
end
end
describe "invites" do
test "make one member each, once", %{scope: scope, friend: friend, stranger: stranger} do
trip = trip_fixture(scope)
{:ok, invite} = Trips.create_invite(scope, trip)
assert byte_size(invite.token) >= 40
assert Repo.reload!(invite).token == nil
assert [%{id: id}] = Trips.list_pending_invites(trip)
assert id == invite.id
assert {:ok, %{id: trip_id}} = Trips.accept_invite(friend, invite.token)
assert trip_id == trip.id
assert Trips.list_pending_invites(trip) == []
assert {:error, :used} = Trips.accept_invite(stranger, invite.token)
end
test "someone already in leaves the invite for whoever it was for", %{
scope: scope,
friend: friend
} do
trip = trip_fixture(scope)
{:ok, invite} = Trips.create_invite(scope, trip)
assert {:ok, _} = Trips.accept_invite(scope, invite.token)
assert {:ok, _} = Trips.accept_invite(friend, invite.token)
end
test "expire, can be revoked, and are only the owner's to make", %{
scope: scope,
friend: friend
} do
trip = trip_fixture(scope)
{:ok, invite} = Trips.create_invite(scope, trip)
Repo.update!(
Ecto.Changeset.change(invite,
expires_at: DateTime.add(DateTime.utc_now(:second), -1, :day)
)
)
assert {:error, :expired} = Trips.accept_invite(friend, invite.token)
{:ok, other} = Trips.create_invite(scope, trip)
:ok = Trips.revoke_invite(scope, trip, other.id)
assert {:error, :not_found} = Trips.get_invite(other.token)
assert {:error, :not_found} = Trips.get_invite("nope")
end
test "anyone in the trip can invite, nobody else", %{
scope: scope,
friend: friend,
stranger: stranger
} do
trip = trip_fixture(scope)
assert {:error, :unauthorized} = Trips.create_invite(stranger, trip)
join_trip(friend, scope, trip)
assert {:ok, %TripInvite{}} = Trips.create_invite(friend, trip)
end
test "tokens are stored hashed" do
assert TripInvite.hash("abc") == :crypto.hash(:sha256, "abc")
end
end
describe "invites by address" do
import Swoosh.TestAssertions
defp url(token), do: "https://cartosnap.test/trips/join/#{token}"
# The account fixtures mail login links; start from an empty mailbox.
setup do
flush_emails()
:ok
end
defp flush_emails do
receive do
{:email, _} -> flush_emails()
after
0 -> :ok
end
end
test "are mailed, and not doubled up", %{scope: scope, friend: friend} do
trip = trip_fixture(scope, name: "Maine")
assert {:ok, invite} = Trips.create_invite(scope, trip, " Pal@Example.COM ", &url/1)
assert invite.email == "pal@example.com"
assert_email_sent(fn email ->
assert email.to == [{"", "pal@example.com"}]
assert email.subject =~ "invited you to Maine"
assert email.text_body =~ url(invite.token)
end)
assert {:error, :already_invited} =
Trips.create_invite(scope, trip, "pal@example.com", &url/1)
join_trip(friend, scope, trip)
assert {:error, :already_member} =
Trips.create_invite(scope, trip, String.upcase(friend.user.email), &url/1)
assert {:error, %Ecto.Changeset{}} =
Trips.create_invite(scope, trip, "not an address", &url/1)
end
test "resending makes a new link and mails it", %{scope: scope} do
trip = trip_fixture(scope)
{:ok, invite} = Trips.create_invite(scope, trip, "pal@example.com", &url/1)
assert_email_sent()
{:ok, resent} = Trips.resend_invite(scope, trip, invite.id, &url/1)
assert resent.token != invite.token
assert {:error, :not_found} = Trips.get_invite(invite.token)
assert {:ok, _} = Trips.get_invite(resent.token)
assert_email_sent(text_body: ~r/#{resent.token}/)
end
test "the owner can take back anyone's invite, a member only their own",
%{scope: scope, friend: friend, stranger: stranger} do
trip = trip_fixture(scope)
join_trip(friend, scope, trip)
{:ok, owners} = Trips.create_invite(scope, trip, "a@example.com", &url/1)
{:ok, friends} = Trips.create_invite(friend, trip, "b@example.com", &url/1)
assert {:error, :unauthorized} = Trips.revoke_invite(friend, trip, owners.id)
assert {:error, :unauthorized} = Trips.revoke_invite(stranger, trip, friends.id)
assert :ok = Trips.revoke_invite(friend, trip, friends.id)
assert :ok = Trips.revoke_invite(scope, trip, owners.id)
assert Trips.list_pending_invites(trip) == []
end
test "wait on the invites page, and join from it", %{scope: scope} do
pal = user_fixture(%{email: "pal@example.com"})
flush_emails()
pal_scope = CartosnapApp.Accounts.Scope.for_user(pal)
trip = trip_fixture(scope, name: "Maine")
{:ok, _} = Trips.create_invite(scope, trip, "pal@example.com", &url/1)
assert [%{trip: %{name: "Maine"}} = invite] = Trips.list_my_invites(pal_scope)
assert {:error, :not_found} = Trips.accept_invite_for_me(scope, invite.id)
assert {:ok, %{id: trip_id}} = Trips.accept_invite_for_me(pal_scope, invite.id)
assert trip_id == trip.id
assert Trips.list_my_invites(pal_scope) == []
end
test "an unconfirmed address sees none", %{scope: scope} do
pal = unconfirmed_user_fixture(%{email: "pal@example.com"})
trip = trip_fixture(scope)
{:ok, _} = Trips.create_invite(scope, trip, "pal@example.com", &url/1)
assert Trips.list_my_invites(CartosnapApp.Accounts.Scope.for_user(pal)) == []
end
test "can make an account for a new address, never sign into an existing one", %{
scope: scope,
friend: friend
} do
trip = trip_fixture(scope)
{:ok, invite} = Trips.create_invite(scope, trip, "new@example.com", &url/1)
assert {:ok, user, %{id: trip_id}} = Trips.join_by_invite(invite.token)
assert trip_id == trip.id
assert user.email == "new@example.com"
assert user.confirmed_at
assert Trips.member?(CartosnapApp.Accounts.Scope.for_user(user), trip)
{:ok, existing} = Trips.create_invite(scope, trip, friend.user.email, &url/1)
assert {:error, :has_account} = Trips.join_by_invite(existing.token)
{:ok, link} = Trips.create_invite(scope, trip)
assert {:error, :not_addressed} = Trips.join_by_invite(link.token)
end
test "suggestions are people from your other trips, less those already in or invited",
%{scope: scope, friend: friend, stranger: stranger} do
old = trip_fixture(scope)
join_trip(friend, scope, old)
{:ok, _} = Trips.create_invite(scope, old, "someone@example.com", &url/1)
trip = trip_fixture(scope)
suggestions = Trips.suggested_invitees(scope, trip)
assert Enum.map(suggestions, & &1.email) == [
String.downcase(friend.user.email),
"someone@example.com"
]
refute Enum.any?(suggestions, &(&1.email == String.downcase(stranger.user.email)))
{:ok, _} = Trips.create_invite(scope, trip, friend.user.email, &url/1)
assert Enum.map(Trips.suggested_invitees(scope, trip), & &1.email) == [
"someone@example.com"
]
end
end
describe "members" do
test "list_people/1 is the owner then the members", %{scope: scope, friend: friend} do
trip = trip_fixture(scope)
join_trip(friend, scope, trip)
assert [%{owner?: true, user: owner}, %{owner?: false, user: member}] =
Trips.list_people(trip)
assert {owner.id, member.id} == {scope.user.id, friend.user.id}
end
test "leaving or being removed takes your photos out of the album", %{
scope: scope,
friend: friend
} do
trip = trip_fixture(scope)
join_trip(friend, scope, trip)
theirs = photo_fixture(friend)
{:ok, 1} = Trips.add_photos(friend, trip.id, [theirs.id])
Trips.subscribe(trip)
assert :ok = Trips.leave_trip(friend, trip)
assert_receive {:member_removed, id}
assert id == friend.user.id
assert Trips.list_album(trip) == []
refute Trips.member?(friend, trip)
# The photo itself is still theirs.
assert Repo.reload(theirs)
join_trip(friend, scope, trip)
{:ok, 1} = Trips.add_photos(friend, trip.id, [theirs.id])
assert :ok = Trips.remove_member(scope, trip, friend.user.id)
assert Trips.list_album(trip) == []
end
test "the owner can't leave, and members can't remove anyone", %{scope: scope, friend: friend} do
trip = trip_fixture(scope)
join_trip(friend, scope, trip)
assert {:error, :owner_cannot_leave} = Trips.leave_trip(scope, trip)
assert {:error, :unauthorized} = Trips.remove_member(friend, trip, scope.user.id)
end
end
describe "sharing by link" do
test "opens by its token, keeps its link, and stops", %{scope: scope} do
trip = trip_fixture(scope)
assert Trips.get_shared_trip(nil) == nil
{:ok, shared} = Trips.share_trip(scope, trip)
assert byte_size(shared.share_token) >= 40
assert Trips.get_shared_trip(shared.share_token).id == trip.id
assert {:ok, ^shared} = Trips.share_trip(scope, shared)
Phoenix.PubSub.subscribe(CartosnapApp.PubSub, Trips.share_topic(trip))
{:ok, rotated} = Trips.rotate_share_link(scope, shared)
assert rotated.share_token != shared.share_token
assert Trips.get_shared_trip(shared.share_token) == nil
assert_receive :share_revoked
{:ok, stopped} = Trips.stop_sharing(scope, rotated)
assert stopped.share_token == nil
assert_receive :share_revoked
end
test "only the owner can share", %{scope: scope, friend: friend} do
trip = trip_fixture(scope)
join_trip(friend, scope, trip)
assert_raise FunctionClauseError, fn -> Trips.share_trip(friend, trip) end
end
test "shows everyone's photos oldest first, or just the promoted ones", %{
scope: scope,
friend: friend
} do
trip = trip_fixture(scope)
join_trip(friend, scope, trip)
later = photo_fixture(friend, date: ~U[2025-06-03 00:00:00Z], promoted: true)
earlier = photo_fixture(scope, date: ~U[2025-06-01 00:00:00Z])
{:ok, 1} = Trips.add_photos(friend, trip.id, [later.id])
{:ok, 1} = Trips.add_photos(scope, trip.id, [earlier.id])
assert Enum.map(Trips.list_shared_photos(trip), & &1.id) == [earlier.id, later.id]
{:ok, trip} = Trips.set_share_promoted_only(scope, trip, true)
assert Enum.map(Trips.list_shared_photos(trip), & &1.id) == [later.id]
end
test "deleting a shared trip sends its viewers away", %{scope: scope} do
{:ok, trip} = scope |> trip_fixture() |> then(&Trips.share_trip(scope, &1))
Phoenix.PubSub.subscribe(CartosnapApp.PubSub, Trips.share_topic(trip))
{:ok, _} = Trips.delete_trip(scope, trip)
assert_receive :share_revoked
end
end
describe "public" do
test "is its own thing, apart from the share link", %{scope: scope} do
trip = trip_fixture(scope)
assert Trips.get_public_trip(trip.id) == nil
{:ok, trip} = Trips.set_public(scope, trip, true)
assert trip.public
assert trip.share_token == nil
assert Trips.get_public_trip(trip.id).id == trip.id
# Sharing by link and stopping it leave public alone...
{:ok, trip} = Trips.share_trip(scope, trip)
{:ok, trip} = Trips.stop_sharing(scope, trip)
assert Trips.get_public_trip(trip.id)
# ...and making it private leaves the link alone.
{:ok, trip} = Trips.share_trip(scope, trip)
{:ok, trip} = Trips.set_public(scope, trip, false)
assert Trips.get_shared_trip(trip.share_token)
assert Trips.get_public_trip(trip.id) == nil
end
test "only the owner opens a trip to the public", %{scope: scope, friend: friend} do
trip = trip_fixture(scope)
join_trip(friend, scope, trip)
assert_raise FunctionClauseError, fn -> Trips.set_public(friend, trip, true) end
end
test "the front page lists public trips with what the public see", %{
scope: scope,
friend: friend
} do
shown = trip_fixture(scope, name: "Shown")
join_trip(friend, scope, shown)
a = photo_fixture(scope, promoted: true)
b = photo_fixture(friend)
{:ok, 1} = Trips.add_photos(scope, shown.id, [a.id])
{:ok, 1} = Trips.add_photos(friend, shown.id, [b.id])
{:ok, shown} = Trips.set_public(scope, shown, true)
# Shared by link only, or private: never listed.
linked = trip_fixture(scope, name: "Linked")
{:ok, 1} = Trips.add_photos(scope, linked.id, [a.id])
{:ok, _} = Trips.share_trip(scope, linked)
# Public, but promoted only with nothing promoted: nothing to show.
empty = trip_fixture(scope, name: "Empty")
plain = photo_fixture(scope)
{:ok, 1} = Trips.add_photos(scope, empty.id, [plain.id])
{:ok, empty} = Trips.set_public(scope, empty, true)
{:ok, _} = Trips.set_public_promoted_only(scope, empty, true)
assert [%{trip: %{name: "Shown"}, count: 2, owner: owner}] = Trips.list_public_trips()
assert owner == CartosnapApp.Accounts.display_name(scope.user)
{:ok, _} = Trips.set_public_promoted_only(scope, shown, true)
assert [%{count: 1, photos: [%{id: id}]}] = Trips.list_public_trips()
assert id == a.id
assert Enum.map(Trips.list_public_photos(Trips.get_public_trip(shown.id)), & &1.id) == [
a.id
]
end
test "going private sends the public away, and tells the front page", %{scope: scope} do
{:ok, trip} = scope |> trip_fixture() |> then(&Trips.set_public(scope, &1, true))
Trips.subscribe_public()
Phoenix.PubSub.subscribe(CartosnapApp.PubSub, Trips.public_topic(trip))
Phoenix.PubSub.subscribe(CartosnapApp.PubSub, Trips.share_topic(trip))
{:ok, _} = Trips.set_public(scope, trip, false)
assert_receive :public_trips_changed
assert_receive :share_revoked
# One message: the share room heard nothing.
refute_receive :share_revoked
end
end
end
neiam
/cartosnap_ex
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
19.0 KiB · text
History
44bc4f7