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

expiry_notification_service_spec.rb « keys « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7cb6cbce311d56269a6bd51ecbba8c49f9da94de (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Keys::ExpiryNotificationService do
  let_it_be_with_reload(:user) { create(:user) }

  let(:params) { { keys: user.keys, expiring_soon: expiring_soon } }

  subject { described_class.new(user, params) }

  shared_examples 'sends a notification' do
    it do
      perform_enqueued_jobs do
        subject.execute
      end
      should_email(user)
    end
  end

  shared_examples 'uses notification service to send email to the user' do |notification_method|
    it do
      expect_next_instance_of(NotificationService) do |notification_service|
        expect(notification_service).to receive(notification_method).with(key.user, [key.fingerprint])
      end

      subject.execute
    end
  end

  shared_examples 'does not send notification' do
    it do
      perform_enqueued_jobs do
        subject.execute
      end
      should_not_email(user)
    end
  end

  shared_context 'block user' do
    before do
      user.block!
    end
  end

  context 'with key expiring today', :mailer do
    let_it_be_with_reload(:key) { create(:key, expires_at: 10.minutes.from_now, user: user) }

    let(:expiring_soon) { false }

    context 'when user has permission to receive notification' do
      it_behaves_like 'sends a notification'

      it_behaves_like 'uses notification service to send email to the user', :ssh_key_expired

      it 'updates notified column' do
        expect { subject.execute }.to change { key.reload.expiry_notification_delivered_at }
      end
    end

    context 'when user does NOT have permission to receive notification' do
      include_context 'block user'

      it_behaves_like 'does not send notification'

      it 'does not update notified column' do
        expect { subject.execute }.not_to change { key.reload.expiry_notification_delivered_at }
      end
    end
  end

  context 'with key expiring soon', :mailer do
    let_it_be_with_reload(:key) { create(:key, expires_at: 3.days.from_now, user: user) }

    let(:expiring_soon) { true }

    context 'when user has permission to receive notification' do
      it_behaves_like 'sends a notification'

      it_behaves_like 'uses notification service to send email to the user', :ssh_key_expiring_soon

      it 'updates notified column' do
        expect { subject.execute }.to change { key.reload.before_expiry_notification_delivered_at }
      end
    end

    context 'when user does NOT have permission to receive notification' do
      include_context 'block user'

      it_behaves_like 'does not send notification'

      it 'does not update notified column' do
        expect { subject.execute }.not_to change { key.reload.before_expiry_notification_delivered_at }
      end
    end
  end
end