Files
music_library/test/music_library_web/controllers/cover_controller_test.exs
T
Claudio Ortolina 9f383dfc5b Make db-dependent tests sync
As explained at
https://hexdocs.pm/ecto_sqlite3/Ecto.Adapters.SQLite3.html#module-async-sandbox-testing:

The Ecto SQLite3 adapter does not support async tests when used with
Ecto.Adapters.SQL.Sandbox. This is due to SQLite only allowing up one
write transaction at a time, which often does not work with the Sandbox
approach of wrapping each test in a transaction.
2024-10-10 15:54:22 +01:00

57 lines
1.6 KiB
Elixir

defmodule MusicLibraryWeb.CoverControllerTest do
use MusicLibraryWeb.ConnCase
import MusicLibrary.RecordsFixtures
defp create_record(_) do
%{record: record_fixture()}
end
describe "GET /covers/:record_id" do
setup [:create_record]
test "404s when record doesn't exist", %{conn: conn} do
id = Ecto.UUID.generate()
conn = get(conn, ~p"/covers/#{id}")
assert text_response(conn, 404) == "Not found"
end
test "serves the cover when etag doesn't exist", %{conn: conn, record: record} do
conn = get(conn, ~p"/covers/#{record.id}")
assert conn.status == 200
assert get_resp_header(conn, "content-type") == ["image/jpeg; charset=utf-8"]
assert get_resp_header(conn, "etag") == [record.cover_hash]
assert conn.resp_body == record.cover_data
end
test "serves the cover when etag doesn't match", %{conn: conn, record: record} do
conn =
conn
|> put_req_header("if-none-match", "invalid-etag")
|> get(~p"/covers/#{record.id}")
assert conn.status == 200
assert get_resp_header(conn, "content-type") == ["image/jpeg; charset=utf-8"]
assert get_resp_header(conn, "etag") == [record.cover_hash]
assert conn.resp_body == record.cover_data
end
test "serves a 304 when etag matches", %{conn: conn, record: record} do
conn =
conn
|> put_req_header("if-none-match", record.cover_hash)
|> get(~p"/covers/#{record.id}")
assert conn.status == 304
assert get_resp_header(conn, "content-type") == []
assert get_resp_header(conn, "etag") == []
assert conn.resp_body == <<>>
end
end
end