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.MRFTest do
6 use ExUnit.Case, async: true
7 use Pleroma.Tests.Helpers
8 alias Pleroma.Web.ActivityPub.MRF
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
17 describe "subdomain_match/2" do
18 test "common domains" do
19 regexes = MRF.subdomains_regex(["unsafe.tld", "unsafe2.tld"])
21 assert regexes == [~r/^(.+\.)?unsafe\.tld$/i, ~r/^(.+\.)?unsafe2\.tld$/i]
23 assert MRF.subdomain_match?(regexes, "unsafe.tld")
24 assert MRF.subdomain_match?(regexes, "unsafe2.tld")
26 refute MRF.subdomain_match?(regexes, "example.com")
29 test "wildcard domains with one subdomain" do
30 regexes = MRF.subdomains_regex(["unsafe.tld"])
32 assert regexes == [~r/^(.+\.)?unsafe\.tld$/i]
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")
40 test "wildcard domains with two subdomains" do
41 regexes = MRF.subdomains_regex(["unsafe.tld"])
43 assert regexes == [~r/^(.+\.)?unsafe\.tld$/i]
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")
51 test "wildcard on the tld" do
52 regexes = MRF.subdomains_regex(["somewhere.*"])
54 assert regexes == [~r/^(.+\.)?somewhere\.(.+)$/i]
56 assert MRF.subdomain_match?(regexes, "somewhere.net")
57 assert MRF.subdomain_match?(regexes, "somewhere.com")
58 assert MRF.subdomain_match?(regexes, "somewhere.somewherelese.net")
59 refute MRF.subdomain_match?(regexes, "somewhere")
62 test "wildcards on subdomain _and_ tld" do
63 regexes = MRF.subdomains_regex(["*.somewhere.*"])
65 assert regexes == [~r/^(.+\.)?somewhere\.(.+)$/i]
67 assert MRF.subdomain_match?(regexes, "somewhere.net")
68 assert MRF.subdomain_match?(regexes, "somewhere.com")
69 assert MRF.subdomain_match?(regexes, "sub.somewhere.net")
70 assert MRF.subdomain_match?(regexes, "sub.somewhere.com")
71 assert MRF.subdomain_match?(regexes, "sub.sub.somewhere.net")
72 assert MRF.subdomain_match?(regexes, "sub.sub.somewhere.com")
73 refute MRF.subdomain_match?(regexes, "somewhere")
76 test "matches are case-insensitive" do
77 regexes = MRF.subdomains_regex(["UnSafe.TLD", "UnSAFE2.Tld"])
79 assert regexes == [~r/^(.+\.)?UnSafe\.TLD$/i, ~r/^(.+\.)?UnSAFE2\.Tld$/i]
81 assert MRF.subdomain_match?(regexes, "UNSAFE.TLD")
82 assert MRF.subdomain_match?(regexes, "UNSAFE2.TLD")
83 assert MRF.subdomain_match?(regexes, "unsafe.tld")
84 assert MRF.subdomain_match?(regexes, "unsafe2.tld")
86 refute MRF.subdomain_match?(regexes, "EXAMPLE.COM")
87 refute MRF.subdomain_match?(regexes, "example.com")
91 describe "instance_list_from_tuples/1" do
92 test "returns a list of instances from a list of {instance, reason} tuples" do
93 list = [{"some.tld", "a reason"}, {"other.tld", "another reason"}]
94 expected = ["some.tld", "other.tld"]
96 assert MRF.instance_list_from_tuples(list) == expected
100 describe "describe/0" do
101 test "it works as expected with noop policy" do
102 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.NoOpPolicy])
105 mrf_policies: ["NoOpPolicy", "HashtagPolicy", "InlineQuotePolicy", "NormalizeMarkup"],
107 federated_timeline_removal: [],
114 {:ok, ^expected} = MRF.describe()
117 test "it works as expected with mock policy" do
118 clear_config([:mrf, :policies], [MRFModuleMock])
121 mrf_policies: ["MRFModuleMock", "HashtagPolicy", "InlineQuotePolicy", "NormalizeMarkup"],
122 mrf_module_mock: "some config data",
124 federated_timeline_removal: [],
131 {:ok, ^expected} = MRF.describe()
135 test "config_descriptions/0" do
136 descriptions = MRF.config_descriptions()
138 good_mrf = Enum.find(descriptions, fn %{key: key} -> key == :good_mrf end)
140 assert good_mrf == %{
142 related_policy: "Fixtures.Modules.GoodMRF",
144 description: "Some description",