summaryrefslogtreecommitdiff
path: root/spec/models/email_spec.rb
blob: b76063bfa1ab4baaf56949a25bfd58a1f3a2a6fa (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Email do
  describe 'modules' do
    subject { described_class }

    it { is_expected.to include_module(AsyncDeviseEmail) }
  end

  describe 'validations' do
    it_behaves_like 'an object with email-formatted attributes', :email do
      subject { build(:email) }
    end

    context 'when the email conflicts with the primary email of a different user' do
      let(:user) { create(:user) }
      let(:email) { build(:email, email: user.email) }

      it 'is invalid' do
        expect(email).to be_invalid
      end
    end
  end

  it 'normalize email value' do
    expect(described_class.new(email: ' inFO@exAMPLe.com ').email)
      .to eq 'info@example.com'
  end

  describe '#update_invalid_gpg_signatures' do
    let(:user) { create(:user) }

    it 'synchronizes the gpg keys when the email is updated' do
      email = user.emails.create!(email: 'new@email.com')

      expect(user).to receive(:update_invalid_gpg_signatures)

      email.confirm
    end
  end

  describe 'scopes' do
    let(:user) { create(:user, :unconfirmed) }

    it 'scopes confirmed emails' do
      create(:email, :confirmed, user: user)
      create(:email, user: user)

      expect(user.emails.count).to eq 2
      expect(user.emails.confirmed.count).to eq 1
    end
  end

  describe 'delegations' do
    it { is_expected.to delegate_method(:can?).to(:user) }
    it { is_expected.to delegate_method(:username).to(:user) }
    it { is_expected.to delegate_method(:pending_invitations).to(:user) }
    it { is_expected.to delegate_method(:accept_pending_invitations!).to(:user) }
  end

  describe 'Devise emails' do
    let!(:user) { create(:user) }

    describe 'behaviour' do
      it 'sends emails asynchronously' do
        expect do
          user.emails.create!(email: 'hello@hello.com')
        end.to have_enqueued_job.on_queue('mailers')
      end
    end
  end

  describe '#confirm' do
    let(:expired_confirmation_sent_at) { Date.today - described_class.confirm_within - 7.days }
    let(:extant_confirmation_sent_at) { Date.today }

    let(:email) do
      create(:email, email: 'test@gitlab.com').tap do |email|
        email.update!(confirmation_sent_at: confirmation_sent_at)
      end
    end

    shared_examples_for 'unconfirmed email' do
      it 'returns unconfirmed' do
        expect(email.confirmed?).to be_falsey
      end
    end

    context 'when the confirmation period has expired' do
      let(:confirmation_sent_at) {  expired_confirmation_sent_at }

      it_behaves_like 'unconfirmed email'

      it 'does not confirm the email' do
        email.confirm

        expect(email.confirmed?).to be_falsey
      end
    end

    context 'when the confirmation period has not expired' do
      let(:confirmation_sent_at) {  extant_confirmation_sent_at }

      it_behaves_like 'unconfirmed email'

      it 'confirms the email' do
        email.confirm

        expect(email.confirmed?).to be_truthy
      end
    end
  end

  describe '#force_confirm' do
    let(:expired_confirmation_sent_at) { Date.today - described_class.confirm_within - 7.days }
    let(:extant_confirmation_sent_at) { Date.today }

    let(:email) do
      create(:email, email: 'test@gitlab.com').tap do |email|
        email.update!(confirmation_sent_at: confirmation_sent_at)
      end
    end

    shared_examples_for 'unconfirmed email' do
      it 'returns unconfirmed' do
        expect(email.confirmed?).to be_falsey
      end
    end

    shared_examples_for 'confirms the email on force_confirm' do
      it 'confirms an email' do
        email.force_confirm

        expect(email.reload.confirmed?).to be_truthy
      end
    end

    context 'when the confirmation period has expired' do
      let(:confirmation_sent_at) {  expired_confirmation_sent_at }

      it_behaves_like 'unconfirmed email'
      it_behaves_like 'confirms the email on force_confirm'
    end

    context 'when the confirmation period has not expired' do
      let(:confirmation_sent_at) {  extant_confirmation_sent_at }

      it_behaves_like 'unconfirmed email'
      it_behaves_like 'confirms the email on force_confirm'
    end
  end
end