-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Credential code following running of "mix phx.gen.context Account…
…s Credential ..." on p.73 for #55
- Loading branch information
Showing
4 changed files
with
199 additions
and
0 deletions.
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
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,21 @@ | ||
defmodule Rumbl.Accounts.Credential do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
|
||
schema "credentials" do | ||
field :email, :string | ||
field :password_hash, :string | ||
field :user_id, :id | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(credential, attrs) do | ||
credential | ||
|> cast(attrs, [:email, :password_hash]) | ||
|> validate_required([:email, :password_hash]) | ||
|> unique_constraint(:email) | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
rumbl/priv/repo/migrations/20190123175540_create_credentials.exs
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,16 @@ | ||
defmodule Rumbl.Repo.Migrations.CreateCredentials do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:credentials) do | ||
add :email, :string | ||
add :password_hash, :string | ||
add :user_id, references(:users, on_delete: :nothing) | ||
|
||
timestamps() | ||
end | ||
|
||
create unique_index(:credentials, [:email]) | ||
create index(:credentials, [:user_id]) | ||
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,66 @@ | ||
defmodule Rumbl.AccountsTest do | ||
use Rumbl.DataCase | ||
|
||
alias Rumbl.Accounts | ||
|
||
describe "credentials" do | ||
alias Rumbl.Accounts.Credential | ||
|
||
@valid_attrs %{email: "some email", password_hash: "some password_hash"} | ||
@update_attrs %{email: "some updated email", password_hash: "some updated password_hash"} | ||
@invalid_attrs %{email: nil, password_hash: nil} | ||
|
||
def credential_fixture(attrs \\ %{}) do | ||
{:ok, credential} = | ||
attrs | ||
|> Enum.into(@valid_attrs) | ||
|> Accounts.create_credential() | ||
|
||
credential | ||
end | ||
|
||
test "list_credentials/0 returns all credentials" do | ||
credential = credential_fixture() | ||
assert Accounts.list_credentials() == [credential] | ||
end | ||
|
||
test "get_credential!/1 returns the credential with given id" do | ||
credential = credential_fixture() | ||
assert Accounts.get_credential!(credential.id) == credential | ||
end | ||
|
||
test "create_credential/1 with valid data creates a credential" do | ||
assert {:ok, %Credential{} = credential} = Accounts.create_credential(@valid_attrs) | ||
assert credential.email == "some email" | ||
assert credential.password_hash == "some password_hash" | ||
end | ||
|
||
test "create_credential/1 with invalid data returns error changeset" do | ||
assert {:error, %Ecto.Changeset{}} = Accounts.create_credential(@invalid_attrs) | ||
end | ||
|
||
test "update_credential/2 with valid data updates the credential" do | ||
credential = credential_fixture() | ||
assert {:ok, %Credential{} = credential} = Accounts.update_credential(credential, @update_attrs) | ||
assert credential.email == "some updated email" | ||
assert credential.password_hash == "some updated password_hash" | ||
end | ||
|
||
test "update_credential/2 with invalid data returns error changeset" do | ||
credential = credential_fixture() | ||
assert {:error, %Ecto.Changeset{}} = Accounts.update_credential(credential, @invalid_attrs) | ||
assert credential == Accounts.get_credential!(credential.id) | ||
end | ||
|
||
test "delete_credential/1 deletes the credential" do | ||
credential = credential_fixture() | ||
assert {:ok, %Credential{}} = Accounts.delete_credential(credential) | ||
assert_raise Ecto.NoResultsError, fn -> Accounts.get_credential!(credential.id) end | ||
end | ||
|
||
test "change_credential/1 returns a credential changeset" do | ||
credential = credential_fixture() | ||
assert %Ecto.Changeset{} = Accounts.change_credential(credential) | ||
end | ||
end | ||
end |