From b184415c19139ad8b886ac4875fcdc537ee7e78b Mon Sep 17 00:00:00 2001
From: Florian Staudacher <florian_staudacher@yahoo.de>
Date: Fri, 15 Jun 2012 19:30:08 +0200
Subject: [PATCH] cleanup session cukes

---
 features/closes_account.feature            |  3 +-
 features/step_definitions/debug_steps.rb   |  2 +-
 features/step_definitions/session_steps.rb | 32 ++++++++++------------
 features/support/user_cuke_helpers.rb      | 30 ++++++++++++++++++++
 4 files changed, 46 insertions(+), 21 deletions(-)

diff --git a/features/closes_account.feature b/features/closes_account.feature
index 647edf2159..60a01e03ce 100644
--- a/features/closes_account.feature
+++ b/features/closes_account.feature
@@ -15,8 +15,7 @@ Feature: Close Account
 
     When I try to sign in manually
     Then I should be on the new user session page
-    When I wait for the ajax to finish
-    Then I should see "Invalid email or password"
+    And I should see a flash message containing "Invalid email or password"
 
   Scenario: post display should not throw error when mention is removed for the user whose account is closed
     Given a user named "Bob Jones" with email "bob@bob.bob"
diff --git a/features/step_definitions/debug_steps.rb b/features/step_definitions/debug_steps.rb
index 4fcc79a171..2d5e17d433 100644
--- a/features/step_definitions/debug_steps.rb
+++ b/features/step_definitions/debug_steps.rb
@@ -6,7 +6,7 @@ end
 
 When /^I wait for (\d+) seconds?$/ do |seconds|
   sleep seconds.to_i
-  warn "DELETEME - this step is for debugging only"
+  warn "DELETEME - this step is for debugging only\n"
 end
 
 When /^I open the error console$/ do
diff --git a/features/step_definitions/session_steps.rb b/features/step_definitions/session_steps.rb
index 81ee127550..3ed1b5a525 100644
--- a/features/step_definitions/session_steps.rb
+++ b/features/step_definitions/session_steps.rb
@@ -1,38 +1,35 @@
+
 Given /^(?:I am signed in|I sign in)$/ do
-  step %(I try to sign in)
-  wait_until { page.has_content?("#{@me.first_name} #{@me.last_name}") }
+  automatic_login
+  confirm_login
 end
 
 When /^I try to sign in$/ do
-  @me ||= Factory(:user_with_aspect, :getting_started => false)
-  page.driver.visit(new_integration_sessions_path(:user_id => @me.id))
-  click_button "Login"
-  # To save time as compared to:
-  #step %(I go to the new user session page)
-  #step %(I fill in "Username" with "#{@me.username}")
-  #step %(I fill in "Password" with "#{@me.password}")
-  #step %(I press "Sign in")
+  automatic_login
 end
 
 When /^I try to sign in manually$/ do
-  visit login_page
-  login_as @me.username, @me.password
+  manual_login
 end
 
 When /^I (?:sign|log) in manually as "([^"]*)" with password "([^"]*)"$/ do |username, password|
-  visit login_page
-  login_as username, password
+  @me = User.find_by_username(username)
+  @me.password ||= password
+  manual_login
+  confirm_login
 end
 
 When /^I (?:sign|log) in as "([^"]*)"$/ do |email|
   @me = User.find_by_email(email)
   @me.password ||= 'password'
-  step 'I am signed in'
+  automatic_login
+  confirm_login
 end
 
 When /^I (?:sign|log) in with password "([^"]*)"$/ do |password|
   @me.password = password
-  step 'I am signed in'
+  automatic_login
+  confirm_login
 end
 
 When /^I put in my password in "([^"]*)"$/ do |field|
@@ -40,6 +37,5 @@ When /^I put in my password in "([^"]*)"$/ do |field|
 end
 
 When /^I (?:log|sign) out$/ do
-  $browser.delete_cookie('_session', 'path=/') if $browser
-  $browser.delete_all_visible_cookies if $browser
+  logout
 end
diff --git a/features/support/user_cuke_helpers.rb b/features/support/user_cuke_helpers.rb
index 3d0b6653e1..84b755d00d 100644
--- a/features/support/user_cuke_helpers.rb
+++ b/features/support/user_cuke_helpers.rb
@@ -1,4 +1,8 @@
 module UserCukeHelpers
+
+  # creates a new user object from the factory with some default attributes
+  # and the given override attributes, adds the standard aspects to it
+  # and returns it
   def create_user(overrides={})
     default_attrs = {
         :password => 'password',
@@ -11,16 +15,42 @@ module UserCukeHelpers
     user
   end
 
+  # create the default testing aspects for a given user
   def add_standard_aspects(user)
     user.aspects.create(:name => "Besties")
     user.aspects.create(:name => "Unicorns")
   end
 
+  # fill out the fields on the sign_in page and press submit
   def login_as(user, pass)
     fill_in 'user_username', :with=>user
     fill_in 'user_password', :with=>pass
     click_button :submit
   end
+
+  # create a new @me user, if not present, and log in using the
+  # integration_sessions controller (automatic)
+  def automatic_login
+    @me ||= Factory(:user_with_aspect, :getting_started => false)
+    page.driver.visit(new_integration_sessions_path(:user_id => @me.id))
+    click_button "Login"
+  end
+
+  # use the @me user to perform a manual login via the sign_in page
+  def manual_login
+    visit login_page
+    login_as @me.username, @me.password
+  end
+
+  def confirm_login
+    wait_until { page.has_content?("#{@me.first_name} #{@me.last_name}") }
+  end
+
+  # delete all cookies, destroying the current session
+  def logout
+    $browser.delete_cookie('_session', 'path=/') if $browser
+    $browser.delete_all_visible_cookies if $browser
+  end
 end
 
 World(UserCukeHelpers)
-- 
GitLab