MRF Policies: Return a {:reject, reason} instead of {:reject, nil}
[akkoma] / test / web / activity_pub / mrf / mrf_test.exs
1 defmodule Pleroma.Web.ActivityPub.MRFTest do
2 use ExUnit.Case, async: true
3 use Pleroma.Tests.Helpers
4 alias Pleroma.Web.ActivityPub.MRF
5
6 test "subdomains_regex/1" do
7 assert MRF.subdomains_regex(["unsafe.tld", "*.unsafe.tld"]) == [
8 ~r/^unsafe.tld$/i,
9 ~r/^(.*\.)*unsafe.tld$/i
10 ]
11 end
12
13 describe "subdomain_match/2" do
14 test "common domains" do
15 regexes = MRF.subdomains_regex(["unsafe.tld", "unsafe2.tld"])
16
17 assert regexes == [~r/^unsafe.tld$/i, ~r/^unsafe2.tld$/i]
18
19 assert MRF.subdomain_match?(regexes, "unsafe.tld")
20 assert MRF.subdomain_match?(regexes, "unsafe2.tld")
21
22 refute MRF.subdomain_match?(regexes, "example.com")
23 end
24
25 test "wildcard domains with one subdomain" do
26 regexes = MRF.subdomains_regex(["*.unsafe.tld"])
27
28 assert regexes == [~r/^(.*\.)*unsafe.tld$/i]
29
30 assert MRF.subdomain_match?(regexes, "unsafe.tld")
31 assert MRF.subdomain_match?(regexes, "sub.unsafe.tld")
32 refute MRF.subdomain_match?(regexes, "anotherunsafe.tld")
33 refute MRF.subdomain_match?(regexes, "unsafe.tldanother")
34 end
35
36 test "wildcard domains with two subdomains" do
37 regexes = MRF.subdomains_regex(["*.unsafe.tld"])
38
39 assert regexes == [~r/^(.*\.)*unsafe.tld$/i]
40
41 assert MRF.subdomain_match?(regexes, "unsafe.tld")
42 assert MRF.subdomain_match?(regexes, "sub.sub.unsafe.tld")
43 refute MRF.subdomain_match?(regexes, "sub.anotherunsafe.tld")
44 refute MRF.subdomain_match?(regexes, "sub.unsafe.tldanother")
45 end
46
47 test "matches are case-insensitive" do
48 regexes = MRF.subdomains_regex(["UnSafe.TLD", "UnSAFE2.Tld"])
49
50 assert regexes == [~r/^UnSafe.TLD$/i, ~r/^UnSAFE2.Tld$/i]
51
52 assert MRF.subdomain_match?(regexes, "UNSAFE.TLD")
53 assert MRF.subdomain_match?(regexes, "UNSAFE2.TLD")
54 assert MRF.subdomain_match?(regexes, "unsafe.tld")
55 assert MRF.subdomain_match?(regexes, "unsafe2.tld")
56
57 refute MRF.subdomain_match?(regexes, "EXAMPLE.COM")
58 refute MRF.subdomain_match?(regexes, "example.com")
59 end
60 end
61
62 describe "describe/0" do
63 test "it works as expected with noop policy" do
64 expected = %{
65 mrf_policies: ["NoOpPolicy"],
66 exclusions: false
67 }
68
69 {:ok, ^expected} = MRF.describe()
70 end
71
72 test "it works as expected with mock policy" do
73 clear_config([:mrf, :policies], [MRFModuleMock])
74
75 expected = %{
76 mrf_policies: ["MRFModuleMock"],
77 mrf_module_mock: "some config data",
78 exclusions: false
79 }
80
81 {:ok, ^expected} = MRF.describe()
82 end
83 end
84 end