Jumping on the Rails Pt.3 – Getting Authenticated

July 3, 2009

Carrying on from my last post, I’ll introduce Rest Authentication to the Story Board project. The plugin comes with its own stories & definitions, so it will be a nice learning source for us whilst learning Cucumber & RSpec πŸ™‚

So to start off with lets install the plugin. Now I’ve experienced some issues with Technoweenie‘s branch of the code, so I ended up doing some digging and finding a fork by Ombwa, we’ll install this to get our authentication done quickly.

 script/plugin install git://github.com/ombwa/restful-authentication.git

As per the docs you’ll need to do the following if your using Rails 2.1+:

cd vendor/plugins &&
mv restful-authentication restful_authentication &&
cd ../../

Now we need the act as state machine plugin.

script/plugin install http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk

Next we need to create our user & sesion models

./script/generate authenticated user sessions \
			--include-activation \
			--stateful \
			--rspec &&
rake db:migrate RAILS_ENV=test &&
rake db:fixtures:load RAILS_ENV=test

Now some of the plugins tests fail, so we need to get those passing before we can tick this off the to do list.

Now we need to map our session controller, to do this we need to add the code below to route.rb

 map.session '/session', :controller => 'sessions', :action => 'create'

If we’re using activation we’ll need to add the following to the environment.rb file.

config.active_record.observers = :user_observer

If we have activation set we’ll need to also do the following:

map.activate '/activate/:activation_code', 
             :controller => 'users', 
             :action => 'activate', 
             :activation_code => nil

And finally we added the following to the ApplicationController

include AuthenticatedSystem

Now there are some tests that may fail, namely the routing related user controller specs, you may see something similar to:

 undefined method `formatted_users_path' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_2::Subclass_3:0x22f8698>

Simply rename this to new_users_path and do the same for the other methods with the methods with ‘formatted’ prefixed:

  • formatted_users_path
  • formatted_new_users_path

I also had issues with the following user model specs, there are not critical for myself so committed a sin and commented them out. The following where the tests that I had issues with:

  • should route users’s ‘update’ action correctly
  • should route users’s ‘delete’ action correctly

Next we’ll get a run of errors from out view specs. We’ll quickly create an application layout that will help us pass the rest of the restful-authentication’s plugin tests. Most of these are asserting that we have a div.notice & div.error element, the below fixed those tests for me.

		<title>Story Board</title>
		<div id="notice" class="notice"><%= flash[:notice] %></div>
		<div id="error" class="error"><%= flash[:error] %></div>
		<%= yield :layout %>

One final thing, I came across authentication tests that fail, this is usually due to the records not being reloaded after a creation. Adding the following code to both after_create & after_save calls within the UserObserver class, so that our tokens are correct.


Now that all our tests are passing, we’re ready to start delving into the main requirements of the application. The next feature is to allow registered users to create a new project. So well create a new file called registered_users.feature and add the following content.

Feature: As a registered user I must be able to create projects
  In order to create a new project I must be logged in
  As a registered user
  I want I want to be able to create new projects

	Scenario: A registered user must be able to create projects
	  Given I am logged in
	  When I click new project
		And fill in the new project form
	  Then I should get confirmation of the successful creation

We’ll focus on this functionality in part 4 of ‘Jumping on the Rails‘. In the meantime I recommend having a look at the stories and definitions that come packed with Restful Authentication, I’ve found very useful in helping me to improve my own stories and definitions πŸ˜‰


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: