defmodule CartosnapApp.PhotosTest do
use CartosnapApp.DataCase, async: true
use Oban.Testing, repo: CartosnapApp.Repo
import CartosnapApp.AccountsFixtures
import CartosnapApp.PhotosFixtures
alias CartosnapApp.Photos
alias CartosnapApp.Trips
alias CartosnapApp.Photos.Photo
alias CartosnapApp.Workers.ProcessImage
setup do
%{scope: user_scope_fixture(), other: user_scope_fixture()}
end
describe "scoping" do
test "list_photos/2 only returns the user's own photos", %{scope: scope, other: other} do
mine = photo_fixture(scope)
_theirs = photo_fixture(other)
assert [%Photo{id: id}] = Photos.list_photos(scope)
assert id == mine.id
end
test "get_photo!/2 won't fetch someone else's photo", %{scope: scope, other: other} do
theirs = photo_fixture(other)
assert_raise Ecto.NoResultsError, fn -> Photos.get_photo!(scope, theirs.id) end
end
test "update and delete refuse someone else's photo", %{scope: scope, other: other} do
theirs = photo_fixture(other)
assert_raise FunctionClauseError, fn -> Photos.update_photo(scope, theirs, %{name: "x"}) end
assert_raise FunctionClauseError, fn -> Photos.delete_photo(scope, theirs) end
end
end
describe "get_viewable_photo!/2" do
test "someone else's photo is viewable only through a trip you share", %{
scope: scope,
other: other
} do
theirs = photo_fixture(other)
assert_raise Ecto.NoResultsError, fn -> Photos.get_viewable_photo!(scope, theirs.id) end
trip = trip_fixture(other)
{:ok, 1} = Trips.add_photos(other, trip.id, [theirs.id])
assert_raise Ecto.NoResultsError, fn -> Photos.get_viewable_photo!(scope, theirs.id) end
join_trip(scope, other, trip)
assert Photos.get_viewable_photo!(scope, theirs.id).id == theirs.id
# ...but it's still not theirs to change.
assert_raise Ecto.NoResultsError, fn -> Photos.get_photo!(scope, theirs.id) end
end
end
describe "many at once" do
test "set_promoted/3 changes only the user's own photos", %{scope: scope, other: other} do
a = photo_fixture(scope)
b = photo_fixture(scope, promoted: true)
theirs = photo_fixture(other)
assert {:ok, 1} = Photos.set_promoted(scope, [a.id, b.id, theirs.id], true)
assert Photos.get_photo!(scope, a.id).promoted
refute Repo.reload!(theirs).promoted
assert {:ok, 2} = Photos.set_promoted(scope, [a.id, b.id], false)
end
test "delete_photos/2 deletes only the user's own", %{scope: scope, other: other} do
a = photo_fixture(scope)
theirs = photo_fixture(other)
assert {:ok, 1} = Photos.delete_photos(scope, [a.id, theirs.id])
assert Photos.list_photos(scope) == []
assert Repo.reload(theirs)
end
end
describe "stored positions" do
test "are PostGIS order, x = lon and y = lat", %{scope: scope} do
photo = photo_fixture(scope, lat: 42.3601, lon: -71.0589)
assert %{rows: [[x, y]]} =
Repo.query!(
"SELECT ST_X(geo_point), ST_Y(geo_point) FROM photo_photos WHERE id = $1",
[Ecto.UUID.dump!(photo.id)]
)
assert {x, y} == {-71.0589, 42.3601}
assert Photo.lat_lon(Photos.get_photo!(scope, photo.id)) == {42.3601, -71.0589}
end
test "so geographic distances come out right", %{scope: scope} do
# Boston to New York is about 306 km as the crow flies; with lat and
# lon swapped the same query lands thousands of km off.
boston = photo_fixture(scope, lat: 42.3601, lon: -71.0589)
nyc = photo_fixture(scope, lat: 40.7128, lon: -74.0060)
%{rows: [[metres]]} =
Repo.query!(
"""
SELECT ST_Distance(a.geo_point::geography, b.geo_point::geography)
FROM photo_photos a, photo_photos b WHERE a.id = $1 AND b.id = $2
""",
[Ecto.UUID.dump!(boston.id), Ecto.UUID.dump!(nyc.id)]
)
assert_in_delta metres / 1000, 306, 5
end
end
describe "set_photo_location/4" do
test "pins a photo to a picked place, replacing GPS, and unpins it", %{scope: scope} do
Photos.subscribe(scope)
photo = photo_fixture(scope, lat: 1.0, lon: 2.0)
{:ok, photo} =
Photos.set_photo_location(scope, photo, {42.3467, -71.0972}, "Fenway Park, Boston")
assert Photo.lat_lon(photo) == {42.3467, -71.0972}
assert photo.place_name == "Fenway Park, Boston"
assert_receive {:photo_updated, %Photo{place_name: "Fenway Park, Boston"}}
{:ok, photo} = Photos.set_photo_location(scope, photo, nil)
assert Photo.lat_lon(photo) == nil
assert photo.place_name == nil
end
test "a dropped pin has no place name", %{scope: scope} do
photo = photo_fixture(scope)
{:ok, photo} = Photos.set_photo_location(scope, photo, {10.0, 20.0})
assert Photo.lat_lon(photo) == {10.0, 20.0}
assert photo.place_name == nil
end
test "refuses positions off the Earth and other people's photos", %{
scope: scope,
other: other
} do
photo = photo_fixture(scope)
assert {:error, changeset} = Photos.set_photo_location(scope, photo, {91.0, 0.0})
assert "is not a position on Earth" in errors_on(changeset).geo_point
theirs = photo_fixture(other)
assert_raise FunctionClauseError, fn ->
Photos.set_photo_location(scope, theirs, {1.0, 1.0})
end
end
end
describe "list_photos/2 filters" do
test "newest first, filtered by promoted, mapped and trip", %{scope: scope} do
old =
photo_fixture(scope,
date: ~U[2024-01-01 10:00:00Z],
promoted: true,
lat: 42.37,
lon: -71.11
)
new = photo_fixture(scope, date: ~U[2025-01-01 10:00:00Z])
trip = trip_fixture(scope)
{:ok, _} = Trips.add_photos(scope, trip.id, [new.id])
assert Enum.map(Photos.list_photos(scope), & &1.id) == [new.id, old.id]
assert Enum.map(Photos.list_photos(scope, promoted: true), & &1.id) == [old.id]
assert Enum.map(Photos.list_photos(scope, mapped: true), & &1.id) == [old.id]
assert Enum.map(Photos.list_photos(scope, trip_id: trip.id), & &1.id) == [new.id]
assert Photos.count_photos(scope) == 2
end
end
describe "camera details" do
setup %{scope: scope} do
a = photo_fixture(scope, camera: "Canon EOS R6", f_number: 2.8, iso: 100)
b = photo_fixture(scope, camera: "Canon EOS R6", f_number: 8.0, iso: 100)
c = photo_fixture(scope, camera: "Apple iPhone 15", f_number: 1.8, iso: 100)
photo_fixture(scope)
photo_fixture(user_scope_fixture(), camera: "Canon EOS R6", f_number: 2.8)
%{a: a, b: b, c: c}
end
test "filter photos", %{scope: scope, a: a, b: b} do
ids = fn opts -> scope |> Photos.list_photos(opts) |> Enum.map(& &1.id) |> Enum.sort() end
assert ids.(filters: [camera: "Canon EOS R6"]) == Enum.sort([a.id, b.id])
assert ids.(filters: [camera: "Canon EOS R6", f_number: 2.8]) == [a.id]
assert ids.(filters: [camera: "Canon EOS R6", f_number: 1.8]) == []
end
test "are counted, each narrowed by the other filters", %{scope: scope} do
all = Photos.facet_counts(scope)
assert all.camera == [{"Canon EOS R6", 2}, {"Apple iPhone 15", 1}]
assert all.f_number == [{1.8, 1}, {2.8, 1}, {8.0, 1}]
assert all.iso == [{100, 3}]
assert all.lens == []
canon = Photos.facet_counts(scope, filters: [camera: "Canon EOS R6", f_number: 2.8])
# Each facet ignores its own filter: every Canon aperture is on offer,
# and only the cameras with an f/2.8 photo.
assert canon.camera == [{"Canon EOS R6", 1}]
assert canon.f_number == [{2.8, 1}, {8.0, 1}]
iphone = Photos.facet_counts(scope, filters: [f_number: 1.8])
assert iphone.camera == [{"Apple iPhone 15", 1}]
end
end
describe "neighbors/3" do
test "steps newer and older, and within promoted or a trip", %{scope: scope} do
a = photo_fixture(scope, date: ~U[2025-01-01 00:00:00Z], promoted: true)
b = photo_fixture(scope, date: ~U[2025-01-02 00:00:00Z])
c = photo_fixture(scope, date: ~U[2025-01-03 00:00:00Z], promoted: true)
assert %{newer: %{id: newer}, older: %{id: older}} = Photos.neighbors(scope, b)
assert {newer, older} == {c.id, a.id}
assert %{newer: %{id: c_id}, older: nil} = Photos.neighbors(scope, a, :promoted)
assert c_id == c.id
trip = trip_fixture(scope)
{:ok, _} = Trips.add_photos(scope, trip.id, [a.id])
{:ok, _} = Trips.add_photos(scope, trip.id, [b.id])
assert %{newer: %{id: b_id}, older: nil} = Photos.neighbors(scope, a, trip)
assert b_id == b.id
end
test "photos taken in the same second are still all reachable", %{scope: scope} do
at = ~U[2025-05-05 12:00:00Z]
photos = for _ <- 1..3, do: photo_fixture(scope, date: at)
[first | _] = ordered = Photos.list_photos(scope)
walked =
Stream.unfold(first, fn
nil -> nil
photo -> {photo.id, Photos.neighbors(scope, photo).older}
end)
|> Enum.to_list()
assert walked == Enum.map(ordered, & &1.id)
assert length(walked) == length(photos)
end
end
describe "update_photo/3" do
test "renames and promotes, and broadcasts", %{scope: scope} do
Photos.subscribe(scope)
photo = photo_fixture(scope)
{:ok, photo} = Photos.update_photo(scope, photo, %{name: " Harbor at dusk "})
assert photo.name == "Harbor at dusk"
assert_receive {:photo_updated, %Photo{name: "Harbor at dusk"}}
{:ok, photo} = Photos.toggle_promoted(scope, photo)
assert photo.promoted
end
test "a blank name falls back to the file name", %{scope: scope} do
photo = photo_fixture(scope, name: "Named", filename: "IMG_1.jpg")
{:ok, photo} = Photos.update_photo(scope, photo, %{name: " "})
assert photo.name == nil
assert Photo.title(photo) == "IMG_1.jpg"
end
end
defp join_trip(scope, owner, trip) do
{:ok, invite} = Trips.create_invite(owner, trip)
{:ok, _} = Trips.accept_invite(scope, invite.token)
end
describe "ingest_upload/4" do
setup do
path =
Path.join(System.tmp_dir!(), "cartosnap-test-#{System.unique_integer([:positive])}.png")
{_, 0} = System.cmd("magick", ["-size", "64x48", "gradient:red-blue", path])
on_exit(fn -> File.rm(path) end)
%{path: path}
end
test "stores the original, records the photo and queues resizing", %{scope: scope, path: path} do
Photos.subscribe(scope)
assert {:ok, photo} =
Photos.ingest_upload(scope, path, %{
client_name: "sunset.png",
client_type: "image/png"
})
assert photo.user == scope.user.id
assert photo.filename == "sunset.png"
assert photo.url_original == "#{scope.user.id}/#{photo.id}/orig.png"
assert is_binary(photo.blurhash)
refute Photo.processed?(photo)
assert {:ok, _body} = CartosnapApp.Storage.get(photo.url_original)
assert_enqueued(worker: ProcessImage, args: %{id: photo.id})
assert_receive {:photo_created, %Photo{id: id}}
assert id == photo.id
end
test "files the upload straight into a trip", %{scope: scope, path: path} do
trip = trip_fixture(scope)
Trips.subscribe(trip)
{:ok, photo} =
Photos.ingest_upload(scope, path, %{client_name: "a.png", client_type: "image/png"},
trip_id: trip.id
)
assert [%Photo{id: id}] = Trips.list_album(trip)
assert id == photo.id
assert_receive {:photo_created, %Photo{}}
end
test "won't file into a trip the user can't see", %{scope: scope, other: other, path: path} do
theirs = trip_fixture(other)
assert_raise Ecto.NoResultsError, fn ->
Photos.ingest_upload(scope, path, %{client_name: "a.png", client_type: "image/png"},
trip_id: theirs.id
)
end
assert Photos.list_photos(scope) == []
end
end
end
neiam
/cartosnap_ex
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
11.9 KiB · text
History
44bc4f7