Skip to content

Commit

Permalink
Refactor authentication tests
Browse files Browse the repository at this point in the history
  • Loading branch information
relaxdiego committed Apr 4, 2012
1 parent f901e80 commit ac90b81
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 58 deletions.
5 changes: 2 additions & 3 deletions doc/features.html

Large diffs are not rendered by default.

29 changes: 16 additions & 13 deletions features/categories/authentication/logging_in.feature
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
Feature: Logging In
This feature helps ensure that only authenticated users are able to use the application.

Background:
* The following user exists:
Scenario Outline: Someone attempts to log in
Given a user with the following credentials exists:
| email | password |
| relaxdiego@gmail.com | as4943dladdsf |


Scenario Outline: User tries to log in
When he logs in with the following credentials: <email>, <password>
When someone attempts to log in with the following credentials: <email>, <password>
Then he will be redirected to the <page> page
And the system will display '<message>'

Examples:
Examples: Valid Credentials
| email | password | page | message |
| relaxdiego@gmail.com | as4943dladdsf | home | Signed in successfully |
| RELAXDIEGO@GMAIL.COM | as4943dladdsf | home | Signed in successfully |

Examples: Invalid Credentials
| email | password | page | message |
| relaxdiego@gmail.com | wrong-password | login | Invalid email or password |
| aaaaaaaaaa@gmail.com | as4943dladdsf | login | Invalid email or password |
| | | login | Invalid email or password |
| relaxdiego@gmail.com | | login | Invalid email or password |


Scenario Outline: User attempts to access a secure page without logging in
When he attempts to access <page> without logging in first
Scenario Outline: Someone attempts to access a secure page without logging in
When someone attempts to access <page> without logging in first
Then he will be redirected to the log in page

Examples:
Expand All @@ -32,18 +33,20 @@ Feature: Logging In


Scenario: User tries to visit the login page when he's already logged in
Given he is logged in
Given a user is logged in
When he visits the log in page
Then he will be redirected to his home page


Scenario Outline: User logs in after being redirected

This outline ensures that the user doesn't have to manually
go back to the page he was trying to access before logging in.

Given he successfully logged in after being redirected from <page>
Then he will be redirected to the <page> page
Given a user exists
When he attempts to access <page> without logging in first
Then he will be redirected to the log in page
When he logs in
Then he will be redirected to the <page> page

Examples:
| page |
Expand Down
8 changes: 1 addition & 7 deletions features/categories/authentication/logging_out.feature
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
Feature: Logging Out

Background:
* The following user exists:
| email | password |
| mmaglana@gmail.com | as4943dladdsf |


Scenario: User tries to log out
Given he is logged in
Given a user is logged in
When he tries to log out
Then he should be logged out
And he should see the log in page
Expand Down
32 changes: 19 additions & 13 deletions features/step_definitions/authentication_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@
# GIVENs
#==========================

Given /^(?:[Hh]e|[Ss]he) is logged in$/ do
login_with :email => @user.email, :password => @user.password
Given /^[Aa] user is logged in$/ do
@user = create_user
login @user
end

Given /^[Aa] user named (.+) is logged in$/ do |name|
steps %{
Given the following user exists:
| first_name | email | password |
| #{name} | relaxdiego@gmail.com | as4943dladdsf |
And she is logged in
}
@user = create_user(:first_name => name)
login @user
end

Given /^(?:[Hh]e|[Ss]he) successfully logged in after being redirected from (.+)$/ do |page|
visit eval("#{page}_path")
login_with :email => @user.email, :password => @user.password
login @user
end

#==========================
# WHENs
#==========================

When /^(?:he|she) logs in with the following credentials: (.*), (.*)$/ do |email, password|
login_with :email => email, :password => password
When /^someone attempts to log in with the following credentials: (.*), (.*)$/ do |email, password|
visit new_user_session_path
fill_in 'user_email', :with => email
fill_in 'user_password', :with => password
click_button 'login'
end

When /^(?:he|she) attempts to access (.+) without logging in first$/ do |page|
When /^(?:he|she|someone) attempts to access (.+) without logging in first$/ do |page|
visit eval("#{page}_path")
end

Expand All @@ -40,6 +40,12 @@
click_on 'logout'
end

When /^(?:he|she) logs in$/ do
fill_in 'user_email', :with => @user.email
fill_in 'user_password', :with => @user.password
click_button 'login'
end

#==========================
# THENs
#==========================
Expand All @@ -52,7 +58,7 @@
end

Then /^the system should display '(.+)'$/ do |message|
page.should have_content(message)
page.should have_content(message)
end

Then /^he should see the log in page$/ do
Expand Down
15 changes: 6 additions & 9 deletions features/step_definitions/user_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
# GIVENs
#==========================

Given /^[Tt]he following user exists:$/ do |credentials|
attributes = credentials.hashes[0]
@user = Factory.create(
:user,
:email => attributes['email'],
:password => attributes['password'],
:password_confirmation => attributes['password'],
:first_name => attributes['first_name'] || ''
)
Given /^[Aa] user exists$/ do
@user = create_user
end

Given /^[Aa] user with the following credentials exists:$/ do |credentials|
@user = create_user(credentials.hashes[0])
end

Given /^(?:[Hh]e|[Ss]he) has an (?:officemate|teammate) named (.+)$/ do |name|
Expand Down
21 changes: 15 additions & 6 deletions features/support/authentication_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
def login_with(credentials)
visit new_user_session_path if current_path != new_user_session_path
fill_in 'user_email', :with => credentials[:email]
fill_in 'user_password', :with => credentials[:password]
click_button 'login'
end
module EmailAndPasswordAuthenticationHelpers
def login(user)
credentials = { :email => user.email, :password => user.password }
login_with_credentials credentials
end

def login_with_credentials(credentials)
visit new_user_session_path if current_path != new_user_session_path
fill_in 'user_email', :with => credentials[:email]
fill_in 'user_password', :with => credentials[:password]
click_button 'login'
end
end

World(EmailAndPasswordAuthenticationHelpers)
26 changes: 19 additions & 7 deletions features/support/user_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
def get_user(name)
first_name = name.split(' ')[0]
return @user if @user.first_name == first_name
user = User.find_by_first_name(first_name)
raise "Couldn't find a user with first name #{first_name}" if user.nil?
user
end
module UserHelpers
def create_user(attributes = {})
# Make sure all keys are symbols
attributes.keys.each { |key| attributes[key.to_sym] = attributes.delete(key) }

attributes[:password_confirmation] ||= attributes[:password]
Factory.create(:user, attributes)
end

def get_user(name)
first_name = name.split(' ')[0]
return @user if @user.first_name == first_name
user = User.find_by_first_name(first_name)
raise "Couldn't find a user with first name #{first_name}" if user.nil?
user
end
end

World(UserHelpers)

0 comments on commit ac90b81

Please sign in to comment.