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 emoji_path = [:instance, :static_dir] |> Config.get() |> Path.join("emoji/stolen")
20 "emoji" => [{"firedfox", "https://example.org/emoji/firedfox.png"}],
21 "actor" => "https://example.org/users/admin"
26 File.rm_rf!(emoji_path)
29 [message: message, path: emoji_path]
32 test "does nothing by default", %{message: message} do
33 refute "firedfox" in installed()
35 assert {:ok, _message} = StealEmojiPolicy.filter(message)
37 refute "firedfox" in installed()
40 test "Steals emoji on unknown shortcode from allowed remote host", %{
44 refute "firedfox" in installed()
45 refute File.exists?(path)
47 Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox.png"} ->
48 %Tesla.Env{status: 200, body: File.read!("test/fixtures/image.jpg")}
51 clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
53 assert {:ok, _message} = StealEmojiPolicy.filter(message)
55 assert "firedfox" in installed()
56 assert File.exists?(path)
59 |> Path.join("firedfox.png")
63 test "reject shortcode", %{message: message} do
64 refute "firedfox" in installed()
66 clear_config(:mrf_steal_emoji,
67 hosts: ["example.org"],
69 rejected_shortcodes: [~r/firedfox/]
72 assert {:ok, _message} = StealEmojiPolicy.filter(message)
74 refute "firedfox" in installed()
77 test "reject if size is above the limit", %{message: message} do
78 refute "firedfox" in installed()
80 Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox.png"} ->
81 %Tesla.Env{status: 200, body: File.read!("test/fixtures/image.jpg")}
84 clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 50_000)
86 assert {:ok, _message} = StealEmojiPolicy.filter(message)
88 refute "firedfox" in installed()
91 test "reject if host returns error", %{message: message} do
92 refute "firedfox" in installed()
94 Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox.png"} ->
95 {:ok, %Tesla.Env{status: 404, body: "Not found"}}
98 clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
100 ExUnit.CaptureLog.capture_log(fn ->
101 assert {:ok, _message} = StealEmojiPolicy.filter(message)
102 end) =~ "MRF.StealEmojiPolicy: Failed to fetch https://example.org/emoji/firedfox.png"
104 refute "firedfox" in installed()
107 defp installed, do: Emoji.get_all() |> Enum.map(fn {k, _} -> k end)