summaryrefslogtreecommitdiff
path: root/spec/migrations/normalize_ldap_extern_uids_spec.rb
blob: a23a5d54e0aa7c84945e5b2878cf2e7aa377b9dd (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
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20170921101004_normalize_ldap_extern_uids')

describe NormalizeLdapExternUids, :migration, :sidekiq do
  let!(:identities) { table(:identities) }

  around do |example|
    Timecop.freeze { example.run }
  end

  before do
    stub_const("Gitlab::Database::MigrationHelpers::BACKGROUND_MIGRATION_BATCH_SIZE", 2)
    stub_const("Gitlab::Database::MigrationHelpers::BACKGROUND_MIGRATION_JOB_BUFFER_SIZE", 2)

    # LDAP identities
    (1..4).each do |i|
      identities.create!(id: i, provider: 'ldapmain', extern_uid: " uid = foo #{i}, ou = People, dc = example, dc = com ", user_id: i)
    end

    # Non-LDAP identity
    identities.create!(id: 5, provider: 'foo', extern_uid: " uid = foo 5, ou = People, dc = example, dc = com ", user_id: 5)
  end

  it 'correctly schedules background migrations' do
    Sidekiq::Testing.fake! do
      Timecop.freeze do
        migrate!

        expect(BackgroundMigrationWorker.jobs[0]['args']).to eq([described_class::MIGRATION, [1, 2]])
        expect(BackgroundMigrationWorker.jobs[0]['at']).to eq(2.minutes.from_now.to_f)
        expect(BackgroundMigrationWorker.jobs[1]['args']).to eq([described_class::MIGRATION, [3, 4]])
        expect(BackgroundMigrationWorker.jobs[1]['at']).to eq(4.minutes.from_now.to_f)
        expect(BackgroundMigrationWorker.jobs[2]['args']).to eq([described_class::MIGRATION, [5, 5]])
        expect(BackgroundMigrationWorker.jobs[2]['at']).to eq(6.minutes.from_now.to_f)
        expect(BackgroundMigrationWorker.jobs.size).to eq 3
      end
    end
  end

  it 'migrates the LDAP identities' do
    perform_enqueued_jobs do
      migrate!
      identities.where(id: 1..4).each do |identity|
        expect(identity.extern_uid).to eq("uid=foo #{identity.id},ou=people,dc=example,dc=com")
      end
    end
  end

  it 'does not modify non-LDAP identities' do
    perform_enqueued_jobs do
      migrate!
      identity = identities.last
      expect(identity.extern_uid).to eq(" uid = foo 5, ou = People, dc = example, dc = com ")
    end
  end
end