Merge remote-tracking branch 'origin/develop' into moderators
[akkoma] / test / pleroma / web / activity_pub / mrf / steal_emoji_policy_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicyTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Config
9 alias Pleroma.Emoji
10 alias Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy
11
12 setup do
13 emoji_path = [:instance, :static_dir] |> Config.get() |> Path.join("emoji/stolen")
14
15 Emoji.reload()
16
17 message = %{
18 "type" => "Create",
19 "object" => %{
20 "emoji" => [{"firedfox", "https://example.org/emoji/firedfox.png"}],
21 "actor" => "https://example.org/users/admin"
22 }
23 }
24
25 on_exit(fn ->
26 File.rm_rf!(emoji_path)
27 end)
28
29 [message: message, path: emoji_path]
30 end
31
32 test "does nothing by default", %{message: message} do
33 refute "firedfox" in installed()
34
35 assert {:ok, _message} = StealEmojiPolicy.filter(message)
36
37 refute "firedfox" in installed()
38 end
39
40 test "Steals emoji on unknown shortcode from allowed remote host", %{
41 message: message,
42 path: path
43 } do
44 refute "firedfox" in installed()
45 refute File.exists?(path)
46
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")}
49 end)
50
51 clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
52
53 assert {:ok, _message} = StealEmojiPolicy.filter(message)
54
55 assert "firedfox" in installed()
56 assert File.exists?(path)
57
58 assert path
59 |> Path.join("firedfox.png")
60 |> File.exists?()
61 end
62
63 test "reject shortcode", %{message: message} do
64 refute "firedfox" in installed()
65
66 clear_config(:mrf_steal_emoji,
67 hosts: ["example.org"],
68 size_limit: 284_468,
69 rejected_shortcodes: [~r/firedfox/]
70 )
71
72 assert {:ok, _message} = StealEmojiPolicy.filter(message)
73
74 refute "firedfox" in installed()
75 end
76
77 test "reject if size is above the limit", %{message: message} do
78 refute "firedfox" in installed()
79
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")}
82 end)
83
84 clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 50_000)
85
86 assert {:ok, _message} = StealEmojiPolicy.filter(message)
87
88 refute "firedfox" in installed()
89 end
90
91 test "reject if host returns error", %{message: message} do
92 refute "firedfox" in installed()
93
94 Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox.png"} ->
95 {:ok, %Tesla.Env{status: 404, body: "Not found"}}
96 end)
97
98 clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
99
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"
103
104 refute "firedfox" in installed()
105 end
106
107 defp installed, do: Emoji.get_all() |> Enum.map(fn {k, _} -> k end)
108 end