Skip to content

Commit

Permalink
add rails route loader for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Sonnek committed Oct 31, 2011
1 parent 46b1109 commit dcf0735
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
10 changes: 7 additions & 3 deletions spec/routing/authorizations_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
require 'spec_helper'

describe Devise::Oauth2Providable::AuthorizationsController do
before :all do
Devise::Oauth2Providable::Engine.load_engine_routes
end
describe 'routing' do
it 'routes POST /oauth2/authorizations' do
{:post => '/oauth2/authorizations'}.should route_to(:controller => 'oauth2/authorizations', :action => 'create')
post('/oauth2/authorizations').should route_to('devise/oauth2_providable/authorizations#create')
end
it 'routes GET /oauth2/authorize' do
{:get => '/oauth2/authorize'}.should route_to(:controller => 'oauth2/authorizations', :action => 'new')
get('/oauth2/authorize').should route_to('devise/oauth2_providable/authorizations#new')
end
it 'routes POST /oauth2/authorize' do
{:post => '/oauth2/authorize'}.should route_to(:controller => 'oauth2/authorizations', :action => 'new')
#FIXME: this is valid, but the route is not being loaded into the test
post('/oauth2/authorize').should route_to('devise/oauth2_providable/authorizations#new')
end
end
end
5 changes: 4 additions & 1 deletion spec/routing/tokens_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
require 'spec_helper'

describe Devise::Oauth2Providable::TokensController do
before :all do
Devise::Oauth2Providable::Engine.load_engine_routes
end
describe 'routing' do
it 'routes POST /oauth2/token' do
{:post => '/oauth2/token'}.should route_to(:controller => 'oauth2/tokens', :action => 'create')
post('/oauth2/token').should route_to('devise/oauth2_providable/tokens#create')
end
end
end
74 changes: 74 additions & 0 deletions spec/support/inject_engine_routes_into_application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# see http://www.builtfromsource.com/2011/09/21/testing-routes-with-rails-3-1-engines/
module Devise
module Oauth2Providable
module EngineHacks
##
# Automatically append all of the current engine's routes to the main
# application's route set. This needs to be done for ALL functional tests that
# use engine routes, since the mounted routes don't work during tests.
#
# @param [Symbol] engine_symbol Optional; if provided, uses this symbol to
# locate the engine class by name, otherwise uses the module of the calling
# test case as the presumed name of the engine.
#
# @author Jason Hamilton ([email protected])
# @author Matthew Ratzloff ([email protected])
def load_engine_routes(engine_symbol = nil)
if engine_symbol
engine_name = engine_symbol.to_s.camelize
else
# No engine provided, so presume the current engine is the one to load
engine_name = self.class.name.split("::").first.split("(").last
end
engine = ("#{engine_name}::Engine").constantize

engine_name = 'oauth2'
engine = Devise::Oauth2Providable::Engine
named_routes = engine.routes.named_routes.routes
resourced_routes = []

# Append the routes for this module to the existing routes
# ::Rails.application.routes.disable_clear_and_finalize = true
# ::Rails.application.routes.clear!
# ::Rails.application.routes_reloader.paths.each { |path| load(path) }
::Rails.application.routes.draw do

# unnamed_routes = engine.routes.routes - named_routes.values

engine.routes.routes.each do |route|
# Call the method by hand based on the symbol
path = "/#{engine_name.underscore}#{route.path}"
requirements = route.requirements
if path_helper = named_routes[route]
requirements[:as] = path_helper
elsif route.requirements[:controller].present?
# Presume that all controllers referenced in routes should also be
# resources and append that routing on the end so that *_path helpers
# will still work
resourced_routes << route.requirements[:controller].gsub("#{engine_name.downcase}/", "").to_sym
end

verb = (route.verb.blank? ? "GET" : route.verb).downcase.to_sym
send(verb, path, requirements) if respond_to?(verb)
end

# Add each route, once, to the end under a scope to trick path helpers.
# This will probably break as soon as there is route name overlap, but
# we'll cross that bridge when we get to it.
# resourced_routes.uniq!
# scope engine_name.downcase do
# resourced_routes.each do |resource|
# resources resource
# end
# end
end

# Finalize the routes
::Rails.application.routes.finalize!
::Rails.application.routes.disable_clear_and_finalize = false
end
end
end
end

Rails::Engine.send(:include, Devise::Oauth2Providable::EngineHacks)

0 comments on commit dcf0735

Please sign in to comment.