Skip to content
This repository was archived by the owner on Dec 3, 2019. It is now read-only.

Commit 541617a

Browse files
committed
WIP: adding tests for the password forgot/reset/update endpoints
* controller test for forgot and model test for the generate_password_token! method that forgot uses
1 parent 0dbb193 commit 541617a

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'test_helper'
2+
3+
class Api::V1::Users::PasswordsControllerTest < ActionDispatch::IntegrationTest
4+
test 'forgot returns successful given an email of an existing user' do
5+
create :user, email: 'forgetful@example.com'
6+
7+
post api_v1_users_passwords_forgot_url,
8+
params: { email: 'forgetful@example.com' },
9+
as: :json
10+
11+
assert_equal 200, response.status
12+
end
13+
14+
test 'forgot generates a password reset token' do
15+
user = create :user
16+
User.any_instance.expects(:generate_password_token!)
17+
18+
post api_v1_users_passwords_forgot_url,
19+
params: { email: user.email },
20+
as: :json
21+
end
22+
23+
test 'forgot emails user instructions to reset their password' do
24+
user = create :user
25+
User.any_instance.expects(:send_reset_password_instructions)
26+
27+
post api_v1_users_passwords_forgot_url,
28+
params: { email: user.email },
29+
as: :json
30+
end
31+
32+
test 'forgot returns unprocessable error when given an email that does not match an existing user' do
33+
post api_v1_users_passwords_forgot_url,
34+
params: { email: 'totallydoesnotexist@example.com' },
35+
as: :json
36+
37+
assert_equal 422, response.status
38+
end
39+
40+
test 'forgot requires an email address be given' do
41+
post api_v1_users_passwords_forgot_url,
42+
params: { no: 'email key in params' },
43+
as: :json
44+
assert_equal 422, response.status
45+
end
46+
end

test/models/user_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,16 @@ def user_opts
269269
assert create(:user, military_status: nil).valid?
270270
refute User.new(military_status: 'spaghetti monster').valid?
271271
end
272+
273+
test 'generate_password_token! does what it says' do
274+
user = create :user
275+
assert_nil user.reset_password_token
276+
assert_nil user.reset_password_sent_at
277+
278+
user.generate_password_token!
279+
280+
user.reload
281+
assert_not_nil user.reset_password_token
282+
assert_not_nil user.reset_password_sent_at
283+
end
272284
end

0 commit comments

Comments
 (0)