Commit e5e38f1f

mo <mokha@cisco.com>
2017-09-01 01:33:08
fix feature specs.
1 parent 63183ed
spec/features/add_to_favorites_spec.rb
@@ -4,19 +4,12 @@ describe "adding a cake to your favorites" do
   let!(:creation) { create(:creation, user: create(:user), photos: [create(:photo)]) }
   let!(:me) { create(:user, password: "password") }
 
-  before :each do
-    visit login_path
-    within('.form-inline') do
-      fill_in('session_username', with: me.email)
-      fill_in('session_password', with: "password")
-    end
-    click_button("Sign In")
+  before { http_login(me) }
+
+  it "redirects you to the cake after" do
     visit root_path
     click_link(creation.name[0...12])
     click_link_or_button("add-to-favorites-button")
-  end
-
-  it "redirects you to the cake after" do
     expect(page).to have_content("This has been added to your favorites")
   end
 end
spec/features/change_password_spec.rb
@@ -4,25 +4,17 @@ describe "changing my password", js: true do
   context "when changing my password" do
     let(:user) { create(:user, password: "password") }
 
-    before :each do
-      visit login_path
-      within(".form-inline") do
-        fill_in("session_username", with: user.email)
-        fill_in("session_password", with: "password")
-      end
-      click_button("Sign In")
+    before { http_login(user) }
+
+    it "displays a confirmation message" do
       visit my_dashboard_path
       click_link("Account")
       within(".form-horizontal") do
-        fill_in("user_password", with: "mopass")
-        fill_in("user_password_confirmation", with: "mopass")
+        fill_in("user_password", with: "secret")
+        fill_in("user_password_confirmation", with: "secret")
       end
       click_button "Update password"
-    end
-
-    it "displays a confirmation message" do
-      confirmation_message = I18n.translate("my.passwords.updated")
-      expect(page).to have_content(confirmation_message)
+      expect(page).to have_content(I18n.translate("my.passwords.updated"))
     end
   end
 end
spec/features/change_profile_settings_spec.rb
@@ -1,26 +1,19 @@
 require "rails_helper"
 
 describe "Change settings", js: true do
-  let(:user) { create(:user, :password => "password") }
+  let(:user) { create(:user, password: "password") }
 
-  before :each do
-    visit login_path
-    within('.form-inline') do
-      fill_in('session_username', :with => user.email)
-      fill_in('session_password', :with => "password")
-    end
-    click_button("Sign In")
+  before { http_login(user) }
+
+  it "saves the changes properly" do
     visit my_settings_path
     within(".form-horizontal") do
-      fill_in('user_city', :with => "Calgary, Alberta, Canada")
-      fill_in('user_website', :with => "http://mokhan.ca/")
-      fill_in('user_twitter', :with => "mocheen")
-      fill_in('user_facebook', :with => "yeah right!")
+      fill_in('user_city', with: "Calgary, Alberta, Canada")
+      fill_in('user_website', with: "https://www.cakeside.com/")
+      fill_in('user_twitter', with: "cakeside")
+      fill_in('user_facebook', with: "cakeside")
     end
     click_button "Save changes"
-  end
-
-  it "should save the changes properly" do
     expect(page).to have_content(I18n.translate(:profile_saved))
   end
 end
spec/features/upload_avatar_spec.rb
@@ -2,23 +2,16 @@ require "rails_helper"
 
 describe "uploading an avatar", js: true do
   let(:user) { create(:user, :password => "password") }
+  let(:file) { File.expand_path(File.join(Rails.root, '/spec/fixtures/images/gorilla.jpg')) }
 
-  before :each do
-    visit login_path
-    within('.form-inline') do
-      fill_in('session_username', :with => user.email)
-      fill_in('session_password', :with => "password")
-    end
-    click_button("Sign In")
+  before { http_login(user) }
+
+  it "displays a success message" do
     visit new_my_avatar_path
-    file = File.expand_path(File.join(Rails.root, '/spec/fixtures/images/gorilla.jpg'))
     within(".new_photo") do
       attach_file('photo_image', file)
     end
     click_button "Upload picture"
-  end
-
-  it "should display a success message" do
     expect(page).to have_content(I18n.t(:avatar_uploaded))
   end
 end
spec/support/authentication.rb
@@ -11,4 +11,10 @@ module Authentication
       encode_credentials(user.authentication_token)
     request.env["HTTP_AUTHORIZATION"] = encoded_credentials
   end
+
+  module Capybara
+    def http_login(user)
+      LoginPage.new.visit_page.login_with(email: user.email, password: "password")
+    end
+  end
 end
spec/rails_helper.rb
@@ -50,6 +50,7 @@ RSpec.configure do |config|
   # https://relishapp.com/rspec/rspec-rails/docs
   config.infer_spec_type_from_file_location!
   config.include Authentication, type: :controller
+  config.include Authentication::Capybara, type: :feature
   config.include WaitForAjax, type: :feature
   config.include FactoryGirl::Syntax::Methods