Remove Unnecessary .downcases From the Stories::Controller (#7646)

* Remove unnecessary .downcases from methods in Stories::Controller
  * Removes .downcase from methods using params[:username]
  * Removes .downcase from handle_user_or_organization_or_podcast_or_page_index
  * Removes .downcase from handle_user_index

* Add shared_examples for lowercase routing to stories_index_spec.rb

* Replace create with build in stories_index_spec.rb
This commit is contained in:
Julianna Tetreault 2020-05-01 11:24:22 -06:00 committed by GitHub
parent 96c27ecdc6
commit 0ee7b97b44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 11 deletions

View file

@ -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

View file

@ -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