Add tests for /api/collection/random endpoint
This commit is contained in:
@@ -96,4 +96,15 @@ defmodule MusicLibrary.CollectionTest do
|
|||||||
assert expected_record.id == most_recent_purchase.id
|
assert expected_record.id == most_recent_purchase.id
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
@@ -37,4 +37,31 @@ defmodule MusicLibraryWeb.CollectionControllerTest do
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user