Fix Project#to_param to keep invalid project suitable for use in URLs

parent 6dfaf4fe
...@@ -586,7 +586,11 @@ class Project < ActiveRecord::Base ...@@ -586,7 +586,11 @@ class Project < ActiveRecord::Base
end end
def to_param def to_param
path if persisted? && errors.include?(:path)
path_was
else
path
end
end end
def to_reference(_from_project = nil) def to_reference(_from_project = nil)
......
...@@ -23,17 +23,7 @@ module Projects ...@@ -23,17 +23,7 @@ module Projects
if project.previous_changes.include?('path') if project.previous_changes.include?('path')
project.rename_repo project.rename_repo
end end
else
restore_attributes
false
end end
end end
private
def restore_attributes
project.path = project.path_was if project.errors.include?(:path)
project.name = project.name_was if project.errors.include?(:name)
end
end end
end end
- if @project.errors.blank? - if @project.valid?
:plain :plain
location.href = "#{edit_namespace_project_path(@project.namespace, @project)}"; location.href = "#{edit_namespace_project_path(@project.namespace, @project)}";
- else - else
......
...@@ -32,8 +32,8 @@ describe 'Edit Project Settings', feature: true do ...@@ -32,8 +32,8 @@ describe 'Edit Project Settings', feature: true do
click_button 'Rename project' click_button 'Rename project'
expect(page).to have_field 'Project name', with: 'sample' expect(page).to have_field 'Project name', with: 'foo&bar'
expect(page).to have_field 'Path', with: 'gitlab' expect(page).to have_field 'Path', with: 'foo&bar'
expect(page).to have_content "Name can contain only letters, digits, '_', '.', dash and space. It must start with letter, digit or '_'." expect(page).to have_content "Name can contain only letters, digits, '_', '.', dash and space. It must start with letter, digit or '_'."
expect(page).to have_content "Path can contain only letters, digits, '_', '-' and '.'. Cannot start with '-', end in '.git' or end in '.atom'" expect(page).to have_content "Path can contain only letters, digits, '_', '-' and '.'. Cannot start with '-', end in '.git' or end in '.atom'"
end end
......
...@@ -372,6 +372,24 @@ describe Project, models: true do ...@@ -372,6 +372,24 @@ describe Project, models: true do
it { expect(@project.to_param).to eq('gitlabhq') } it { expect(@project.to_param).to eq('gitlabhq') }
end end
context 'with invalid path' do
it 'returns previous path to keep project suitable for use in URLs when persisted' do
project = create(:empty_project, path: 'gitlab')
project.path = 'foo&bar'
expect(project).not_to be_valid
expect(project.to_param).to eq 'gitlab'
end
it 'returns current path when new record' do
project = build(:empty_project, path: 'gitlab')
project.path = 'foo&bar'
expect(project).not_to be_valid
expect(project.to_param).to eq 'foo&bar'
end
end
end end
describe '#repository' do describe '#repository' do
......
...@@ -139,27 +139,6 @@ describe Projects::UpdateService, services: true do ...@@ -139,27 +139,6 @@ describe Projects::UpdateService, services: true do
end end
end end
context 'for invalid project path/name' do
let(:user) { create(:user, admin: true) }
let(:project) { create(:empty_project, path: 'gitlab', name: 'sample') }
let(:params) { { path: 'foo&bar', name: 'foo&bar' } }
it 'resets to previous values to keep project in a valid state' do
update_project(project, user, params)
expect(project.path).to eq 'gitlab'
expect(project.name).to eq 'sample'
end
it 'keeps error messages' do
update_project(project, user, params)
expect(project.errors).not_to be_blank
expect(project.errors[:name]).to include("can contain only letters, digits, '_', '.', dash and space. It must start with letter, digit or '_'.")
expect(project.errors[:path]).to include("can contain only letters, digits, '_', '-' and '.'. Cannot start with '-', end in '.git' or end in '.atom'")
end
end
def update_project(project, user, opts) def update_project(project, user, opts)
Projects::UpdateService.new(project, user, opts).execute Projects::UpdateService.new(project, user, opts).execute
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment