master
1require "rails_helper"
2
3describe PasswordsController do
4 describe "#new" do
5 it "loads a page to reset a password" do
6 get :new
7 expect(assigns(:user)).to be_new_record
8 end
9 end
10
11 describe "#create" do
12 let(:email) { FFaker::Internet.email }
13
14 it "sends a password reset email for the user" do
15 allow(PasswordReset).to receive(:send_reset_instructions_to)
16
17 post :create, params: { user: { email: email } }
18
19 expect(PasswordReset).to have_received(:send_reset_instructions_to).with(email)
20 expect(response).to redirect_to(new_session_path)
21 expect(flash[:notice]).to_not be_empty
22 end
23 end
24
25 describe "#edit" do
26 let(:reset_token) { SecureRandom.hex(32) }
27 let(:user) { build(:user) }
28
29 it "loads the password reset token" do
30 allow(User).to receive(:find_by).with(reset_password_token: reset_token).and_return(user)
31 get :edit, params: { id: reset_token }
32 expect(assigns(:user)).to eql(user)
33 end
34
35 it "redirects to the homepage if the user cannot be found" do
36 allow(User).to receive(:find_by).with(reset_password_token: reset_token).and_return(nil)
37 get :edit, params: { id: reset_token }
38 expect(response).to redirect_to(root_path)
39 end
40 end
41
42 describe "#update" do
43 let(:user) { double(change_password: true, valid?: true) }
44 let(:reset_token) { SecureRandom.hex(32) }
45 let(:password) { SecureRandom.hex(8) }
46
47 it "resets the users password" do
48 allow(PasswordReset).to receive(:reset).with(reset_token, password).and_return(user)
49
50 patch :update, params: { id: reset_token, user: { password: password } }
51 expect(PasswordReset).to have_received(:reset).with(reset_token, password)
52 expect(response).to redirect_to(new_session_path)
53 end
54 end
55end