annotate test/mocks/open_id_authentication_mock.rb @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children cbb26bc654de
rev   line source
Chris@0 1 # Mocks out OpenID
Chris@0 2 #
Chris@0 3 # http://www.northpub.com/articles/2007/04/02/testing-openid-support
Chris@0 4 module OpenIdAuthentication
Chris@0 5
Chris@0 6 EXTENSION_FIELDS = {'email' => 'user@somedomain.com',
Chris@0 7 'nickname' => 'cool_user',
Chris@0 8 'country' => 'US',
Chris@0 9 'postcode' => '12345',
Chris@0 10 'fullname' => 'Cool User',
Chris@0 11 'dob' => '1970-04-01',
Chris@0 12 'language' => 'en',
Chris@0 13 'timezone' => 'America/New_York'}
Chris@0 14
Chris@0 15 protected
Chris@0 16
Chris@0 17 def authenticate_with_open_id(identity_url = params[:openid_url], options = {}) #:doc:
Chris@0 18 if User.find_by_identity_url(identity_url) || identity_url.include?('good')
Chris@0 19 # Don't process registration fields unless it is requested.
Chris@0 20 unless identity_url.include?('blank') || (options[:required].nil? && options[:optional].nil?)
Chris@0 21 extension_response_fields = {}
Chris@0 22
Chris@0 23 options[:required].each do |field|
Chris@0 24 extension_response_fields[field.to_s] = EXTENSION_FIELDS[field.to_s]
Chris@0 25 end unless options[:required].nil?
Chris@0 26
Chris@0 27 options[:optional].each do |field|
Chris@0 28 extension_response_fields[field.to_s] = EXTENSION_FIELDS[field.to_s]
Chris@0 29 end unless options[:optional].nil?
Chris@0 30 end
Chris@0 31
Chris@0 32 yield Result[:successful], identity_url , extension_response_fields
Chris@0 33 else
Chris@0 34 logger.info "OpenID authentication failed: #{identity_url}"
Chris@0 35 yield Result[:failed], identity_url, nil
Chris@0 36 end
Chris@0 37 end
Chris@0 38
Chris@0 39 private
Chris@0 40
Chris@0 41 def add_simple_registration_fields(open_id_response, fields)
Chris@0 42 open_id_response.add_extension_arg('sreg', 'required', [ fields[:required] ].flatten * ',') if fields[:required]
Chris@0 43 open_id_response.add_extension_arg('sreg', 'optional', [ fields[:optional] ].flatten * ',') if fields[:optional]
Chris@0 44 end
Chris@0 45 end