RichMedia: Add StatusView backfill streaming tests

This commit is contained in:
Phantasm 2026-05-23 19:43:44 +02:00
commit 6ee40cb2eb
No known key found for this signature in database
GPG key ID: 2669E588BCC634C8

View file

@ -8,6 +8,7 @@ defmodule Pleroma.Web.RichMedia.BackfillTest do
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.RichMedia.Backfill
alias Pleroma.Web.RichMedia.Card
alias Pleroma.Tests.ObanHelpers
import Mox
import Pleroma.Factory
@ -15,6 +16,7 @@ defmodule Pleroma.Web.RichMedia.BackfillTest do
setup do
clear_config([:rich_media, :enabled], true)
Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Test.StaticConfig)
Mox.stub_with(Pleroma.CachexMock, Pleroma.NullCache)
:ok
@ -99,4 +101,64 @@ defmodule Pleroma.Web.RichMedia.BackfillTest do
Backfill.run(%{"url" => url, "activity_id" => "#{activity.data["id"]}", "stream" => false})
end
# NOTE: Below two MastoAPI tests cover almost the same code paths.
# index.json will always prefetch rich media, while show.json will try to get the card and
# fetch it when it isn't cached (both use Card.get_by_activity in the end).
# So if index.json doesn't fetch the rich media, show.json will when it renders the post,
# hence why index.json test will only call ActivityPub.stream_out twice,
# if streaming is re-enabled for in both.
test "does not stream out in MastoAPI StatusView index" do
url = "https://example.com"
user = insert(:user)
Tesla.Mock.mock(fn %{url: ^url} ->
{:ok,
%Tesla.Env{
status: 200,
body: "<head><meta name=\"twitter:title\" content=\"Cofe\"></head>"
}}
end)
# CommonAPI federation processing will stream out once as a new post
Pleroma.Web.ActivityPub.ActivityPubMock
|> expect(:stream_out, 1, fn _ -> :ok end)
{:ok, activity} = CommonAPI.post(user, %{status: "#cofe #{url}"})
ObanHelpers.perform_all()
# Clear cache to force backfill below
Pleroma.Activity.HTML.invalidate_cache_for(activity.id)
Pleroma.Web.RichMedia.Card.delete(url)
Pleroma.Web.MastodonAPI.StatusView.render("index.json", %{activities: [activity], as: :activity})
ObanHelpers.perform_all()
end
test "does not stream out in MastoAPI StatusView show" do
url = "https://example.com"
user = insert(:user)
Tesla.Mock.mock(fn %{url: ^url} ->
{:ok,
%Tesla.Env{
status: 200,
body: "<head><meta name=\"twitter:title\" content=\"Cofe\"></head>"
}}
end)
# CommonAPI federation processing will stream out once as a new post
Pleroma.Web.ActivityPub.ActivityPubMock
|> expect(:stream_out, 1, fn _ -> :ok end)
{:ok, activity} = CommonAPI.post(user, %{status: "#cofe #{url}"})
ObanHelpers.perform_all()
# Clear cache to force backfill below
Pleroma.Activity.HTML.invalidate_cache_for(activity.id)
Pleroma.Web.RichMedia.Card.delete(url)
Pleroma.Web.MastodonAPI.StatusView.render("show.json", activity: activity)
ObanHelpers.perform_all()
end
end