Merge remote-tracking branch 'origin/develop' into manifest
[akkoma] / test / pleroma / web / activity_pub / mrf_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.MRFTest do
6 use ExUnit.Case, async: true
7 use Pleroma.Tests.Helpers
8 alias Pleroma.Web.ActivityPub.MRF
9
10 test "subdomains_regex/1" do
11 assert MRF.subdomains_regex(["unsafe.tld", "*.unsafe.tld"]) == [
12 ~r/^unsafe.tld$/i,
13 ~r/^(.*\.)*unsafe.tld$/i
14 ]
15 end
16
17 describe "subdomain_match/2" do
18 test "common domains" do
19 regexes = MRF.subdomains_regex(["unsafe.tld", "unsafe2.tld"])
20
21 assert regexes == [~r/^unsafe.tld$/i, ~r/^unsafe2.tld$/i]
22
23 assert MRF.subdomain_match?(regexes, "unsafe.tld")
24 assert MRF.subdomain_match?(regexes, "unsafe2.tld")
25
26 refute MRF.subdomain_match?(regexes, "example.com")
27 end
28
29 test "wildcard domains with one subdomain" do
30 regexes = MRF.subdomains_regex(["*.unsafe.tld"])
31
32 assert regexes == [~r/^(.*\.)*unsafe.tld$/i]
33
34 assert MRF.subdomain_match?(regexes, "unsafe.tld")
35 assert MRF.subdomain_match?(regexes, "sub.unsafe.tld")
36 refute MRF.subdomain_match?(regexes, "anotherunsafe.tld")
37 refute MRF.subdomain_match?(regexes, "unsafe.tldanother")
38 end
39
40 test "wildcard domains with two subdomains" do
41 regexes = MRF.subdomains_regex(["*.unsafe.tld"])
42
43 assert regexes == [~r/^(.*\.)*unsafe.tld$/i]
44
45 assert MRF.subdomain_match?(regexes, "unsafe.tld")
46 assert MRF.subdomain_match?(regexes, "sub.sub.unsafe.tld")
47 refute MRF.subdomain_match?(regexes, "sub.anotherunsafe.tld")
48 refute MRF.subdomain_match?(regexes, "sub.unsafe.tldanother")
49 end
50
51 test "matches are case-insensitive" do
52 regexes = MRF.subdomains_regex(["UnSafe.TLD", "UnSAFE2.Tld"])
53
54 assert regexes == [~r/^UnSafe.TLD$/i, ~r/^UnSAFE2.Tld$/i]
55
56 assert MRF.subdomain_match?(regexes, "UNSAFE.TLD")
57 assert MRF.subdomain_match?(regexes, "UNSAFE2.TLD")
58 assert MRF.subdomain_match?(regexes, "unsafe.tld")
59 assert MRF.subdomain_match?(regexes, "unsafe2.tld")
60
61 refute MRF.subdomain_match?(regexes, "EXAMPLE.COM")
62 refute MRF.subdomain_match?(regexes, "example.com")
63 end
64 end
65
66 describe "instance_list_from_tuples/1" do
67 test "returns a list of instances from a list of {instance, reason} tuples" do
68 list = [{"some.tld", "a reason"}, {"other.tld", "another reason"}]
69 expected = ["some.tld", "other.tld"]
70
71 assert MRF.instance_list_from_tuples(list) == expected
72 end
73 end
74
75 describe "describe/0" do
76 test "it works as expected with noop policy" do
77 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.NoOpPolicy])
78
79 expected = %{
80 mrf_policies: ["NoOpPolicy", "HashtagPolicy"],
81 mrf_hashtag: %{
82 federated_timeline_removal: [],
83 reject: [],
84 sensitive: ["nsfw"]
85 },
86 exclusions: false
87 }
88
89 {:ok, ^expected} = MRF.describe()
90 end
91
92 test "it works as expected with mock policy" do
93 clear_config([:mrf, :policies], [MRFModuleMock])
94
95 expected = %{
96 mrf_policies: ["MRFModuleMock", "HashtagPolicy"],
97 mrf_module_mock: "some config data",
98 mrf_hashtag: %{
99 federated_timeline_removal: [],
100 reject: [],
101 sensitive: ["nsfw"]
102 },
103 exclusions: false
104 }
105
106 {:ok, ^expected} = MRF.describe()
107 end
108 end
109
110 test "config_descriptions/0" do
111 descriptions = MRF.config_descriptions()
112
113 good_mrf = Enum.find(descriptions, fn %{key: key} -> key == :good_mrf end)
114
115 assert good_mrf == %{
116 key: :good_mrf,
117 related_policy: "Fixtures.Modules.GoodMRF",
118 label: "Good MRF",
119 description: "Some description",
120 group: :pleroma,
121 tab: :mrf,
122 type: :group
123 }
124 end
125 end