defmodule CartosnapAppWeb.Api.V1Test do
use CartosnapAppWeb.ConnCase, async: true
import CartosnapApp.AccountsFixtures
import CartosnapApp.PhotosFixtures
alias CartosnapApp.Accounts
alias CartosnapApp.Photos
alias CartosnapApp.Trips
setup do
user = user_fixture()
%{user: user, scope: Accounts.Scope.for_user(user)}
end
defp authed(conn, token), do: put_req_header(conn, "authorization", "Bearer #{token}")
describe "pairing" do
test "trades a code from settings for a key, once", %{conn: conn, user: user} do
code = Accounts.create_pairing_code(user)
conn = post(conn, ~p"/api/v1/pair", %{code: String.downcase(code), label: "Pixel 8"})
assert %{"token" => token, "user" => %{"email" => email}} = json_response(conn, 201)
assert email == user.email
assert [%{label: "Pixel 8"}] = Accounts.list_api_tokens(user)
assert build_conn() |> authed(token) |> get(~p"/api/v1/me") |> json_response(200)
again = post(build_conn(), ~p"/api/v1/pair", %{code: code})
assert %{"error" => %{"code" => "invalid_code"}} = json_response(again, 401)
end
test "a new code cancels the old one", %{conn: conn, user: user} do
old = Accounts.create_pairing_code(user)
_new = Accounts.create_pairing_code(user)
assert conn |> post(~p"/api/v1/pair", %{code: old}) |> json_response(401)
end
end
describe "with a device key" do
setup %{user: user} do
%{token: Accounts.create_api_token(user, "Phone")}
end
test "needs one", %{conn: conn} do
assert %{"error" => %{"code" => "unauthenticated"}} =
conn |> get(~p"/api/v1/me") |> json_response(401)
assert conn |> authed("nope") |> get(~p"/api/v1/trips") |> json_response(401)
end
test "lists the trips you can add to", %{conn: conn, token: token, scope: scope} do
mine = trip_fixture(scope, name: "Maine")
theirs = trip_fixture(user_scope_fixture(), name: "Not mine")
%{"trips" => trips} = conn |> authed(token) |> get(~p"/api/v1/trips") |> json_response(200)
assert [%{"id" => id, "name" => "Maine", "owned" => true, "photo_count" => 0}] = trips
assert id == mine.id
refute Enum.any?(trips, &(&1["id"] == theirs.id))
end
test "names and unpairs the device", %{conn: conn, token: token, user: user} do
assert conn
|> authed(token)
|> put(~p"/api/v1/device", %{label: "Pixel 9"})
|> response(204)
assert [%{label: "Pixel 9"}] = Accounts.list_api_tokens(user)
assert build_conn() |> authed(token) |> delete(~p"/api/v1/device") |> response(204)
assert build_conn() |> authed(token) |> get(~p"/api/v1/me") |> json_response(401)
end
end
describe "uploading" do
setup %{user: user} do
path = Path.join(System.tmp_dir!(), "api-#{System.unique_integer([:positive])}.jpg")
{_, 0} = System.cmd("magick", ["-size", "64x48", "gradient:red-blue", path])
on_exit(fn -> File.rm(path) end)
%{
token: Accounts.create_api_token(user),
upload: %Plug.Upload{path: path, filename: "PXL_1.jpg", content_type: "image/jpeg"}
}
end
test "stores a photo into a trip, and a retry finds it rather than storing it again",
%{conn: conn, token: token, upload: upload, scope: scope} do
trip = trip_fixture(scope)
params = %{photo: upload, client_ref: "ref-1", trip_id: trip.id}
first = conn |> authed(token) |> post(~p"/api/v1/photos", params) |> json_response(201)
assert %{"photo" => %{"id" => id}, "trip_id" => trip_id, "duplicate" => false} = first
assert trip_id == trip.id
assert [%{id: ^id}] = Trips.list_album(trip)
retry = build_conn() |> authed(token) |> post(~p"/api/v1/photos", params)
assert %{"photo" => %{"id" => ^id}, "duplicate" => true} = json_response(retry, 200)
assert Photos.count_photos(scope) == 1
end
test "keeps the photo when the trip is gone", %{conn: conn, token: token, upload: upload} do
gone = trip_fixture(user_scope_fixture())
params = %{photo: upload, client_ref: "ref-2", trip_id: gone.id}
assert %{"trip_id" => nil, "trip_unavailable" => true} =
conn |> authed(token) |> post(~p"/api/v1/photos", params) |> json_response(201)
end
test "refuses what isn't a photo", %{conn: conn, token: token, upload: upload} do
text = %{upload | filename: "notes.txt", content_type: "text/plain"}
assert %{"error" => %{"code" => "unsupported_media_type"}} =
conn
|> authed(token)
|> post(~p"/api/v1/photos", %{photo: text, client_ref: "ref-3"})
|> json_response(415)
assert conn |> authed(token) |> post(~p"/api/v1/photos", %{}) |> json_response(400)
end
end
end
neiam
/cartosnap_ex
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
⭐
Log in to mark this repository.
4.7 KiB · text
History
e569b6d