c941066f2ead00d882846807a5816925e096d731
[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 setup do: clear_config([:instance, :rewrite_policy])
64
65 test "it works as expected with noop policy" do
66 expected = %{
67 mrf_policies: ["NoOpPolicy"],
68 exclusions: false
69 }
70
71 {:ok, ^expected} = MRF.describe()
72 end
73
74 test "it works as expected with mock policy" do
75 Pleroma.Config.put([:instance, :rewrite_policy], [MRFModuleMock])
76
77 expected = %{
78 mrf_policies: ["MRFModuleMock"],
79 mrf_module_mock: "some config data",
80 exclusions: false
81 }
82
83 {:ok, ^expected} = MRF.describe()
84 end
85 end
86 end