diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index d9f3b1e47..d2287cff2 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -77,7 +77,7 @@ class StoriesController < ApplicationController end def redirect_to_changed_username_profile - potential_username = params[:username].tr("@", "").downcase + potential_username = params[:username].tr("@", "") user_or_org = User.find_by("old_username = ? OR old_old_username = ?", potential_username, potential_username) || Organization.find_by("old_slug = ? OR old_old_slug = ?", potential_username, potential_username) if user_or_org.present? && !user_or_org.decorate.fully_banished? @@ -101,9 +101,9 @@ class StoriesController < ApplicationController end def handle_user_or_organization_or_podcast_or_page_index - @podcast = Podcast.available.find_by(slug: params[:username].downcase) - @organization = Organization.find_by(slug: params[:username].downcase) - @page = Page.find_by(slug: params[:username].downcase, is_top_level_path: true) + @podcast = Podcast.available.find_by(slug: params[:username]) + @organization = Organization.find_by(slug: params[:username]) + @page = Page.find_by(slug: params[:username], is_top_level_path: true) if @podcast handle_podcast_index elsif @organization @@ -195,7 +195,7 @@ class StoriesController < ApplicationController end def handle_user_index - @user = User.find_by(username: params[:username].tr("@", "").downcase) + @user = User.find_by(username: params[:username].tr("@", "")) unless @user redirect_to_changed_username_profile return diff --git a/spec/requests/stories_index_spec.rb b/spec/requests/stories_index_spec.rb index 716c6cfa1..583e370fd 100644 --- a/spec/requests/stories_index_spec.rb +++ b/spec/requests/stories_index_spec.rb @@ -1,5 +1,15 @@ 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", type: :request do describe "GET stories index" do it "renders page with article list" do @@ -171,6 +181,10 @@ RSpec.describe "StoriesIndex", type: :request 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) @@ -331,13 +345,14 @@ RSpec.describe "StoriesIndex", type: :request do end describe "GET user_path" do - let(:user) { create(:user) } + include_examples "redirects to the lowercase route" do + let(:path) { "/#{build(:user).username.upcase}" } + end + end - it "redirects to the lowercase route for usernames", :aggregate_failures do - get "/#{user.username.capitalize}" - expect(response).to have_http_status(:moved_permanently) - expect(response).to redirect_to("/#{user.username.downcase}") - expect(response).not_to redirect_to("/#{user.username.capitalize}") + describe "GET organization_path" do + include_examples "redirects to the lowercase route" do + let(:path) { "/#{build(:organization).slug.upcase}" } end end end