summaryrefslogtreecommitdiff
path: root/spec/migrations/20210914095310_cleanup_orphan_project_access_tokens_spec.rb
blob: 2b755dfe11cd030e1a4eee2ad972e3ed64a61260 (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
# frozen_string_literal: true

require 'spec_helper'
require_migration!

RSpec.describe CleanupOrphanProjectAccessTokens, :migration do
  def create_user(**extra_options)
    defaults = { state: 'active', projects_limit: 0, email: "#{extra_options[:username]}@example.com" }

    table(:users).create!(defaults.merge(extra_options))
  end

  def create_membership(**extra_options)
    defaults = { access_level: 30, notification_level: 0, source_id: 1, source_type: 'Project' }

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

  let!(:regular_user) { create_user(username: 'regular') }
  let!(:orphan_bot) { create_user(username: 'orphaned_bot', user_type: 6) }
  let!(:used_bot) do
    create_user(username: 'used_bot', user_type: 6).tap do |bot|
      create_membership(user_id: bot.id)
    end
  end

  it 'marks all bots without memberships as deactivated' do
    expect do
      migrate!
      regular_user.reload
      orphan_bot.reload
      used_bot.reload
    end.to change {
      [regular_user.state, orphan_bot.state, used_bot.state]
    }.from(%w[active active active]).to(%w[active deactivated active])
  end

  it 'schedules for deletion all bots without memberships' do
    job_class = 'DeleteUserWorker'.safe_constantize

    if job_class
      expect(job_class).to receive(:bulk_perform_async).with([[orphan_bot.id, orphan_bot.id, skip_authorization: true]])

      migrate!
    end
  end
end