Skip to content

Commit

Permalink
Upgrade to RSpec 2.0, add universal options hash for strategies, cust…
Browse files Browse the repository at this point in the history
…om paths, closes omniauth#54
  • Loading branch information
Michael Bleigh committed Nov 3, 2010
1 parent c9157ec commit 3843745
Show file tree
Hide file tree
Showing 49 changed files with 272 additions and 202 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--format=nested
22 changes: 19 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ rescue LoadError
def cyan; '' end
def blue; '' end
def clear; '' end
def green; '' end
def red; '' end
end

OMNIAUTH_GEMS = %w(oa-basic oa-core oa-oauth oa-openid oa-enterprise omniauth)

def each_gem(action, &block)
OMNIAUTH_GEMS.each_with_index do |dir, i|
print blue, "\n\n== ", cyan, dir, blue, " ", action, clear, "\n\n"
Dir.chdir(dir, &block)
Dir.chdir(dir) do
block.call(dir)
end
end
end

Expand Down Expand Up @@ -52,8 +56,20 @@ end

desc 'Run specs for all of the gems.'
task :spec do
each_gem('specs are running...') do
system('rake spec')
error_gems = []
each_gem('specs are running...') do |jem|
ENV['RSPEC_FORMAT'] = 'progress'
unless system('rake spec')
error_gems << jem
end
end

puts
if error_gems.any?
puts "#{red}#{error_gems.size} gems with failing specs: #{error_gems.join(', ')}#{clear}"
exit(1)
else
puts "#{green}All gems passed specs.#{clear}"
end
end

Expand Down
2 changes: 1 addition & 1 deletion development_dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
gem 'rack'
gem 'rake'
gem 'mg', '~> 0.0.8'
gem 'rspec', '~> 1.3.0'
gem 'rspec', '~> 2.0.0'
gem 'webmock', '~> 1.3.4'
gem 'rack-test', '~> 0.5.4'
gem 'json', '~> 1.4.3' # multi_json implementation
Expand Down
7 changes: 3 additions & 4 deletions oa-basic/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ require 'rake'
require 'mg'
MG.new('oa-basic.gemspec')

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << '../oa-core/lib' << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |s|
s.rspec_opts = "--format=#{ENV['RSPEC_FORMAT'] || 'nested'} --colour"
end

task :default => :spec
4 changes: 2 additions & 2 deletions oa-basic/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'rubygems'
require 'spec'
require 'spec/autorun'
require 'rspec'
require 'rspec/autorun'
require 'rack/test'
require 'webmock/rspec'

Expand Down
2 changes: 1 addition & 1 deletion oa-core/.rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
--colour
--format=specdoc
--format=nested
7 changes: 3 additions & 4 deletions oa-core/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ require 'rake'
require 'mg'
MG.new('oa-core.gemspec')

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |s|
s.rspec_opts = "--format=#{ENV['RSPEC_FORMAT'] || 'nested'} --colour"
end

task :default => :spec
1 change: 1 addition & 0 deletions oa-core/autotest/discover.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Autotest.add_discovery { "rspec2" }
20 changes: 17 additions & 3 deletions oa-core/lib/omniauth/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ module Strategy

def self.included(base)
base.class_eval do
attr_reader :app, :name, :env
attr_reader :app, :name, :env, :options
end
end

def initialize(app, name, *args)
@app = app
@name = name.to_sym
@options = args.last.is_a?(Hash) ? args.pop : {}
end

def call(env)
Expand All @@ -21,9 +22,9 @@ def call(env)

def call!(env)
@env = env
if request.path == "#{OmniAuth.config.path_prefix}/#{name}"
if request.path == request_path
request_phase
elsif request.path == "#{OmniAuth.config.path_prefix}/#{name}/callback"
elsif request.path == callback_path
callback_phase
else
if respond_to?(:other_phase)
Expand All @@ -43,7 +44,20 @@ def callback_phase
call_app!
end

def path_prefix
options[:path_prefix] || OmniAuth.config.path_prefix
end

def request_path
options[:request_path] || "#{path_prefix}/#{name}"
end

def callback_path
options[:callback_path] || "#{path_prefix}/#{name}/callback"
end

def call_app!
# TODO: Remove this when we get to 0.2.0
@env['rack.auth'] = env['omniauth.auth'] if env.key?('omniauth.auth')
@env['rack.auth.error'] = env['omniauth.error'] if env.key?('omniauth.error')

Expand Down
11 changes: 8 additions & 3 deletions oa-core/lib/omniauth/test/strategy_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ module Test
module StrategyTestCase

def app
strategy = self.strategy
strat = self.strategy
resp = self.app_response
Rack::Builder.new {
use OmniAuth::Test::PhonySession
use *strategy
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, [Rack::Request.new(env).params.key?('auth').to_s]] }
use *strat
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, [resp || env.key?('omniauth.auth').to_s]] }
}.to_app
end

def app_response
nil
end

def session
last_request.env['rack.session']
Expand Down
67 changes: 67 additions & 0 deletions oa-core/spec/omniauth/strategy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'spec_helper'

class ExampleStrategy
include OmniAuth::Strategy
def call(env); self.call!(env) end
def request_phase
raise "Request Phase"
end
def callback_phase
raise "Callback Phase"
end
end

describe OmniAuth::Strategy do
let(:app){ lambda{|env| [200, {}, ['Awesome']]}}
describe '#initialize' do
context 'options extraction' do
it 'should be the last argument if the last argument is a Hash' do
ExampleStrategy.new(app, 'test', :abc => 123).options[:abc].should == 123
end

it 'should be a blank hash if none are provided' do
ExampleStrategy.new(app, 'test').options.should == {}
end
end
end

describe '#call' do
let(:strategy){ ExampleStrategy.new(app, 'test', @options) }

context 'default paths' do
it 'should use the default request path' do
lambda{ strategy.call({'PATH_INFO' => '/auth/test'}) }.should raise_error("Request Phase")
end

it 'should use the default callback path' do
lambda{ strategy.call({'PATH_INFO' => '/auth/test/callback'}) }.should raise_error("Callback Phase")
end
end

context 'custom paths' do
it 'should use a custom request_path if one is provided' do
@options = {:request_path => '/awesome'}
lambda{ strategy.call({'PATH_INFO' => '/awesome'}) }.should raise_error("Request Phase")
end

it 'should use a custom callback_path if one is provided' do
@options = {:callback_path => '/radical'}
lambda{ strategy.call({'PATH_INFO' => '/radical'}) }.should raise_error("Callback Phase")
end
end

context 'custom prefix' do
before do
@options = {:path_prefix => '/wowzers'}
end

it 'should use a custom prefix for request' do
lambda{ strategy.call({'PATH_INFO' => '/wowzers/test'}) }.should raise_error("Request Phase")
end

it 'should use a custom prefix for callback' do
lambda{ strategy.call({'PATH_INFO' => '/wowzers/test/callback'}) }.should raise_error("Callback Phase")
end
end
end
end
6 changes: 3 additions & 3 deletions oa-core/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require 'rubygems'
require 'bundler'
Bundler.setup
require 'spec'
require 'spec/autorun'
require 'rspec'
require 'rspec/autorun'
require 'rack/test'
require 'omniauth/core'
require 'omniauth/test'

Spec::Runner.configure do |config|
RSpec.configure do |config|
config.include Rack::Test::Methods
config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
end
Expand Down
7 changes: 3 additions & 4 deletions oa-enterprise/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ require 'rake'
require 'mg'
MG.new('oa-enterprise.gemspec')

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << '../oa-core/lib' << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |s|
s.rspec_opts = "--format=#{ENV['RSPEC_FORMAT'] || 'nested'} --colour"
end

task :default => :spec
2 changes: 1 addition & 1 deletion oa-enterprise/lib/omniauth/strategies/cas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CAS
autoload :ServiceTicketValidator, 'omniauth/strategies/cas/service_ticket_validator'

def initialize(app, options = {})
super(app, options.delete(:name) || :cas)
super(app, options.delete(:name) || :cas, options)
@configuration = OmniAuth::Strategies::CAS::Configuration.new(options)
end

