From beb9d0f112c0f1a716f02af9a6c6ac3774e27595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20N=C3=BDvlt?= Date: Fri, 9 Aug 2024 01:28:57 +0200 Subject: [PATCH] Update user gen --- lib/chore_tracker/accounts.ex | 4 +-- lib/chore_tracker/accounts/user.ex | 8 +++++- priv/repo/seeds.exs | 43 +++++++++++++++++------------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/lib/chore_tracker/accounts.ex b/lib/chore_tracker/accounts.ex index 964dd4e..ed4b6ca 100644 --- a/lib/chore_tracker/accounts.ex +++ b/lib/chore_tracker/accounts.ex @@ -88,9 +88,9 @@ defmodule ChoreTracker.Accounts do {:error, %Ecto.Changeset{}} """ - def register_user(attrs) do + def register_user(attrs, opts) do %User{} - |> User.registration_changeset(attrs) + |> User.registration_changeset(attrs, opts) |> Repo.insert() end diff --git a/lib/chore_tracker/accounts/user.ex b/lib/chore_tracker/accounts/user.ex index c20b6d4..e173ffc 100644 --- a/lib/chore_tracker/accounts/user.ex +++ b/lib/chore_tracker/accounts/user.ex @@ -54,7 +54,13 @@ defmodule ChoreTracker.Accounts.User do defp validate_password(changeset, opts) do changeset |> validate_required([:password]) - |> validate_length(:password, min: 12, max: 72) + |> then(fn changeset -> + if opts[:disable_password_validation] do + changeset + else + changeset |> validate_length(:password, min: 12, max: 72) + end + end) # Examples of additional password validation: # |> validate_format(:password, ~r/[a-z]/, message: "at least one lower case character") # |> validate_format(:password, ~r/[A-Z]/, message: "at least one upper case character") diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 23b9fae..f314aa0 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -10,28 +10,35 @@ # We recommend using the bang functions (`insert!`, `update!` # and so on) as they will fail if something goes wrong. -alias ChoreTracker.Helpers - -admin_password = Helpers.random_string(16) +admin_password = "secret" {:ok, admin} = - ChoreTracker.Accounts.register_user(%{ - email: "admin@jednota.party", - password: admin_password, - display_name: "Admin" - }) + ChoreTracker.Accounts.register_user( + %{ + email: "ondrej@jednota.party", + password: admin_password, + display_name: "Ondřej" + }, + disable_password_validation: true + ) -ChoreTracker.Accounts.register_user(%{ - email: "matej@jednota.party", - password: Helpers.random_string(16), - display_name: "Matěj" -}) +ChoreTracker.Accounts.register_user( + %{ + email: "matej@jednota.party", + password: "secret", + display_name: "Matěj" + }, + disable_password_validation: true +) -ChoreTracker.Accounts.register_user(%{ - email: "valentyna@jednota.party", - password: Helpers.random_string(16), - display_name: "Valentýna" -}) +ChoreTracker.Accounts.register_user( + %{ + email: "valentyna@jednota.party", + password: "secret", + display_name: "Valentýna" + }, + disable_password_validation: true +) ChoreTracker.Chores.create_chore(%{ name: "Uklidit kuchyň",