1 defmodule Pleroma.MediaProxyTest do
3 import Pleroma.Web.MediaProxy
4 alias Pleroma.Web.MediaProxy.MediaProxyController
6 describe "when enabled" do
8 enabled = Pleroma.Config.get([:media_proxy, :enabled])
11 Pleroma.Config.put([:media_proxy, :enabled], true)
12 on_exit(fn -> Pleroma.Config.put([:media_proxy, :enabled], enabled) end)
18 test "ignores invalid url" do
19 assert url(nil) == nil
23 test "ignores relative url" do
24 assert url("/local") == "/local"
25 assert url("/") == "/"
28 test "ignores local url" do
29 local_url = Pleroma.Web.Endpoint.url() <> "/hello"
30 local_root = Pleroma.Web.Endpoint.url()
31 assert url(local_url) == local_url
32 assert url(local_root) == local_root
35 test "encodes and decodes URL" do
36 url = "https://pleroma.soykaf.com/static/logo.png"
39 assert String.starts_with?(
41 Pleroma.Config.get([:media_proxy, :base_url], Pleroma.Web.base_url())
44 assert String.ends_with?(encoded, "/logo.png")
46 assert decode_result(encoded) == url
49 test "encodes and decodes URL without a path" do
50 url = "https://pleroma.soykaf.com"
52 assert decode_result(encoded) == url
55 test "encodes and decodes URL without an extension" do
56 url = "https://pleroma.soykaf.com/path/"
58 assert String.ends_with?(encoded, "/path")
59 assert decode_result(encoded) == url
62 test "encodes and decodes URL and ignores query params for the path" do
63 url = "https://pleroma.soykaf.com/static/logo.png?93939393939&bunny=true"
65 assert String.ends_with?(encoded, "/logo.png")
66 assert decode_result(encoded) == url
69 test "ensures urls are url-encoded" do
70 assert decode_result(url("https://pleroma.social/Hello world.jpg")) ==
71 "https://pleroma.social/Hello%20world.jpg"
73 assert decode_result(url("https://pleroma.social/Hello%20world.jpg")) ==
74 "https://pleroma.social/Hello%20world.jpg"
77 test "validates signature" do
78 secret_key_base = Pleroma.Config.get([Pleroma.Web.Endpoint, :secret_key_base])
81 Pleroma.Config.put([Pleroma.Web.Endpoint, :secret_key_base], secret_key_base)
84 encoded = url("https://pleroma.social")
87 [Pleroma.Web.Endpoint, :secret_key_base],
88 "00000000000000000000000000000000000000000000000"
91 [_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/")
92 assert decode_url(sig, base64) == {:error, :invalid_signature}
95 test "filename_matches matches url encoded paths" do
96 assert MediaProxyController.filename_matches(
99 "http://pleroma.social/Hello world.jpg"
102 assert MediaProxyController.filename_matches(
104 "/Hello%20world.jpg",
105 "http://pleroma.social/Hello%20world.jpg"
109 test "filename_matches matches non-url encoded paths" do
110 assert MediaProxyController.filename_matches(
113 "http://pleroma.social/Hello%20world.jpg"
116 assert MediaProxyController.filename_matches(
119 "http://pleroma.social/Hello world.jpg"
123 test "uses the configured base_url" do
124 base_url = Pleroma.Config.get([:media_proxy, :base_url])
128 Pleroma.Config.put([:media_proxy, :base_url], base_url)
132 Pleroma.Config.put([:media_proxy, :base_url], "https://cache.pleroma.social")
134 url = "https://pleroma.soykaf.com/static/logo.png"
137 assert String.starts_with?(encoded, Pleroma.Config.get([:media_proxy, :base_url]))
141 describe "when disabled" do
143 enabled = Pleroma.Config.get([:media_proxy, :enabled])
146 Pleroma.Config.put([:media_proxy, :enabled], false)
149 Pleroma.Config.put([:media_proxy, :enabled], enabled)
157 test "does not encode remote urls" do
158 assert url("https://google.fr") == "https://google.fr"
162 defp decode_result(encoded) do
163 [_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/")
164 {:ok, decoded} = decode_url(sig, base64)