Last active
October 17, 2016 09:29
-
-
Save harfangk/c4fcf5fed2de35a6038c7e535eced474 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
test/models/user_repo_test.ex | |
defmodule Storybook.UserRepoTest do | |
use Storybook.ModelCase | |
import Storybook.Factory | |
alias Storybook.User | |
@valid_attrs %{email: "[email protected]", username: "user", password: "password"} | |
@test_attrs %{email: "[email protected]", username: "different_user", password: "different_password"} | |
setup do | |
insert(:user, @valid_attrs) | |
end | |
test "converts unique_constraint on username to error" do | |
attrs = Map.put(@test_attrs, :username, "user") | |
changeset = User.registration_changeset(%User{}, attrs) | |
assert {:error, changeset} = Repo.insert(changeset) | |
assert {:username, {"has already been taken", []}} in changeset.errors | |
end | |
test "converts unique_constraint on email to error" do | |
attrs = Map.put(@test_attrs, :email, "[email protected]") | |
changeset = User.registration_changeset(%User{}, attrs) | |
assert {:error, changeset} = Repo.insert(changeset) | |
assert {:email, {"has already been taken", []}} in changeset.errors | |
end | |
end | |
======================================================================================================================== | |
test result | |
2) test converts unique_constraint on username to error (Storybook.UserRepoTest) | |
test/models/user_repo_test.exs:9 | |
match (=) failed | |
code: {:error, changeset} = Repo.insert(changeset) | |
rhs: {:ok, | |
%Storybook.User{__meta__: #Ecto.Schema.Metadata<:loaded, "users">, | |
email: "[email protected]", id: 96, | |
inserted_at: #Ecto.DateTime<2016-10-17 07:49:42>, name: nil, | |
password: "different_password", | |
password_hash: "$2b$12$Hnj8diCdDn9Elj0l9.uOQuAnVtpFDkrThfzsyJswMCjU2ZraYXJdW", | |
updated_at: #Ecto.DateTime<2016-10-17 07:49:42>, username: "user"}} | |
stacktrace: | |
test/models/user_repo_test.exs:16: (test) | |
======================================================================================================================== | |
models/user.ex | |
defmodule Storybook.User do | |
use Storybook.Web, :model | |
schema "users" do | |
field :email, :string | |
field :name, :string | |
field :username, :string | |
field :password, :string, virtual: true | |
field :password_hash, :string | |
timestamps() | |
end | |
@doc """ | |
Builds a changeset based on the `struct` and `params`. | |
""" | |
def changeset(struct, params \\ %{}) do | |
struct | |
|> cast(params, [:email, :username]) | |
|> validate_required([:email, :username]) | |
|> validate_length(:username, max: 40) | |
|> unique_constraint(:username) | |
|> unique_constraint(:email) | |
end | |
def registration_changeset(struct, params \\ %{}) do | |
struct | |
|> changeset(params) | |
|> cast(params, [:password]) | |
|> validate_required([:password]) | |
|> validate_length(:password, min: 6) | |
|> put_password_hash() | |
end | |
defp put_password_hash(changeset) do | |
case changeset do | |
%Ecto.Changeset{valid?: true, changes: %{password: password}} -> | |
put_change(changeset, :password_hash, Comeonin.Bcrypt.hashpwsalt(password)) | |
_ -> | |
changeset | |
end | |
end | |
end | |
================================================================================================ | |
defmodule Storybook.Factory do | |
use ExMachina.Ecto, repo: Storybook.Repo | |
def user_factory do | |
%Storybook.User{ | |
username: "Json Mraz", | |
email: sequence(:email, &"email-#{&1}@example.com") | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment