Ruby on Rails Mock For Testing OpenId 5
I know that everybody using Rails has a thorough and robust testing suite to go with their app.
When I went to implement OpenId in my app. I used the open_id_authentication plugin and this excellent tutorial.
Testing of my controllers became an issue though. I didn’t want to set up OpenId’s for each of my test users. As a solution I created the following mock object.
Take this code and put it into a file called open_id_authentication_mock.rb.
module OpenIdAuthentication
EXTENSION_FIELDS = {'email' => 'user@somedomain.com',
'nickname' => 'cool_user',
'country' => 'US',
'postcode' => '12345',
'fullname' => 'Cool User',
'dob' => '1970-04-01',
'language' => 'en',
'timezone' => 'America/New_York'}
protected
def authenticate_with_open_id(identity_url = params[:openid_url], fields = {}) #:doc:
identity_url = normalize_url(identity_url)
if User.find_by_openid_url(identity_url) || identity_url.include?('good')
# Don't process registration fields unless it is requested.
unless identity_url.include?('blank') || (fields[:required].nil? && fields[:optional].nil?)
extension_response_fields = {}
# fields[:required].each do |field|
# extension_response_fields[field.to_s] = EXTENSION_FIELDS[field.to_s]
# end
fields[:optional].each do |field|
extension_response_fields[field.to_s] = EXTENSION_FIELDS[field.to_s]
end
end
yield Result[:successful], identity_url , extension_response_fields
else
logger.info "OpenID authentication failed: #{identity_url}"
yield Result[:failed], identity_url, nil
end
end
private
def add_simple_registration_fields(open_id_response, fields)
open_id_response.add_extension_arg('sreg', 'required', [ fields[:required] ].flatten * ',') if fields[:required]
open_id_response.add_extension_arg('sreg', 'optional', [ fields[:optional] ].flatten * ',') if fields[:optional]
end
end
Take this code and put it into a file called open_id_authentication_mock.rb. This mock is not a wholesale replacement for open_id_authentication.rb in the plugin. That file has the Result class and other code that is needed in the functional tests. I don’t want to have to maintain that code as it evolves in the future.
Add the following line to your test_helper.rb
load File.join(RAILS_ROOT,'test', 'mocks', 'test', 'open_id_authentication_mock.rb')
This will cause the mock’s methods to be loaded and to replace those in the plugin.
This mock does a little more than just stub out the plugin’s functionality. It will return a successful result if the requested openid_url is in the database, or if the url has the word ‘good’ in any part of it. This allows you to structure positive and negative tests through the contents of the url.
The mock will also return a set of static results if simple registration fields are requested. This met my needs, I’ll be happy to post a change to this if somebody wants to make this part more robust.
I hope all of this is clear and helpful. Comment below with any suggestions.
Steve
Trackbacks
Use the following link to trackback from your own site:
http://www.northpub.com/trackbacks?article_id=testing-openid-support&day=02&month=04&year=2007

This is just what I needed. Thank you for not making figure this out myself.
That worked great, thanks!
Thanks Steve, that was very helpful.
Thanks Steve, that was very helpful.
It approves anyone automatically, whatever you put in the last part of the URL.
One caveat—it does the Wrong Thing with the nickname field (rather than providing the tail of the url it sends some mondo string).