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"]) == [
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 "matches are case-insensitive" do
52 regexes = MRF.subdomains_regex(["UnSafe.TLD", "UnSAFE2.Tld"])
54 assert regexes == [~r/^UnSafe.TLD$/i, ~r/^UnSAFE2.Tld$/i]
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")
61 refute MRF.subdomain_match?(regexes, "EXAMPLE.COM")
62 refute MRF.subdomain_match?(regexes, "example.com")
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"]
71 assert MRF.instance_list_from_tuples(list) == expected
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])
80 mrf_policies: ["NoOpPolicy", "HashtagPolicy"],
82 federated_timeline_removal: [],
89 {:ok, ^expected} = MRF.describe()
92 test "it works as expected with mock policy" do
93 clear_config([:mrf, :policies], [MRFModuleMock])
96 mrf_policies: ["MRFModuleMock", "HashtagPolicy"],
97 mrf_module_mock: "some config data",
99 federated_timeline_removal: [],
106 {:ok, ^expected} = MRF.describe()
110 test "config_descriptions/0" do
111 descriptions = MRF.config_descriptions()
113 good_mrf = Enum.find(descriptions, fn %{key: key} -> key == :good_mrf end)
115 assert good_mrf == %{
117 related_policy: "Fixtures.Modules.GoodMRF",
119 description: "Some description",