Delete test/plausible_web/live/shields/ip_addresses_test.exs with huggingface_hub
Browse files
test/plausible_web/live/shields/ip_addresses_test.exs
DELETED
|
@@ -1,202 +0,0 @@
|
|
| 1 |
-
defmodule PlausibleWeb.Live.Shields.IPAddressesTest 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 "IP Rules - static" do
|
| 11 |
-
test "renders ip rules page with empty list", %{conn: conn, site: site} do
|
| 12 |
-
conn = get(conn, "/#{site.domain}/settings/shields/ip_addresses")
|
| 13 |
-
resp = html_response(conn, 200)
|
| 14 |
-
|
| 15 |
-
assert resp =~ "Block an IP address"
|
| 16 |
-
assert resp =~ "IP block list"
|
| 17 |
-
end
|
| 18 |
-
|
| 19 |
-
test "lists ip rules with remove actions", %{conn: conn, site: site} do
|
| 20 |
-
{:ok, r1} =
|
| 21 |
-
Shields.add_ip_rule(site, %{"inet" => "127.0.0.1", "description" => "Alice"})
|
| 22 |
-
|
| 23 |
-
{:ok, r2} =
|
| 24 |
-
Shields.add_ip_rule(site, %{"inet" => "127.0.0.2", "description" => "Bob"})
|
| 25 |
-
|
| 26 |
-
conn = get(conn, "/#{site.domain}/settings/shields/ip_addresses")
|
| 27 |
-
resp = html_response(conn, 200)
|
| 28 |
-
|
| 29 |
-
assert resp =~ "127.0.0.1"
|
| 30 |
-
assert resp =~ "Alice"
|
| 31 |
-
|
| 32 |
-
assert resp =~ "127.0.0.2"
|
| 33 |
-
assert resp =~ "Bob"
|
| 34 |
-
|
| 35 |
-
assert remove_button_1 = find(resp, "#remove-ip-rule-#{r1.id}")
|
| 36 |
-
assert remove_button_2 = find(resp, "#remove-ip-rule-#{r2.id}")
|
| 37 |
-
|
| 38 |
-
assert text_of_attr(remove_button_1, "phx-click") == "remove-ip-rule"
|
| 39 |
-
assert text_of_attr(remove_button_1, "phx-value-rule-id") == r1.id
|
| 40 |
-
assert text_of_attr(remove_button_2, "phx-click") == "remove-ip-rule"
|
| 41 |
-
assert text_of_attr(remove_button_2, "phx-value-rule-id") == r2.id
|
| 42 |
-
end
|
| 43 |
-
|
| 44 |
-
test "add rule button is rendered", %{conn: conn, site: site} do
|
| 45 |
-
conn = get(conn, "/#{site.domain}/settings/shields/ip_addresses")
|
| 46 |
-
resp = html_response(conn, 200)
|
| 47 |
-
|
| 48 |
-
assert element_exists?(resp, ~s/button#add-ip-rule/)
|
| 49 |
-
end
|
| 50 |
-
|
| 51 |
-
test "add rule button is not rendered when maximum reached", %{conn: conn, site: site} do
|
| 52 |
-
for i <- 1..Shields.maximum_ip_rules() do
|
| 53 |
-
assert {:ok, _} =
|
| 54 |
-
Shields.add_ip_rule(site, %{"inet" => "1.1.1.#{i}"})
|
| 55 |
-
end
|
| 56 |
-
|
| 57 |
-
conn = get(conn, "/#{site.domain}/settings/shields/ip_addresses")
|
| 58 |
-
resp = html_response(conn, 200)
|
| 59 |
-
|
| 60 |
-
refute element_exists?(resp, ~s/button#add-ip-rule[x-data]/)
|
| 61 |
-
assert resp =~ "Maximum number of addresses reached"
|
| 62 |
-
assert resp =~ "You've reached the maximum number of IP addresses you can block (30)"
|
| 63 |
-
end
|
| 64 |
-
end
|
| 65 |
-
|
| 66 |
-
describe "IP Rules - LiveView" do
|
| 67 |
-
test "modal contains form", %{site: site, conn: conn} do
|
| 68 |
-
lv = get_liveview(conn, site)
|
| 69 |
-
html = render(lv)
|
| 70 |
-
|
| 71 |
-
assert element_exists?(
|
| 72 |
-
html,
|
| 73 |
-
~s/form[phx-submit="save-ip-rule"] input[name="ip_rule\[inet\]"]/
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
assert element_exists?(
|
| 77 |
-
html,
|
| 78 |
-
~s/form[phx-submit="save-ip-rule"] input[name="ip_rule\[description\]"]/
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
assert submit_button(html, ~s/form[phx-submit="save-ip-rule"]/)
|
| 82 |
-
end
|
| 83 |
-
|
| 84 |
-
test "form modal contains link to add own IP", %{site: site, conn: conn} do
|
| 85 |
-
ip = PlausibleWeb.RemoteIP.get(conn)
|
| 86 |
-
lv = get_liveview(conn, site)
|
| 87 |
-
html = render(lv)
|
| 88 |
-
|
| 89 |
-
assert text(html) =~ "Your current IP address is: #{ip}"
|
| 90 |
-
assert element_exists?(html, ~s/a[phx-click="prefill-own-ip-rule"]/)
|
| 91 |
-
end
|
| 92 |
-
|
| 93 |
-
test "form modal does not contain link to add own IP if already added", %{
|
| 94 |
-
site: site,
|
| 95 |
-
conn: conn
|
| 96 |
-
} do
|
| 97 |
-
ip = PlausibleWeb.RemoteIP.get(conn)
|
| 98 |
-
|
| 99 |
-
{:ok, _} =
|
| 100 |
-
Shields.add_ip_rule(site, %{
|
| 101 |
-
"inet" => ip,
|
| 102 |
-
"description" => "Alice"
|
| 103 |
-
})
|
| 104 |
-
|
| 105 |
-
lv = get_liveview(conn, site)
|
| 106 |
-
html = render(lv)
|
| 107 |
-
|
| 108 |
-
refute text(html) =~ "Your current IP address is: #{ip}"
|
| 109 |
-
refute element_exists?(html, ~s/a[phx-click="prefill-own-ip-rule"]/)
|
| 110 |
-
end
|
| 111 |
-
|
| 112 |
-
test "clicking the link prefills own IP", %{conn: conn, site: site, user: user} do
|
| 113 |
-
lv = get_liveview(conn, site)
|
| 114 |
-
lv |> element(~s/a[phx-click="prefill-own-ip-rule"]/) |> render_click()
|
| 115 |
-
|
| 116 |
-
html = render(lv)
|
| 117 |
-
|
| 118 |
-
assert text_of_attr(html, "input[name=\"ip_rule[inet]\"]", "value") ==
|
| 119 |
-
PlausibleWeb.RemoteIP.get(conn)
|
| 120 |
-
|
| 121 |
-
assert text_of_attr(html, "input[name=\"ip_rule[description]\"]", "value") == user.name
|
| 122 |
-
end
|
| 123 |
-
|
| 124 |
-
test "submitting own IP saves it", %{conn: conn, site: site, user: user} do
|
| 125 |
-
ip = PlausibleWeb.RemoteIP.get(conn)
|
| 126 |
-
assert [] = Shields.list_ip_rules(site)
|
| 127 |
-
|
| 128 |
-
lv = get_liveview(conn, site)
|
| 129 |
-
lv |> element(~s/a[phx-click="prefill-own-ip-rule"]/) |> render_click()
|
| 130 |
-
lv |> element(~s/form/) |> render_submit()
|
| 131 |
-
|
| 132 |
-
html = render(lv)
|
| 133 |
-
|
| 134 |
-
assert html =~ ip
|
| 135 |
-
assert html =~ user.name
|
| 136 |
-
|
| 137 |
-
assert [%{id: id}] = Shields.list_ip_rules(site)
|
| 138 |
-
|
| 139 |
-
tooltip = text_of_attr(html, "#inet-#{id}", "title")
|
| 140 |
-
assert tooltip =~ "Added at #{Date.utc_today()}"
|
| 141 |
-
assert tooltip =~ "by #{user.name} <#{user.email}>"
|
| 142 |
-
|
| 143 |
-
assert [_] = Shields.list_ip_rules(site)
|
| 144 |
-
end
|
| 145 |
-
|
| 146 |
-
test "submitting a valid IP saves it", %{conn: conn, site: site} do
|
| 147 |
-
lv = get_liveview(conn, site)
|
| 148 |
-
|
| 149 |
-
lv
|
| 150 |
-
|> element("form")
|
| 151 |
-
|> render_submit(%{
|
| 152 |
-
"ip_rule[inet]" => "1.1.1.1",
|
| 153 |
-
"ip_rule[description]" => "A happy song"
|
| 154 |
-
})
|
| 155 |
-
|
| 156 |
-
html = render(lv)
|
| 157 |
-
|
| 158 |
-
assert html =~ "1.1.1.1"
|
| 159 |
-
assert html =~ "A happy song"
|
| 160 |
-
|
| 161 |
-
assert [%{inet: ip, description: "A happy song"}] = Shields.list_ip_rules(site)
|
| 162 |
-
assert to_string(ip) == "1.1.1.1"
|
| 163 |
-
end
|
| 164 |
-
|
| 165 |
-
test "submitting invalid IP renders error", %{conn: conn, site: site} do
|
| 166 |
-
lv = get_liveview(conn, site)
|
| 167 |
-
|
| 168 |
-
lv
|
| 169 |
-
|> element("form")
|
| 170 |
-
|> render_submit(%{
|
| 171 |
-
"ip_rule[inet]" => "WRONG"
|
| 172 |
-
})
|
| 173 |
-
|
| 174 |
-
html = render(lv)
|
| 175 |
-
assert html =~ "is invalid"
|
| 176 |
-
end
|
| 177 |
-
|
| 178 |
-
test "clicking Remove deletes the rule", %{conn: conn, site: site} do
|
| 179 |
-
{:ok, _} =
|
| 180 |
-
Shields.add_ip_rule(site, %{"inet" => "2.2.2.2", "description" => "Alice"})
|
| 181 |
-
|
| 182 |
-
lv = get_liveview(conn, site)
|
| 183 |
-
|
| 184 |
-
html = render(lv)
|
| 185 |
-
assert html =~ "2.2.2.2"
|
| 186 |
-
|
| 187 |
-
lv |> element(~s/button[phx-click="remove-ip-rule"]/) |> render_click()
|
| 188 |
-
|
| 189 |
-
html = render(lv)
|
| 190 |
-
refute html =~ "2.2.2.2"
|
| 191 |
-
|
| 192 |
-
assert Shields.count_ip_rules(site) == 0
|
| 193 |
-
end
|
| 194 |
-
|
| 195 |
-
defp get_liveview(conn, site) do
|
| 196 |
-
conn = assign(conn, :live_module, PlausibleWeb.Live.Shields)
|
| 197 |
-
{:ok, lv, _html} = live(conn, "/#{site.domain}/settings/shields/ip_addresses")
|
| 198 |
-
|
| 199 |
-
lv
|
| 200 |
-
end
|
| 201 |
-
end
|
| 202 |
-
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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