Delete test/plausible/shields/hostname_test.exs with huggingface_hub
Browse files
test/plausible/shields/hostname_test.exs
DELETED
|
@@ -1,198 +0,0 @@
|
|
| 1 |
-
defmodule Plausible.Shields.HostnameTest do
|
| 2 |
-
use Plausible.DataCase
|
| 3 |
-
import Plausible.Shields
|
| 4 |
-
|
| 5 |
-
setup do
|
| 6 |
-
site = insert(:site)
|
| 7 |
-
{:ok, %{site: site}}
|
| 8 |
-
end
|
| 9 |
-
|
| 10 |
-
describe "add_hostname_rule/2" do
|
| 11 |
-
test "no input", %{site: site} do
|
| 12 |
-
assert {:error, changeset} = add_hostname_rule(site, %{})
|
| 13 |
-
assert changeset.errors == [hostname: {"can't be blank", [validation: :required]}]
|
| 14 |
-
refute changeset.valid?
|
| 15 |
-
end
|
| 16 |
-
|
| 17 |
-
test "lengthy", %{site: site} do
|
| 18 |
-
long = "/" <> :binary.copy("a", 251)
|
| 19 |
-
assert {:error, changeset} = add_hostname_rule(site, %{"hostname" => long})
|
| 20 |
-
assert [hostname: {"should be at most %{count} character(s)", _}] = changeset.errors
|
| 21 |
-
refute changeset.valid?
|
| 22 |
-
end
|
| 23 |
-
|
| 24 |
-
test "double insert", %{site: site} do
|
| 25 |
-
assert {:ok, _} = add_hostname_rule(site, %{"hostname" => "/test"})
|
| 26 |
-
assert {:error, changeset} = add_hostname_rule(site, %{"hostname" => "/test"})
|
| 27 |
-
refute changeset.valid?
|
| 28 |
-
|
| 29 |
-
assert changeset.errors == [
|
| 30 |
-
hostname:
|
| 31 |
-
{"rule already exists",
|
| 32 |
-
[
|
| 33 |
-
{:constraint, :unique},
|
| 34 |
-
{:constraint_name, "shield_rules_hostname_site_id_hostname_pattern_index"}
|
| 35 |
-
]}
|
| 36 |
-
]
|
| 37 |
-
end
|
| 38 |
-
|
| 39 |
-
test "equivalent rules are counted as dupes", %{site: site} do
|
| 40 |
-
assert {:ok, _} = add_hostname_rule(site, %{"hostname" => "test*"})
|
| 41 |
-
assert {:error, changeset} = add_hostname_rule(site, %{"hostname" => "test**"})
|
| 42 |
-
|
| 43 |
-
assert changeset.errors == [
|
| 44 |
-
hostname:
|
| 45 |
-
{"rule already exists",
|
| 46 |
-
[
|
| 47 |
-
{:constraint, :unique},
|
| 48 |
-
{:constraint_name, "shield_rules_hostname_site_id_hostname_pattern_index"}
|
| 49 |
-
]}
|
| 50 |
-
]
|
| 51 |
-
end
|
| 52 |
-
|
| 53 |
-
test "regex storage: wildcard", %{site: site} do
|
| 54 |
-
assert {:ok, rule} = add_hostname_rule(site, %{"hostname" => "*test"})
|
| 55 |
-
assert rule.hostname_pattern == "^.*test$"
|
| 56 |
-
end
|
| 57 |
-
|
| 58 |
-
test "regex storage: no wildcard", %{site: site} do
|
| 59 |
-
assert {:ok, rule} = add_hostname_rule(site, %{"hostname" => "test"})
|
| 60 |
-
assert rule.hostname_pattern == "^test$"
|
| 61 |
-
end
|
| 62 |
-
|
| 63 |
-
test "regex storage: escaping", %{site: site} do
|
| 64 |
-
assert {:ok, rule} = add_hostname_rule(site, %{"hostname" => "test.*.**.|+[0-9]"})
|
| 65 |
-
assert rule.hostname_pattern == "^test\\..*\\..*\\.\\|\\+\\[0\\-9\\]$"
|
| 66 |
-
end
|
| 67 |
-
|
| 68 |
-
test "over limit", %{site: site} do
|
| 69 |
-
for i <- 1..maximum_hostname_rules() do
|
| 70 |
-
assert {:ok, _} =
|
| 71 |
-
add_hostname_rule(site, %{"hostname" => "test-#{i}"})
|
| 72 |
-
end
|
| 73 |
-
|
| 74 |
-
assert count_hostname_rules(site) == maximum_hostname_rules()
|
| 75 |
-
|
| 76 |
-
assert {:error, changeset} =
|
| 77 |
-
add_hostname_rule(site, %{"hostname" => "test.limit"})
|
| 78 |
-
|
| 79 |
-
refute changeset.valid?
|
| 80 |
-
assert changeset.errors == [hostname: {"maximum reached", []}]
|
| 81 |
-
end
|
| 82 |
-
|
| 83 |
-
test "with added_by", %{site: site} do
|
| 84 |
-
assert {:ok, rule} =
|
| 85 |
-
add_hostname_rule(site, %{"hostname" => "test.example.com"},
|
| 86 |
-
added_by: build(:user, name: "Joe", email: "joe@example.com")
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
assert rule.added_by == "Joe <joe@example.com>"
|
| 90 |
-
end
|
| 91 |
-
|
| 92 |
-
test "ignores site_id supplied in params", %{site: site} do
|
| 93 |
-
victim = insert(:site)
|
| 94 |
-
|
| 95 |
-
assert {:ok, rule} =
|
| 96 |
-
add_hostname_rule(site, %{"hostname" => "example.com", "site_id" => victim.id})
|
| 97 |
-
|
| 98 |
-
assert rule.site_id == site.id
|
| 99 |
-
assert count_hostname_rules(victim) == 0
|
| 100 |
-
end
|
| 101 |
-
end
|
| 102 |
-
|
| 103 |
-
describe "hostname pattern matching" do
|
| 104 |
-
test "no wildcard", %{site: site} do
|
| 105 |
-
assert {:ok, _} = add_hostname_rule(site, %{"hostname" => "test.example.com"})
|
| 106 |
-
assert hostname_allowed?(site, "test.example.com")
|
| 107 |
-
refute hostname_allowed?(site, "subdmoain.example.com")
|
| 108 |
-
refute hostname_allowed?(site, "test")
|
| 109 |
-
end
|
| 110 |
-
|
| 111 |
-
test "wildcard - subdomains", %{site: site} do
|
| 112 |
-
assert {:ok, _} = add_hostname_rule(site, %{"hostname" => "*.example.com"})
|
| 113 |
-
refute hostname_allowed?(site, "example.com")
|
| 114 |
-
refute hostname_allowed?(site, "example.com.pl")
|
| 115 |
-
assert hostname_allowed?(site, "subdomain.example.com")
|
| 116 |
-
end
|
| 117 |
-
|
| 118 |
-
test "wildcard - any prefix", %{site: site} do
|
| 119 |
-
assert {:ok, _} = add_hostname_rule(site, %{"hostname" => "*example.com"})
|
| 120 |
-
assert hostname_allowed?(site, "example.com")
|
| 121 |
-
refute hostname_allowed?(site, "example.com.pl")
|
| 122 |
-
assert hostname_allowed?(site, "subdomain.example.com")
|
| 123 |
-
end
|
| 124 |
-
end
|
| 125 |
-
|
| 126 |
-
describe "remove_hostname_rule/2" do
|
| 127 |
-
test "is idempontent", %{site: site} do
|
| 128 |
-
{:ok, rule} = add_hostname_rule(site, %{"hostname" => "test"})
|
| 129 |
-
assert remove_hostname_rule(site, rule.id) == :ok
|
| 130 |
-
refute Repo.get(Plausible.Shield.HostnameRule, rule.id)
|
| 131 |
-
assert remove_hostname_rule(site, rule.id) == :ok
|
| 132 |
-
end
|
| 133 |
-
end
|
| 134 |
-
|
| 135 |
-
describe "list_hostname_rules/1" do
|
| 136 |
-
test "empty", %{site: site} do
|
| 137 |
-
assert(list_hostname_rules(site) == [])
|
| 138 |
-
end
|
| 139 |
-
|
| 140 |
-
@tag :slow
|
| 141 |
-
test "many", %{site: site} do
|
| 142 |
-
{:ok, %{id: id1}} = add_hostname_rule(site, %{"hostname" => "test1.example.com"})
|
| 143 |
-
:timer.sleep(1000)
|
| 144 |
-
{:ok, %{id: id2}} = add_hostname_rule(site, %{"hostname" => "test2.example.com"})
|
| 145 |
-
assert [%{id: ^id2}, %{id: ^id1}] = list_hostname_rules(site)
|
| 146 |
-
end
|
| 147 |
-
end
|
| 148 |
-
|
| 149 |
-
describe "count_hostname_rules/1" do
|
| 150 |
-
test "counts", %{site: site} do
|
| 151 |
-
assert count_hostname_rules(site) == 0
|
| 152 |
-
{:ok, _} = add_hostname_rule(site, %{"hostname" => "test1"})
|
| 153 |
-
assert count_hostname_rules(site) == 1
|
| 154 |
-
{:ok, _} = add_hostname_rule(site, %{"hostname" => "test2"})
|
| 155 |
-
assert count_hostname_rules(site) == 2
|
| 156 |
-
end
|
| 157 |
-
end
|
| 158 |
-
|
| 159 |
-
describe "allowed_hostname_patterns/1" do
|
| 160 |
-
test "returns a list of regular expressions when rules are defined", %{site: site} do
|
| 161 |
-
{:ok, _} = add_hostname_rule(site, %{"hostname" => "example.com"})
|
| 162 |
-
{:ok, _} = add_hostname_rule(site, %{"hostname" => "another.example.com"})
|
| 163 |
-
{:ok, _} = add_hostname_rule(site, %{"hostname" => "app.*"})
|
| 164 |
-
|
| 165 |
-
allowed = allowed_hostname_patterns(site.domain)
|
| 166 |
-
|
| 167 |
-
assert length(allowed) == 3
|
| 168 |
-
assert "^example\\.com$" in allowed
|
| 169 |
-
assert "^another\\.example\\.com$" in allowed
|
| 170 |
-
assert "^app\\..*$" in allowed
|
| 171 |
-
end
|
| 172 |
-
end
|
| 173 |
-
|
| 174 |
-
describe "Hostname Rules" do
|
| 175 |
-
test "end to end", %{site: site} do
|
| 176 |
-
site2 = insert(:site)
|
| 177 |
-
|
| 178 |
-
assert count_hostname_rules(site.id) == 0
|
| 179 |
-
assert list_hostname_rules(site.id) == []
|
| 180 |
-
|
| 181 |
-
assert {:ok, rule} =
|
| 182 |
-
add_hostname_rule(site.id, %{
|
| 183 |
-
"hostname" => "blog.example.com"
|
| 184 |
-
})
|
| 185 |
-
|
| 186 |
-
add_hostname_rule(site2, %{"hostname" => "portral.example.com"})
|
| 187 |
-
|
| 188 |
-
assert count_hostname_rules(site) == 1
|
| 189 |
-
assert [%{id: rule_id}] = list_hostname_rules(site)
|
| 190 |
-
assert rule.id == rule_id
|
| 191 |
-
assert rule.hostname == "blog.example.com"
|
| 192 |
-
assert rule.action == :allow
|
| 193 |
-
refute rule.from_cache?
|
| 194 |
-
assert hostname_allowed?(site, "blog.example.com")
|
| 195 |
-
refute hostname_allowed?(site, "portal.example.com")
|
| 196 |
-
end
|
| 197 |
-
end
|
| 198 |
-
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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