Clean up test auth helpers

This commit is contained in:
Marcin Kulik 2014-03-16 16:03:33 +01:00
parent 72248d7b83
commit 56b99f11e3
8 changed files with 70 additions and 25 deletions

View File

@ -1,6 +1,6 @@
notification :tmux, :color_location => 'status-right-bg'
guard 'rspec', :cmd => 'spring rspec --fail-fast --tag ~js --tag ~slow' do
guard 'rspec', all_on_start: false, cmd: 'spring rspec --fail-fast --tag ~js --tag ~slow' do
watch('spec/spec_helper.rb') { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }

View File

@ -5,8 +5,8 @@ module Api
private
def authenticate
warden.authenticate(:api_token)
def warden_strategies
[:api_token]
end
end

View File

@ -17,6 +17,10 @@ class ApplicationController < ActionController::Base
private
def warden_strategies
[:auth_cookie]
end
def decorated_current_user
current_user && current_user.decorate
end

View File

@ -1,5 +0,0 @@
module TestAuthentication
attr_accessor :current_user
end

View File

@ -3,7 +3,7 @@ module WardenAuthentication
private
def current_user
warden.authenticate(:auth_cookie) unless warden.authenticated?
warden.authenticate(*warden_strategies) unless warden.authenticated?
warden.user
end
@ -22,4 +22,8 @@ module WardenAuthentication
request.env['warden']
end
def warden_strategies
raise NotImplementedError
end
end

View File

@ -22,8 +22,6 @@ require 'sidekiq/testing'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
Dir[Rails.root.join("spec/shared/**/*.rb")].each { |f| require f }
require 'authentication/test_authentication'
Capybara.javascript_driver = :poltergeist
OmniAuth.config.test_mode = true
@ -42,7 +40,6 @@ RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include Asciinema::FixtureHelpers
config.include Asciinema::FeatureHelpers
config.include Asciinema::ControllerHelpers, type: :controller
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
@ -52,10 +49,6 @@ RSpec.configure do |config|
DatabaseCleaner.strategy = :transaction
end
config.before(:each, type: :controller) do
controller.class_eval { include TestAuthentication }
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end

View File

@ -0,0 +1,58 @@
module Asciinema
module Test
module Warden
class EmailStrategy < ::Warden::Strategies::Base
def valid?
email.present?
end
def authenticate!
user = User.find_by_email(email)
user && success!(user)
end
private
def email
request.params['email']
end
end
end
module Authentication
attr_accessor :current_user
end
module ControllerHelpers
def login_as(user)
controller.current_user = user
end
end
module FeatureHelpers
def login_as(user)
visit edit_user_path(email: user.email)
page.save_screenshot 'a.png'
end
end
end
end
Warden::Strategies.add(:test, Asciinema::Test::Warden::EmailStrategy)
ApplicationController.class_eval do
def warden_strategies
[:test]
end
end
RSpec.configure do |config|
config.before(:each, type: :controller) do
controller.class_eval { include Asciinema::Test::Authentication }
end
config.include Asciinema::Test::ControllerHelpers, type: :controller
config.include Asciinema::Test::FeatureHelpers, type: :feature
end

View File

@ -1,9 +0,0 @@
module Asciinema
module ControllerHelpers
def login_as(user)
controller.current_user = user
end
end
end