# Integration test receives HTTP 200 response when it should be a 	redirect

**URL:** <https://discuss.rubyonrails.org/t/integration-test-receives-http-200-response-when-it-should-be-a-redirect/44742>\
**Category:** rubyonrails-talk\
**Created:** [July 31, 2009, 9:08pm UTC](https://discuss.rubyonrails.org/t/integration-test-receives-http-200-response-when-it-should-be-a-redirect/44742 "2009-07-31T21:08:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phoenix\_Rising](https://avatars.discourse-cdn.com/v4/letter/p/e68b1a/32.png) [@Phoenix\_Rising](https://discuss.rubyonrails.org/u/Phoenix_Rising)\
**Post date:** [July 31, 2009, 9:08pm UTC](https://discuss.rubyonrails.org/t/integration-test-receives-http-200-response-when-it-should-be-a-redirect/44742/1 "2009-07-31T21:08:27Z")

</div>

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 &nbsp;&nbsp;# ...snip ...

&nbsp;&nbsp;def login\_required &nbsp;&nbsp;&nbsp;&nbsp;if session[:username] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Has this user set his/her home location yet? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user = User.find(:first, :conditions =\> {:account\_username =\> session[:username]}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue ActiveRecord::RecordNotFound =\> e &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.warn "Couldn't find user with account\_username #{session [:username]} - AR threw #{e}" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reset\_session &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :controller =\> "authenticate", :action =\> "index" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if !@user.has\_home\_location? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Do the redirect &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :controller =\> "users", :action =\> "assign\_location" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true &nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;flash[:warning] = "Please login to continue." &nbsp;&nbsp;&nbsp;&nbsp;session[:return\_to] = request.request\_uri &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :controller =\> "authenticate" &nbsp;&nbsp;&nbsp;&nbsp;return false &nbsp;&nbsp;end

I have an integration test that just tries to do a straight get request on a particular URL. For example:

&nbsp;&nbsp;&nbsp;&nbsp;get "/users/" &nbsp;&nbsp;&nbsp;&nbsp;assert\_response :redirect &nbsp;&nbsp;&nbsp;&nbsp;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:

&nbsp;&nbsp;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?

---

<div class="post-metadata">

**Author:** ![Phoenix\_Rising](https://avatars.discourse-cdn.com/v4/letter/p/e68b1a/32.png) [@Phoenix\_Rising](https://discuss.rubyonrails.org/u/Phoenix_Rising)\
**Post date:** [July 31, 2009, 9:30pm UTC](https://discuss.rubyonrails.org/t/integration-test-receives-http-200-response-when-it-should-be-a-redirect/44742/2 "2009-07-31T21:30:06Z")

</div>

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](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.
