2017-12-16 01:39:55 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe CheckGcpProjectBillingWorker do
|
|
|
|
describe '.perform' do
|
|
|
|
let(:token) { 'bogustoken' }
|
2018-01-07 14:07:37 +00:00
|
|
|
|
2018-01-06 18:02:18 +00:00
|
|
|
subject { described_class.new.perform('token_key') }
|
2017-12-16 01:39:55 +00:00
|
|
|
|
2018-02-06 22:01:36 +00:00
|
|
|
before do
|
2018-02-08 15:57:14 +00:00
|
|
|
allow(described_class).to receive(:get_billing_state)
|
2018-02-06 22:01:36 +00:00
|
|
|
allow_any_instance_of(described_class).to receive(:update_billing_change_counter)
|
|
|
|
end
|
|
|
|
|
2018-01-06 18:02:18 +00:00
|
|
|
context 'when there is a token in redis' do
|
2017-12-16 03:21:13 +00:00
|
|
|
before do
|
2018-01-11 00:38:47 +00:00
|
|
|
allow(described_class).to receive(:get_session_token).and_return(token)
|
2017-12-16 03:21:13 +00:00
|
|
|
end
|
2017-12-16 01:39:55 +00:00
|
|
|
|
2018-01-06 18:02:18 +00:00
|
|
|
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
|
2017-12-16 03:21:13 +00:00
|
|
|
|
2018-01-06 18:02:18 +00:00
|
|
|
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
|
|
|
|
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([double])
|
2018-02-08 15:57:14 +00:00
|
|
|
expect(described_class).to receive(:set_billing_state).with(token, true)
|
2018-01-06 18:02:18 +00:00
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2017-12-16 03:21:13 +00:00
|
|
|
end
|
|
|
|
|
2018-01-06 18:02:18 +00:00
|
|
|
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
|
2017-12-16 03:21:13 +00:00
|
|
|
|
2018-01-06 18:02:18 +00:00
|
|
|
it 'does not call the service' do
|
|
|
|
expect(CheckGcpProjectBillingService).not_to receive(:new)
|
2017-12-16 03:21:13 +00:00
|
|
|
|
2018-01-06 18:02:18 +00:00
|
|
|
subject
|
|
|
|
end
|
2017-12-16 03:21:13 +00:00
|
|
|
end
|
2017-12-16 01:39:55 +00:00
|
|
|
end
|
|
|
|
|
2018-01-06 18:02:18 +00:00
|
|
|
context 'when there is no token in redis' do
|
2017-12-16 03:21:13 +00:00
|
|
|
before do
|
2018-02-08 15:57:14 +00:00
|
|
|
allow(described_class).to receive(:get_session_token).and_return(nil)
|
2017-12-16 03:21:13 +00:00
|
|
|
end
|
2017-12-16 03:00:54 +00:00
|
|
|
|
2017-12-16 03:21:13 +00:00
|
|
|
it 'does not call the service' do
|
|
|
|
expect(CheckGcpProjectBillingService).not_to receive(:new)
|
2017-12-16 01:39:55 +00:00
|
|
|
|
2017-12-16 03:21:13 +00:00
|
|
|
subject
|
|
|
|
end
|
2017-12-16 01:39:55 +00:00
|
|
|
end
|
|
|
|
end
|
2018-02-06 22:01:36 +00:00
|
|
|
|
|
|
|
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')
|
2018-02-08 15:57:14 +00:00
|
|
|
allow(described_class).to receive(:set_billing_state)
|
2018-02-06 22:01:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when previous state was false' do
|
|
|
|
before do
|
2018-02-08 15:57:14 +00:00
|
|
|
expect(described_class).to receive(:get_billing_state).and_return(false)
|
2018-02-06 22:01:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the current state is false' do
|
|
|
|
before do
|
|
|
|
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([])
|
|
|
|
end
|
|
|
|
|
2018-02-12 17:32:10 +00:00
|
|
|
it 'increments the billing change counter' do
|
|
|
|
expect_any_instance_of(described_class).to receive_message_chain(:billing_changed_counter, :increment)
|
2018-02-06 22:01:36 +00:00
|
|
|
|
|
|
|
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
|
2018-02-07 16:41:03 +00:00
|
|
|
expect_any_instance_of(described_class).to receive_message_chain(:billing_changed_counter, :increment)
|
2018-02-06 22:01:36 +00:00
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when previous state was true' do
|
|
|
|
before do
|
2018-02-08 15:57:14 +00:00
|
|
|
expect(described_class).to receive(:get_billing_state).and_return(true)
|
2018-02-06 22:01:36 +00:00
|
|
|
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([double])
|
|
|
|
end
|
|
|
|
|
2018-02-12 17:32:10 +00:00
|
|
|
it 'increment the billing change counter' do
|
|
|
|
expect_any_instance_of(described_class).to receive_message_chain(:billing_changed_counter, :increment)
|
2018-02-06 22:01:36 +00:00
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-12-16 01:39:55 +00:00
|
|
|
end
|