# Has\_session\_object

**URL:** <https://www.ruby-forum.com/t/has-session-object/111865>\
**Category:** Rails\
**Created:** [August 12, 2007, 4:12pm UTC](https://www.ruby-forum.com/t/has-session-object/111865 "2007-08-12T16:12:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [August 12, 2007, 4:12pm UTC](https://www.ruby-forum.com/t/has-session-object/111865/1 "2007-08-12T16:12:49Z")

</div>

Hi,

No matter what I do, has\_session\_object always return false…  
The site itself has no problem logging in users.  
Can someone help please ?

in my test unit:

class AccountControllerTest \< Test::Unit::TestCase

fixtures :users

def setup  
@controller = AccountController.new  
@request = ActionController::TestRequest.new  
@response = ActionController::TestResponse.new  
@request.host = “127.0.0.1”  
end

def test\_auth\_bob  
#check we can login  
login\_as :bob #post :login, :user=\> { :login =\> “bob”, :password  
=\> “test” }  
assert @response.has\_session\_object?(:user)  
assert\_equal @bob, session[:user]  
assert\_response :redirect  
assert\_redirected\_to :controller=\>‘welcome’, :action=\>‘index’  
end

end

my fixtures:

bob:  
id: 1  
name: Bob  
login: bob  
email: removed\_email\_address@domain.invalid  
password: \<%=Digest::SHA1.hexdigest(“laphrasedelamorttestquitue”)%\>

# ‘test’ is inside a long sentence

My controller:

class AccountController \< ApplicationController  
layout ‘login’

def index  
redirect\_to :action =\> :login  
end

def login  
puts “Login”  
case request.method  
when :post  
if session[‘user’] = User.authenticate(params[‘user’]  
[‘login’], params[‘user’][‘password’])  
puts “OK”  
AppMailer.deliver\_alert(“Login Alert”,"#{session[‘user’]  
[‘name’]} just logged on PTM")  
flash[‘notice’] = “Login successful”  
redirect\_back\_or\_default :controller =\> “welcome”  
else  
puts “Error”  
@login = params[‘user\_login’]  
@message = “Login unsuccessful”  
end  
end  
end

[…]

end

My model:

require ‘digest/sha1’

# this model expects a certain database layout and its based on the

name/login pattern.  
class User \< ActiveRecord::Base  
has\_many :tasks  
has\_many :notes  
has\_and\_belongs\_to\_many :projects

def self.authenticate(login, pass)  
puts login # gets printed out  
puts pass # gets printed out  
find(:first, :conditions=\>[“login=? AND password=?”, login,  
sha1(pass)])  
end

protected

def self.sha1(pass)  
Digest::SHA1.hexdigest(“laphrasedelamort#{pass}quitue”)  
end

[…]

end

OK gets printed out. (see controller)  
The site works.  
The unit test does not…

Thanks for any help,  
Mickael.

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [August 17, 2007, 9:46pm UTC](https://www.ruby-forum.com/t/has-session-object/111865/2 "2007-08-17T21:46:37Z")

</div>

I am still puzzled by this error.  
I can not run my tests, snif ☹

Could someone help ? 🙂

Mickael.
