2020-06-14 18:02:57 +00:00
|
|
|
defmodule Pleroma.Web.MediaProxy.InvalidationTest do
|
|
|
|
use ExUnit.Case
|
|
|
|
use Pleroma.Tests.Helpers
|
|
|
|
|
|
|
|
alias Pleroma.Config
|
|
|
|
alias Pleroma.Web.MediaProxy.Invalidation
|
|
|
|
|
|
|
|
import ExUnit.CaptureLog
|
|
|
|
import Mock
|
|
|
|
import Tesla.Mock
|
|
|
|
|
|
|
|
setup do: clear_config([:media_proxy])
|
|
|
|
|
|
|
|
setup do
|
2020-06-17 18:13:55 +00:00
|
|
|
on_exit(fn -> Cachex.clear(:banned_urls_cache) end)
|
2020-06-14 18:02:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Invalidation.Http" do
|
|
|
|
test "perform request to clear cache" do
|
|
|
|
Config.put([:media_proxy, :enabled], false)
|
|
|
|
Config.put([:media_proxy, :invalidation, :enabled], true)
|
|
|
|
Config.put([:media_proxy, :invalidation, :provider], Invalidation.Http)
|
|
|
|
|
|
|
|
Config.put([Invalidation.Http], method: :purge, headers: [{"x-refresh", 1}])
|
|
|
|
image_url = "http://example.com/media/example.jpg"
|
2020-06-17 18:13:55 +00:00
|
|
|
Pleroma.Web.MediaProxy.put_in_banned_urls(image_url)
|
2020-06-14 18:02:57 +00:00
|
|
|
|
|
|
|
mock(fn
|
|
|
|
%{
|
|
|
|
method: :purge,
|
|
|
|
url: "http://example.com/media/example.jpg",
|
|
|
|
headers: [{"x-refresh", 1}]
|
|
|
|
} ->
|
|
|
|
%Tesla.Env{status: 200}
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert capture_log(fn ->
|
2020-06-17 18:13:55 +00:00
|
|
|
assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
|
2020-06-14 18:02:57 +00:00
|
|
|
assert Invalidation.purge([image_url]) == {:ok, [image_url]}
|
2020-06-17 18:13:55 +00:00
|
|
|
assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
|
2020-06-14 18:02:57 +00:00
|
|
|
end) =~ "Running cache purge: [\"#{image_url}\"]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Invalidation.Script" do
|
|
|
|
test "run script to clear cache" do
|
|
|
|
Config.put([:media_proxy, :enabled], false)
|
|
|
|
Config.put([:media_proxy, :invalidation, :enabled], true)
|
|
|
|
Config.put([:media_proxy, :invalidation, :provider], Invalidation.Script)
|
|
|
|
Config.put([Invalidation.Script], script_path: "purge-nginx")
|
|
|
|
|
|
|
|
image_url = "http://example.com/media/example.jpg"
|
2020-06-17 18:13:55 +00:00
|
|
|
Pleroma.Web.MediaProxy.put_in_banned_urls(image_url)
|
2020-06-14 18:02:57 +00:00
|
|
|
|
|
|
|
with_mocks [{System, [], [cmd: fn _, _ -> {"ok", 0} end]}] do
|
|
|
|
assert capture_log(fn ->
|
2020-06-17 18:13:55 +00:00
|
|
|
assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
|
2020-06-14 18:02:57 +00:00
|
|
|
assert Invalidation.purge([image_url]) == {:ok, [image_url]}
|
2020-06-17 18:13:55 +00:00
|
|
|
assert Pleroma.Web.MediaProxy.in_banned_urls(image_url)
|
2020-06-14 18:02:57 +00:00
|
|
|
end) =~ "Running cache purge: [\"#{image_url}\"]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|