Skip to content

Commit

Permalink
Merge pull request #2643 from alphagov/update-route-validation
Browse files Browse the repository at this point in the history
Allow translations to be nested under their parent document
  • Loading branch information
brucebolt authored Feb 20, 2024
2 parents 45d8996 + 6ec69db commit d443c8c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
7 changes: 3 additions & 4 deletions app/validators/routes_and_redirects_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,10 @@ def validate(record, attribute, route, base_path)
def below_base_path?(path, base_path)
return true if path =~ %r{^#{base_path}\.[\w-]+\z}

path_segments = segments(path)
base_segments = segments(base_path)
suffix = /\.([\w-]+\z)$/.match(base_path).to_a&.first || ""
base_path_without_suffix = base_path.gsub(suffix, "")

pairs = base_segments.zip(path_segments)
pairs.all? { |a, b| a == b }
/^#{base_path_without_suffix}.*#{suffix}/.match?(path)
end

def segments(path)
Expand Down
33 changes: 33 additions & 0 deletions spec/support/routes_and_redirects_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,39 @@
expect(subject.errors[:routes]).to eq(["path must be below the base path"])
end

it "is valid when a route has a document type suffix" do
edition.routes = [
{ path: subject.base_path, type: "exact" },
{ path: "#{subject.base_path}.atom", type: "exact" },
]

expect(subject).to be_valid
end

describe "with nested translated routes" do
let(:edition) { build(:edition, base_path: "/vat-rates.fr") }

it "is valid when a translated route is below the base path" do
edition.routes = [
{ path: "/vat-rates.fr", type: "exact" },
{ path: "/vat-rates/path/document.fr", type: "exact" },
]

expect(subject).to be_valid
end

it "is invalid when a translated route is below the base path but has the wrong translation" do
edition.routes = [
{ path: "/vat-rates.fr", type: "exact" },
{ path: "/vat-rates/path/document.fr", type: "exact" },
{ path: "/vat-rates/path/document.es", type: "exact" },
]

expect(subject).to be_invalid
expect(subject.errors[:routes]).to eq(["path must be below the base path"])
end
end

it "must have unique paths" do
edition.routes = [
{ path: subject.base_path, type: "exact" },
Expand Down

0 comments on commit d443c8c

Please sign in to comment.