Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into websearch
[akkoma] / test / pleroma / web / activity_pub / mrf / media_proxy_warming_policy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.MediaProxyWarmingPolicy
11
12 import Mock
13
14 @message %{
15 "type" => "Create",
16 "object" => %{
17 "type" => "Note",
18 "content" => "content",
19 "attachment" => [
20 %{"url" => [%{"href" => "http://example.com/image.jpg"}]}
21 ]
22 }
23 }
24
25 setup do: clear_config([:media_proxy, :enabled], true)
26
27 test "it prefetches media proxy URIs" do
28 Tesla.Mock.mock(fn %{method: :get, url: "http://example.com/image.jpg"} ->
29 {:ok, %Tesla.Env{status: 200, body: ""}}
30 end)
31
32 with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do
33 MediaProxyWarmingPolicy.filter(@message)
34
35 assert called(HTTP.get(:_, :_, :_))
36 end
37 end
38
39 test "it does nothing when no attachments are present" do
40 object =
41 @message["object"]
42 |> Map.delete("attachment")
43
44 message =
45 @message
46 |> Map.put("object", object)
47
48 with_mock HTTP, get: fn _, _, _ -> {:ok, []} end do
49 MediaProxyWarmingPolicy.filter(message)
50 refute called(HTTP.get(:_, :_, :_))
51 end
52 end
53 end