Delete test/plausible_web/live/shields/hostnames_test.exs with huggingface_hub
Browse files
test/plausible_web/live/shields/hostnames_test.exs
DELETED
|
@@ -1,197 +0,0 @@
|
|
| 1 |
-
defmodule PlausibleWeb.Live.Shields.HostnamesTest do
|
| 2 |
-
use PlausibleWeb.ConnCase, async: true
|
| 3 |
-
|
| 4 |
-
import Phoenix.LiveViewTest
|
| 5 |
-
|
| 6 |
-
alias Plausible.Shields
|
| 7 |
-
|
| 8 |
-
setup [:create_user, :create_site, :log_in]
|
| 9 |
-
|
| 10 |
-
describe "Hostname Rules - static" do
|
| 11 |
-
test "renders hostname rules hostname with empty list", %{conn: conn, site: site} do
|
| 12 |
-
conn = get(conn, "/#{site.domain}/settings/shields/hostnames")
|
| 13 |
-
resp = html_response(conn, 200)
|
| 14 |
-
|
| 15 |
-
assert resp =~ "Allow a hostname"
|
| 16 |
-
assert resp =~ "Hostnames allow list"
|
| 17 |
-
assert resp =~ "Traffic from all hostnames is recorded until you add your first rule"
|
| 18 |
-
end
|
| 19 |
-
|
| 20 |
-
test "lists hostname rules with remove actions", %{conn: conn, site: site} do
|
| 21 |
-
{:ok, r1} =
|
| 22 |
-
Shields.add_hostname_rule(site, %{"hostname" => "example.com"})
|
| 23 |
-
|
| 24 |
-
{:ok, r2} =
|
| 25 |
-
Shields.add_hostname_rule(site, %{"hostname" => "example.org"})
|
| 26 |
-
|
| 27 |
-
conn = get(conn, "/#{site.domain}/settings/shields/hostnames")
|
| 28 |
-
resp = html_response(conn, 200)
|
| 29 |
-
|
| 30 |
-
assert resp =~ "example.com"
|
| 31 |
-
assert resp =~ "example.org"
|
| 32 |
-
|
| 33 |
-
assert remove_button_1 = find(resp, "#remove-hostname-rule-#{r1.id}")
|
| 34 |
-
assert remove_button_2 = find(resp, "#remove-hostname-rule-#{r2.id}")
|
| 35 |
-
|
| 36 |
-
assert text_of_attr(remove_button_1, "phx-click") == "remove-hostname-rule"
|
| 37 |
-
assert text_of_attr(remove_button_1, "phx-value-rule-id") == r1.id
|
| 38 |
-
assert text_of_attr(remove_button_2, "phx-click") == "remove-hostname-rule"
|
| 39 |
-
assert text_of_attr(remove_button_2, "phx-value-rule-id") == r2.id
|
| 40 |
-
end
|
| 41 |
-
|
| 42 |
-
test "add rule button is rendered", %{conn: conn, site: site} do
|
| 43 |
-
conn = get(conn, "/#{site.domain}/settings/shields/hostnames")
|
| 44 |
-
resp = html_response(conn, 200)
|
| 45 |
-
|
| 46 |
-
assert element_exists?(resp, ~s/button#add-hostname-rule[x-data]/)
|
| 47 |
-
attr = text_of_attr(resp, ~s/button#add-hostname-rule/, "x-on:click")
|
| 48 |
-
|
| 49 |
-
assert attr =~ "open-modal"
|
| 50 |
-
assert attr =~ "hostname-rule-form-modal"
|
| 51 |
-
end
|
| 52 |
-
|
| 53 |
-
test "add rule button is not rendered when maximum reached", %{conn: conn, site: site} do
|
| 54 |
-
for i <- 1..Shields.maximum_hostname_rules() do
|
| 55 |
-
assert {:ok, _} =
|
| 56 |
-
Shields.add_hostname_rule(site, %{"hostname" => "#{i}.example.com"})
|
| 57 |
-
end
|
| 58 |
-
|
| 59 |
-
conn = get(conn, "/#{site.domain}/settings/shields/hostnames")
|
| 60 |
-
resp = html_response(conn, 200)
|
| 61 |
-
|
| 62 |
-
refute element_exists?(resp, ~s/button#add-hostname-rule[x-data]/)
|
| 63 |
-
assert resp =~ "Maximum number of hostnames reached"
|
| 64 |
-
assert resp =~ "You've reached the maximum number of hostnames you can block (10)"
|
| 65 |
-
end
|
| 66 |
-
end
|
| 67 |
-
|
| 68 |
-
describe "Hostname Rules - LiveView" do
|
| 69 |
-
test "modal contains form", %{site: site, conn: conn} do
|
| 70 |
-
lv = get_liveview(conn, site)
|
| 71 |
-
html = render(lv)
|
| 72 |
-
|
| 73 |
-
assert element_exists?(
|
| 74 |
-
html,
|
| 75 |
-
~s/form[phx-submit="save-hostname-rule"] input[name="hostname_rule\[hostname\]"]/
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
assert submit_button(html, ~s/form[phx-submit="save-hostname-rule"]/)
|
| 79 |
-
end
|
| 80 |
-
|
| 81 |
-
test "if no rules are added yet, form displays hint", %{site: site, conn: conn} do
|
| 82 |
-
lv = get_liveview(conn, site)
|
| 83 |
-
html = render(lv)
|
| 84 |
-
|
| 85 |
-
assert text(html) =~
|
| 86 |
-
"NB: Once added, we will start rejecting traffic from non-matching hostnames within a few minutes."
|
| 87 |
-
|
| 88 |
-
refute text(html) =~ "we will start accepting"
|
| 89 |
-
end
|
| 90 |
-
|
| 91 |
-
test "if rules are added, form changes the hint", %{site: site, conn: conn} do
|
| 92 |
-
{:ok, _} =
|
| 93 |
-
Shields.add_hostname_rule(site, %{"hostname" => "*.example.com"})
|
| 94 |
-
|
| 95 |
-
lv = get_liveview(conn, site)
|
| 96 |
-
html = render(lv)
|
| 97 |
-
|
| 98 |
-
refute text(html) =~ "we will start rejecting traffic"
|
| 99 |
-
|
| 100 |
-
assert text(html) =~
|
| 101 |
-
"Once added, we will start accepting traffic from this hostname within a few minutes."
|
| 102 |
-
end
|
| 103 |
-
|
| 104 |
-
test "submitting a valid Hostname saves it", %{conn: conn, site: site} do
|
| 105 |
-
lv = get_liveview(conn, site)
|
| 106 |
-
|
| 107 |
-
lv
|
| 108 |
-
|> element("form")
|
| 109 |
-
|> render_submit(%{
|
| 110 |
-
"hostname_rule[hostname]" => "*.example.com"
|
| 111 |
-
})
|
| 112 |
-
|
| 113 |
-
html = render(lv)
|
| 114 |
-
|
| 115 |
-
assert html =~ "*.example.com"
|
| 116 |
-
|
| 117 |
-
assert [%{hostname: "*.example.com"}] = Shields.list_hostname_rules(site)
|
| 118 |
-
end
|
| 119 |
-
|
| 120 |
-
test "submitting invalid Hostname renders error", %{conn: conn, site: site} do
|
| 121 |
-
lv = get_liveview(conn, site)
|
| 122 |
-
|
| 123 |
-
lv
|
| 124 |
-
|> element("form")
|
| 125 |
-
|> render_submit(%{
|
| 126 |
-
"hostname_rule[hostname]" => :binary.copy("a", 256)
|
| 127 |
-
})
|
| 128 |
-
|
| 129 |
-
html = render(lv)
|
| 130 |
-
assert html =~ "should be at most 250 character(s)"
|
| 131 |
-
end
|
| 132 |
-
|
| 133 |
-
test "clicking Remove deletes the rule", %{conn: conn, site: site} do
|
| 134 |
-
{:ok, _} =
|
| 135 |
-
Shields.add_hostname_rule(site, %{"hostname" => "*.example.com"})
|
| 136 |
-
|
| 137 |
-
lv = get_liveview(conn, site)
|
| 138 |
-
|
| 139 |
-
html = render(lv)
|
| 140 |
-
assert html =~ "*.example.com"
|
| 141 |
-
|
| 142 |
-
lv |> element(~s/button[phx-click="remove-hostname-rule"]/) |> render_click()
|
| 143 |
-
|
| 144 |
-
html = render(lv)
|
| 145 |
-
refute html =~ "*.example.com"
|
| 146 |
-
|
| 147 |
-
assert Shields.count_hostname_rules(site) == 0
|
| 148 |
-
end
|
| 149 |
-
|
| 150 |
-
test "conclicting rules are annotated with a warning", %{conn: conn, site: site} do
|
| 151 |
-
lv = get_liveview(conn, site)
|
| 152 |
-
|
| 153 |
-
lv
|
| 154 |
-
|> element("form")
|
| 155 |
-
|> render_submit(%{
|
| 156 |
-
"hostname_rule[hostname]" => "*example.com"
|
| 157 |
-
})
|
| 158 |
-
|
| 159 |
-
html = render(lv)
|
| 160 |
-
refute html =~ "This rule might be redundant"
|
| 161 |
-
|
| 162 |
-
lv
|
| 163 |
-
|> element("form")
|
| 164 |
-
|> render_submit(%{
|
| 165 |
-
"hostname_rule[hostname]" => "subdomain.example.com"
|
| 166 |
-
})
|
| 167 |
-
|
| 168 |
-
html = render(lv)
|
| 169 |
-
|
| 170 |
-
assert html =~ "*example.com"
|
| 171 |
-
assert html =~ "subdomain.example.com"
|
| 172 |
-
|
| 173 |
-
assert html =~
|
| 174 |
-
"This rule might be redundant because the following rules may match first:\n\n*example.com"
|
| 175 |
-
|
| 176 |
-
broader_rule_id =
|
| 177 |
-
site
|
| 178 |
-
|> Shields.list_hostname_rules()
|
| 179 |
-
|> Enum.find(&(&1.hostname == "*example.com"))
|
| 180 |
-
|> Map.fetch!(:id)
|
| 181 |
-
|
| 182 |
-
lv |> element(~s/button#remove-hostname-rule-#{broader_rule_id}/) |> render_click()
|
| 183 |
-
html = render(lv)
|
| 184 |
-
|
| 185 |
-
assert html =~ "subdomain.example.com"
|
| 186 |
-
refute html =~ "*example.com"
|
| 187 |
-
refute html =~ "This rule might be redundant"
|
| 188 |
-
end
|
| 189 |
-
|
| 190 |
-
defp get_liveview(conn, site) do
|
| 191 |
-
conn = assign(conn, :live_module, PlausibleWeb.Live.Shields)
|
| 192 |
-
{:ok, lv, _html} = live(conn, "/#{site.domain}/settings/shields/hostnames")
|
| 193 |
-
|
| 194 |
-
lv
|
| 195 |
-
end
|
| 196 |
-
end
|
| 197 |
-
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|