Welcome to mirror list, hosted at ThFree Co, Russian Federation.

recoverable_by_any_email_spec.rb « concerns « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c17507cae83b585999c32e9dce5110c37efef1a0 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RecoverableByAnyEmail, feature_category: :system_access do
  describe '.send_reset_password_instructions' do
    let_it_be(:user) { create(:user, email: 'test@example.com') }
    let_it_be(:verified_email) { create(:email, :confirmed, user: user) }
    let_it_be(:unverified_email) { create(:email, user: user) }

    subject(:send_reset_password_instructions) do
      User.send_reset_password_instructions(email: email)
    end

    shared_examples 'sends the password reset email' do
      it 'finds the user' do
        expect(send_reset_password_instructions).to eq(user)
      end

      it 'sends the email' do
        expect { send_reset_password_instructions }.to have_enqueued_mail(DeviseMailer, :reset_password_instructions)
      end
    end

    shared_examples 'does not send the password reset email' do
      it 'does not find the user' do
        expect(subject.id).to be_nil
        expect(subject.errors).not_to be_empty
      end

      it 'does not send any email' do
        subject

        expect { subject }.not_to have_enqueued_mail(DeviseMailer, :reset_password_instructions)
      end
    end

    context 'with user primary email' do
      let(:email) { user.email }

      it_behaves_like 'sends the password reset email'
    end

    context 'with user verified email' do
      let(:email) { verified_email.email }

      it_behaves_like 'sends the password reset email'
    end

    context 'with user unverified email' do
      let(:email) { unverified_email.email }

      it_behaves_like 'does not send the password reset email'
    end

    context 'with one email matching user and one not matching' do
      let(:email) { [verified_email.email, 'other_email@example.com'] }

      it 'sends an email only to the user verified email' do
        expect { send_reset_password_instructions }
          .to have_enqueued_mail(DeviseMailer, :reset_password_instructions)
                .with(
                  user,
                  anything, # reset token
                  to: user.verified_emails(include_private_email: false)
                )
      end
    end
  end
end