summaryrefslogtreecommitdiff
path: root/spec/workers/check_gcp_project_billing_worker_spec.rb
blob: 5f473d05e600c92327e874b9aebba5eb7e5cd97c (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
require 'spec_helper'

describe CheckGcpProjectBillingWorker do
  describe '.perform' do
    let(:token) { 'bogustoken' }

    subject { described_class.new.perform('token_key') }

    before do
      allow_any_instance_of(described_class).to receive(:check_previous_state)
      allow_any_instance_of(described_class).to receive(:update_billing_change_counter)
    end

    context 'when there is a token in redis' do
      before do
        allow(described_class).to receive(:get_session_token).and_return(token)
      end

      context 'when there is no lease' do
        before do
          allow_any_instance_of(described_class).to receive(:try_obtain_lease_for).and_return('randomuuid')
        end

        it 'calls the service' do
          expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([double])

          subject
        end

        it 'stores billing status in redis' do
          redis_double = double

          expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([double])
          expect(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double)
          expect(redis_double).to receive(:set).with(described_class.redis_shared_state_key_for(token), anything, anything)

          subject
        end
      end

      context 'when there is a lease' do
        before do
          allow_any_instance_of(described_class).to receive(:try_obtain_lease_for).and_return(false)
        end

        it 'does not call the service' do
          expect(CheckGcpProjectBillingService).not_to receive(:new)

          subject
        end
      end
    end

    context 'when there is no token in redis' do
      before do
        allow_any_instance_of(described_class).to receive(:get_session_token).and_return(nil)
      end

      it 'does not call the service' do
        expect(CheckGcpProjectBillingService).not_to receive(:new)

        subject
      end
    end
  end

  describe 'billing change counter' do
    subject { described_class.new.perform('token_key') }

    before do
      allow(described_class).to receive(:get_session_token).and_return('bogustoken')
      allow_any_instance_of(described_class).to receive(:try_obtain_lease_for).and_return('randomuuid')

      Gitlab::Redis::SharedState.with do |redis|
        allow(redis).to receive(:set)
      end
    end

    context 'when previous state was false' do
      before do
        expect_any_instance_of(described_class).to receive(:check_previous_state).and_return('false')
      end

      context 'when the current state is false' do
        before do
          expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([])
        end

        it 'does not increment the billing change counter' do
          Gitlab::Redis::SharedState.with do |redis|
            expect(redis).not_to receive(:incr)
          end

          subject
        end
      end

      context 'when the current state is true' do
        before do
          expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([double])
        end

        it 'increments the billing change counter' do
          Gitlab::Redis::SharedState.with do |redis|
            expect(redis).to receive(:incr)
          end

          subject
        end
      end
    end

    context 'when previous state was true' do
      before do
        expect_any_instance_of(described_class).to receive(:check_previous_state).and_return('true')
        expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([double])
      end

      it 'does not increment the billing change counter' do
        Gitlab::Redis::SharedState.with do |redis|
          expect(redis).not_to receive(:incr)
        end

        subject
      end
    end
  end
end