From 25d497cc4b4fd4c2b4fa8dc643213486d7c5c025 Mon Sep 17 00:00:00 2001 From: Paul Robert Lloyd Date: Wed, 16 Mar 2022 15:37:45 +0000 Subject: [PATCH] Outlaw underscores in URLs --- config/routes.rb | 3 +-- spec/config/routes_spec.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 spec/config/routes_spec.rb diff --git a/config/routes.rb b/config/routes.rb index 867980bc1..261491519 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,4 @@ +# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html Rails.application.routes.draw do devise_for :admin_users, { path: :admin, @@ -28,12 +29,10 @@ Rails.application.routes.draw do get "/health", to: ->(_) { [204, {}, [nil]] } - # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html ActiveAdmin.routes(self) root to: "start#index" - # Content pages get "/accessibility-statement", to: "content#accessibility_statement" get "/privacy-notice", to: "content#privacy_notice" diff --git a/spec/config/routes_spec.rb b/spec/config/routes_spec.rb new file mode 100644 index 000000000..8d5cb91ef --- /dev/null +++ b/spec/config/routes_spec.rb @@ -0,0 +1,18 @@ +require "rails_helper" + +RSpec.describe "routes.rb" do + it "does not use underscores" do + paths = Rails.application.routes.routes.map { |r| r.path.spec.to_s if r.defaults[:controller] }.compact + + # Allow underscores for ActiveAdmin, Rails and Turbo routes + paths = paths.reject { |p| p.starts_with?("/admin") } + paths = paths.reject { |p| p.starts_with?("/rails") } + paths = paths.reject { |p| p.include?("_historical_location") } + + paths.each do |path| + has_underscores = path.split("/").any? { |component| !component.start_with?(":") && component.match("_") } + + expect(has_underscores).to be(false), "#{path} should not have underscores" + end + end +end