Skip to content

Commit 53140c4

Browse files
authored
Merge pull request #742 from code-corps/reset-password
[USER]: reset_password_changeset
2 parents 6bad267 + 60db624 commit 53140c4

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

test/models/user_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,16 @@ defmodule CodeCorps.UserTest do
132132
assert message == "invalid transition yehaww from signed_up"
133133
end
134134
end
135+
136+
test "reset_password_changeset with valid passwords" do
137+
changeset = User.reset_password_changeset(%User{}, %{password: "foobar", password_confirmation: "foobar"})
138+
assert changeset.valid?
139+
assert changeset.changes.encrypted_password
140+
end
141+
142+
test "reset_password_changeset with invalid passwords generates message" do
143+
changeset = User.reset_password_changeset(%User{}, %{password: "wat", password_confirmation: "foobar"})
144+
refute changeset.valid?
145+
assert error_message(changeset, :password_confirmation) == "passwords do not match"
146+
end
135147
end

web/models/user.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ defmodule CodeCorps.User do
2525
field :first_name, :string
2626
field :last_name, :string
2727
field :password, :string, virtual: true
28+
field :password_confirmation, :string, virtual: true
2829
field :twitter, :string
2930
field :username, :string
3031
field :website, :string
@@ -96,6 +97,13 @@ defmodule CodeCorps.User do
9697
|> apply_state_transition(struct)
9798
end
9899

100+
def reset_password_changeset(struct, params) do
101+
struct
102+
|> cast(params, [:password, :password_confirmation])
103+
|> validate_confirmation(:password, message: "passwords do not match")
104+
|> put_pass_hash
105+
end
106+
99107
def apply_state_transition(changeset, %{state: current_state}) do
100108
state_transition = get_field(changeset, :state_transition)
101109
next_state = CodeCorps.Transition.UserState.next(current_state, state_transition)

0 commit comments

Comments
 (0)