Cucumber features devise authentication - moralesalberto/personal GitHub Wiki

Login Feature

Feature: A user needs to log-in before using the app
  Scenario: A user that has not logged in goes to the root page of the application
    Given a user is not logged in
    When the user goes to the root page of the application
    Then the user should be redirected to the login page

  Scenario: A logged in user goes to the root page of the application
    Given a user is not logged in
    And is a valid user with email "[email protected]" and password "working123"
    When the user goes to the root page of the application
    Then the user should be redirected to the login page
    When the user enters his correct credentials in the login page, email: "[email protected]" and password "working123"
    And submits the login page
    Then the user should be shown the root page

  Scenario: A user attempts to login with the incorrect password
    Given a user is not logged in
    And is a valid user with email "[email protected]" and password "working123"
    When the user goes to the root page of the application
    Then the user should be redirected to the login page
    When the user enters the incorrect password in the login page, email: "[email protected]" and password "working123"
    And submits the login page
    Then the user should be redirected to the login page

  Scenario: A user attempts to login with the incorrect username
    Given a user is not logged in
    And is a valid user with email "[email protected]" and password "working123"
    When the user goes to the root page of the application
    Then the user should be redirected to the login page
    When the user enters the incorrect email in the login page, email: "[email protected]" and password "working123"
    And submits the login page
    Then the user should be redirected to the login page

Login Feature - Step Definitions


Given(/^a user is not logged in$/) do
  #nothing to do
end

When(/^the user goes to the root page of the application$/) do
  visit(root_path)
end

Then(/^the user should be redirected to the login page$/) do
  page.current_path.should eq('/users/sign_in')
end

And(/^is a valid user with email "(.*?)" and password "(.*?)"$/) do |email, password|
  user = FactoryGirl.create(:user, :email => email, :password => password)
end

When(/^the user enters his correct credentials in the login page, email: "(.*?)" and password "(.*?)"$/) do |email, password|
  fill_in('user_email', :with => email)
  fill_in('user_password', :with => password)
end

When(/^the user enters the incorrect password in the login page, email: "(.*?)" and password "(.*?)"$/) do |email, password|
  fill_in('user_email', :with => '[email protected]')
  fill_in('user_password', :with => "#{password}1234" )
end

When(/^the user enters the incorrect email in the login page, email: "(.*?)" and password "(.*?)"$/) do |email, password|
  fill_in('user_email', :with => "wrong#{email}")
  fill_in('user_password', :with => password)
end

And(/^submits the login page$/) do
  click_on('Sign in')
end

Then(/^the user should be shown the root page$/) do
  page.current_path.should eq(root_path)
end

Logout Feature

Feature: A user logs out of the application
  Scenario: A logged in user logs out of the application
    Given a user is not logged in
    And is a valid user with email "[email protected]" and password "working123"
    When the user goes to the root page of the application
    Then the user should be redirected to the login page
    When the user enters his correct credentials in the login page, email: "[email protected]" and password "working123"
    And submits the login page
    Then the user should be shown the root page
    When the user clicks on the logout link
    Then the user should be redirected the login page

  Scenario: A user logs out and then tries to go back to the application
    Given a user is not logged in
    And is a valid user with email "[email protected]" and password "working123"
    When the user goes to the root page of the application
    Then the user should be redirected to the login page
    When the user enters his correct credentials in the login page, email: "[email protected]" and password "working123"    
    And submits the login page
    Then the user should be shown the root page
    When the user clicks on the logout link
    Then the user should be redirected the login page
    And if the user attempts to go to the root page again
    Then the user should be redirected to the login page

Logout Step Definitions

When(/^the user clicks on the logout link$/) do
  click_on('Logout')
end

Then(/^the user should be redirected the login page$/) do
  page.current_path.should eq('/users/sign_in')
end

Then(/^if the user attempts to go to the root page again$/) do
  visit(root_path)
end

Forgot Password Feature

