Skip to content

Commit

Permalink
Merge pull request #515 from arnvald/integration_test_helper
Browse files Browse the repository at this point in the history
Integration test helper
  • Loading branch information
arnvald committed Apr 1, 2014
2 parents a555932 + a7daf00 commit 02b7af5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
6 changes: 5 additions & 1 deletion lib/sorcery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ module CryptoProviders
autoload :SHA512, 'sorcery/crypto_providers/sha512'
end
module TestHelpers
autoload :Rails, 'sorcery/test_helpers/rails'
autoload :Internal, 'sorcery/test_helpers/internal'
module Internal
autoload :Rails, 'sorcery/test_helpers/internal/rails'
end
autoload :Rails, 'sorcery/test_helpers/rails'
module Rails
autoload :Controller, 'sorcery/test_helpers/rails/controller'
autoload :Integration, 'sorcery/test_helpers/rails/integration'
end

end

Expand Down
2 changes: 1 addition & 1 deletion lib/sorcery/test_helpers/internal/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Sorcery
module TestHelpers
module Internal
module Rails
include ::Sorcery::TestHelpers::Rails
include ::Sorcery::TestHelpers::Rails::Controller

SUBMODUELS_AUTO_ADDED_CONTROLLER_FILTERS = [
:register_last_activity_time_to_db,
Expand Down
13 changes: 0 additions & 13 deletions lib/sorcery/test_helpers/rails.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
module Sorcery
module TestHelpers
module Rails
# logins a user and calls all callbacks
def login_user(user = nil)
user ||= @user
@controller.send(:auto_login, user)
@controller.send(:after_login!, user, [user.send(user.sorcery_config.username_attribute_names.first), 'secret'])
end

def logout_user
@controller.send(:logout)
end

def logged_in?
@controller.send(:logged_in?)
end
end
end
end
17 changes: 17 additions & 0 deletions lib/sorcery/test_helpers/rails/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Sorcery
module TestHelpers
module Rails
module Controller
def login_user(user = nil, test_context = {})
user ||= @user
@controller.send(:auto_login, user)
@controller.send(:after_login!, user, [user.send(user.sorcery_config.username_attribute_names.first), 'secret'])
end

def logout_user
@controller.send(:logout)
end
end
end
end
end
26 changes: 26 additions & 0 deletions lib/sorcery/test_helpers/rails/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Sorcery
module TestHelpers
module Rails
module Integration

#Accepts arguments for user to login, route to use and HTTP method
#Defaults - @user, 'sessions_url' and POST
def login_user(user = nil, route = nil, http_method = :post)
user ||= @user
route ||= sessions_url

username_attr = user.sorcery_config.username_attribute_names.first
username = user.send(username_attr)
page.driver.send(http_method, route, { :"#{username_attr}" => username, :password => 'secret' })
end

#Accepts route and HTTP method arguments
#Default - 'logout_url' and GET
def logout_user(route = nil, http_method = :get)
route ||= logout_url
page.driver.send(http_method, route)
end
end
end
end
end

0 comments on commit 02b7af5

Please sign in to comment.