Skip to content

Commit

Permalink
Merge pull request #144 from fwininger/rails_5.1
Browse files Browse the repository at this point in the history
Add Rails 5.1 tests
  • Loading branch information
mgomes authored Aug 8, 2017
2 parents bd79cf2 + 4e0ab78 commit 22e91aa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 58 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ rvm:
- 2.1.9
- 2.2.6
- 2.3.3
- 2.4.0
- 2.4.1
gemfile:
- gemfiles/rails_4.gemfile
- gemfiles/rails_41.gemfile
- gemfiles/rails_42.gemfile
- gemfiles/rails_5.gemfile
- gemfiles/rails_51.gemfile
env:
- TEST_SUITE=rake

Expand All @@ -24,8 +25,10 @@ matrix:
exclude:
- rvm: 2.1.9
gemfile: gemfiles/rails_5.gemfile
- rvm: 2.1.9
gemfile: gemfiles/rails_51.gemfile
include:
- rvm: 2.3.3
- rvm: 2.4.1
gemfile: gemfiles/rails_5.gemfile
env: TEST_SUITE="rubocop lib/ spec/"

Expand Down
9 changes: 9 additions & 0 deletions gemfiles/rails_51.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "actionpack", "~> 5.1.1"
gem "activeresource", "~> 5.0.0", git: 'https://github.com/rails/activeresource.git'
gem "activesupport", "~> 5.1.1"

gemspec :path => "../"
86 changes: 30 additions & 56 deletions spec/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,125 +15,99 @@ def require_api_auth
respond_to do |format|
format.xml { render xml: 'You are unauthorized to perform this action.', status: 401 }
format.json { render json: 'You are unauthorized to perform this action.', status: 401 }
format.html { render text: 'You are unauthorized to perform this action', status: 401 }
format.html { render plain: 'You are unauthorized to perform this action', status: 401 }
end
end
end

class TestController < ApplicationController
before_filter :require_api_auth, only: [:index]
before_action :require_api_auth, only: [:index]

if defined?(ActionDispatch)
def self._routes
ActionDispatch::Routing::RouteSet.new
end
def self._routes
ActionDispatch::Routing::RouteSet.new
end

def index
render text: 'OK'
render json: 'OK'
end

def public
render text: 'OK'
render json: 'OK'
end

def rescue_action(e)
raise(e)
end
end

unless defined?(ActionDispatch)
ActionController::Routing::Routes.draw { |map| map.resources :test }
end

def generated_response(request, action = :index)
if defined?(ActionDispatch)
response = ActionDispatch::TestResponse.new
controller = TestController.new
controller.request = request
controller.response = response
controller.process(action)
response
else
request.action = action.to_s
request.path = "/#{action}"
TestController.new.process(request, ActionController::TestResponse.new)
end
response = ActionDispatch::TestResponse.new
controller = TestController.new
controller.request = request
controller.response = response
controller.process(action)
response
end

it 'should permit a request with properly signed headers' do
def generated_request
request = if ActionController::TestRequest.respond_to?(:create)
ActionController::TestRequest.create
if Gem.loaded_specs['actionpack'].version < Gem::Version.new('5.1.0')
ActionController::TestRequest.create
else
ActionController::TestRequest.create(TestController)
end
else
ActionController::TestRequest.new
end
request.accept = ['application/json']
request
end

it 'should permit a request with properly signed headers' do
request = generated_request
request.env['DATE'] = Time.now.utc.httpdate
ApiAuth.sign!(request, '1044', API_KEY_STORE['1044'])
response = generated_response(request, :index)
expect(response.code).to eq('200')
end

it 'should forbid a request with properly signed headers but timestamp > 15 minutes ago' do
request = if ActionController::TestRequest.respond_to?(:create)
ActionController::TestRequest.create
else
ActionController::TestRequest.new
end
request = generated_request
request.env['DATE'] = 'Mon, 23 Jan 1984 03:29:56 GMT'
ApiAuth.sign!(request, '1044', API_KEY_STORE['1044'])
response = generated_response(request, :index)
expect(response.code).to eq('401')
end

it 'should forbid a request with properly signed headers but timestamp > 15 minutes in the future' do
request = if ActionController::TestRequest.respond_to?(:create)
ActionController::TestRequest.create
else
ActionController::TestRequest.new
end
request = generated_request
request.env['DATE'] = 'Mon, 23 Jan 2100 03:29:56 GMT'
ApiAuth.sign!(request, '1044', API_KEY_STORE['1044'])
response = generated_response(request, :index)
expect(response.code).to eq('401')
end

it "should insert a DATE header in the request when one hasn't been specified" do
request = if ActionController::TestRequest.respond_to?(:create)
ActionController::TestRequest.create
else
ActionController::TestRequest.new
end
request = generated_request
ApiAuth.sign!(request, '1044', API_KEY_STORE['1044'])
expect(request.headers['DATE']).not_to be_nil
end

it 'should forbid an unsigned request to a protected controller action' do
request = if ActionController::TestRequest.respond_to?(:create)
ActionController::TestRequest.create
else
ActionController::TestRequest.new
end
request = generated_request
response = generated_response(request, :index)
expect(response.code).to eq('401')
end

it 'should forbid a request with a bogus signature' do
request = if ActionController::TestRequest.respond_to?(:create)
ActionController::TestRequest.create
else
ActionController::TestRequest.new
end
request = generated_request
request.env['Authorization'] = 'APIAuth bogus:bogus'
response = generated_response(request, :index)
expect(response.code).to eq('401')
end

it 'should allow non-protected controller actions to function as before' do
request = if ActionController::TestRequest.respond_to?(:create)
ActionController::TestRequest.create
else
ActionController::TestRequest.new
end
request = generated_request
response = generated_response(request, :public)
expect(response.code).to eq('200')
end
Expand Down

0 comments on commit 22e91aa

Please sign in to comment.