Feature: The app should allow the user to reset their password if they forgot it
  Scenario: A user forgets his password has a way to get a reset link
    Given a user that forgot his password
    And is a valid user with email "[email protected]" and password "working123"
    When the user goes to the forgot password page
    And the user fills in his email: "[email protected]" in the forgot password form
    Then the user should receive an email with a reset link
  
  Scenario: A user is able to reset their password with a reset link
    Given a user that forgot his password
    And is a valid user with email "[email protected]" and password "working123"
    When the user goes to the forgot password page
    And the user fills in his email: "[email protected]" in the forgot password form
    Then the user should receive an email with a reset link
    When the user goes to the reset link from the email
    Then user should be presented with a reset your password form
    When the user fills in the new password: "somenewpassword123!" and submits the forgot password form
    Then the user should be shown the root page

Forgot Password Step Definitions

Given(/^a user that forgot his password$/) do
  #nothing here
end

When(/^the user goes to the forgot password page$/) do
  visit(new_user_password_path)
end

When(/^the user fills in his email: "(.*?)" in the forgot password form$/) do |email|
  fill_in('user_email', :with => email)
  click_on('Send me reset password instructions')
end

Then(/^the user should receive an email with a reset link$/) do
  user = User.first
  @matching_emails = ActionMailer::Base.deliveries.select {|email| email.to.first == user.email and email.subject =~ /Reset password instructions/}
  @matching_emails.size.should eq(1)
end


When(/^the user goes to the reset link from the email$/) do
  html = Nokogiri::HTML(@matching_emails.first.body.to_s)
  link = html.css('a').select {|link| link['href'] =~ /reset_password/}.first
  visit(link['href'])
end

Then(/^user should be presented with a reset your password form$/) do
  page.html.should have_selector(:xpath, '//form[@action="/users/password"]')
  page.html.should have_selector(:xpath, '//form[@method="post"]')
end


When(/^the user fills in the new password: "(.*?)" and submits the forgot password form$/) do |new_password|
  fill_in('user_password_confirmation', :with => new_password)
  fill_in('user_password', :with => new_password)
  click_on('Change my password')
end

Change password feature

Feature: A user should be able to change his password
  Background: A user logs into the application
    Given a user is not logged in
    And is a valid user with email "[email protected]" and password "working123"
    When the user goes to the root page of the application
    Then the user should be redirected to the login page
    When the user enters his correct credentials in the login page, email: "[email protected]" and password "working123"    
    And submits the login page
    Then the user should be shown the root page

  Scenario: A user that is logged in should be able to change their password
    When the user goes to the change password page
    And fills in current password: "working123", new password: "newpasswordHere!", and new password confirmation "newpasswordHere!"
    And submits the change password form
    Then the user should be redirected to the login page
    When the user enters his correct credentials in the login page, email: "[email protected]" and password "newpasswordHere!"
    And submits the login page
    Then the user should be shown the root page

  Scenario: A user that that attempts to change their password should get an error if they enter the wrong current password
    When the user goes to the change password page
    And fills in current password: "thispassword!", new password: "newpasswordHere!", and new password confirmation "newpasswordHere!"
    And submits the change password form
    Then the user should be shown the error "Current password is invalid"
    And sent back to the change password form

Change password step definitions

When(/^the user goes to the change password page$/) do
  visit(edit_user_path)
end

When(/^fills in current password: "(.*?)", new password: "(.*?)", and new password confirmation "(.*?)"$/)  do |current_password, new_password, new_password_confirmation|
  fill_in('user_current_password', :with => current_password)
  fill_in('user_password', :with => new_password)
  fill_in('user_password_confirmation', :with => new_password_confirmation)
end

When(/^submits the change password form$/) do
  click_on('Change Password')
end

Then(/^the user should be shown the error "(.*?)"$/) do |error_message|
  page.should have_content(error_message)
end

Then(/^sent back to the change password form$/) do
  page.html.should have_selector(:xpath, '//form[@action="/user/update_password"]')
  page.html.should have_selector(:xpath, '//form[@method="post"]')
end