Skip to content

Commit

Permalink
Add the request spec helper method, #login_user.
Browse files Browse the repository at this point in the history
  • Loading branch information
ebihara99999 committed Apr 26, 2017
1 parent cef2ae2 commit 6782e3f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/sorcery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module TestHelpers
module Rails
require 'sorcery/test_helpers/rails/controller'
require 'sorcery/test_helpers/rails/integration'
require 'sorcery/test_helpers/rails/request'
end

module Internal
Expand Down
20 changes: 20 additions & 0 deletions lib/sorcery/test_helpers/rails/request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Sorcery
module TestHelpers
module Rails
module Request
# Accepts arguments for user to login, the password, route to use and HTTP method
# Defaults - @user, 'secret', 'user_sessions_url' and http_method: POST
def login_user(user = nil, password = 'secret', route = nil, http_method = :post)
user ||= @user
route ||= user_sessions_url

username_attr = user.sorcery_config.username_attribute_names.first
username = user.send(username_attr)
password_attr = user.sorcery_config.password_attribute_name

self.send(http_method, route, params: { "#{username_attr}": username, "#{password_attr}": password })
end
end
end
end
end

0 comments on commit 6782e3f

Please sign in to comment.