diff options
-rw-r--r-- | app/controllers/user_controller.rb | 4 | ||||
-rw-r--r-- | spec/controllers/user_controller_spec.rb | 30 |
2 files changed, 27 insertions, 7 deletions
diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 1bf5a5316..175425280 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -19,8 +19,8 @@ class UserController < ApplicationController # Show page about a user def show long_cache - if MySociety::Format.simplify_url_part(params[:url_name], 'user', 32) != params[:url_name] - redirect_to :url_name => MySociety::Format.simplify_url_part(params[:url_name], 'user', 32), :status => :moved_permanently + if MySociety::Format.simplify_url_part(params[:url_name], 'user') != params[:url_name] + redirect_to :url_name => MySociety::Format.simplify_url_part(params[:url_name], 'user'), :status => :moved_permanently return end if params[:view].nil? diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index fe95aa31c..0033309a5 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -3,6 +3,31 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') # XXX Use route_for or params_from to check /c/ links better # http://rspec.rubyforge.org/rspec-rails/1.1.12/classes/Spec/Rails/Example/ControllerExampleGroup.html +describe UserController, "when redirecting a show request to a canonical url" do + + it "should redirect to lower case name if given one with capital letters" do + get :show, :url_name => "Bob_Smith" + response.should redirect_to(:controller => 'user', :action => 'show', :url_name => "bob_smith") + end + + it 'should redirect a long non-canonical name that has a numerical suffix, + retaining the suffix' do + get :show, :url_name => 'Bob_SmithBob_SmithBob_SmithBob_S_2' + response.should redirect_to(:controller => 'user', + :action => 'show', + :url_name => 'bob_smithbob_smithbob_smithbob_s_2') + end + + it 'should not redirect a long canonical name that has a numerical suffix' do + User.stub!(:find).with(:first, anything()).and_return(mock_model(User, + :url_name => 'bob_smithbob_smithbob_smithbob_s_2', + :name => 'Bob Smith Bob Smith Bob Smith Bob Smith')) + User.stub!(:find).with(:all, anything()).and_return([]) + get :show, :url_name => 'bob_smithbob_smithbob_smithbob_s_2' + response.should be_success + end + +end describe UserController, "when showing a user" do render_views @@ -16,11 +41,6 @@ describe UserController, "when showing a user" do response.should be_success end - it "should redirect to lower case name if given one with capital letters" do - get :show, :url_name => "Bob_Smith" - response.should redirect_to(:controller => 'user', :action => 'show', :url_name => "bob_smith") - end - it "should render with 'show' template" do get :show, :url_name => "bob_smith" response.should render_template('show') |