Permalink
Browse files

Refactoring of sanity checks on adding extensions for more descriptiv…

…e exceptions.

Additional tests on extension handling.
  • Loading branch information...
1 parent 0a895a2 commit 49ebc8b1a086edc7d6160f02799fcd62fa88039b @scytrin scytrin committed Jul 1, 2008
Showing with 66 additions and 21 deletions.
  1. +21 −9 lib/rack/auth/openid.rb
  2. +45 −12 test/spec_rack_auth_openid.rb
View
30 lib/rack/auth/openid.rb
@@ -401,15 +401,27 @@ def finish(consumer, session, req)
# the session, which is the namespace uri by default.
def add_extension ext, *args
if not ext.is_a? Module
- raise TypeError, "Extension #{ext.inspect} is not a module"
- elsif not %w'Request Response NS_URI'.all?{|c| ext.constants.include?(c) }
- raise ArgumentError, "Extension #{ext.inspect} does not contain required constants"
- elsif not %w'Request Response'.all?{|c| (r=ext.const_get(c)).is_a? Class and ::OpenID::Extension > r }
- raise TypeError, "Extension #{ext.inspect}'s Request or Response not a decendant of OpenID::Extension"
- elsif not ext::NS_URI.is_a? String
- raise TypeError, "Extension #{ext.inspect}'s NS_URI is not a string"
- elsif not (uri = URI(ext::NS_URI) and uri.absolute? and uri.scheme =~ /^https?$/)
- raise ArgumentError, "Extension #{ext.inspect}'s NS_URI is not an absolute http uri"
+ raise TypeError, "#{ext.inspect} is not a module"
+ elsif not (m = %w'Request Response NS_URI' - ext.constants).empty?
+ raise ArgumentError, "#{ext.inspect} missing #{m*', '}"
+ end
+
+ consts = [ext::Request, ext::Response]
+
+ if not consts.all?{|c| c.is_a? Class }
+ raise TypeError, "#{ext.inspect}'s Request or Response is not a class"
+ elsif not consts.all?{|c| ::OpenID::Extension > c }
+ raise ArgumentError, "#{ext.inspect}'s Request or Response not a decendant of OpenID::Extension"
+ end
+
+ if not ext::NS_URI.is_a? String
+ raise TypeError, "#{ext.inspect}'s NS_URI is not a string"
+ elsif not uri = URI(ext::NS_URI)
+ raise ArgumentError, "#{ext.inspect}'s NS_URI is not a valid uri"
+ elsif not uri.scheme =~ /^https?$/
+ raise ArgumentError, "#{ext.inspect}'s NS_URI is not an http uri"
+ elsif not uri.absolute?
+ raise ArgumentError, "#{ext.inspect}'s NS_URI is not and absolute uri"
end
@extensions[ext] = args
return ext::NS_URI
View
57 test/spec_rack_auth_openid.rb
@@ -53,43 +53,76 @@
specify 'extensions should be a module' do
ext = Object.new
- lambda{OID.new(realm).add_extension(ext)}.should.raise TypeError
+ lambda{OID.new(realm).add_extension(ext)}.
+ should.raise(TypeError).
+ message.should.match(/not a module/)
ext2 = Module.new
- lambda{OID.new(realm).add_extension(ext2)}.should.raise ArgumentError
+ lambda{OID.new(realm).add_extension(ext2)}.
+ should.raise(ArgumentError).
+ message.should.not.match(/not a module/)
end
specify 'extensions should have required constants defined' do
ext = Module.new
- lambda{OID.new(realm).add_extension(ext)}.should.raise ArgumentError
+ lambda{OID.new(realm).add_extension(ext)}.
+ should.raise(ArgumentError).
+ message.should.match(/missing/)
ext::Request = nil
- lambda{OID.new(realm).add_extension(ext)}.should.raise ArgumentError
+ lambda{OID.new(realm).add_extension(ext)}.
+ should.raise(ArgumentError).
+ message.should.match(/missing/).
+ should.not.match(/Request/)
ext::Response = nil
- lambda{OID.new(realm).add_extension(ext)}.should.raise ArgumentError
+ lambda{OID.new(realm).add_extension(ext)}.
+ should.raise(ArgumentError).
+ message.should.match(/missing/).
+ should.not.match(/Response/)
ext::NS_URI = nil
- lambda{OID.new(realm).add_extension(ext)}.should.raise TypeError
+ lambda{OID.new(realm).add_extension(ext)}.
+ should.raise(TypeError).
+ message.should.not.match(/missing/)
end
specify 'extensions should have Request and Response defined and inherit from OpenID::Extension' do
ext = Module.new
ext::Request = nil
ext::Response = nil
ext::NS_URI = nil
- lambda{OID.new(realm).add_extension(ext)}.should.raise TypeError
+ lambda{OID.new(realm).add_extension(ext)}.
+ should.raise(TypeError).
+ message.should.match(/not a class/)
+ ext::Request = Class.new()
+ lambda{OID.new(realm).add_extension(ext)}.
+ should.raise(TypeError).
+ message.should.match(/not a class/)
+ ext::Response = Class.new()
+ lambda{OID.new(realm).add_extension(ext)}.
+ should.raise(ArgumentError).
+ message.should.match(/not a decendant/)
ext::Request = Class.new(::OpenID::Extension)
- lambda{OID.new(realm).add_extension(ext)}.should.raise TypeError
+ lambda{OID.new(realm).add_extension(ext)}.
+ should.raise(ArgumentError).
+ message.should.match(/not a decendant/)
ext::Response = Class.new(::OpenID::Extension)
- lambda{OID.new(realm).add_extension(ext)}.should.raise TypeError
+ lambda{OID.new(realm).add_extension(ext)}.
+ should.raise(TypeError).
+ message.should.match(/NS_URI/)
end
specify 'extensions should have NS_URI defined and be a string of an absolute http uri' do
ext = Module.new
ext::Request = Class.new(::OpenID::Extension)
ext::Response = Class.new(::OpenID::Extension)
ext::NS_URI = nil
- lambda{OID.new(realm).add_extension(ext)}.should.raise TypeError
+ lambda{OID.new(realm).add_extension(ext)}.
+ should.raise(TypeError).
+ message.should.match(/not a string/)
ext::NS_URI = 'openid.net'
- lambda{OID.new(realm).add_extension(ext)}.should.raise ArgumentError
+ lambda{OID.new(realm).add_extension(ext)}.
+ should.raise(ArgumentError).
+ message.should.match(/not an http uri/)
ext::NS_URI = 'http://openid.net'
- lambda{OID.new(realm).add_extension(ext)}.should.not.raise
+ lambda{OID.new(realm).add_extension(ext)}.
+ should.not.raise
end
end

0 comments on commit 49ebc8b

Please sign in to comment.