Delete test/plausible_web/live/components/form_test.exs with huggingface_hub
Browse files
test/plausible_web/live/components/form_test.exs
DELETED
|
@@ -1,181 +0,0 @@
|
|
| 1 |
-
defmodule PlausibleWeb.Live.Components.FormTest do
|
| 2 |
-
use PlausibleWeb.ConnCase, async: true
|
| 3 |
-
import Phoenix.LiveViewTest, only: [render_component: 2]
|
| 4 |
-
|
| 5 |
-
alias Plausible.Auth.User
|
| 6 |
-
alias PlausibleWeb.Live.Components.Form
|
| 7 |
-
|
| 8 |
-
describe "password_input_with_strength/1" do
|
| 9 |
-
test "renders for correct, strong password" do
|
| 10 |
-
doc = render_password_input_with_strength("very-secret-and-very-long-123")
|
| 11 |
-
|
| 12 |
-
assert element_exists?(doc, ~s/input#user_password[type="password"][name="user[password]"]/)
|
| 13 |
-
refute element_exists?(doc, "label")
|
| 14 |
-
assert text_of_element(doc, "p") =~ "Password strength:"
|
| 15 |
-
assert text_of_element(doc, "p") =~ "Strong"
|
| 16 |
-
end
|
| 17 |
-
|
| 18 |
-
test "renders with label when passed" do
|
| 19 |
-
doc =
|
| 20 |
-
render_password_input_with_strength(
|
| 21 |
-
"very-secret-and-very-long-123",
|
| 22 |
-
label: "New password"
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
assert element_exists?(doc, ~s/input#user_password[type="password"][name="user[password]"]/)
|
| 26 |
-
assert element_exists?(doc, ~s/label[for="user_password"]/)
|
| 27 |
-
assert text_of_element(doc, ~s/label[for="user_password"]/) == "New password"
|
| 28 |
-
end
|
| 29 |
-
|
| 30 |
-
test "renders weak password warning and hints when password too short" do
|
| 31 |
-
doc = render_password_input_with_strength("too-short")
|
| 32 |
-
|
| 33 |
-
assert text_of_element(doc, "p:first-of-type") == "Password is too weak."
|
| 34 |
-
assert text_of_element(doc, "p:last-of-type") != ""
|
| 35 |
-
end
|
| 36 |
-
|
| 37 |
-
test "does not render hints and suggestions paragraph when there's none" do
|
| 38 |
-
doc =
|
| 39 |
-
render_password_input_with_strength("very-secret-but-too-short",
|
| 40 |
-
strength: %{
|
| 41 |
-
score: 2,
|
| 42 |
-
warning: "",
|
| 43 |
-
suggestions: []
|
| 44 |
-
}
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
assert warning_p = find(doc, "p")
|
| 48 |
-
assert text(warning_p) == "Password is too weak."
|
| 49 |
-
end
|
| 50 |
-
|
| 51 |
-
test "renders too long password case gracefully" do
|
| 52 |
-
too_long_password = String.duplicate("very-long-very-secret-1234567890", 10)
|
| 53 |
-
doc = render_password_input_with_strength(too_long_password)
|
| 54 |
-
|
| 55 |
-
assert error_p = find(doc, "p")
|
| 56 |
-
assert text(error_p) =~ "cannot be longer than"
|
| 57 |
-
end
|
| 58 |
-
end
|
| 59 |
-
|
| 60 |
-
describe "password_length_hint/1" do
|
| 61 |
-
test "renders for long enough password" do
|
| 62 |
-
doc = render_password_length_hint("very-secret-and-very-long-123", 12)
|
| 63 |
-
|
| 64 |
-
assert p_hint = find(doc, "p")
|
| 65 |
-
assert text_of_attr(p_hint, "class") =~ "text-gray-500"
|
| 66 |
-
assert text(p_hint) == "At least 12 characters"
|
| 67 |
-
end
|
| 68 |
-
|
| 69 |
-
test "renders for too short password" do
|
| 70 |
-
doc = render_password_length_hint("too-short", 12)
|
| 71 |
-
|
| 72 |
-
assert p_hint = find(doc, "p")
|
| 73 |
-
assert text_of_attr(p_hint, "class") =~ "text-red-500"
|
| 74 |
-
assert text(p_hint) == "At least 12 characters"
|
| 75 |
-
end
|
| 76 |
-
|
| 77 |
-
test "renders gracefully for too long password" do
|
| 78 |
-
too_long_password = String.duplicate("very-long-very-secret-1234567890", 10)
|
| 79 |
-
doc = render_password_length_hint(too_long_password, 12)
|
| 80 |
-
|
| 81 |
-
assert p_hint = find(doc, "p")
|
| 82 |
-
assert text_of_attr(p_hint, "class") =~ "text-gray-500"
|
| 83 |
-
assert text(p_hint) == "At least 12 characters"
|
| 84 |
-
end
|
| 85 |
-
end
|
| 86 |
-
|
| 87 |
-
describe "strength_meter/1" do
|
| 88 |
-
test "renders too weak level" do
|
| 89 |
-
doc = render_component(&Form.strength_meter/1, score: 0, warning: "", suggestions: [])
|
| 90 |
-
|
| 91 |
-
refute element_exists?(doc, "div.rounded-full")
|
| 92 |
-
assert text_of_element(doc, "p") == "Password is too weak."
|
| 93 |
-
end
|
| 94 |
-
|
| 95 |
-
test "renders very weak level" do
|
| 96 |
-
doc = render_component(&Form.strength_meter/1, score: 1, warning: "", suggestions: [])
|
| 97 |
-
|
| 98 |
-
assert text_of_element(doc, "p") == "Password is too weak."
|
| 99 |
-
end
|
| 100 |
-
|
| 101 |
-
test "renders somewhat weak level" do
|
| 102 |
-
doc = render_component(&Form.strength_meter/1, score: 2, warning: "", suggestions: [])
|
| 103 |
-
|
| 104 |
-
assert text_of_element(doc, "p") == "Password is too weak."
|
| 105 |
-
end
|
| 106 |
-
|
| 107 |
-
test "renders strong level" do
|
| 108 |
-
doc = render_component(&Form.strength_meter/1, score: 3, warning: "", suggestions: [])
|
| 109 |
-
|
| 110 |
-
assert text_of_element(doc, "p") =~ "Password strength:"
|
| 111 |
-
assert text_of_element(doc, "p") =~ "Good"
|
| 112 |
-
end
|
| 113 |
-
|
| 114 |
-
test "renders very strong level" do
|
| 115 |
-
doc = render_component(&Form.strength_meter/1, score: 4, warning: "", suggestions: [])
|
| 116 |
-
|
| 117 |
-
assert text_of_element(doc, "p") =~ "Password strength:"
|
| 118 |
-
assert text_of_element(doc, "p") =~ "Strong"
|
| 119 |
-
end
|
| 120 |
-
|
| 121 |
-
test "renders hints paragraph when warning hint is present" do
|
| 122 |
-
doc =
|
| 123 |
-
render_component(&Form.strength_meter/1,
|
| 124 |
-
score: 2,
|
| 125 |
-
warning: "Test warning hint",
|
| 126 |
-
suggestions: []
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
-
assert text_of_element(doc, "p:last-of-type") == "Test warning hint."
|
| 130 |
-
end
|
| 131 |
-
|
| 132 |
-
test "renders only first suggestion when no warning present" do
|
| 133 |
-
doc =
|
| 134 |
-
render_component(&Form.strength_meter/1,
|
| 135 |
-
score: 2,
|
| 136 |
-
warning: "",
|
| 137 |
-
suggestions: ["Test suggestion 1.", "Test suggestion 2."]
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
assert text_of_element(doc, "p:last-of-type") == "Test suggestion 1."
|
| 141 |
-
end
|
| 142 |
-
|
| 143 |
-
@tag :slow
|
| 144 |
-
test "favors hint warning over suggestion when both present" do
|
| 145 |
-
doc =
|
| 146 |
-
render_component(&Form.strength_meter/1,
|
| 147 |
-
score: 2,
|
| 148 |
-
warning: "Test warning hint",
|
| 149 |
-
suggestions: ["Test suggestion 1.", "Test suggestion 2."]
|
| 150 |
-
)
|
| 151 |
-
|
| 152 |
-
assert text_of_element(doc, "p:last-of-type") == "Test warning hint."
|
| 153 |
-
end
|
| 154 |
-
end
|
| 155 |
-
|
| 156 |
-
defp render_password_input_with_strength(password, attrs \\ []) do
|
| 157 |
-
changeset =
|
| 158 |
-
%{"password" => password}
|
| 159 |
-
|> User.new()
|
| 160 |
-
|> Map.put(:action, :validate)
|
| 161 |
-
|
| 162 |
-
strength = User.password_strength(changeset)
|
| 163 |
-
form = Phoenix.Component.to_form(changeset)
|
| 164 |
-
|
| 165 |
-
render_component(
|
| 166 |
-
&Form.password_input_with_strength/1,
|
| 167 |
-
Keyword.merge([field: form[:password], strength: strength], attrs)
|
| 168 |
-
)
|
| 169 |
-
end
|
| 170 |
-
|
| 171 |
-
defp render_password_length_hint(password, minimum) do
|
| 172 |
-
changeset =
|
| 173 |
-
%{"password" => password}
|
| 174 |
-
|> User.new()
|
| 175 |
-
|> Map.put(:action, :validate)
|
| 176 |
-
|
| 177 |
-
form = Phoenix.Component.to_form(changeset)
|
| 178 |
-
|
| 179 |
-
render_component(&Form.password_length_hint/1, field: form[:password], minimum: minimum)
|
| 180 |
-
end
|
| 181 |
-
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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