1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicyTest do
7 use Pleroma.Tests.Helpers
10 alias Pleroma.Web.ActivityPub.MRF
11 alias Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy
19 "content" => "content",
21 %{"url" => [%{"href" => "http://example.com/image.jpg"}]}
26 @message_with_history %{
30 "content" => "content",
31 "formerRepresentations" => %{
35 "content" => "content",
37 %{"url" => [%{"href" => "http://example.com/image.jpg"}]}
45 setup do: clear_config([:media_proxy, :enabled], true)
47 test "it prefetches media proxy URIs" do
48 Tesla.Mock.mock(fn %{method: :get, url: "http://example.com/image.jpg"} ->
49 {:ok, %Tesla.Env{status: 200, body: ""}}
52 with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do
53 MediaProxyWarmingPolicy.filter(@message)
55 assert called(HTTP.get(:_, :_, :_))
59 test "it does nothing when no attachments are present" do
62 |> Map.delete("attachment")
66 |> Map.put("object", object)
68 with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do
69 MediaProxyWarmingPolicy.filter(message)
70 refute called(HTTP.get(:_, :_, :_))
74 test "history-aware" do
75 Tesla.Mock.mock(fn %{method: :get, url: "http://example.com/image.jpg"} ->
76 {:ok, %Tesla.Env{status: 200, body: ""}}
79 with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do
80 MRF.filter_one(MediaProxyWarmingPolicy, @message_with_history)
82 assert called(HTTP.get(:_, :_, :_))
86 test "works with Updates" do
87 Tesla.Mock.mock(fn %{method: :get, url: "http://example.com/image.jpg"} ->
88 {:ok, %Tesla.Env{status: 200, body: ""}}
91 with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do
92 MRF.filter_one(MediaProxyWarmingPolicy, @message_with_history |> Map.put("type", "Update"))
94 assert called(HTTP.get(:_, :_, :_))