Mar 31, 2009

Testing Rails Authlogic with Cucumber

In early rails projects I rolled my own authentication system, later I used restful-authentication. This time I'm going to try out AuthLogic which seems to be fairly popular, cleanly packaged and actively developed. I may walk through an AuthLogic installation in a future post but today I just want to make sure it can play nicely with cucumber.

First I wrote a fairly basic login scenario. You'll notice I stuck with the declarative style in writing this and I followed the advice provided in "The RSpec Book". I use direct model access to create the registered user in the 'Given' but then use simulated browser access to do the login and verify the actions success.

Scenario: successful login
Given I am the registered user John Doe
And I am on the login page
When I login with valid credentials
Then I should be on the account page
And I should see "Login successful!"


This left me with three steps to implement:

Given /^I am the registered user (.+)$/ do |login|
params = {
"login"=> login,
"password"=>"password",
"password_confirmation"=>"password"
}
@user = User.create(params)
end

When /^I login with valid credentials$/ do
fill_in('Login', :with => @user.login)
fill_in('Password', :with => "password")
click_button("Login")
end

Then /^I should be on ([^\"]*)$/ do |page_name|
response.request.path.should == path_to(page_name)
end

All fairly straight forward. I don't expect any problems using Cucumber with Authlogic.

10 comments:

Jesse said...

this really helps me out.. however, why aren't the other steps listed? i had to make a "visit path_to /the login page/" step. ?

bantic said...

The visit* steps come from webrat, you'll want to install the webrat steps along w/ cucumber. I believe the cucumber generator creates these.

Anonymous said...

Well, considering how many people (including me) who are having problems integrating authlogic with cucumber testing, I'm curious to hear whether your expectations were correct..?

Unknown said...

This attempt is almost correct (at least in the current version of authlogic, v2.1). Instead of calling User.create you'll want to call User.new and then save it using @user.save_without_session_maintenance.

Otherwise authlogic will blow up trying to look for non-existent http request data in a non-existent controller. However nice authlogic may be, it blows Rails's MVC separation all to hell.

Unknown said...

I am working on a scenario in which i am using DMA to put data in database model Agent.
I am writing this code at the start of the scenario.
Below is the code:

@person=Agent.new(:about_me=>"i am cool ,funny,passionate,sporty guy",
:first_name=>"abhay",
:last_name=>"kumar",
:initials=>"AK",
:dob=>"2009-07-07",
:gender=>"Male",
:education=>"Bachelor Degree",
:employment_preference=>"Permanent",
:current_status=>"Working",
:address=>"Vidydhar heights,Laxmiroad",
:city=>"Pune",
:state=>"Alaska",
:country=>"India",
:zipcode =>"412101",
:work_phone=>"+919767389326",
:primary_industry=>"Information Technology",
:secondary_industry=>"Information Technology",
:major=>"",
:mobile_phone=>"",
:home_phone=>"",
:fax_number=>""

)
@person.save!

Interestingly data is going to mysql database after the scenario is complete.
I need that data in between scenario but not able to access it as it's not in database.
But After the scenario is complete above row is present in database.

Is there any problem with configurations of database transactions ?

Unknown said...

Abhay: The short answer is yes. It's because the tests are run in separate transactions and setting "config.use_transactional_fixutres = true" in your spec_helper.rb file should have the effect you're asking about.

The longer answer is that you most likely don't want to do that. I'd suggest creating a new model for each scenario and setting only those attributes you intend to test.

Here's an example of something I've done....

Scenario: do something interesting
  Given I am the registered user "name"
  And I my "attribute" is "value"

The step definition for the first line creates a valid user model with default attributes. Then I call the second line as many times as needed to modify only those attributes I intend to test in that specific scenario.

example....

Scenario: successful login
  Given I am the registered user "John Doe"
  And I my "password" is "password123"
  And I my "email" is "jdoe@example.com"
  And I my "role" is "admin"

abhay said...

>The short answer is yes. It's because >the tests are run in separate >transactions and setting >"config.use_transactional_fixutres = >true" in your spec_helper.rb file >should have the effect you're asking about.

Mike:I tried "config.use_transactional_fixutres = false"
but it didn't work.

In the features/support/env.rb file
I also did
Cucumber::Rails::World.use_transactional_fixtures = false ,
but it also didn't work.

>The longer answer is that you most >likely don't want to do that. I'd >suggest creating a new model for each >scenario and setting only those >attributes you intend to test.

I am not testing model or it's attributes.I am using DMA + webrat + selenium in my scenario.

It's a long scenario and I have to test the behaviour of application.At the start of scenario I am using DMA to create account in database.I don't want to create account in database using browser by filling form as it wastes a lot of time when the scenario is being run.

But the problem is account is being created after scenario is complete not at the start of scenario in the Given step.

For e.g.
Given a exists
When I search a
Then I should see a

I am using DMA in Given step and verifying a in Then step.But it's not showing a in Then step.When I am login to mysql database using console, a is present in database. I am sure it's after completing of scenario as i am checking database in between when the scenario is being run.

Any ideas will be appreciated....

Rha7 said...

Bottom line is as long as Authlogic doesn't come with a usable cucumber/rspec test helper it's not worth the hassle.

Given I am logged in <- should be enough.

Any other solutions should do to read the DRY definition. Having to know the internals of Authlogic to make it work with my test framework totally breaks the philosophy.

Anonymous said...

Have you tried to test a failure scenario like signing-up with badly formatted email? The error message shows up testing directly in the browser, but doesn't seem to be generated through cucumber...so the scenario fails. any idea?

Unknown said...

Here's a scenario from one of my current projects. It should work to detect failed logins. Not sure how to help.

http://gist.github.com/507336