Add tests for /api/collection/random endpoint

This commit is contained in:
Claudio Ortolina
2025-01-01 20:57:01 +00:00
parent d194a54760
commit 65899b8077
2 changed files with 38 additions and 0 deletions
+11
View File
@@ -96,4 +96,15 @@ defmodule MusicLibrary.CollectionTest do
assert expected_record.id == most_recent_purchase.id
end
end
describe "get_random_record!/0" do
setup [:fill_collection]
test "returns a random record", %{collection: collection} do
random_record = Collection.get_latest_record!()
collection_ids = Enum.map(collection, & &1.id)
assert random_record.id in collection_ids
end
end
end
@@ -37,4 +37,31 @@ defmodule MusicLibraryWeb.CollectionControllerTest do
}
end
end
describe "GET /api/collection/random" do
setup [:create_record]
test "it requires authentication", %{conn: conn} do
conn = get(conn, ~p"/api/collection/latest")
assert conn.status == 401
end
# We're not testing random here - the query is solid enough
test "it returns a random record", %{conn: conn, record: record} do
conn =
conn
|> put_req_header("authorization", "Bearer #{api_token()}")
|> get(~p"/api/collection/random")
assert json_response(conn, 200) == %{
"artists" => ["Steven Wilson"],
"title" => record.title,
"cover_url" =>
"http://localhost:4002/api/covers/#{record.id}?vsn=#{record.cover_hash}",
"thumb_url" =>
"http://localhost:4002/api/covers/#{record.id}?vsn=#{record.cover_hash}&size=480"
}
end
end
end