Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a signed method to ActiveSupport::TestCase #67

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/api_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
require 'api_auth/headers'
require 'api_auth/base'
require 'api_auth/railtie'

# The signed method for tests is modeled after the xhr method which was introduced in Rails 3 and will not work in earlier versions of Rails
require 'api_auth/extensions/action_controller/test_case' if defined?(Rails::VERSION::STRING) && Rails::VERSION::STRING >= '3' && Rails.env.test?
26 changes: 26 additions & 0 deletions lib/api_auth/extensions/action_controller/test_case.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'active_support/test_case'

module ActionController
class TestCase < ActiveSupport::TestCase
module Behavior

def signed(request_method, action, access_id, secret_key, parameters = nil, session = nil, flash = nil)
@request.env['API_AUTH_ACCESS_ID'] = access_id
@request.env['API_AUTH_SECRET_KEY'] ||= secret_key
__send__(request_method, action, parameters, session, flash).tap do
@request.env.delete 'API_AUTH_ACCESS_ID'
@request.env.delete 'API_AUTH_SECRET_KEY'
end
end

end
end

class Base
# Override so we can sign the request if the access_id and secret key have been set, now that all the headers have been set.
def process(action, *args)
ApiAuth.sign!(request, request.env['API_AUTH_ACCESS_ID'], request.env['API_AUTH_SECRET_KEY']) if request.env['API_AUTH_ACCESS_ID'] && request.env['API_AUTH_SECRET_KEY']
super
end
end
end