-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create accounts via internal functions
- Loading branch information
Showing
8 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"priv/repo/migrations": { "type": "migration" }, | ||
"lib/*.ex": { "alternate": "test/{}_test.exs" }, | ||
"test/*_test.exs": { "alternate": "lib/{}.ex" } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule IdeaPortal.Accounts do | ||
@moduledoc """ | ||
Context for user accounts | ||
""" | ||
|
||
alias IdeaPortal.Accounts.User | ||
alias IdeaPortal.Repo | ||
|
||
@doc """ | ||
Register an account | ||
""" | ||
def register(params) do | ||
%User{} | ||
|> User.create_changeset(params) | ||
|> Repo.insert() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule IdeaPortal.Accounts.User do | ||
@moduledoc """ | ||
User schema | ||
""" | ||
|
||
use Ecto.Schema | ||
|
||
import Ecto.Changeset | ||
|
||
@type t :: %__MODULE__{} | ||
|
||
schema "users" do | ||
field(:email, :string) | ||
field(:password_hash, :string) | ||
field(:password, :string, virtual: true) | ||
field(:password_confirmation, :string, virtual: true) | ||
|
||
field(:first_name, :string) | ||
field(:last_name, :string) | ||
field(:phone_number, :string) | ||
|
||
timestamps() | ||
end | ||
|
||
def create_changeset(struct, params) do | ||
struct | ||
|> cast(params, [:email, :password, :password_confirmation, :first_name, :last_name, :phone_number]) | ||
|> validate_required([:email, :first_name, :last_name]) | ||
|> validate_confirmation(:password) | ||
|> validate_format(:email, ~r/.+@.+\..+/) | ||
|> Stein.Accounts.hash_password() | ||
|> validate_required([:password_hash]) | ||
|> unique_constraint(:email, name: :users_lower_email_index) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule IdeaPortal.Repo.Migrations.CreateUsers do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table("users") do | ||
add(:email, :string, null: false) | ||
add(:password_hash, :string, null: false) | ||
|
||
add(:first_name, :string, null: false) | ||
add(:last_name, :string, null: false) | ||
add(:phone_number, :string, null: true) | ||
|
||
timestamps() | ||
end | ||
|
||
create index(:users, ["lower(email)"], unique: true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule IdeaPortal.Accounts.UserTest do | ||
use ExUnit.Case | ||
|
||
alias IdeaPortal.Accounts.User | ||
|
||
describe "validations" do | ||
test "email format" do | ||
changeset = User.create_changeset(%User{}, %{email: "[email protected]"}) | ||
refute changeset.errors[:email] | ||
|
||
changeset = User.create_changeset(%User{}, %{email: "userexample.com"}) | ||
assert changeset.errors[:email] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
defmodule IdeaPortal.AccountsTest do | ||
use IdeaPortal.DataCase | ||
|
||
alias IdeaPortal.Accounts | ||
|
||
describe "registering an account" do | ||
test "creating successfully" do | ||
{:ok, account} = Accounts.register(%{ | ||
email: "[email protected]", | ||
first_name: "John", | ||
last_name: "Smith", | ||
phone_number: "123-123-1234", | ||
password: "password", | ||
password_confirmation: "password" | ||
}) | ||
|
||
assert account.email == "[email protected]" | ||
assert account.password_hash | ||
end | ||
|
||
test "unique emails" do | ||
{:ok, account} = Accounts.register(%{ | ||
email: "[email protected]", | ||
first_name: "John", | ||
last_name: "Smith", | ||
phone_number: "123-123-1234", | ||
password: "password", | ||
password_confirmation: "password" | ||
}) | ||
|
||
{:error, changeset} = Accounts.register(%{ | ||
email: account.email, | ||
first_name: "John", | ||
last_name: "Smith", | ||
password: "password", | ||
password_confirmation: "password" | ||
}) | ||
|
||
assert changeset.errors[:email] | ||
end | ||
|
||
test "with errors" do | ||
{:error, changeset} = Accounts.register(%{ | ||
email: "[email protected]", | ||
phone_number: "123-123-1234", | ||
password: "password", | ||
password_confirmation: "password" | ||
}) | ||
|
||
assert changeset.errors[:first_name] | ||
end | ||
end | ||
end |