summaryrefslogtreecommitdiff
path: root/spec/controllers/passwords_controller_spec.rb
blob: 5e0510d97ac4c906da57eb0e94f86270c8cc8a7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require "rails_helper"

describe PasswordsController do
  describe "#new" do
    it "loads a page to reset a password" do
      get :new
      expect(assigns(:user)).to be_new_record
    end
  end

  describe "#create" do
    let(:email) { FFaker::Internet.email }

    it "sends a password reset email for the user" do
      allow(PasswordReset).to receive(:send_reset_instructions_to)

      post :create, params: { user: { email: email } }

      expect(PasswordReset).to have_received(:send_reset_instructions_to).with(email)
      expect(response).to redirect_to(new_session_path)
      expect(flash[:notice]).to_not be_empty
    end
  end

  describe "#edit" do
    let(:reset_token) { SecureRandom.hex(32) }
    let(:user) { build(:user) }

    it "loads the password reset token" do
      allow(User).to receive(:find_by).with(reset_password_token: reset_token).and_return(user)
      get :edit, params: { id: reset_token }
      expect(assigns(:user)).to eql(user)
    end

    it "redirects to the homepage if the user cannot be found" do
      allow(User).to receive(:find_by).with(reset_password_token: reset_token).and_return(nil)
      get :edit, params: { id: reset_token }
      expect(response).to redirect_to(root_path)
    end
  end

  describe "#update" do
    let(:user) { double(change_password: true, valid?: true) }
    let(:reset_token) { SecureRandom.hex(32) }
    let(:password) { SecureRandom.hex(8) }

    it "resets the users password" do
      allow(PasswordReset).to receive(:reset).with(reset_token, password).and_return(user)

      patch :update, params: { id: reset_token, user: { password: password } }
      expect(PasswordReset).to have_received(:reset).with(reset_token, password)
      expect(response).to redirect_to(new_session_path)
    end
  end
end