summaryrefslogtreecommitdiff
path: root/spec/migrations/generate_missing_routes_for_bots_spec.rb
blob: 8af22042350750559510f512997ba59230c4279e (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
74
75
76
77
78
79
80
# frozen_string_literal: true

require 'spec_helper'

require Rails.root.join('db', 'post_migrate', '20200703064117_generate_missing_routes_for_bots.rb')

RSpec.describe GenerateMissingRoutesForBots, :migration do
  let(:users) { table(:users) }
  let(:namespaces) { table(:namespaces) }
  let(:routes) { table(:routes) }

  let(:visual_review_bot) do
    users.create!(email: 'visual-review-bot@gitlab.com', name: 'GitLab Visual Review Bot', username: 'visual-review-bot', user_type: 3, projects_limit: 5)
  end

  let(:migration_bot) do
    users.create!(email: 'migration-bot@gitlab.com', name: 'GitLab Migration Bot', username: 'migration-bot', user_type: 7, projects_limit: 5)
  end

  let!(:visual_review_bot_namespace) do
    namespaces.create!(owner_id: visual_review_bot.id, name: visual_review_bot.name, path: visual_review_bot.username)
  end

  let!(:migration_bot_namespace) do
    namespaces.create!(owner_id: migration_bot.id, name: migration_bot.name, path: migration_bot.username)
  end

  context 'for bot users without an existing route' do
    it 'creates new routes' do
      expect { migrate! }.to change { routes.count }.by(2)
    end

    it 'creates new routes with the same path and name as their namespace' do
      migrate!

      [visual_review_bot, migration_bot].each do |bot|
        namespace = namespaces.find_by(owner_id: bot.id)
        route = route_for(namespace: namespace)

        expect(route.path).to eq(namespace.path)
        expect(route.name).to eq(namespace.name)
      end
    end
  end

  it 'does not create routes for bot users with existing routes' do
    create_route!(namespace: visual_review_bot_namespace)
    create_route!(namespace: migration_bot_namespace)

    expect { migrate! }.not_to change { routes.count }
  end

  it 'does not create routes for human users without an existing route' do
    human_namespace = create_human_namespace!(name: 'GitLab Human', username: 'human')

    expect { migrate! }.not_to change { route_for(namespace: human_namespace) }
  end

  it 'does not create route for a bot user with a missing route, if a human user with the same path already exists' do
    human_namespace = create_human_namespace!(name: visual_review_bot.name, username: visual_review_bot.username)
    create_route!(namespace: human_namespace)

    expect { migrate! }.not_to change { route_for(namespace: visual_review_bot_namespace) }
  end

  private

  def create_human_namespace!(name:, username:)
    human = users.create!(email: 'human@gitlab.com', name: name, username: username, user_type: nil, projects_limit: 5)
    namespaces.create!(owner_id: human.id, name: human.name, path: human.username)
  end

  def create_route!(namespace:)
    routes.create!(path: namespace.path, name: namespace.name, source_id: namespace.id, source_type: 'Namespace')
  end

  def route_for(namespace:)
    routes.find_by(source_type: 'Namespace', source_id: namespace.id)
  end
end