2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
require Rails.root.join('db', 'post_migrate', '20170518231126_fix_wrongly_renamed_routes.rb')
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe FixWronglyRenamedRoutes, :migration do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:subject) { described_class.new }
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:namespaces_table) { table(:namespaces) }
|
|
|
|
let(:projects_table) { table(:projects) }
|
|
|
|
let(:routes_table) { table(:routes) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:broken_namespace) do
|
2018-03-17 18:26:18 +05:30
|
|
|
namespaces_table.create!(name: 'apiis', path: 'apiis').tap do |namespace|
|
|
|
|
routes_table.create!(source_type: 'Namespace', source_id: namespace.id, name: 'api0is', path: 'api0is')
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:broken_namespace_route) { routes_table.where(source_type: 'Namespace', source_id: broken_namespace.id).first }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
describe '#wrongly_renamed' do
|
|
|
|
it "includes routes that have names that don't match their namespace" do
|
|
|
|
broken_namespace
|
2018-03-17 18:26:18 +05:30
|
|
|
other_namespace = namespaces_table.create!(name: 'api0', path: 'api0')
|
|
|
|
routes_table.create!(source_type: 'Namespace', source_id: other_namespace.id, name: 'api0', path: 'api0')
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(subject.wrongly_renamed.map(&:id))
|
2018-03-17 18:26:18 +05:30
|
|
|
.to contain_exactly(broken_namespace_route.id)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#paths_and_corrections" do
|
|
|
|
it 'finds the wrong path and gets the correction from the namespace' do
|
|
|
|
broken_namespace
|
2018-03-17 18:26:18 +05:30
|
|
|
namespaces_table.create!(name: 'uploads-test', path: 'uploads-test').tap do |namespace|
|
|
|
|
routes_table.create!(source_type: 'Namespace', source_id: namespace.id, name: 'uploads-test', path: 'uploads0-test')
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expected_result = [
|
|
|
|
{ 'namespace_path' => 'apiis', 'path' => 'api0is' },
|
|
|
|
{ 'namespace_path' => 'uploads-test', 'path' => 'uploads0-test' }
|
|
|
|
]
|
|
|
|
|
|
|
|
expect(subject.paths_and_corrections).to include(*expected_result)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#routes_in_namespace_query' do
|
|
|
|
it 'includes only the required routes' do
|
2018-03-17 18:26:18 +05:30
|
|
|
namespace = namespaces_table.create!(name: 'hello', path: 'hello')
|
|
|
|
namespace_route = routes_table.create!(source_type: 'Namespace', source_id: namespace.id, name: 'hello', path: 'hello')
|
|
|
|
project = projects_table.new(name: 'my-project', path: 'my-project', namespace_id: namespace.id).tap do |project|
|
|
|
|
project.save!(validate: false)
|
|
|
|
end
|
|
|
|
routes_table.create!(source_type: 'Project', source_id: project.id, name: 'my-project', path: 'hello/my-project')
|
|
|
|
_other_namespace = namespaces_table.create!(name: 'hello0', path: 'hello0')
|
|
|
|
|
|
|
|
result = routes_table.where(subject.routes_in_namespace_query('hello'))
|
|
|
|
project_route = routes_table.where(source_type: 'Project', source_id: project.id).first
|
|
|
|
|
|
|
|
expect(result).to contain_exactly(namespace_route, project_route)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#up' do
|
|
|
|
it 'renames incorrectly named routes' do
|
2018-03-17 18:26:18 +05:30
|
|
|
broken_project =
|
|
|
|
projects_table.new(name: 'broken-project', path: 'broken-project', namespace_id: broken_namespace.id).tap do |project|
|
|
|
|
project.save!(validate: false)
|
|
|
|
routes_table.create!(source_type: 'Project', source_id: project.id, name: 'broken-project', path: 'api0is/broken-project')
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
subject.up
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
broken_project_route = routes_table.where(source_type: 'Project', source_id: broken_project.id).first
|
|
|
|
|
|
|
|
expect(broken_project_route.path).to eq('apiis/broken-project')
|
|
|
|
expect(broken_namespace_route.reload.path).to eq('apiis')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't touch namespaces that look like something that should be renamed" do
|
2018-03-17 18:26:18 +05:30
|
|
|
namespaces_table.create!(name: 'apiis', path: 'apiis')
|
|
|
|
namespace = namespaces_table.create!(name: 'hello', path: 'api0')
|
|
|
|
namespace_route = routes_table.create!(source_type: 'Namespace', source_id: namespace.id, name: 'hello', path: 'api0')
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
subject.up
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(namespace_route.reload.path).to eq('api0')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|