diff options
author | Douwe Maan <douwe@gitlab.com> | 2018-01-12 11:13:14 +0300 |
---|---|---|
committer | Luke Bennett <lbennett@gitlab.com> | 2018-01-17 19:03:02 +0300 |
commit | 0e06553494f994b07441552b6407b17d8cb7e905 (patch) | |
tree | 4ba35864b3a3132f697cd3a7f7e4a1ae208a4cd7 /spec/models | |
parent | 6671e32810a955511e3344e81b679b68b6b3c583 (diff) |
Merge branch 'mk-fix-permanent-redirect-validation' into 'master'
Fix Route validation when conflicting permanent redirects exist
Closes gitlab-com/support-forum#2883 and #41786
See merge request gitlab-org/gitlab-ce!16397
(cherry picked from commit d607f16fe5a1ec51eaea5811b5118b1ba48acfbc)
1dc30595 Revert "Revert "Fix Route validation for unchanged path""
c517788e Fix Rubocop offense
601c24d5 Add RedirectRoute factory
d4c8d1b7 Add changelog entry
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/route_spec.rb | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/spec/models/route_spec.rb b/spec/models/route_spec.rb index ddad6862a63..8a3b1034f3c 100644 --- a/spec/models/route_spec.rb +++ b/spec/models/route_spec.rb @@ -16,6 +16,66 @@ describe Route do it { is_expected.to validate_presence_of(:source) } it { is_expected.to validate_presence_of(:path) } it { is_expected.to validate_uniqueness_of(:path).case_insensitive } + + describe '#ensure_permanent_paths' do + context 'when the route is not yet persisted' do + let(:new_route) { described_class.new(path: 'foo', source: build(:group)) } + + context 'when permanent conflicting redirects exist' do + it 'is invalid' do + redirect = build(:redirect_route, :permanent, path: 'foo/bar/baz') + redirect.save!(validate: false) + + expect(new_route.valid?).to be_falsey + expect(new_route.errors.first[1]).to eq('foo has been taken before. Please use another one') + end + end + + context 'when no permanent conflicting redirects exist' do + it 'is valid' do + expect(new_route.valid?).to be_truthy + end + end + end + + context 'when path has changed' do + before do + route.path = 'foo' + end + + context 'when permanent conflicting redirects exist' do + it 'is invalid' do + redirect = build(:redirect_route, :permanent, path: 'foo/bar/baz') + redirect.save!(validate: false) + + expect(route.valid?).to be_falsey + expect(route.errors.first[1]).to eq('foo has been taken before. Please use another one') + end + end + + context 'when no permanent conflicting redirects exist' do + it 'is valid' do + expect(route.valid?).to be_truthy + end + end + end + + context 'when path has not changed' do + context 'when permanent conflicting redirects exist' do + it 'is valid' do + redirect = build(:redirect_route, :permanent, path: 'git_lab/foo/bar') + redirect.save!(validate: false) + + expect(route.valid?).to be_truthy + end + end + context 'when no permanent conflicting redirects exist' do + it 'is valid' do + expect(route.valid?).to be_truthy + end + end + end + end end describe 'callbacks' do |