fix tests broken by relay defaults changing (#314)
[akkoma] / test / pleroma / web / media_proxy / invalidation_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MediaProxy.InvalidationTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Web.MediaProxy.Invalidation
9
10 import ExUnit.CaptureLog
11 import Mock
12 import Tesla.Mock
13
14 setup do: clear_config([:media_proxy])
15
16 describe "Invalidation.Http" do
17 test "perform request to clear cache" do
18 clear_config([:media_proxy, :enabled], false)
19 clear_config([:media_proxy, :invalidation, :enabled], true)
20 clear_config([:media_proxy, :invalidation, :provider], Invalidation.Http)
21
22 clear_config([Invalidation.Http], method: :purge, headers: [{"x-refresh", 1}])
23 image_url = "http://example.com/media/example.jpg"
24 Pleroma.Web.MediaProxy.put_in_banned_urls(image_url)
25
26 mock(fn
27 %{
28 method: :purge,
29 url: "http://example.com/media/example.jpg",
30 headers: [{"x-refresh", 1}]
31 } ->
32 %Tesla.Env{status: 200}
33 end)
34
35 assert capture_log(fn ->
36 assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
37 assert Invalidation.purge([image_url]) == {:ok, [image_url]}
38 assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
39 end) =~ "Running cache purge: [\"#{image_url}\"]"
40 end
41 end
42
43 describe "Invalidation.Script" do
44 test "run script to clear cache" do
45 clear_config([:media_proxy, :enabled], false)
46 clear_config([:media_proxy, :invalidation, :enabled], true)
47 clear_config([:media_proxy, :invalidation, :provider], Invalidation.Script)
48 clear_config([Invalidation.Script], script_path: "purge-nginx")
49
50 image_url = "http://example.com/media/example.jpg"
51 Pleroma.Web.MediaProxy.put_in_banned_urls(image_url)
52
53 with_mocks [{System, [], [cmd: fn _, _ -> {"ok", 0} end]}] do
54 assert capture_log(fn ->
55 assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
56 assert Invalidation.purge([image_url]) == {:ok, [image_url]}
57 assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
58 end) =~ "Running cache purge: [\"#{image_url}\"]"
59 end
60 end
61 end
62 end