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

**URL:** <https://www.ruby-forum.com/t/integration-test-receives-http-200-response-when-it-should-be-a-redirect/173046>\
**Category:** Rails\
**Created:** [July 31, 2009, 11:09pm UTC](https://www.ruby-forum.com/t/integration-test-receives-http-200-response-when-it-should-be-a-redirect/173046 "2009-07-31T23:09:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phoenix\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@Phoenix\_R](https://www.ruby-forum.com/u/Phoenix_R)\
**Post date:** [July 31, 2009, 11:09pm UTC](https://www.ruby-forum.com/t/integration-test-receives-http-200-response-when-it-should-be-a-redirect/173046/1 "2009-07-31T23:09:49Z")

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

# …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?

---

<div class="post-metadata">

**Author:** ![Phoenix\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5e9695/32.png) [@Phoenix\_R](https://www.ruby-forum.com/u/Phoenix_R)\
**Post date:** [July 31, 2009, 11:30pm UTC](https://www.ruby-forum.com/t/integration-test-receives-http-200-response-when-it-should-be-a-redirect/173046/2 "2009-07-31T23:30:54Z")

</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.
