summaryrefslogtreecommitdiff
path: root/spec/models/password_reset_spec.rb
blob: 0dfc6181469774a754c6cc77cf243d03224b663e (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 PasswordReset do
  describe ".send_reset_instructions_to" do
    let(:user) { create(:user, reset_password_token: nil, reset_password_sent_at: nil) }
    let(:mailer) { double(deliver_later: true) }

    before :each do
      allow(PasswordResetMailer).
        to receive(:send_password_reset_instructions_to).
        with(user).
        and_return(mailer)
    end

    it "creates a new reset token for the user" do
      PasswordReset.send_reset_instructions_to(user.email)
      user.reload
      expect(user.reset_password_token).to_not be_nil
      expect(user.reset_password_sent_at).to_not be_nil
    end

    it "sends an email to the user" do
      PasswordReset.send_reset_instructions_to(user.email)
      expect(mailer).to have_received(:deliver_later)
    end

    it "does nothing if the email is not known" do
      PasswordReset.send_reset_instructions_to(FFaker::Internet.email)
      expect(mailer).to_not have_received(:deliver_later)
    end
  end

  describe ".reset" do
    let(:reset_token) { SecureRandom.hex(32) }
    let(:password) { SecureRandom.hex(8) }
    let!(:user) { create(:user, reset_password_token: reset_token) }

    it "changes the users password" do
      PasswordReset.reset(reset_token, password)
      user.reload
      expect(user.authenticate(password)).to be_truthy
    end

    it "deletes the reset token" do
      PasswordReset.reset(reset_token, password)
      user.reload
      expect(user.reset_password_token).to be_nil
      expect(user.reset_password_sent_at).to be_nil
    end

    it "does nothing if the token cannot be found" do
      PasswordReset.reset(SecureRandom.hex(32), password)
    end
  end
end