Merge branch 'ejabberd-pleroma-auth' into 'develop'
[akkoma] / test / pleroma / web / media_proxy / invalidation_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.MediaProxy.InvalidationTest do
6 use ExUnit.Case
7 use Pleroma.Tests.Helpers
8
9 alias Pleroma.Config
10 alias Pleroma.Web.MediaProxy.Invalidation
11
12 import ExUnit.CaptureLog
13 import Mock
14 import Tesla.Mock
15
16 setup do: clear_config([:media_proxy])
17
18 setup do
19 on_exit(fn -> Cachex.clear(:banned_urls_cache) end)
20 end
21
22 describe "Invalidation.Http" do
23 test "perform request to clear cache" do
24 Config.put([:media_proxy, :enabled], false)
25 Config.put([:media_proxy, :invalidation, :enabled], true)
26 Config.put([:media_proxy, :invalidation, :provider], Invalidation.Http)
27
28 Config.put([Invalidation.Http], method: :purge, headers: [{"x-refresh", 1}])
29 image_url = "http://example.com/media/example.jpg"
30 Pleroma.Web.MediaProxy.put_in_banned_urls(image_url)
31
32 mock(fn
33 %{
34 method: :purge,
35 url: "http://example.com/media/example.jpg",
36 headers: [{"x-refresh", 1}]
37 } ->
38 %Tesla.Env{status: 200}
39 end)
40
41 assert capture_log(fn ->
42 assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
43 assert Invalidation.purge([image_url]) == {:ok, [image_url]}
44 assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
45 end) =~ "Running cache purge: [\"#{image_url}\"]"
46 end
47 end
48
49 describe "Invalidation.Script" do
50 test "run script to clear cache" do
51 Config.put([:media_proxy, :enabled], false)
52 Config.put([:media_proxy, :invalidation, :enabled], true)
53 Config.put([:media_proxy, :invalidation, :provider], Invalidation.Script)
54 Config.put([Invalidation.Script], script_path: "purge-nginx")
55
56 image_url = "http://example.com/media/example.jpg"
57 Pleroma.Web.MediaProxy.put_in_banned_urls(image_url)
58
59 with_mocks [{System, [], [cmd: fn _, _ -> {"ok", 0} end]}] do
60 assert capture_log(fn ->
61 assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
62 assert Invalidation.purge([image_url]) == {:ok, [image_url]}
63 assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
64 end) =~ "Running cache purge: [\"#{image_url}\"]"
65 end
66 end
67 end
68 end