Phoenix Rising
2009-Jul-31 21:08 UTC
Integration test receives HTTP 200 response when it should be a redirect
This is probably due to my novice understanding of integration tests, but this behavior looked pretty strange, so I figured I''d ask. The application I''m working on at present requires users, upon login, to assign themselves to a location. Locations are pre-loaded in the database. Because I don''t want the user "getting around" this functionality by ignoring it and just clicking something else (this is a workplace-only application), I''m using this as part of my authentication process. Long story short, every controller has a before_filter called "login_required". The login_required method exists in ApplicationController. It''s very simple: checks to see if the user''s session contains a "username" value. If not, they''re kicked back to the authenticate controller and told to login. If they DO have this in their session, however, is where the fun starts. At that point, it looks for the user in the database (original authentication is done by Active Directory, and I have a local database that "syncs" with AD nightly). If found, it then checks to see if the user''s home location (called a "store" in this case) is set. If NOT, it redirects them to /users/assign_location. And there''s the rub: it''s an actual call to redirect_to. Yet my integration test receives a 200 response (success), which of course causes assert_redirected_to to fail. Here''s my before_filter method that''s being called (what''s responsible for said redirect): class ApplicationController < ActionController::Base # ...snip ... def login_required if session[:username] # Has this user set his/her home location yet? begin @user = User.find(:first, :conditions => {:account_username => session[:username]}) rescue ActiveRecord::RecordNotFound => e logger.warn "Couldn''t find user with account_username #{session [:username]} - AR threw #{e}" reset_session redirect_to :controller => "authenticate", :action => "index" return false end if !@user.has_home_location? # Do the redirect redirect_to :controller => "users", :action => "assign_location" end return true end flash[:warning] = "Please login to continue." session[:return_to] = request.request_uri redirect_to :controller => "authenticate" return false end I have an integration test that just tries to do a straight get request on a particular URL. For example: get "/users/" assert_response :redirect assert_redirected_to "/users/assign_location" In this test, the session is totally blank. It doesn''t exist yet. So, by default, the user should be redirected to /users/ assign_location. However, my tests report that the server responds with code 200: 1) Failure: test_user_assings_own_location(UserFlowsTest) [/test/integration/ user_flows_test.rb:24]: Expected response to be a <:redirect>, but was <200> Any thoughts as to what I''m doing wrong here?
Phoenix Rising
2009-Jul-31 21:30 UTC
Re: Integration test receives HTTP 200 response when it should be a redirect
And another thing I forgot to mention: I fired up script/server to see if it was ACTUALLY redirecting, and it sure is. Using Firebug (Firefox extension) under Firefox 3.5, I can see: Location http://localhost:3000/users/assign_location So it''s definitely throwing a redirect, but I have no clue why the tests won''t "see it" as one. On Jul 31, 3:08 pm, Phoenix Rising <polarisris...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is probably due to my novice understanding of integration tests, > but this behavior looked pretty strange, so I figured I''d ask. > > The application I''m working on at present requires users, upon login, > to assign themselves to a location. Locations are pre-loaded in the > database. Because I don''t want the user "getting around" this > functionality by ignoring it and just clicking something else (this is > a workplace-only application), I''m using this as part of my > authentication process. > > Long story short, every controller has a before_filter called > "login_required". The login_required method exists in > ApplicationController. It''s very simple: checks to see if the user''s > session contains a "username" value. If not, they''re kicked back to > the authenticate controller and told to login. > > If they DO have this in their session, however, is where the fun > starts. At that point, it looks for the user in the database > (original authentication is done by Active Directory, and I have a > local database that "syncs" with AD nightly). If found, it then > checks to see if the user''s home location (called a "store" in this > case) is set. If NOT, it redirects them to /users/assign_location. > > And there''s the rub: it''s an actual call to redirect_to. Yet my > integration test receives a 200 response (success), which of course > causes assert_redirected_to to fail. > > Here''s my before_filter method that''s being called (what''s responsible > for said redirect): > > class ApplicationController < ActionController::Base > # ...snip ... > > def login_required > if session[:username] > # Has this user set his/her home location yet? > begin > @user = User.find(:first, :conditions => {:account_username => > session[:username]}) > rescue ActiveRecord::RecordNotFound => e > logger.warn "Couldn''t find user with account_username #{session > [:username]} - AR threw #{e}" > reset_session > redirect_to :controller => "authenticate", :action => "index" > return false > end > > if !...@user.has_home_location? > # Do the redirect > redirect_to :controller => "users", :action => > "assign_location" > end > > return true > end > > flash[:warning] = "Please login to continue." > session[:return_to] = request.request_uri > redirect_to :controller => "authenticate" > return false > end > > I have an integration test that just tries to do a straight get > request on a particular URL. For example: > > get "/users/" > assert_response :redirect > assert_redirected_to "/users/assign_location" > > In this test, the session is totally blank. It doesn''t exist yet. > So, by default, the user should be redirected to /users/ > assign_location. However, my tests report that the server responds > with code 200: > > 1) Failure: > test_user_assings_own_location(UserFlowsTest) [/test/integration/ > user_flows_test.rb:24]: > Expected response to be a <:redirect>, but was <200> > > Any thoughts as to what I''m doing wrong here?