Ruby on Rails Mock For Testing OpenId 5

Posted by Steven Hammond Tue, 03 Apr 2007 01:18:00 GMT

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

Can I Only Support Open Id? 3

Posted by Steven Hammond Mon, 02 Apr 2007 07:22:00 GMT

I am putting the finishing touches on a web site to provide some tools and material for table top role playing games.

I am leaning towards only supporting login through OpenID.

Why, you ask? What about users that don’t have OpenIds?

That’s a good question. DHH spelled out the advantages of OpenId here. This is why I’m supporting. I could choose to offer traditional registration with a userid and password, managed by me. From my perspective, that’s more code that I have to maintain and it undoes many of the advantages I was supposed to gain by offering OpenId.

From a users perspective, if they don’t have an OpenId they will have to fill out a registration form on my site. There isn’t much additional effort to have them to get an OpenId which they can use on many sites.

In fact I can use myopenid’s affliate site to make it even easier for them.

By only supporting OpenId everybody wins. I don’t have to manage passwords and logins and related code and users, if they have to signup, get a shiney new OpenID.

Comment below and let me know where I’m wrong?

Steve