Speed up your features with a backdoor login route

In any application that tracks users, I’m guessing that the vast majority of features will require that a user is logged in. This however can add a huge amount of overhead to your features at run time.

To speed things up, you can create a backdoor route to allow your features to bypass the login page and form submission, and simply hit a url to create the session.

First, define the route scoped by a condition – you don’t want this in production mode!

if %w( development test cucumber ).include?(Rails.env.to_s)
  map.backdoor '/backdoor', :controller => :user_sessions, :action => :backdoor

Then define the controller action. Bear in mind we are using authlogic. Adjust this to suite your own requirements.

# for cucumber testing only
def backdoor
  @user_session = UserSession.new(User.find_by_email(params[:email]))
  head 'ok'

Finally, your cucumber step goes from something like this…

Given /^I authenticate successfully$/ do
  Given 'I am on the login page'
  And 'I fill in "Email" with "eddie@hotmail.com"'
  And 'I fill in "Password" with "secret"'
  When 'I press "Login"'
  Then 'I should see "Welcome, Service Account (AD)"'

…to something like this…

Given /^I am already logged in as a user$/ do
  visit "/backdoor?email=eddie@hotmail.com"

We shaved nearly 2 minutes off our 12-minute build!

Thanks to this post for the original idea.


One thought on “Speed up your features with a backdoor login route

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