Merge branch 'chores/bump-copyright' into 'develop'
[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_all do
13 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
14 :ok
15 end
16
17 setup do
18 emoji_path = [:instance, :static_dir] |> Config.get() |> Path.join("emoji/stolen")
19
20 Emoji.reload()
21
22 message = %{
23 "type" => "Create",
24 "object" => %{
25 "emoji" => [{"firedfox", "https://example.org/emoji/firedfox.png"}],
26 "actor" => "https://example.org/users/admin"
27 }
28 }
29
30 on_exit(fn ->
31 File.rm_rf!(emoji_path)
32 end)
33
34 [message: message, path: emoji_path]
35 end
36
37 test "does nothing by default", %{message: message} do
38 refute "firedfox" in installed()
39
40 assert {:ok, _message} = StealEmojiPolicy.filter(message)
41
42 refute "firedfox" in installed()
43 end
44
45 test "Steals emoji on unknown shortcode from allowed remote host", %{
46 message: message,
47 path: path
48 } do
49 refute "firedfox" in installed()
50 refute File.exists?(path)
51
52 clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
53
54 assert {:ok, _message} = StealEmojiPolicy.filter(message)
55
56 assert "firedfox" in installed()
57 assert File.exists?(path)
58
59 assert path
60 |> Path.join("firedfox.png")
61 |> File.exists?()
62 end
63
64 test "reject shortcode", %{message: message} do
65 refute "firedfox" in installed()
66
67 clear_config(:mrf_steal_emoji,
68 hosts: ["example.org"],
69 size_limit: 284_468,
70 rejected_shortcodes: [~r/firedfox/]
71 )
72
73 assert {:ok, _message} = StealEmojiPolicy.filter(message)
74
75 refute "firedfox" in installed()
76 end
77
78 test "reject if size is above the limit", %{message: message} do
79 refute "firedfox" in installed()
80
81 clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 50_000)
82
83 assert {:ok, _message} = StealEmojiPolicy.filter(message)
84
85 refute "firedfox" in installed()
86 end
87
88 test "reject if host returns error", %{message: message} do
89 refute "firedfox" in installed()
90
91 Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox.png"} ->
92 {:ok, %Tesla.Env{status: 404, body: "Not found"}}
93 end)
94
95 clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
96
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"
100
101 refute "firedfox" in installed()
102 end
103
104 defp installed, do: Emoji.get_all() |> Enum.map(fn {k, _} -> k end)
105 end