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