summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/fix_user_project_route_names_spec.rb
blob: 4c04043ebd026edddad4e2242c61e35d34f446f7 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::FixUserProjectRouteNames, schema: 20190620112608 do
  let(:namespaces) { table(:namespaces) }
  let(:users) { table(:users) }
  let(:routes) { table(:routes) }
  let(:projects) { table(:projects) }

  let(:user) { users.create(name: "The user's full name", projects_limit: 10, username: 'not-null', email: '1') }

  let(:namespace) do
    namespaces.create(
      owner_id: user.id,
      name: "Should eventually be the user's name",
      path: user.username
    )
  end

  let(:project) do
    projects.create(namespace_id: namespace.id, name: 'Project Name')
  end

  it "updates the route for a project if it did not match the user's name" do
    route = routes.create(
      id: 1,
      path: "#{user.username}/#{project.path}",
      source_id: project.id,
      source_type: 'Project',
      name: 'Completely wrong'
    )

    described_class.new.perform(1, 5)

    expect(route.reload.name).to eq("The user's full name / Project Name")
  end

  it 'updates the route for a project if the name was nil' do
    route = routes.create(
      id: 1,
      path: "#{user.username}/#{project.path}",
      source_id: project.id,
      source_type: 'Project',
      name: nil
    )

    described_class.new.perform(1, 5)

    expect(route.reload.name).to eq("The user's full name / Project Name")
  end

  it 'does not update routes that were are out of the range' do
    route = routes.create(
      id: 6,
      path: "#{user.username}/#{project.path}",
      source_id: project.id,
      source_type: 'Project',
      name: 'Completely wrong'
    )

    expect { described_class.new.perform(1, 5) }
      .not_to change { route.reload.name }
  end

  it 'does not update routes for projects in groups owned by the user' do
    group = namespaces.create(
      owner_id: user.id,
      name: 'A group',
      path: 'a-path',
      type: ''
    )
    project = projects.create(namespace_id: group.id, name: 'Project Name')
    route = routes.create(
      id: 1,
      path: "#{group.path}/#{project.path}",
      source_id: project.id,
      source_type: 'Project',
      name: 'Completely wrong'
    )

    expect { described_class.new.perform(1, 5) }
      .not_to change { route.reload.name }
  end

  it 'does not update routes for namespaces' do
    route = routes.create(
      id: 1,
      path: namespace.path,
      source_id: namespace.id,
      source_type: 'Namespace',
      name: 'Completely wrong'
    )

    expect { described_class.new.perform(1, 5) }
      .not_to change { route.reload.name }
  end
end