summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/reset_duplicate_ci_runners_token_encrypted_values_spec.rb
blob: b6da8f7fc2d16505fa619a14136d35319ece4c6c (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::ResetDuplicateCiRunnersTokenEncryptedValues,
  :migration,
  schema: 20220922143634 do
  it { expect(described_class).to be < Gitlab::BackgroundMigration::BatchedMigrationJob }

  describe '#perform' do
    let(:ci_runners) { table(:ci_runners, database: :ci) }

    let(:test_worker) do
      described_class.new(
        start_id: 1,
        end_id: 4,
        batch_table: :ci_runners,
        batch_column: :id,
        sub_batch_size: 2,
        pause_ms: 0,
        connection: Ci::ApplicationRecord.connection
      )
    end

    subject(:perform) { test_worker.perform }

    before do
      ci_runners.create!(id: 1, runner_type: 1, token_encrypted: 'duplicate')
      ci_runners.create!(id: 2, runner_type: 1, token_encrypted: 'a-token')
      ci_runners.create!(id: 3, runner_type: 1, token_encrypted: 'duplicate-2')
      ci_runners.create!(id: 4, runner_type: 1, token_encrypted: nil)
      ci_runners.create!(id: 5, runner_type: 1, token_encrypted: 'duplicate-2')
      ci_runners.create!(id: 6, runner_type: 1, token_encrypted: 'duplicate')
      ci_runners.create!(id: 7, runner_type: 1, token_encrypted: 'another-token')
      ci_runners.create!(id: 8, runner_type: 1, token_encrypted: 'another-token')
    end

    it 'nullifies duplicate encrypted tokens', :aggregate_failures do
      expect { perform }.to change { ci_runners.all.order(:id).pluck(:id, :token_encrypted).to_h }
                              .from(
                                {
                                  1 => 'duplicate',
                                  2 => 'a-token',
                                  3 => 'duplicate-2',
                                  4 => nil,
                                  5 => 'duplicate-2',
                                  6 => 'duplicate',
                                  7 => 'another-token',
                                  8 => 'another-token'
                                }
                              )
                              .to(
                                {
                                  1 => nil,
                                  2 => 'a-token',
                                  3 => nil,
                                  4 => nil,
                                  5 => nil,
                                  6 => nil,
                                  7 => 'another-token',
                                  8 => 'another-token'
                                }
                              )
      expect(ci_runners.count).to eq(8)
      expect(ci_runners.pluck(:token_encrypted).uniq).to match_array [
        nil, 'a-token', 'another-token'
      ]
    end
  end
end