John Postlethwait
2010-May-27 20:07 UTC
before_filter always running in test, not in development or production mode.
I''m using Shoulda and restful_authentication on Rails3 and whenever I run my tests a before filter for login_required is always triggered, despite my filter being set up as: before_filter :login_required, :only => [:update, :edit, :suspend, :unsuspend, :destroy, :purge] My test is this: context ''A guest to the site'' do context ''on GET to :show'' do setup { get :show, { :id => users(:alexander).id } } should_not_set_the_flash should_assign_to :user should_respond_with :success end end The test fails though: 3) Failure: test: A guest to the site on GET to :show should assign @user. (UsersControllerTest) []: Expected action to assign a value for @user 4) Failure: test: A guest to the site on GET to :show should not set the flash. (UsersControllerTest) []: Did not expect the flash to be set, but was {:error=>"Sorry! You need to log in before visiting that page."} 5) Failure: test: A guest to the site on GET to :show should respond with success. (UsersControllerTest) []: Expected response to be a 200, but was 302 Here is the code for my before_filter that is actually making this test fail: def login_required authorized? || access_denied end def access_denied session[:return_to] = request.fullpath flash[:error] = t(''application.flash.error.login_required'') redirect_to(new_sessions_path) end This code, as far as my understanding goes, should NOT ever be called from this test though... I have the :only => [] in the controller to not do it on show. Can anyone lend any insight as to why it is being triggered? Another strange thing is this DOES NOT happen in production or development modes using the browser. I can be logged out and visit this page just fine without this before_filter being called. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Postlethwait
2010-May-27 22:27 UTC
Re: before_filter always running in test, not in development or production mode.
This is apparently not related to Shoulda... This test also fails using Test::Unit by itself: test ''A guest on GET to #show'' do get :show, { :id => users(:alexander).id } assert flash.empty? assert_not_nil assigns(:user) assert_response :success end Failure: 1) Failure: test_A_guest_on_GET_to_#show(UsersControllerTest) [test/functional/ users_controller_test.rb:44]: <false> is not true. Line 44 is the flash being empty, it is, once again set to: {:error=>"Sorry! You need to log in before visiting that page."} Am I missing something obvious? Why is this filter always running? Is this related to Rails3? Thanks in advance. -John Postlethwait On May 27, 1:07 pm, John Postlethwait <john.postlethw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m using Shoulda and restful_authentication on Rails3 and whenever I > run my tests a before filter for login_required is always triggered, > despite my filter being set up as: > > before_filter :login_required, :only => > [:update, :edit, :suspend, :unsuspend, :destroy, :purge] > > My test is this: > > context ''A guest to the site'' do > context ''on GET to :show'' do > setup { get :show, { :id => users(:alexander).id } } > > should_not_set_the_flash > should_assign_to :user > should_respond_with :success > end > end > > The test fails though: > > 3) Failure: > test: A guest to the site on GET to :show should assign @user. > (UsersControllerTest) > []: > Expected action to assign a value for @user > > 4) Failure: > test: A guest to the site on GET to :show should not set the flash. > (UsersControllerTest) > []: > Did not expect the flash to be set, but was {:error=>"Sorry! You > need to log in before visiting that page."} > > 5) Failure: > test: A guest to the site on GET to :show should respond with > success. (UsersControllerTest) > []: > Expected response to be a 200, but was 302 > > Here is the code for my before_filter that is actually making this > test fail: > > def login_required > authorized? || access_denied > end > > def access_denied > session[:return_to] = request.fullpath > flash[:error] = t(''application.flash.error.login_required'') > > redirect_to(new_sessions_path) > end > > This code, as far as my understanding goes, should NOT ever be called > from this test though... I have the :only => [] in the controller to > not do it on show. Can anyone lend any insight as to why it is being > triggered? Another strange thing is this DOES NOT happen in production > or development modes using the browser. I can be logged out and visit > this page just fine without this before_filter being called.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Postlethwait
2010-May-27 23:13 UTC
Re: before_filter always running in test, not in development or production mode.
I found a way around this, though I still have no clue why this works: In my controller, if I add: def show end The before filter DOES NOT run, as I expect, as soon as I remove the def show;end the filter is once again applied to the #show action. Is this a Rails3 bug? -John On May 27, 3:27 pm, John Postlethwait <john.postlethw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is apparently not related to Shoulda... This test also fails > using Test::Unit by itself: > > test ''A guest on GET to #show'' do > get :show, { :id => users(:alexander).id } > > assert flash.empty? > assert_not_nil assigns(:user) > assert_response :success > end > > Failure: > 1) Failure: > test_A_guest_on_GET_to_#show(UsersControllerTest) [test/functional/ > users_controller_test.rb:44]: > <false> is not true. > > Line 44 is the flash being empty, it is, once again set to: > {:error=>"Sorry! You need to log in before visiting that page."} > > Am I missing something obvious? Why is this filter always running? Is > this related to Rails3? > > Thanks in advance. > > -John Postlethwait > > On May 27, 1:07 pm, John Postlethwait <john.postlethw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > I''m using Shoulda and restful_authentication on Rails3 and whenever I > > run my tests a before filter for login_required is always triggered, > > despite my filter being set up as: > > > before_filter :login_required, :only => > > [:update, :edit, :suspend, :unsuspend, :destroy, :purge] > > > My test is this: > > > context ''A guest to the site'' do > > context ''on GET to :show'' do > > setup { get :show, { :id => users(:alexander).id } } > > > should_not_set_the_flash > > should_assign_to :user > > should_respond_with :success > > end > > end > > > The test fails though: > > > 3) Failure: > > test: A guest to the site on GET to :show should assign @user. > > (UsersControllerTest) > > []: > > Expected action to assign a value for @user > > > 4) Failure: > > test: A guest to the site on GET to :show should not set the flash. > > (UsersControllerTest) > > []: > > Did not expect the flash to be set, but was {:error=>"Sorry! You > > need to log in before visiting that page."} > > > 5) Failure: > > test: A guest to the site on GET to :show should respond with > > success. (UsersControllerTest) > > []: > > Expected response to be a 200, but was 302 > > > Here is the code for my before_filter that is actually making this > > test fail: > > > def login_required > > authorized? || access_denied > > end > > > def access_denied > > session[:return_to] = request.fullpath > > flash[:error] = t(''application.flash.error.login_required'') > > > redirect_to(new_sessions_path) > > end > > > This code, as far as my understanding goes, should NOT ever be called > > from this test though... I have the :only => [] in the controller to > > not do it on show. Can anyone lend any insight as to why it is being > > triggered? Another strange thing is this DOES NOT happen in production > > or development modes using the browser. I can be logged out and visit > > this page just fine without this before_filter being called.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Keith Tom
2010-Nov-09 02:13 UTC
Re: before_filter always running in test, not in development or production mode.
John Postlethwait wrote in post #915199:> I found a way around this, though I still have no clue why this works: > > In my controller, if I add: > > def show > end > > The before filter DOES NOT run, as I expect, as soon as I remove the > def show;end the filter is once again applied to the #show action. Is > this a Rails3 bug? > > -John > > On May 27, 3:27 pm, John Postlethwait <john.postlethw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>This might be of interest: https://rails.lighthouseapp.com/projects/8994/tickets/5673-rails3-conditional-before_filter-not-always-executed-for-implicit-resource-actions#ticket-5673-33 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.