FIX: allow ampersand in site_texts routes

FIX: allow ampersand in site_texts routes

diff --git a/config/routes.rb b/config/routes.rb
index 1cfc99d..44a2663 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -221,12 +221,12 @@ Discourse::Application.routes.draw do
 
       # They have periods in their URLs often:
       get 'site_texts'             => 'site_texts#index'
-      get 'site_texts/:id.json'    => 'site_texts#show',   constraints: { id: /[\w.\-\+]+/i }
-      get 'site_texts/:id'         => 'site_texts#show',   constraints: { id: /[\w.\-\+]+/i }
-      put 'site_texts/:id.json'    => 'site_texts#update', constraints: { id: /[\w.\-\+]+/i }
-      put 'site_texts/:id'         => 'site_texts#update', constraints: { id: /[\w.\-\+]+/i }
-      delete 'site_texts/:id.json' => 'site_texts#revert', constraints: { id: /[\w.\-\+]+/i }
-      delete 'site_texts/:id'      => 'site_texts#revert', constraints: { id: /[\w.\-\+]+/i }
+      get 'site_texts/:id.json'    => 'site_texts#show',   constraints: { id: /[\w.\-\+\%\&]+/i }
+      get 'site_texts/:id'         => 'site_texts#show',   constraints: { id: /[\w.\-\+\%\&]+/i }
+      put 'site_texts/:id.json'    => 'site_texts#update', constraints: { id: /[\w.\-\+\%\&]+/i }
+      put 'site_texts/:id'         => 'site_texts#update', constraints: { id: /[\w.\-\+\%\&]+/i }
+      delete 'site_texts/:id.json' => 'site_texts#revert', constraints: { id: /[\w.\-\+\%\&]+/i }
+      delete 'site_texts/:id'      => 'site_texts#revert', constraints: { id: /[\w.\-\+\%\&]+/i }
 
       get 'reseed' => 'site_texts#get_reseed_options'
       post 'reseed' => 'site_texts#reseed'
diff --git a/spec/requests/admin/site_texts_controller_spec.rb b/spec/requests/admin/site_texts_controller_spec.rb
index 89132b9..a2edd37 100644
--- a/spec/requests/admin/site_texts_controller_spec.rb
+++ b/spec/requests/admin/site_texts_controller_spec.rb
@@ -172,6 +172,18 @@ RSpec.describe Admin::SiteTextsController do
         expect(site_text['value']).to eq(I18n.t("js.topic.list"))
       end
 
+      it 'returns a site text for a key with ampersand' do
+        get "/admin/customize/site_texts/js.emoji_picker.food_&_drink.json"
+        expect(response.status).to eq(200)
+
+        json = ::JSON.parse(response.body)
+
+        site_text = json['site_text']
+
+        expect(site_text['id']).to eq('js.emoji_picker.food_&_drink')
+        expect(site_text['value']).to eq(I18n.t("js.emoji_picker.food_&_drink"))
+      end
+
       it 'returns not found for missing keys' do
         get "/admin/customize/site_texts/made_up_no_key_exists.json"
         expect(response.status).to eq(404)
@@ -263,8 +275,8 @@ RSpec.describe Admin::SiteTextsController do
         expect(json['error_type']).to eq('not_found')
       end
 
-      it "works as expectd with correct keys" do
-        put '/admin/customize/site_texts/login_required.welcome_message.json', params: {
+      it "works as expected with correct keys" do
+        put '/admin/customize/site_texts/js.emoji_picker.animals_%26_nature.json', params: {
           site_text: { value: 'foo' }
         }
 
@@ -273,7 +285,7 @@ RSpec.describe Admin::SiteTextsController do
         json = ::JSON.parse(response.body)
         site_text = json['site_text']
 
-        expect(site_text['id']).to eq('login_required.welcome_message')
+        expect(site_text['id']).to eq('js.emoji_picker.animals_&_nature')
         expect(site_text['value']).to eq('foo')
       end

GitHub sha: fffca423

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: