Created
January 23, 2009 19:12
-
-
Save jeffrafter/51141 to your computer and use it in GitHub Desktop.
Using cucumber, shoulda and factory_girl to check logging in and signing up against clearance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENV["RAILS_ENV"] = "test" | |
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment') | |
require 'shoulda' | |
require 'webrat' | |
require 'factory_girl' | |
require 'test/factories/clearance' | |
require 'clearance/../../shoulda_macros/clearance' | |
require 'cucumber/rails/world' | |
Cucumber::Rails.use_transactional_fixtures | |
Webrat.configure do |config| | |
config.mode = :rails | |
end | |
module CucumberWorldExtension | |
# This may be overkill, but it cleans everything up nicely. If you wanted to | |
# You could just use specific fixtures in tests. You could even stub the | |
# fixtures method in your world | |
# | |
# def fixtures(*args) | |
# Test::Unit::TestCase.fixtures args | |
# end | |
# | |
Test::Unit::TestCase.fixtures :all | |
def login_user(email, password) | |
post "/login", {:session => { :email => email, :password => password }} | |
@user = @controller.current_user | |
end | |
def logout_user | |
get "/logout" | |
@user = nil | |
end | |
# This is pretty dangerous because you really have to make sure you have a | |
# user to use this properly maintaining an instance variable across steps can | |
# sometimes lead to very brittle steps | |
def me | |
@user | |
end | |
end | |
World do |world| | |
world.extend(CucumberWorldExtension) | |
world | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: Logging in | |
As a user | |
I want to login with my details | |
So that I can configure the server | |
Scenario: User is not logged in | |
Given no current user | |
When I access a page | |
Then the login form should be shown | |
And I should not be authorized | |
Scenario: User enters wrong password | |
Given a registered user with the email "[email protected]" with password "doughnuts" exists | |
And I am on the "/login" page | |
When I fill in "email" with "[email protected]" | |
And I fill in "password" with "ticklemeelmo" | |
And I press "Sign in" | |
Then the login form should be shown | |
And I should see "Bad email or password" | |
And I should not be signed in | |
Scenario: User is registered and enters correct password | |
Given a registered user with the email "[email protected]" with password "doughnuts" exists | |
And I am on the "/login" page | |
When I fill in "email" with "[email protected]" | |
And I fill in "password" with "doughnuts" | |
And I press "Sign in" | |
Then the login form should be shown | |
And I should see "User has not confirmed email" | |
And I should not be authorized | |
And I should not be signed in | |
Scenario: User is confirmed and enters correct password | |
Given a confirmed user with the email "[email protected]" with password "doughnuts" exists | |
And I am on the "/login" page | |
When I fill in "email" with "[email protected]" | |
And I fill in "password" with "doughnuts" | |
And I press "Sign in" | |
Then I should be redirected to "/" | |
And I should see "Signed in successfully" | |
And I should be signed in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Given /^a registered user with the email "(.*)" with password "(.*)" exists$/ do |email, password| | |
@user = Factory(:registered_user, :email => email, :password => password, :password_confirmation => password) | |
end | |
Given /^a confirmed user with the email "(.*)" with password "(.*)" exists$/ do |email, password| | |
@user = Factory(:email_confirmed_user, :email => email, :password => password, :password_confirmation => password) | |
end | |
Given /^no current user$/ do | |
logout_user | |
end | |
Then /^the login form should be shown$/ do | |
assert_template "sessions/new" | |
end | |
Then /^I should\s?((?:not)?) be authorized$/ do |present| | |
assert_response present ? :unauthorized : :success | |
end | |
Then /^I should\s?((?:not)?) be signed in$/ do |present| | |
assert_equal controller.signed_in?, present.blank? | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: Signup in | |
As an unregistered user | |
I want to signup with my details | |
So that I can login | |
Scenario: Email is not unique | |
Given a registered user with the email "[email protected]" with password "doughnuts" exists | |
And I am on the "/signup" page | |
When I fill in "Email" with "[email protected]" | |
And I fill in "Password" with "ticklemeelmo" | |
And I fill in "Verify Password" with "ticklemeelmo" | |
And I press "Sign up" | |
Then the signup form should be shown again | |
And I should see "Email has already been taken" | |
And I should not be signed in | |
Scenario: Password is not valid | |
Given I am on the "/signup" page | |
When I fill in "Email" with "[email protected]" | |
And I fill in "Password" with "ticklemeelmo" | |
And I fill in "Verify Password" with "doughnuts" | |
And I press "Sign up" | |
Then the signup form should be shown again | |
And I should see "Password doesn't match confirmation" | |
And I should not be registered | |
And I should not be signed in | |
Scenario: Email is unique and password is valid | |
Given I am on the "/signup" page | |
When I fill in "Email" with "[email protected]" | |
And I fill in "Password" with "ticklemeelmo" | |
And I fill in "Verify Password" with "ticklemeelmo" | |
And I press "Sign up" | |
Then a new user with the email "[email protected]" should be created | |
And an email with the subject "account confirmation" should be sent to me | |
And I should see "You will receive an email within the next few minutes." | |
And I should not be signed in | |
Scenario: User not confirmed | |
Given a registered user with the email "[email protected]" with password "doughnuts" exists | |
When I confirm my email | |
Then I should be redirected to "/" | |
And I should see "Confirmed email and signed in" | |
And I should be signed in | |
Scenario: User already confirmed | |
Given a confirmed user with the email "[email protected]" with password "doughnuts" exists | |
When I confirm my email | |
Then I should be redirected to "/" | |
And I should see "Confirmed email and signed in" | |
And I should be signed in | |
Scenario: User forgot password | |
Given a confirmed user with the email "[email protected]" with password "doughnuts" exists | |
When I go to the forgot password page | |
And I fill in "Email" with "[email protected]" | |
And I press "Reset Password" | |
Then an email with the subject "change your password" should be sent to me | |
And I should see "Details for changing your password have been sent" | |
And I should not be signed in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When /^I confirm my email$/ do | |
get "/users/#{@user.id}/confirmation/new?salt=#{@user.salt}" | |
end | |
When /^I go to the forgot password page$/ do | |
get "/passwords/new" | |
end | |
Then /^a new user with the email "(.*)" should be created$/ do |email| | |
@user = User.find_by_email(email) | |
assert_not_nil @user | |
end | |
Then /^the signup form should be shown again$/ do | |
assert_template "users/new" | |
end | |
Then /^I should\s?((?:not)?) be registered$/ do |present| | |
assert_nil User.find_by_email("[email protected]") | |
end | |
Then /^an email with the subject "(.*)" should be sent to me$/ do |subject| | |
assert_sent_email do |email| | |
@user.blank? || email.to.include?(me.email) | |
email.subject =~ /#{subject}/i | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Commonly used webrat steps | |
# http://github.com/brynary/webrat | |
When /^I press "(.*)"$/ do |button| | |
click_button(button) | |
end | |
When /^I follow "(.*)"$/ do |link| | |
click_link(link) | |
end | |
When /^I fill in "(.*)" with "(.*)"$/ do |field, value| | |
fill_in(field, :with => value) | |
end | |
When /^I select "(.*)" from "(.*)"$/ do |value, field| | |
select(value, :from => field) | |
end | |
# Use this step in conjunction with Rail's datetime_select helper. For example: | |
# When I select "December 25, 2008 10:00" as the date and time | |
When /^I select "(.*)" as the date and time$/ do |time| | |
select_datetime(time) | |
end | |
# Use this step when using multiple datetime_select helpers on a page or | |
# you want to specify which datetime to select. Given the following view: | |
# <%= f.label :preferred %><br /> | |
# <%= f.datetime_select :preferred %> | |
# <%= f.label :alternative %><br /> | |
# <%= f.datetime_select :alternative %> | |
# The following steps would fill out the form: | |
# When I select "November 23, 2004 11:20" as the "Preferred" data and time | |
# And I select "November 25, 2004 10:30" as the "Alternative" data and time | |
When /^I select "(.*)" as the "(.*)" date and time$/ do |datetime, datetime_label| | |
select_datetime(datetime, :from => datetime_label) | |
end | |
# Use this step in conjuction with Rail's time_select helper. For example: | |
# When I select "2:20PM" as the time | |
# Note: Rail's default time helper provides 24-hour time-- not 12 hour time. Webrat | |
# will convert the 2:20PM to 14:20 and then select it. | |
When /^I select "(.*)" as the time$/ do |time| | |
select_time(time) | |
end | |
# Use this step when using multiple time_select helpers on a page or you want to | |
# specify the name of the time on the form. For example: | |
# When I select "7:30AM" as the "Gym" time | |
When /^I select "(.*)" as the "(.*)" time$/ do |time, time_label| | |
select_time(time, :from => time_label) | |
end | |
# Use this step in conjuction with Rail's date_select helper. For example: | |
# When I select "February 20, 1981" as the date | |
When /^I select "(.*)" as the date$/ do |date| | |
select_date(date) | |
end | |
# Use this step when using multiple date_select helpers on one page or | |
# you want to specify the name of the date on the form. For example: | |
# When I select "April 26, 1982" as the "Date of Birth" date | |
When /^I select "(.*)" as the "(.*)" date$/ do |date, date_label| | |
select_date(date, :from => date_label) | |
end | |
When /^I check "(.*)"$/ do |field| | |
check(field) | |
end | |
When /^I uncheck "(.*)"$/ do |field| | |
uncheck(field) | |
end | |
When /^I choose "(.*)"$/ do |field| | |
choose(field) | |
end | |
When /^I attach the file at "(.*)" to "(.*)" $/ do |path, field| | |
attach_file(field, path) | |
end | |
Then /^I should see "(.*)"$/ do |text| | |
assert !!(response.body =~ /#{text}/m), response.body | |
end | |
Then /^I should not see "(.*)"$/ do |text| | |
assert !!(response.body !~ /#{text}/m), response.body | |
end | |
Then /^the "(.*)" checkbox should be checked$/ do |label| | |
assert field_labeled(label).checked? | |
end | |
Then /^I should be redirected to "(.*)"$/ do |path| | |
assert_template path.gsub(/^\//, '') | |
follow_redirect! if response.redirect? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment