How do i write functional tests which include the concept of a user?
Most web applications today have a user and authentication system. Rails has many login / authentication systems including: http://www.agilewebdevelopment.com/plugins/user_engine - User Engine , http://www.agilewebdevelopment.com/plugins/login_engine - Login Engine , http://www.agilewebdevelopment.com/plugins/simple_http_auth - Simple - HTTP Auth , http://www.agilewebdevelopment.com/plugins/authorization - Authorization , http://www.agilewebdevelopment.com/plugins/authenticates_via_typekey - Authenticates Via Typekey , http://www.agilewebdevelopment.com/plugins/activerbac - ActiveRBAC , http://www.agilewebdevelopment.com/plugins/acl_system - - ACL System , and http://www.agilewebdevelopment.com/plugins/htpasswd - htpasswd . My favorite is http://weblog.techno-weenie.net/ - Rick Olson’s http://technoweenie.stikipad.com/plugins/show/Acts+as+Authenticated - acts_as_authenticated . Unfortunately, the existing http://manuals.rubyonrails.org/read/book/5 - testing rails documentation doesn’t cover how to test users for any of the systems.
In this how to, i’m assuming that you are going to be using
acts_as_authenticated. The concepts and most of the code are applicable
to the other login/authentication/user systems.
What is a logged in user?
If you’re using plugin like acts_as_authenticated, you might not be
sure how rails tracks user information between requests. Rails
generates a cookie with a unique id for the session. Each user when
they view the site get a sessid cookie. That tracks their use through
out the site. On the server site there is a shared collection of these
session id’s and a ruby object which maintains the actual session
information. Rails supports storing the session information on the file
system as temp files, via active record in the database, in a memcache
memory only database, or via a distributed ruby DRB simple object store.
The session object is loaded up and available in your environment. You can set new data on it, just by doing session[:favorite_color] = 'light blue' . The next request by that user and you can check the value of session[:favorite_color] .
You can put full model objects in to a user’s session hash, but it’s
not a good idea. Most session stores start choking if you give them to
much data.
The acts_as_authenticated plugin stores just the user id in session
hash. A user is considered logged in if the session[:user] has a valid
user id in it. To set a user, we simply need to set the value of session[:user] to be the user id we want.
|