Welcome to mirror list, hosted at ThFree Co, Russian Federation.

orphaned_invite_tokens_cleanup_spec.rb « migrations « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 56f47fca8645f4b4814a691e00cd27f34225e7b1 (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
# frozen_string_literal: true

require 'spec_helper'
require_migration!

RSpec.describe OrphanedInviteTokensCleanup, :migration, feature_category: :subgroups do
  def create_member(**extra_attributes)
    defaults = {
      access_level: 10,
      source_id: 1,
      source_type: "Project",
      notification_level: 0,
      type: 'ProjectMember'
    }

    table(:members).create!(defaults.merge(extra_attributes))
  end

  shared_examples 'removes orphaned invite tokens' do
    it 'removes invite tokens for accepted records with invite_accepted_at < created_at' do
      record1 = create_member(invite_token: 'foo', invite_accepted_at: 1.day.ago, created_at: 1.hour.ago)
      record2 = create_member(invite_token: 'foo2', invite_accepted_at: nil, created_at: 1.hour.ago)
      record3 = create_member(invite_token: 'foo3', invite_accepted_at: 1.day.ago, created_at: 1.year.ago)

      migrate!

      expect(table(:members).find(record1.id).invite_token).to eq nil
      expect(table(:members).find(record2.id).invite_token).to eq 'foo2'
      expect(table(:members).find(record3.id).invite_token).to eq 'foo3'
    end
  end

  describe '#up', :aggregate_failures do
    it_behaves_like 'removes orphaned invite tokens'
  end

  context 'when there is a mix of timestamptz and timestamp types' do
    around do |example|
      ActiveRecord::Base.connection.execute "ALTER TABLE members alter created_at type timestamp with time zone"

      example.run

      ActiveRecord::Base.connection.execute "ALTER TABLE members alter created_at type timestamp without time zone"
    end

    describe '#up', :aggregate_failures do
      it_behaves_like 'removes orphaned invite tokens'
    end
  end
end