1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicyTest do
10 alias Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy
13 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 emoji_path = [:instance, :static_dir] |> Config.get() |> Path.join("emoji/stolen")
25 "emoji" => [{"firedfox", "https://example.org/emoji/firedfox.png"}],
26 "actor" => "https://example.org/users/admin"
31 File.rm_rf!(emoji_path)
34 [message: message, path: emoji_path]
37 test "does nothing by default", %{message: message} do
38 refute "firedfox" in installed()
40 assert {:ok, _message} = StealEmojiPolicy.filter(message)
42 refute "firedfox" in installed()
45 test "Steals emoji on unknown shortcode from allowed remote host", %{
49 refute "firedfox" in installed()
50 refute File.exists?(path)
52 clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
54 assert {:ok, _message} = StealEmojiPolicy.filter(message)
56 assert "firedfox" in installed()
57 assert File.exists?(path)
60 |> Path.join("firedfox.png")
64 test "reject shortcode", %{message: message} do
65 refute "firedfox" in installed()
67 clear_config(:mrf_steal_emoji,
68 hosts: ["example.org"],
70 rejected_shortcodes: [~r/firedfox/]
73 assert {:ok, _message} = StealEmojiPolicy.filter(message)
75 refute "firedfox" in installed()
78 test "reject if size is above the limit", %{message: message} do
79 refute "firedfox" in installed()
81 clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 50_000)
83 assert {:ok, _message} = StealEmojiPolicy.filter(message)
85 refute "firedfox" in installed()
88 test "reject if host returns error", %{message: message} do
89 refute "firedfox" in installed()
91 Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox.png"} ->
92 {:ok, %Tesla.Env{status: 404, body: "Not found"}}
95 clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
97 ExUnit.CaptureLog.capture_log(fn ->
98 assert {:ok, _message} = StealEmojiPolicy.filter(message)
99 end) =~ "MRF.StealEmojiPolicy: Failed to fetch https://example.org/emoji/firedfox.png"
101 refute "firedfox" in installed()
104 defp installed, do: Emoji.get_all() |> Enum.map(fn {k, _} -> k end)