First
[anni] / test / pleroma / web / media_proxy / invalidation / script_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.MediaProxy.Invalidation.ScriptTest do
6   use ExUnit.Case, async: true
7   alias Pleroma.Web.MediaProxy.Invalidation
8
9   import ExUnit.CaptureLog
10
11   test "it logs error when script is not found" do
12     assert capture_log(fn ->
13              assert {:error, msg} =
14                       Invalidation.Script.purge(
15                         ["http://example.com/media/example.jpg"],
16                         script_path: "./example"
17                       )
18
19              assert msg =~ ~r/%ErlangError{original: :enoent(, reason: nil)?}/
20            end) =~ ~r/Error while cache purge: %ErlangError{original: :enoent(, reason: nil)?}/
21
22     capture_log(fn ->
23       assert Invalidation.Script.purge(
24                ["http://example.com/media/example.jpg"],
25                []
26              ) == {:error, "\"not found script path\""}
27     end)
28   end
29
30   describe "url formatting" do
31     setup do
32       urls = [
33         "https://bikeshed.party/media/foo.png",
34         "http://safe.millennial.space/proxy/wheeeee.gif",
35         "https://lain.com/proxy/mediafile.mp4?foo&bar=true",
36         "http://localhost:4000/media/upload.jpeg"
37       ]
38
39       [urls: urls]
40     end
41
42     test "with invalid formatter", %{urls: urls} do
43       assert urls == Invalidation.Script.maybe_format_urls(urls, nil)
44     end
45
46     test "with :htcacheclean formatter", %{urls: urls} do
47       assert [
48                "https://bikeshed.party:443/media/foo.png?",
49                "http://safe.millennial.space:80/proxy/wheeeee.gif?",
50                "https://lain.com:443/proxy/mediafile.mp4?foo&bar=true",
51                "http://localhost:4000/media/upload.jpeg?"
52              ] == Invalidation.Script.maybe_format_urls(urls, :htcacheclean)
53     end
54   end
55 end