Skip to content

Commit a8b294d

Browse files
authored
Merge pull request #298 from espoo-dev/feat-devise-recoverable
Enable Devise Recoverable for Users
2 parents b66353a + 0d968f5 commit a8b294d

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

config/environments/test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@
6565

6666
# Raise error when a before_action's only/except options reference missing actions
6767
config.action_controller.raise_on_missing_callback_actions = true
68+
69+
config.action_controller.allow_forgery_protection = true
6870
end

spec/models/user_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,37 @@
4545
expect(user).to be_active_for_authentication
4646
end
4747
end
48+
49+
describe "password reset" do
50+
let(:user) { create(:user, email: "test@email.com") }
51+
52+
before do
53+
user.confirm
54+
end
55+
56+
it "sends password reset email" do
57+
expect { user.send_reset_password_instructions }.to change { ActionMailer::Base.deliveries.count }.by(1)
58+
end
59+
60+
it "generates reset password token" do
61+
expect(user.reset_password_token).to be_nil
62+
user.send_reset_password_instructions
63+
expect(user.reset_password_token).not_to be_nil
64+
end
65+
66+
it "resets password" do
67+
user.send_reset_password_instructions
68+
new_password = "new_secure_password"
69+
user.reset_password(new_password, new_password)
70+
expect(user.valid_password?(new_password)).to be(true)
71+
end
72+
73+
it "clears reset password token after password reset" do
74+
user.send_reset_password_instructions
75+
expect(user.reset_password_token).not_to be_nil
76+
new_password = "new_secure_password"
77+
user.reset_password(new_password, new_password)
78+
expect(user.reset_password_token).to be_nil
79+
end
80+
end
4881
end

spec/requests/api/v1/users_request_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,37 @@
6767
end
6868
end
6969
end
70+
71+
describe "Password recovery" do
72+
subject(:get_new_password) { get "/users/password/new" }
73+
74+
let(:user) { create(:user, email: "email@email.com") }
75+
let(:headers) { auth_token_for(user) }
76+
77+
it "returns ok" do
78+
get_new_password
79+
expect(response).to have_http_status(:ok)
80+
end
81+
82+
it "contains reset token" do
83+
get_new_password
84+
token = response.parsed_body.css('input[name="authenticity_token"]').first["value"]
85+
expect(token).not_to be_nil
86+
end
87+
88+
it "sends reset instructions" do
89+
get_new_password
90+
91+
auth_token = response.parsed_body.css('input[name="authenticity_token"]').first["value"]
92+
93+
post "/users/password",
94+
headers: headers,
95+
params: {
96+
authenticity_token: auth_token,
97+
user: { email: user.email }
98+
}
99+
100+
expect { user.reload }.to change(user, :reset_password_token)
101+
end
102+
end
70103
end

0 commit comments

Comments
 (0)