Expand Down
9 changes: 6 additions & 3 deletions oa-enterprise/lib/omniauth/strategies/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ class LDAP
'url' => ['wwwhomepage'],
'image' => 'jpegPhoto',
'description' => 'description'}
# Initialize the LDAP Middleware
#
# @param [Rack Application] app Standard Rack middleware argument.
# @option options [String, 'LDAP Authentication'] :title A title for the authentication form.
def initialize(app, title, options = {})
@options = options.dup
super(app, @options.delete(:name) || :ldap)
super(app, @options.delete(:name) || :ldap, options.dup)
@title = title
@name_proc = (@options.delete(:name_proc) || Proc.new {|name| name})
@adaptor = OmniAuth::Strategies::LDAP::Adaptor.new(options)
Expand All @@ -37,7 +40,7 @@ def request_phase
end

def get_credentials
OmniAuth::Form.build(@title) do
OmniAuth::Form.build(options[:title] || "LDAP Authentication") do
text_field 'Login', 'username'
password_field 'Password', 'password'
end.to_response
Expand Down
6 changes: 3 additions & 3 deletions oa-enterprise/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require 'rubygems'
require 'bundler'
Bundler.setup
require 'spec'
require 'spec/autorun'
require 'rspec'
require 'rspec/autorun'
require 'webmock/rspec'
require 'rack/test'
require 'omniauth/core'
require 'omniauth/test'
require 'omniauth/enterprise'

Spec::Runner.configure do |config|
RSpec.configure do |config|
config.include WebMock
config.include Rack::Test::Methods
config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
Expand Down
3 changes: 1 addition & 2 deletions oa-oauth/.rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
--colour
--format=specdoc
--backtrace
--format=nested
7 changes: 3 additions & 4 deletions oa-oauth/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ require 'rake'
require 'mg'
MG.new('oa-oauth.gemspec')

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << '../oa-core/lib' << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |s|
s.rspec_opts = "--format=#{ENV['RSPEC_FORMAT'] || 'nested'} --colour"
end

task :default => :spec
1 change: 1 addition & 0 deletions oa-oauth/autotest/discover.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Autotest.add_discovery { "rspec2" }
18 changes: 12 additions & 6 deletions oa-oauth/lib/omniauth/strategies/dopplr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ module Strategies
# use OmniAuth::Strategies::Dopplr, 'consumerkey', 'consumersecret'
#
class Dopplr < OmniAuth::Strategies::OAuth
def initialize(app, consumer_key, consumer_secret)
super(app, :dopplr, consumer_key, consumer_secret,
:site => 'https://www.dopplr.com',
:request_token_path => "/oauth/request_token",
:access_token_path => "/oauth/access_token",
:authorize_path => "/oauth/authorize")
# Initialize the Dopplr strategy.
#
# @option options [Hash, {}] :client_options Options to be passed directly to the OAuth Consumer
def initialize(app, consumer_key, consumer_secret, options = {})
client_options = {
:site => 'https://www.dopplr.com',
:request_token_path => "/oauth/request_token",
:access_token_path => "/oauth/access_token",
:authorize_path => "/oauth/authorize"
}

super(app, :dopplr, consumer_key, consumer_secret, client_options, options)
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions oa-oauth/lib/omniauth/strategies/facebook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class Facebook < OAuth2
# @param [String] app_secret the application secret as registered on Facebook
# @option options [String] :scope ('email,offline_access') comma-separated extended permissions such as `email` and `manage_pages`
def initialize(app, app_id, app_secret, options = {})
options[:site] = 'https://graph.facebook.com/'
super(app, :facebook, app_id, app_secret, options)
super(app, :facebook, app_id, app_secret, {:site => 'https://graph.facebook.com/'}, options)
end

def user_data
Expand Down
5 changes: 2 additions & 3 deletions oa-oauth/lib/omniauth/strategies/foursquare.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module OmniAuth
module Strategies
class Foursquare < OAuth
def initialize(app, consumer_key, consumer_secret)
super(app, :foursquare, consumer_key, consumer_secret,
:site => 'http://foursquare.com')
def initialize(app, consumer_key, consumer_secret, options = {})
super(app, :foursquare, consumer_key, consumer_secret, {:site => 'http://foursquare.com'}, options)
end

def auth_hash
Expand Down
Loading

0 comments on commit 3843745

Please sign in to comment.