Implement better user password validation (#3344)
* Add zxcvbn dependency * Change password length range requirement from 6-64 to 12-128 * Reimplement register form in LV * Implement server-side check for password strength * Add rudimentary strength meter * Make password input with strength a separate component and improve it * Fix existing tests to provide strong enough password * Apply formatting * Replace existing registration form with new one * Hide built-in label in `.input` component when none provided * Crop password to first 32 chars for analysis by zxcvbn * Add tests for new form components * Integrate hCaptcha into LV * Fix existing AuthController tests * Add tests for Live.RegisterForm * Hide strength meter when password input is empty * Randomize client IP in headers during tests to avoid hitting rate limit * Apply auxilliary formatting fixes to AuthController * Integrate registration from invitation into LV registration logic * Fix existing password set and reset forms * Make `password_length_hint` component more customizable * Optimize `Auth.User.set_password/2` * Remove unnecessary attribute from registration form * Move password set and reset forms to LV * Add tests for SetPasswordForm LV component * Add tests for password checks in `Auth.User` * Document code a bit * Implement simpler approach to hCaptcha integration * Update CHANGELOG.md * Improve consistency of color scheme * Introduce debounce across all text inputs in registration and password forms * Fix email input background in register form * Ensure only single error is rendered for empty password confirmation case * Remove `/password` form entirely in favor of preferred password reset * Remove unnecessary `router` option from `live_render` calls * Make expensive assigns in LV with `assign_new` (h/t @aerosol) * Accept passwords longer than 32 bytes uniformly as very strong * Avoid displaying blank error side by side with weak password error * Make register actions handle errors gracefully * Render only a single piece of feedback to reduce noise * Make register and password reset forms pw manager friendly (h/t @cnkk) * Move registration forms to live routes * Delete no longer used deadviews * Adjust registration form in accordance to changes in #3290 * Reintroduce dogfood page path for invitation form from #3290 * Use alternative approach to submitting plausible metrics from LV form * Rename metrics events and extend tests to account for themfix/redirect-to-people-settings-after-removing-pending-invitation r2023.09.25-194
parent
b1ade191eb
commit
51c1138d0d
@ -0,0 +1,328 @@
|
||||
defmodule PlausibleWeb.Live.RegisterForm do
|
||||
@moduledoc """
|
||||
LiveView for registration form.
|
||||
"""
|
||||
|
||||
use Phoenix.LiveView
|
||||
use Phoenix.HTML
|
||||
|
||||
import PlausibleWeb.Live.Components.Form
|
||||
|
||||
alias Plausible.Auth
|
||||
alias Plausible.Repo
|
||||
|
||||
alias PlausibleWeb.Router.Helpers, as: Routes
|
||||
|
||||
def mount(params, _session, socket) do
|
||||
socket =
|
||||
assign_new(socket, :invitation, fn ->
|
||||
if invitation_id = params["invitation_id"] do
|
||||
Repo.get_by(Auth.Invitation, invitation_id: invitation_id)
|
||||
end
|
||||
end)
|
||||
|
||||
if socket.assigns.live_action == :register_from_invitation_form and
|
||||
socket.assigns.invitation == nil do
|
||||
{:ok, assign(socket, invitation_expired: true)}
|
||||
else
|
||||
changeset =
|
||||
if invitation = socket.assigns.invitation do
|
||||
Auth.User.changeset(%Auth.User{email: invitation.email})
|
||||
else
|
||||
Auth.User.changeset(%Auth.User{})
|
||||
end
|
||||
|
||||
{:ok,
|
||||
assign(socket,
|
||||
form: to_form(changeset),
|
||||
captcha_error: nil,
|
||||
password_strength: Auth.User.password_strength(changeset),
|
||||
is_selfhost: Plausible.Release.selfhost?(),
|
||||
trigger_submit: false
|
||||
)}
|
||||
end
|
||||
end
|
||||
|
||||
def render(%{invitation_expired: true} = assigns) do
|
||||
~H"""
|
||||
<div class="mx-auto mt-6 text-center dark:text-gray-300">
|
||||
<h1 class="text-3xl font-black">Plausible Analytics</h1>
|
||||
<div class="text-xl font-medium">Lightweight and privacy-friendly web analytics</div>
|
||||
</div>
|
||||
|
||||
<div class="w-full max-w-md mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 py-6 mb-4 mt-8">
|
||||
<h2 class="text-xl font-black dark:text-gray-100">Invitation expired</h2>
|
||||
|
||||
<p class="mt-4 text-sm">
|
||||
Your invitation has expired or been revoked. Please request fresh one or you can <%= link(
|
||||
"sign up",
|
||||
class: "text-indigo-600 hover:text-indigo-900",
|
||||
to: Routes.auth_path(@socket, :register)
|
||||
) %> for a 30-day unlimited free trial without an invitation.
|
||||
</p>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div class="mx-auto mt-6 text-center dark:text-gray-300">
|
||||
<h1 class="text-3xl font-black">
|
||||
<%= if @is_selfhost or @live_action == :register_from_invitation_form do %>
|
||||
Register your Plausible Analytics account
|
||||
<% else %>
|
||||
Register your 30-day unlimited-use free trial
|
||||
<% end %>
|
||||
</h1>
|
||||
<div class="text-xl font-medium">Set up privacy-friendly analytics with just a few clicks</div>
|
||||
</div>
|
||||
|
||||
<div class="w-full max-w-3xl mt-4 mx-auto flex flex-shrink-0">
|
||||
<.form
|
||||
:let={f}
|
||||
for={@form}
|
||||
id="register-form"
|
||||
phx-hook="Metrics"
|
||||
phx-change="validate"
|
||||
phx-submit="register"
|
||||
phx-trigger-action={@trigger_submit}
|
||||
class="w-full max-w-md mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 py-6 mb-4 mt-8"
|
||||
>
|
||||
<input name="_csrf_token" type="hidden" value={Plug.CSRFProtection.get_csrf_token()} />
|
||||
|
||||
<h2 class="text-xl font-black dark:text-gray-100">Enter your details</h2>
|
||||
|
||||
<%= if @invitation do %>
|
||||
<.email_input field={f[:email]} for_invitation={true} />
|
||||
<.name_input field={f[:name]} />
|
||||
<% else %>
|
||||
<.name_input field={f[:name]} />
|
||||
<.email_input field={f[:email]} for_invitation={false} />
|
||||
<% end %>
|
||||
|
||||
<div class="my-4">
|
||||
<div class="flex justify-between">
|
||||
<label
|
||||
for={f[:password].name}
|
||||
class="block text-sm font-medium text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
Password
|
||||
</label>
|
||||
<.password_length_hint minimum={12} field={f[:password]} />
|
||||
</div>
|
||||
<div class="mt-1">
|
||||
<.password_input_with_strength
|
||||
field={f[:password]}
|
||||
strength={@password_strength}
|
||||
phx-debounce={200}
|
||||
class="dark:bg-gray-900 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:text-gray-300"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="my-4">
|
||||
<label
|
||||
for={f[:password_confirmation].name}
|
||||
class="block text-sm font-medium text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
Password confirmation
|
||||
</label>
|
||||
<div class="mt-1">
|
||||
<.input
|
||||
type="password"
|
||||
autocomplete="new-password"
|
||||
field={f[:password_confirmation]}
|
||||
phx-debounce={200}
|
||||
class="dark:bg-gray-900 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:text-gray-300"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<%= if PlausibleWeb.Captcha.enabled?() do %>
|
||||
<div class="mt-4">
|
||||
<div
|
||||
phx-update="ignore"
|
||||
id="hcaptcha-placeholder"
|
||||
class="h-captcha"
|
||||
data-sitekey={PlausibleWeb.Captcha.sitekey()}
|
||||
>
|
||||
</div>
|
||||
<%= if @captcha_error do %>
|
||||
<div class="text-red-500 text-xs italic mt-3"><%= @captcha_error %></div>
|
||||
<% end %>
|
||||
<script
|
||||
phx-update="ignore"
|
||||
id="hcaptcha-script"
|
||||
src="https://hcaptcha.com/1/api.js"
|
||||
async
|
||||
defer
|
||||
>
|
||||
</script>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<% submit_text =
|
||||
if @is_selfhost or @invitation do
|
||||
"Create my account →"
|
||||
else
|
||||
"Start my free trial →"
|
||||
end %>
|
||||
|
||||
<button id="register" type="submit" class="button mt-4 w-full">
|
||||
<%= submit_text %>
|
||||
</button>
|
||||
|
||||
<p class="text-center text-gray-600 dark:text-gray-500 text-xs mt-4">
|
||||
Already have an account? <%= link("Log in",
|
||||
to: "/login",
|
||||
class: "underline text-gray-800 dark:text-gray-50"
|
||||
) %> instead.
|
||||
</p>
|
||||
</.form>
|
||||
<div :if={@live_action == :register_form} class="pt-12 pl-8 hidden md:block">
|
||||
<%= PlausibleWeb.AuthView.render("_onboarding_steps.html", current_step: 0) %>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
defp name_input(assigns) do
|
||||
~H"""
|
||||
<div class="my-4">
|
||||
<label for={@field.name} class="block text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
Full name
|
||||
</label>
|
||||
<div class="mt-1">
|
||||
<.input
|
||||
field={@field}
|
||||
placeholder="Jane Doe"
|
||||
phx-debounce={200}
|
||||
class="dark:bg-gray-900 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:text-gray-300"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
defp email_input(assigns) do
|
||||
email_classes = ~w(
|
||||
dark:bg-gray-900
|
||||
shadow-sm
|
||||
focus:ring-indigo-500
|
||||
focus:border-indigo-500
|
||||
block
|
||||
w-full
|
||||
sm:text-sm
|
||||
border-gray-300
|
||||
dark:border-gray-500
|
||||
rounded-md
|
||||
dark:text-gray-300
|
||||
)
|
||||
|
||||
{email_readonly, email_extra_classes} =
|
||||
if assigns[:for_invitation] do
|
||||
{[readonly: "readonly"], ["bg-gray-100"]}
|
||||
else
|
||||
{[], []}
|
||||
end
|
||||
|
||||
assigns =
|
||||
assigns
|
||||
|> assign(:email_readonly, email_readonly)
|
||||
|> assign(:email_classes, email_classes ++ email_extra_classes)
|
||||
|
||||
~H"""
|
||||
<div class="my-4">
|
||||
<div class="flex justify-between">
|
||||
<label for={@field.name} class="block text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
Email
|
||||
</label>
|
||||
<p class="text-xs text-gray-500 mt-1">No spam, guaranteed.</p>
|
||||
</div>
|
||||
<div class="mt-1">
|
||||
<.input
|
||||
type="email"
|
||||
field={@field}
|
||||
placeholder="example@email.com"
|
||||
phx-debounce={200}
|
||||
class={@email_classes}
|
||||
{@email_readonly}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def handle_event("validate", %{"user" => params}, socket) do
|
||||
changeset =
|
||||
params
|
||||
|> Auth.User.new()
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
password_strength = Auth.User.password_strength(changeset)
|
||||
|
||||
{:noreply, assign(socket, form: to_form(changeset), password_strength: password_strength)}
|
||||
end
|
||||
|
||||
def handle_event(
|
||||
"register",
|
||||
%{"user" => _} = params,
|
||||
%{assigns: %{invitation: %{} = invitation}} = socket
|
||||
) do
|
||||
if not PlausibleWeb.Captcha.enabled?() or
|
||||
PlausibleWeb.Captcha.verify(params["h-captcha-response"]) do
|
||||
user =
|
||||
params["user"]
|
||||
|> Map.put("email", invitation.email)
|
||||
|> Auth.User.new()
|
||||
|
||||
user =
|
||||
case invitation.role do
|
||||
:owner -> user
|
||||
_ -> Plausible.Auth.User.remove_trial_expiry(user)
|
||||
end
|
||||
|
||||
add_user(socket, user)
|
||||
else
|
||||
{:noreply, assign(socket, :captcha_error, "Please complete the captcha to register")}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("register", %{"user" => _} = params, socket) do
|
||||
if not PlausibleWeb.Captcha.enabled?() or
|
||||
PlausibleWeb.Captcha.verify(params["h-captcha-response"]) do
|
||||
user = Auth.User.new(params["user"])
|
||||
|
||||
add_user(socket, user)
|
||||
else
|
||||
{:noreply, assign(socket, :captcha_error, "Please complete the captcha to register")}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("send-metrics-after", _params, socket) do
|
||||
{:noreply, assign(socket, trigger_submit: true)}
|
||||
end
|
||||
|
||||
defp add_user(socket, user) do
|
||||
case Repo.insert(user) do
|
||||
{:ok, _user} ->
|
||||
metrics_params =
|
||||
if socket.assigns.invitation do
|
||||
%{
|
||||
event_name: "Signup via invitation",
|
||||
params: %{u: "/register/invitation/:invitation_id"}
|
||||
}
|
||||
else
|
||||
%{event_name: "Signup", params: %{}}
|
||||
end
|
||||
|
||||
{:noreply, push_event(socket, "send-metrics", metrics_params)}
|
||||
|
||||
{:error, changeset} ->
|
||||
{:noreply,
|
||||
assign(socket,
|
||||
form: to_form(Map.put(changeset, :action, :validate))
|
||||
)}
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,102 @@
|
||||
defmodule PlausibleWeb.Live.ResetPasswordForm do
|
||||
@moduledoc """
|
||||
LiveView for password reset form.
|
||||
"""
|
||||
|
||||
use Phoenix.LiveView
|
||||
use Phoenix.HTML
|
||||
|
||||
import PlausibleWeb.Live.Components.Form
|
||||
|
||||
alias Plausible.Auth
|
||||
alias Plausible.Repo
|
||||
|
||||
def mount(_params, %{"reset_token" => reset_token}, socket) do
|
||||
socket =
|
||||
assign_new(socket, :user, fn ->
|
||||
# by that point token should be already verified
|
||||
{:ok, %{email: email}} = Auth.Token.verify_password_reset(reset_token)
|
||||
Repo.get_by!(Auth.User, email: email)
|
||||
end)
|
||||
|
||||
changeset = Auth.User.changeset(socket.assigns.user)
|
||||
|
||||
{:ok,
|
||||
assign(socket,
|
||||
form: to_form(changeset),
|
||||
reset_token: reset_token,
|
||||
password_strength: Auth.User.password_strength(changeset),
|
||||
trigger_submit: false
|
||||
)}
|
||||
end
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<.form
|
||||
:let={f}
|
||||
for={@form}
|
||||
method="post"
|
||||
phx-change="validate"
|
||||
phx-submit="set"
|
||||
phx-trigger-action={@trigger_submit}
|
||||
class="bg-white dark:bg-gray-800 max-w-md w-full mx-auto shadow-md rounded px-8 py-6 mt-8"
|
||||
>
|
||||
<input name="_csrf_token" type="hidden" value={Plug.CSRFProtection.get_csrf_token()} />
|
||||
<h2 class="text-xl font-black dark:text-gray-100">
|
||||
Reset your password
|
||||
</h2>
|
||||
<div class="my-4">
|
||||
<.password_length_hint
|
||||
minimum={12}
|
||||
field={f[:password]}
|
||||
class={["text-sm", "mt-1", "mb-2"]}
|
||||
ok_class="text-gray-600 dark:text-gray-600"
|
||||
error_class="text-red-600 dark:text-red-500"
|
||||
/>
|
||||
<.password_input_with_strength
|
||||
field={f[:password]}
|
||||
strength={@password_strength}
|
||||
phx-debounce={200}
|
||||
class="transition bg-gray-100 dark:bg-gray-900 outline-none appearance-none border border-transparent rounded w-full p-2 text-gray-700 dark:text-gray-300 leading-normal appearance-none focus:outline-none focus:bg-white dark:focus:bg-gray-800 focus:border-gray-300 dark:focus:border-gray-500"
|
||||
/>
|
||||
</div>
|
||||
<input name="token" type="hidden" value={@reset_token} />
|
||||
<button id="set" type="submit" class="button mt-4 w-full">
|
||||
Set password →
|
||||
</button>
|
||||
<p class="text-center text-gray-500 text-xs mt-4">
|
||||
Don't have an account? <%= link("Register",
|
||||
to: "/register",
|
||||
class: "underline text-gray-800 dark:text-gray-200"
|
||||
) %> instead.
|
||||
</p>
|
||||
</.form>
|
||||
"""
|
||||
end
|
||||
|
||||
def handle_event("validate", %{"user" => %{"password" => password}}, socket) do
|
||||
changeset =
|
||||
socket.assigns.user
|
||||
|> Auth.User.set_password(password)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
password_strength = Auth.User.password_strength(changeset)
|
||||
|
||||
{:noreply, assign(socket, form: to_form(changeset), password_strength: password_strength)}
|
||||
end
|
||||
|
||||
def handle_event("set", %{"user" => %{"password" => password}}, socket) do
|
||||
user = Auth.User.set_password(socket.assigns.user, password)
|
||||
|
||||
case Repo.update(user) do
|
||||
{:ok, _user} ->
|
||||
{:noreply, assign(socket, trigger_submit: true)}
|
||||
|
||||
{:error, changeset} ->
|
||||
{:noreply,
|
||||
assign(socket,
|
||||
form: to_form(Map.put(changeset, :action, :validate))
|
||||
)}
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,37 @@
|
||||
defmodule PlausibleWeb.Plugs.MaybeDisableRegistration do
|
||||
@moduledoc """
|
||||
Plug toggling registration according to selfhosted state.
|
||||
"""
|
||||
|
||||
import Phoenix.Controller
|
||||
import Plug.Conn
|
||||
|
||||
alias Plausible.Release
|
||||
alias PlausibleWeb.Router.Helpers, as: Routes
|
||||
|
||||
def init(opts) do
|
||||
opts
|
||||
end
|
||||
|
||||
def call(conn, _opts) do
|
||||
disabled_for = List.wrap(conn.assigns.disable_registration_for)
|
||||
|
||||
selfhost_config = Application.get_env(:plausible, :selfhost)
|
||||
disable_registration = Keyword.fetch!(selfhost_config, :disable_registration)
|
||||
first_launch? = Release.should_be_first_launch?()
|
||||
|
||||
cond do
|
||||
first_launch? ->
|
||||
conn
|
||||
|
||||
disable_registration in disabled_for ->
|
||||
conn
|
||||
|> put_flash(:error, "Registration is disabled on this instance")
|
||||
|> redirect(to: Routes.auth_path(conn, :login_form))
|
||||
|> halt()
|
||||
|
||||
true ->
|
||||
conn
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,12 +0,0 @@
|
||||
<div class="mx-auto mt-6 text-center dark:text-gray-300">
|
||||
<h1 class="text-3xl font-black">Plausible Analytics</h1>
|
||||
<div class="text-xl font-medium">Lightweight and privacy-friendly web analytics</div>
|
||||
</div>
|
||||
|
||||
<div class="w-full max-w-md mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 py-6 mb-4 mt-8">
|
||||
<h2 class="text-xl font-black dark:text-gray-100">Invitation expired</h2>
|
||||
|
||||
<p class="mt-4 text-sm">
|
||||
Your invitation has expired or been revoked. Please request fresh one or you can <%= link("sign up", class: "text-indigo-600 hover:text-indigo-900", to: Routes.auth_path(@conn, :register)) %> for a 30-day unlimited free trial without an invitation.
|
||||
</p>
|
||||
</div>
|
||||
@ -1,14 +0,0 @@
|
||||
<%= form_for @conn, "/password", [class: "bg-white dark:bg-gray-800 max-w-md w-full mx-auto shadow-md rounded px-8 py-6 mt-8"], fn f -> %>
|
||||
<h2 class="text-xl font-black dark:text-gray-100">Set your password</h2>
|
||||
<div class="my-4">
|
||||
<p class="text-gray-600 dark:text-gray-400 text-sm mt-1 mb-2">Min 6 characters</p>
|
||||
<%= password_input f, :password, class: "transition bg-gray-100 dark:bg-gray-900 outline-none appearance-none border border-transparent rounded w-full p-2 text-gray-700 dark:text-gray-300 leading-normal appearance-none focus:outline-none focus:bg-white dark:focus:bg-gray-800 focus:border-gray-300 dark:focus:border-gray-500" %>
|
||||
<%= if @conn.assigns[:changeset] do %>
|
||||
<%= error_tag @changeset, :password %>
|
||||
<% end %>
|
||||
</div>
|
||||
<%= submit "Set password →", class: "button mt-4 w-full" %>
|
||||
<p class="text-center text-gray-500 text-xs mt-4">
|
||||
Don't have an account? <%= link("Register", to: "/register", class: "underline text-gray-800 dark:text-gray-200") %> instead.
|
||||
</p>
|
||||
<% end %>
|
||||
@ -1,15 +1,4 @@
|
||||
<%= form_for @conn, "/password/reset", [class: "bg-white dark:bg-gray-800 max-w-md w-full mx-auto shadow-md rounded px-8 py-6 mt-8"], fn f -> %>
|
||||
<h2 class="text-xl font-black dark:text-gray-100">Reset your password</h2>
|
||||
<div class="my-4">
|
||||
<p class="text-gray-600 dark:text-gray-400 text-sm mt-1 mb-2">Min 6 characters</p>
|
||||
<%= password_input f, :password, class: "transition bg-gray-100 dark:bg-gray-900 outline-none appearance-none border border-transparent rounded w-full p-2 text-gray-700 dark:text-gray-300 leading-normal appearance-none focus:outline-none focus:bg-white dark:focus:bg-gray-800 focus:border-gray-300 dark:focus:border-gray-500" %>
|
||||
<%= if @conn.assigns[:changeset] do %>
|
||||
<%= error_tag @changeset, :password %>
|
||||
<% end %>
|
||||
</div>
|
||||
<%= hidden_input f, :token, value: @token %>
|
||||
<%= submit "Set password →", class: "button mt-4 w-full" %>
|
||||
<p class="text-center text-gray-500 text-xs mt-4">
|
||||
Don't have an account? <%= link("Register", to: "/register", class: "underline text-gray-800 dark:text-gray-200") %> instead.
|
||||
</p>
|
||||
<% end %>
|
||||
<%= live_render(@conn, PlausibleWeb.Live.ResetPasswordForm,
|
||||
container: {:div, class: "contents"},
|
||||
session: %{"reset_token" => @token}
|
||||
) %>
|
||||
|
||||
@ -1,79 +0,0 @@
|
||||
<div class="mx-auto mt-6 text-center dark:text-gray-300">
|
||||
<h1 class="text-3xl font-black">
|
||||
<%= if @is_selfhost do %>
|
||||
Register your Plausible Analytics account
|
||||
<% else %>
|
||||
Register your 30-day unlimited-use free trial
|
||||
<% end %>
|
||||
</h1>
|
||||
<div class="text-xl font-medium">Set up privacy-friendly analytics with just a few clicks</div>
|
||||
</div>
|
||||
|
||||
<div class="w-full max-w-3xl mt-4 mx-auto flex flex-shrink-0">
|
||||
<%= form_for @changeset, "/register", [class: "w-full max-w-md mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 py-6 mb-4 mt-8 plausible-event-name=Signup"], fn f -> %>
|
||||
<h2 class="text-xl font-black dark:text-gray-100">Enter your details</h2>
|
||||
<div class="my-4">
|
||||
<%= label f, :name, "Full name", class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %>
|
||||
<div class="mt-1">
|
||||
<%= text_input f, :name, class: "dark:bg-gray-900 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:text-gray-300", placeholder: "Jane Doe" %>
|
||||
</div>
|
||||
<%= error_tag f, :name %>
|
||||
</div>
|
||||
<div class="my-4">
|
||||
<div class="flex justify-between">
|
||||
<%= label f, :email, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %>
|
||||
<p class="text-xs text-gray-500 mt-1">No spam, guaranteed.</p>
|
||||
</div>
|
||||
<div class="mt-1">
|
||||
<%= email_input f, :email, class: "dark:bg-gray-900 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:text-gray-300", placeholder: "example@email.com" %>
|
||||
</div>
|
||||
<%= error_tag f, :email %>
|
||||
</div>
|
||||
|
||||
<div class="my-4">
|
||||
<div class="flex justify-between">
|
||||
<%= label f, :password, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %>
|
||||
<p class="text-xs text-gray-500 mt-1">Min 6 characters</p>
|
||||
</div>
|
||||
<div class="mt-1">
|
||||
<%= password_input f, :password, class: "dark:bg-gray-900 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:text-gray-300" %>
|
||||
</div>
|
||||
<%= error_tag f, :password %>
|
||||
</div>
|
||||
|
||||
<div class="my-4">
|
||||
<%= label f, :password_confirmation, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %>
|
||||
<div class="mt-1">
|
||||
<%= password_input f, :password_confirmation, class: "dark:bg-gray-900 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:text-gray-300" %>
|
||||
</div>
|
||||
<%= error_tag f, :password_confirmation %>
|
||||
</div>
|
||||
|
||||
<%= if PlausibleWeb.Captcha.enabled?() do %>
|
||||
<div class="mt-4">
|
||||
<div class="h-captcha" data-sitekey="<%= PlausibleWeb.Captcha.sitekey() %>"></div>
|
||||
<%= if assigns[:captcha_error] do %>
|
||||
<div class="text-red-500 text-xs italic mt-3"><%= @captcha_error %></div>
|
||||
<% end %>
|
||||
<script src="https://hcaptcha.com/1/api.js" async defer></script>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%
|
||||
submit_text =
|
||||
if @is_selfhost do
|
||||
"Create my account →"
|
||||
else
|
||||
"Start my free trial →"
|
||||
end
|
||||
%>
|
||||
<%= submit submit_text, class: "button mt-4 w-full" %>
|
||||
|
||||
<p class="text-center text-gray-600 dark:text-gray-500 text-xs mt-4">
|
||||
Already have an account? <%= link("Log in", to: "/login", class: "underline text-gray-800 dark:text-gray-50") %> instead.
|
||||
</p>
|
||||
<% end %>
|
||||
<div class="pt-12 pl-8 hidden md:block">
|
||||
<%= render(PlausibleWeb.AuthView, "_onboarding_steps.html", current_step: 0) %>
|
||||
</div>
|
||||
</div>
|
||||
@ -1,61 +0,0 @@
|
||||
<div class="mx-auto mt-6 text-center dark:text-gray-300">
|
||||
<h1 class="text-3xl font-black">Register your Plausible Analytics account</h1>
|
||||
<div class="text-xl font-medium">Set up privacy-friendly analytics with just a few clicks</div>
|
||||
</div>
|
||||
|
||||
<%= form_for @changeset, Routes.auth_path(@conn, :register_from_invitation_form, @invitation.invitation_id), [class: "w-full max-w-md mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 py-6 mb-4 mt-8", id: "register-via-invitation-form"], fn f -> %>
|
||||
<h2 class="text-xl font-black dark:text-gray-100">Enter your details</h2>
|
||||
<div class="my-4">
|
||||
<div class="flex justify-between">
|
||||
<%= label f, :email, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %>
|
||||
<p class="text-xs text-gray-500 mt-1">No spam, guaranteed.</p>
|
||||
</div>
|
||||
<div class="mt-1">
|
||||
<%= email_input f, :email, class: "bg-gray-100 dark:bg-gray-900 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:text-gray-300", placeholder: "example@email.com", value: @invitation.email, readonly: "readonly" %>
|
||||
</div>
|
||||
<%= error_tag f, :email %>
|
||||
</div>
|
||||
|
||||
<div class="my-4">
|
||||
<%= label f, :name, "Full name", class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %>
|
||||
<div class="mt-1">
|
||||
<%= text_input f, :name, class: "dark:bg-gray-900 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:text-gray-300", placeholder: "Jane Doe" %>
|
||||
</div>
|
||||
<%= error_tag f, :name %>
|
||||
</div>
|
||||
|
||||
<div class="my-4">
|
||||
<div class="flex justify-between">
|
||||
<%= label f, :password, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %>
|
||||
<p class="text-xs text-gray-500 mt-1">Min 6 characters</p>
|
||||
</div>
|
||||
<div class="mt-1">
|
||||
<%= password_input f, :password, class: "dark:bg-gray-900 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:text-gray-300" %>
|
||||
</div>
|
||||
<%= error_tag f, :password %>
|
||||
</div>
|
||||
|
||||
<div class="my-4">
|
||||
<%= label f, :password_confirmation, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %>
|
||||
<div class="mt-1">
|
||||
<%= password_input f, :password_confirmation, class: "dark:bg-gray-900 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:text-gray-300" %>
|
||||
</div>
|
||||
<%= error_tag f, :password_confirmation %>
|
||||
</div>
|
||||
|
||||
<%= if PlausibleWeb.Captcha.enabled?() do %>
|
||||
<div class="mt-4">
|
||||
<div class="h-captcha" data-sitekey="<%= PlausibleWeb.Captcha.sitekey() %>"></div>
|
||||
<%= if assigns[:captcha_error] do %>
|
||||
<div class="text-red-500 text-xs italic mt-3"><%= @captcha_error %></div>
|
||||
<% end %>
|
||||
<script src="https://hcaptcha.com/1/api.js" async defer></script>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= submit "Create my account →", class: "button mt-4 w-full" %>
|
||||
|
||||
<p class="text-center text-gray-600 dark:text-gray-500 text-xs mt-4">
|
||||
Already have an account? <%= link("Log in", to: "/login", class: "underline text-gray-800 dark:text-gray-50") %> instead.
|
||||
</p>
|
||||
<% end %>
|
||||
@ -0,0 +1,86 @@
|
||||
defmodule Plausible.Auth.UserTest do
|
||||
use Plausible.DataCase, async: true
|
||||
|
||||
alias Plausible.Auth.User
|
||||
|
||||
describe "password_strength/1" do
|
||||
test "scores password with all arguments in changes" do
|
||||
assert %{score: score, warning: warning, suggestions: suggestions} =
|
||||
%User{}
|
||||
|> change(
|
||||
name: "Jane Doe",
|
||||
email: "user@example.com",
|
||||
password: "asd"
|
||||
)
|
||||
|> User.password_strength()
|
||||
|
||||
assert score < 3
|
||||
assert warning != ""
|
||||
assert length(suggestions) > 0
|
||||
end
|
||||
|
||||
test "checks for existing phrases using name and email from changes" do
|
||||
strength =
|
||||
%User{}
|
||||
|> change(
|
||||
name: "Clayman Sillywaggle",
|
||||
email: "clay@example.com",
|
||||
password: "claymansillywaggle"
|
||||
)
|
||||
|> User.password_strength()
|
||||
|
||||
assert strength.score == 1
|
||||
end
|
||||
|
||||
test "checks for existing phrases using name and email from source" do
|
||||
strength =
|
||||
%User{
|
||||
name: "Clayman Sillywaggle",
|
||||
email: "clay@example.com"
|
||||
}
|
||||
|> change(password: "claymansillywaggle")
|
||||
|> User.password_strength()
|
||||
|
||||
assert strength.score == 1
|
||||
end
|
||||
|
||||
test "treats passwords past 32 bytes as very strong" do
|
||||
strength =
|
||||
%User{
|
||||
name: "Clayman Sillywaggle",
|
||||
email: "clay@example.com"
|
||||
}
|
||||
|> change(password: String.duplicate("a", 33))
|
||||
|> User.password_strength()
|
||||
|
||||
assert strength.score == 4
|
||||
end
|
||||
end
|
||||
|
||||
describe "password strength validation" do
|
||||
test "succeeds for complex enough password" do
|
||||
changeset =
|
||||
User.new(%{
|
||||
name: "Jane Doe",
|
||||
email: "jane@example.com",
|
||||
password: "very-secret-and-very-long-123",
|
||||
password_confirmation: "very-secret-and-very-long-123"
|
||||
})
|
||||
|
||||
assert changeset.valid?
|
||||
end
|
||||
|
||||
test "fails for password not complex enough" do
|
||||
changeset =
|
||||
User.new(%{
|
||||
name: "Jane Doe",
|
||||
email: "jane@example.com",
|
||||
password: "asdasdasdasd",
|
||||
password_confirmation: "asdasdasdasd"
|
||||
})
|
||||
|
||||
refute changeset.valid?
|
||||
assert {"is too weak", [validation: :strength]} = changeset.errors[:password]
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,198 @@
|
||||
defmodule PlausibleWeb.Live.Components.FormTest do
|
||||
use PlausibleWeb.ConnCase, async: true
|
||||
import Phoenix.LiveViewTest
|
||||
import Plausible.Test.Support.HTML
|
||||
|
||||
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]"]/)
|
||||
assert element_exists?(doc, ~s/div.rounded-full.bg-indigo-600/)
|
||||
refute element_exists?(doc, "label")
|
||||
refute element_exists?(doc, "p")
|
||||
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 [warning_p, hint_p] = find(doc, "p")
|
||||
assert text(warning_p) == "Password is too weak"
|
||||
assert text(hint_p) != ""
|
||||
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) == "Min 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) == "Min 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) == "Min 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: [])
|
||||
meter = find(doc, "div.rounded-full")
|
||||
|
||||
assert text_of_attr(meter, "style") == "width: 0%"
|
||||
assert [p_warning] = find(doc, "p")
|
||||
assert text(p_warning) == "Password is too weak"
|
||||
end
|
||||
|
||||
test "renders very weak level" do
|
||||
doc = render_component(&Form.strength_meter/1, score: 1, warning: "", suggestions: [])
|
||||
meter = find(doc, "div.rounded-full")
|
||||
|
||||
assert text_of_attr(meter, "style") == "width: 25%"
|
||||
assert [p_warning] = find(doc, "p")
|
||||
assert text(p_warning) == "Password is too weak"
|
||||
end
|
||||
|
||||
test "renders somewhat weak level" do
|
||||
doc = render_component(&Form.strength_meter/1, score: 2, warning: "", suggestions: [])
|
||||
meter = find(doc, "div.rounded-full")
|
||||
|
||||
assert text_of_attr(meter, "style") == "width: 50%"
|
||||
assert [p_warning] = find(doc, "p")
|
||||
assert text(p_warning) == "Password is too weak"
|
||||
end
|
||||
|
||||
test "renders strong level" do
|
||||
doc = render_component(&Form.strength_meter/1, score: 3, warning: "", suggestions: [])
|
||||
meter = find(doc, "div.rounded-full")
|
||||
|
||||
assert text_of_attr(meter, "style") == "width: 75%"
|
||||
assert find(doc, "p") == []
|
||||
end
|
||||
|
||||
test "renders very strong level" do
|
||||
doc = render_component(&Form.strength_meter/1, score: 4, warning: "", suggestions: [])
|
||||
meter = find(doc, "div.rounded-full")
|
||||
|
||||
assert text_of_attr(meter, "style") == "width: 100%"
|
||||
assert find(doc, "p") == []
|
||||
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 [_p_warning, p_hint] = find(doc, "p")
|
||||
assert text(p_hint) == "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 [_p_warning, p_hint] = find(doc, "p")
|
||||
assert text(p_hint) =~ "Test suggestion 1."
|
||||
refute text(p_hint) =~ "Test suggestion 2."
|
||||
end
|
||||
|
||||
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 [_p_warning, p_hint] = find(doc, "p")
|
||||
assert text(p_hint) =~ "Test warning hint."
|
||||
refute text(p_hint) =~ "Test suggestion 1."
|
||||
refute text(p_hint) =~ "Test suggestion 2."
|
||||
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
|
||||
@ -0,0 +1,309 @@
|
||||
defmodule PlausibleWeb.Live.RegisterFormTest do
|
||||
use PlausibleWeb.ConnCase, async: true
|
||||
|
||||
import Mox
|
||||
import Phoenix.LiveViewTest
|
||||
import Plausible.Test.Support.HTML
|
||||
|
||||
alias Plausible.Auth.User
|
||||
alias Plausible.Repo
|
||||
|
||||
setup :verify_on_exit!
|
||||
|
||||
describe "/register" do
|
||||
test "renders registration form (LV)", %{conn: conn} do
|
||||
lv = get_liveview(conn, "/register")
|
||||
html = render(lv)
|
||||
|
||||
assert element_exists?(html, ~s|form[phx-change="validate"][phx-submit="register"]|)
|
||||
assert element_exists?(html, ~s|input[type="hidden"][name="_csrf_token"]|)
|
||||
assert element_exists?(html, ~s|input#register-form_name[type="text"][name="user[name]"]|)
|
||||
|
||||
assert element_exists?(
|
||||
html,
|
||||
~s|input#register-form_email[type="email"][name="user[email]"]|
|
||||
)
|
||||
|
||||
assert element_exists?(
|
||||
html,
|
||||
~s|input#register-form_password[type="password"][name="user[password]"]|
|
||||
)
|
||||
|
||||
assert element_exists?(
|
||||
html,
|
||||
~s|input#register-form_password_confirmation[type="password"][name="user[password_confirmation]"]|
|
||||
)
|
||||
|
||||
assert element_exists?(html, ~s|button[type="submit"]|)
|
||||
end
|
||||
|
||||
test "renders validation errors depending on input", %{conn: conn} do
|
||||
lv = get_liveview(conn, "/register")
|
||||
|
||||
type_into_input(lv, "user[password]", "too-short")
|
||||
html = render(lv)
|
||||
|
||||
assert html =~ "Password is too weak"
|
||||
|
||||
type_into_input(lv, "user[password]", "very-long-and-very-secret-123")
|
||||
html = render(lv)
|
||||
|
||||
refute html =~ "Password is too weak"
|
||||
end
|
||||
|
||||
test "creates user entry on valid input", %{conn: conn} do
|
||||
mock_captcha_success()
|
||||
|
||||
lv = get_liveview(conn, "/register")
|
||||
|
||||
type_into_input(lv, "user[name]", "Mary Sue")
|
||||
type_into_input(lv, "user[email]", "mary.sue@plausible.test")
|
||||
type_into_input(lv, "user[password]", "very-long-and-very-secret-123")
|
||||
type_into_input(lv, "user[password_confirmation]", "very-long-and-very-secret-123")
|
||||
|
||||
html = lv |> element("form") |> render_submit()
|
||||
|
||||
assert_push_event(lv, "send-metrics", %{event_name: "Signup", params: %{}})
|
||||
|
||||
assert [
|
||||
csrf_input,
|
||||
name_input,
|
||||
email_input,
|
||||
password_input,
|
||||
password_confirmation_input | _
|
||||
] = find(html, "input")
|
||||
|
||||
assert String.length(text_of_attr(csrf_input, "value")) > 0
|
||||
assert text_of_attr(name_input, "value") == "Mary Sue"
|
||||
assert text_of_attr(email_input, "value") == "mary.sue@plausible.test"
|
||||
assert text_of_attr(password_input, "value") == "very-long-and-very-secret-123"
|
||||
assert text_of_attr(password_confirmation_input, "value") == "very-long-and-very-secret-123"
|
||||
|
||||
assert %{
|
||||
name: "Mary Sue",
|
||||
email: "mary.sue@plausible.test",
|
||||
password_hash: password_hash
|
||||
} = Repo.one(User)
|
||||
|
||||
assert String.length(password_hash) > 0
|
||||
end
|
||||
|
||||
test "renders only one error on empty password confirmation", %{conn: conn} do
|
||||
mock_captcha_success()
|
||||
|
||||
lv = get_liveview(conn, "/register")
|
||||
|
||||
type_into_input(lv, "user[name]", "Mary Sue")
|
||||
type_into_input(lv, "user[email]", "mary.sue@plausible.test")
|
||||
type_into_input(lv, "user[password]", "very-long-and-very-secret-123")
|
||||
type_into_input(lv, "user[password_confirmation]", "")
|
||||
|
||||
html = lv |> element("form") |> render_submit()
|
||||
|
||||
assert html =~ "does not match confirmation"
|
||||
refute html =~ "can't be blank"
|
||||
end
|
||||
|
||||
test "renders error on failed captcha", %{conn: conn} do
|
||||
mock_captcha_failure()
|
||||
|
||||
lv = get_liveview(conn, "/register")
|
||||
|
||||
type_into_input(lv, "user[name]", "Mary Sue")
|
||||
type_into_input(lv, "user[email]", "mary.sue@plausible.test")
|
||||
type_into_input(lv, "user[password]", "very-long-and-very-secret-123")
|
||||
type_into_input(lv, "user[password_confirmation]", "very-long-and-very-secret-123")
|
||||
|
||||
html = lv |> element("form") |> render_submit()
|
||||
|
||||
assert html =~ "Please complete the captcha to register"
|
||||
|
||||
refute Repo.one(User)
|
||||
end
|
||||
|
||||
test "pushing send-metrics-after event submits the form", %{conn: conn} do
|
||||
lv = get_liveview(conn, "/register")
|
||||
|
||||
refute render(lv) =~ ~s|phx-trigger-action="phx-trigger-action"|
|
||||
|
||||
render_hook(lv, "send-metrics-after", %{event_name: "Signup", params: %{}})
|
||||
|
||||
assert render(lv) =~ ~s|phx-trigger-action="phx-trigger-action"|
|
||||
end
|
||||
end
|
||||
|
||||
describe "/register/invitation/:invitation_id" do
|
||||
setup do
|
||||
inviter = insert(:user)
|
||||
site = insert(:site, members: [inviter])
|
||||
|
||||
invitation =
|
||||
insert(:invitation,
|
||||
site_id: site.id,
|
||||
inviter: inviter,
|
||||
email: "user@email.co",
|
||||
role: :admin
|
||||
)
|
||||
|
||||
{:ok, %{site: site, invitation: invitation, inviter: inviter}}
|
||||
end
|
||||
|
||||
test "registers user from invitation", %{conn: conn, invitation: invitation} do
|
||||
mock_captcha_success()
|
||||
|
||||
lv = get_liveview(conn, "/register/invitation/#{invitation.invitation_id}")
|
||||
|
||||
type_into_input(lv, "user[name]", "Mary Sue")
|
||||
type_into_input(lv, "user[password]", "very-long-and-very-secret-123")
|
||||
type_into_input(lv, "user[password_confirmation]", "very-long-and-very-secret-123")
|
||||
|
||||
html = lv |> element("form") |> render_submit()
|
||||
|
||||
assert_push_event(lv, "send-metrics", %{event_name: "Signup via invitation", params: %{}})
|
||||
|
||||
assert [
|
||||
csrf_input,
|
||||
email_input,
|
||||
name_input,
|
||||
password_input,
|
||||
password_confirmation_input | _
|
||||
] = find(html, "input")
|
||||
|
||||
assert String.length(text_of_attr(csrf_input, "value")) > 0
|
||||
assert text_of_attr(name_input, "value") == "Mary Sue"
|
||||
assert text_of_attr(email_input, "value") == "user@email.co"
|
||||
assert text_of_attr(password_input, "value") == "very-long-and-very-secret-123"
|
||||
assert text_of_attr(password_confirmation_input, "value") == "very-long-and-very-secret-123"
|
||||
|
||||
assert %{
|
||||
name: "Mary Sue",
|
||||
email: "user@email.co",
|
||||
password_hash: password_hash,
|
||||
# leaves trial_expiry_date null when invitation role is not :owner
|
||||
trial_expiry_date: nil
|
||||
} = Repo.get_by(User, email: "user@email.co")
|
||||
|
||||
assert String.length(password_hash) > 0
|
||||
end
|
||||
|
||||
test "preserves trial_expiry_date when invitation role is :owner", %{
|
||||
conn: conn,
|
||||
site: site,
|
||||
inviter: inviter
|
||||
} do
|
||||
mock_captcha_success()
|
||||
|
||||
invitation =
|
||||
insert(:invitation,
|
||||
site_id: site.id,
|
||||
inviter: inviter,
|
||||
email: "owner_user@email.co",
|
||||
role: :owner
|
||||
)
|
||||
|
||||
lv = get_liveview(conn, "/register/invitation/#{invitation.invitation_id}")
|
||||
|
||||
type_into_input(lv, "user[name]", "Mary Sue")
|
||||
type_into_input(lv, "user[password]", "very-long-and-very-secret-123")
|
||||
type_into_input(lv, "user[password_confirmation]", "very-long-and-very-secret-123")
|
||||
|
||||
_html = lv |> element("form") |> render_submit()
|
||||
|
||||
assert %{
|
||||
email: "owner_user@email.co",
|
||||
trial_expiry_date: trial_expiry_date
|
||||
} = Repo.get_by(User, email: "owner_user@email.co")
|
||||
|
||||
assert trial_expiry_date != nil
|
||||
end
|
||||
|
||||
test "always uses original email from the invitation", %{conn: conn, invitation: invitation} do
|
||||
mock_captcha_success()
|
||||
|
||||
lv = get_liveview(conn, "/register/invitation/#{invitation.invitation_id}")
|
||||
|
||||
type_into_input(lv, "user[name]", "Mary Sue")
|
||||
type_into_input(lv, "user[email]", "mary.sue@plausible.test")
|
||||
type_into_input(lv, "user[password]", "very-long-and-very-secret-123")
|
||||
type_into_input(lv, "user[password_confirmation]", "very-long-and-very-secret-123")
|
||||
|
||||
html = lv |> element("form") |> render_submit()
|
||||
|
||||
assert [
|
||||
_csrf_input,
|
||||
email_input | _
|
||||
] = find(html, "input")
|
||||
|
||||
# attempt at tampering with form
|
||||
assert text_of_attr(email_input, "value") == "mary.sue@plausible.test"
|
||||
|
||||
assert Repo.get_by(User, email: "user@email.co")
|
||||
refute Repo.get_by(User, email: "mary.sue@plausible.test")
|
||||
end
|
||||
|
||||
test "renders error on failed captcha", %{conn: conn, invitation: invitation} do
|
||||
mock_captcha_failure()
|
||||
|
||||
lv = get_liveview(conn, "/register/invitation/#{invitation.invitation_id}")
|
||||
|
||||
type_into_input(lv, "user[name]", "Mary Sue")
|
||||
type_into_input(lv, "user[password]", "very-long-and-very-secret-123")
|
||||
type_into_input(lv, "user[password_confirmation]", "very-long-and-very-secret-123")
|
||||
|
||||
html = lv |> element("form") |> render_submit()
|
||||
|
||||
assert html =~ "Please complete the captcha to register"
|
||||
|
||||
refute Repo.get_by(User, email: "user@email.co")
|
||||
end
|
||||
|
||||
test "pushing send-metrics-after event submits the form", %{
|
||||
conn: conn,
|
||||
invitation: invitation
|
||||
} do
|
||||
lv = get_liveview(conn, "/register/invitation/#{invitation.invitation_id}")
|
||||
|
||||
refute render(lv) =~ ~s|phx-trigger-action="phx-trigger-action"|
|
||||
|
||||
render_hook(lv, "send-metrics-after", %{event_name: "Signup via invitation", params: %{}})
|
||||
|
||||
assert render(lv) =~ ~s|phx-trigger-action="phx-trigger-action"|
|
||||
end
|
||||
end
|
||||
|
||||
defp get_liveview(conn, url) do
|
||||
conn = assign(conn, :live_module, PlausibleWeb.Live.RegisterForm)
|
||||
{:ok, lv, _html} = live(conn, url)
|
||||
|
||||
lv
|
||||
end
|
||||
|
||||
defp type_into_input(lv, id, text) do
|
||||
lv
|
||||
|> element("form")
|
||||
|> render_change(%{id => text})
|
||||
end
|
||||
|
||||
defp mock_captcha_success() do
|
||||
mock_captcha(true)
|
||||
end
|
||||
|
||||
defp mock_captcha_failure() do
|
||||
mock_captcha(false)
|
||||
end
|
||||
|
||||
defp mock_captcha(success) do
|
||||
expect(
|
||||
Plausible.HTTPClient.Mock,
|
||||
:post,
|
||||
fn _, _, _ ->
|
||||
{:ok,
|
||||
%Finch.Response{
|
||||
status: 200,
|
||||
headers: [{"content-type", "application/json"}],
|
||||
body: %{"success" => success}
|
||||
}}
|
||||
end
|
||||
)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,57 @@
|
||||
defmodule PlausibleWeb.Live.ResetPasswordFormTest do
|
||||
use PlausibleWeb.ConnCase, async: true
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Plausible.Test.Support.HTML
|
||||
|
||||
alias Plausible.Auth.User
|
||||
alias Plausible.Auth.Token
|
||||
alias Plausible.Repo
|
||||
|
||||
describe "/password/reset" do
|
||||
test "sets new password with valid token", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
token = Token.sign_password_reset(user.email)
|
||||
|
||||
lv = get_liveview(conn, "/password/reset?token=#{token}")
|
||||
|
||||
type_into_passowrd(lv, "very-secret-and-very-long-123")
|
||||
html = lv |> element("form") |> render_submit()
|
||||
|
||||
assert [csrf_input, password_input, token_input | _] = find(html, "input")
|
||||
assert String.length(text_of_attr(csrf_input, "value")) > 0
|
||||
assert text_of_attr(token_input, "value") == token
|
||||
assert text_of_attr(password_input, "value") == "very-secret-and-very-long-123"
|
||||
assert %{password_hash: new_hash} = Repo.one(User)
|
||||
assert new_hash != user.password_hash
|
||||
end
|
||||
|
||||
test "renders error when new password fails validation", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
token = Token.sign_password_reset(user.email)
|
||||
|
||||
lv = get_liveview(conn, "/password/reset?token=#{token}")
|
||||
|
||||
type_into_passowrd(lv, "too-short")
|
||||
html = lv |> element("form") |> render_submit()
|
||||
|
||||
assert html =~ "Password is too weak"
|
||||
|
||||
assert %{password_hash: hash} = Repo.one(User)
|
||||
assert hash == user.password_hash
|
||||
end
|
||||
end
|
||||
|
||||
defp get_liveview(conn, url) do
|
||||
conn = assign(conn, :live_module, PlausibleWeb.Live.ResetPasswordForm)
|
||||
{:ok, lv, _html} = live(conn, url)
|
||||
|
||||
lv
|
||||
end
|
||||
|
||||
defp type_into_passowrd(lv, text) do
|
||||
lv
|
||||
|> element("form")
|
||||
|> render_change(%{"user[password]" => text})
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue