6557c3a98101c92dad21b11ccdfb3bc94ca93705
[anni] / test / pleroma / web / activity_pub / mrf / media_proxy_warming_policy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicyTest do
6   use ExUnit.Case
7   use Pleroma.Tests.Helpers
8
9   alias Pleroma.HTTP
10   alias Pleroma.Web.ActivityPub.MRF
11   alias Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy
12
13   import Mock
14
15   @message %{
16     "type" => "Create",
17     "object" => %{
18       "type" => "Note",
19       "content" => "content",
20       "attachment" => [
21         %{"url" => [%{"href" => "http://example.com/image.jpg"}]}
22       ]
23     }
24   }
25
26   @message_with_history %{
27     "type" => "Create",
28     "object" => %{
29       "type" => "Note",
30       "content" => "content",
31       "formerRepresentations" => %{
32         "orderedItems" => [
33           %{
34             "type" => "Note",
35             "content" => "content",
36             "attachment" => [
37               %{"url" => [%{"href" => "http://example.com/image.jpg"}]}
38             ]
39           }
40         ]
41       }
42     }
43   }
44
45   setup do: clear_config([:media_proxy, :enabled], true)
46
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: ""}}
50     end)
51
52     with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do
53       MediaProxyWarmingPolicy.filter(@message)
54
55       assert called(HTTP.get(:_, :_, :_))
56     end
57   end
58
59   test "it does nothing when no attachments are present" do
60     object =
61       @message["object"]
62       |> Map.delete("attachment")
63
64     message =
65       @message
66       |> Map.put("object", object)
67
68     with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do
69       MediaProxyWarmingPolicy.filter(message)
70       refute called(HTTP.get(:_, :_, :_))
71     end
72   end
73
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: ""}}
77     end)
78
79     with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do
80       MRF.filter_one(MediaProxyWarmingPolicy, @message_with_history)
81
82       assert called(HTTP.get(:_, :_, :_))
83     end
84   end
85
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: ""}}
89     end)
90
91     with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do
92       MRF.filter_one(MediaProxyWarmingPolicy, @message_with_history |> Map.put("type", "Update"))
93
94       assert called(HTTP.get(:_, :_, :_))
95     end
96   end
97 end