diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ecd23acf7..ac36db1b1 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -165,8 +165,12 @@ class ApplicationController < ActionController::Base end end - def redirect_permanently_to(location) - redirect_to location + internal_nav_param, status: :moved_permanently + def redirect_permanently_to(url = nil, **args) + if url + redirect_to(url + internal_nav_param, status: :moved_permanently) + else + redirect_to(args.merge({ i: params[:i] }), status: :moved_permanently) + end end def customize_params diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index aacfef3e0..949034f80 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -324,7 +324,7 @@ class StoriesController < ApplicationController def redirect_to_lowercase_username return unless params[:username]&.match?(/[[:upper:]]/) - redirect_permanently_to("/#{params[:username].downcase}") + redirect_permanently_to(action: :index, username: params[:username].downcase) end def set_user_json_ld diff --git a/spec/requests/stories_index_spec.rb b/spec/requests/stories_index_spec.rb index e450cd5d7..ce17a9719 100644 --- a/spec/requests/stories_index_spec.rb +++ b/spec/requests/stories_index_spec.rb @@ -1,16 +1,16 @@ require "rails_helper" -RSpec.shared_examples "redirects to the lowercase route" do - context "when a path contains uppercase characters" do - it "redirects to the lowercase route" do - get path - expect(response).to have_http_status(:moved_permanently) - expect(response).to redirect_to(path.downcase) - end - end -end - RSpec.describe "StoriesIndex" do + it "redirects to the lowercase route", :aggregate_failures do + get "/Bad_name" + expect(response).to have_http_status(:moved_permanently) + expect(response).to redirect_to("/bad_name") + + get "/Bad_name?i=i" + expect(response).to have_http_status(:moved_permanently) + expect(response).to redirect_to("/bad_name?i=i") + end + describe "GET stories index" do let(:user) { create(:user) } @@ -346,10 +346,6 @@ RSpec.describe "StoriesIndex" do end describe "GET podcast index" do - include_examples "redirects to the lowercase route" do - let(:path) { "/#{build(:podcast).slug.upcase}" } - end - it "renders page with proper header" do podcast = create(:podcast) create(:podcast_episode, podcast: podcast) @@ -357,16 +353,4 @@ RSpec.describe "StoriesIndex" do expect(response.body).to include(podcast.title) end end - - describe "GET user_path" do - include_examples "redirects to the lowercase route" do - let(:path) { "/#{build(:user).username.upcase}" } - end - end - - describe "GET organization_path" do - include_examples "redirects to the lowercase route" do - let(:path) { "/#{build(:organization).slug.upcase}" } - end - end end