summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/safe_request_purger_spec.rb
blob: 02f3f11d469b84e4b729d3952fe8860422680476 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::SafeRequestPurger do
  let(:resource_key) { '_key_' }
  let(:resource_ids) { ['foo'] }
  let(:args) { { resource_key: resource_key, resource_ids: resource_ids } }
  let(:resource_data) { { 'foo' => 'bar' } }

  before do
    Gitlab::SafeRequestStore[resource_key] = resource_data
  end

  describe '.execute', :request_store do
    subject(:execute_instance) { described_class.execute(**args) }

    it 'purges an entry from the store' do
      execute_instance

      expect(Gitlab::SafeRequestStore.fetch(resource_key)).to be_empty
    end
  end

  describe '#execute' do
    subject(:execute_instance) { described_class.new(**args).execute }

    context 'when request store is active', :request_store do
      it 'purges an entry from the store' do
        execute_instance

        expect(Gitlab::SafeRequestStore.fetch(resource_key)).to be_empty
      end

      context 'when there are multiple resource_ids to purge' do
        let(:resource_data) do
          {
            'foo' => 'bar',
            'two' => '_two_',
            'three' => '_three_',
            'four' => '_four_'
          }
        end

        let(:resource_ids) { %w[two three] }

        it 'purges an entry from the store' do
          execute_instance

          expect(Gitlab::SafeRequestStore.fetch(resource_key)).to eq resource_data.slice('foo', 'four')
        end
      end

      context 'when there is no matching resource_ids' do
        let(:resource_ids) { ['_bogus_resource_id_'] }

        it 'purges an entry from the store' do
          execute_instance

          expect(Gitlab::SafeRequestStore.fetch(resource_key)).to eq resource_data
        end
      end
    end

    context 'when request store is not active' do
      let(:resource_ids) { ['_bogus_resource_id_'] }

      it 'does offer the ability to interact with data store' do
        expect(execute_instance).to eq({})
      end
    end
  end
end