Programming
Functional tests using Devise gem and Declarative authorization gem
April 9, 2013
0

Hello,

During these past days, I’ve been busy in writing test methods to check the reliability of a web application I’m working on, and I had to face many different problems; the main one was gathering information about writing good tests and the tools used to do it, while another one was how to test requests to third-party web services and test the response evaluation process.

First of all, I’m talking about a functional test,  whose purpose is to evaluate the reliability of a RAILS controller’s method. When you use RAILS’ scaffolding process,functional  test templates, with some really basic methods, are generated under the folder

/test/functional

In it you will find a file called “your_controller_name_test.rb” with some basic test methods already there.

Writing good tests is not an easy process – not the topic of this post – and a good place to start could be RAILS official guides:

http://guides.rubyonrails.org/testing.html

What I would like to focus on is: how do I test when my web application uses Devise gem and declarative authorization?

First of all, I assume that you have already prepared fixtures with some database data; if not, navigate to:

/lib/tasks/

and create a file called “fixtures.rake” and paste the following code in it:

namespace :db do
  namespace :fixtures do
    desc 'Create a fixtures entry in the default fixtures folder, use a model name as entry and limiting to first 100 results'
    task :dump => :environment do
      sql = "SELECT * FROM %s LIMIT 100"
      table_name = ENV['TABLE']
      ActiveRecord::Base.establish_connection(:development)
      if table_name.blank?
         puts "please enter a TABLE= to start"
      else
         output_dir = "#{Rails.root}/test/fixtures"
         i = '000'
         File.open("#{output_dir}/#{table_name}.yml","w") do |file|
           data = ActiveRecord::Base.connection.select_all(sql % table_name)
           file.write data.inject({}) { |hash,record|
             hash["#{table_name}_#{i.succ!}"] = record
             hash
           }.to_yaml
           puts "wrote #{table_name} to #{output_dir}/"
         end
      end
    end 
    end
end

this task can be called from the terminal using:

rake db:fixtures:dump TABLE=your_table_name

This script dumps  the first 100 records in a fixture (a yaml file).

Now you have to add this to your test_helper file, to tell RAILS that you want to use Devise authentication  and Declarative authorization helpers:

require 'declarative_authorization/maintenance'

class Test::Unit::TestCase
  include Authorization::TestHelper
end

class ActionController::TestCase 
  include Devise::TestHelpers 
end

So far so good, now it’s time to log in using Devise and have a suitable role for your test purposes. Open your test file and write a set up method  like this one:

setup do
     #get the first user from users fixtures
     @user = users(:users_001) 
     #sign in using devise test helper method 
     sign_in @user
 end

In this way, every time you start a functional test that needs authentication, you will be signed in as an admin user. If you need to test different role behaviours, take into account to move the sign_in  directly in the test method you’re considering.

Now you can  use declarative post_with,get_with helpers method in the following way:

hashKey = session["warden.user.user.key"][2]
#now use post_with or get_with passing the warden user key needed 
#by declarative authorization to find you user's roles permissions
post_with(@user, :your_method, {:format=>:your_format},{:your_params},\
     {"warden.user.user.key"=>["User", @user.id, hashKey], "whatever else session variable you need"})

Remember to pass to declarative authorization test helper methods the session id of the logged user which is – since Devise is using Warden – the session variable called warden.user.user.key. If you forget this step, your test will fail because of role permissions.

Well, that’s it! Now it’s time to write down your beautiful test methods and have fun!

Enjoy!

Some useful links:

create a mock web service based on your wsdl :

http://www.soapui.org/Getting-Started/mock-services/1-Creating-a-MockService.html

Devise getting started docs:

https://github.com/plataformatec/devise#getting-started

Warden:

https://github.com/hassox/warden

Declarative authorization:

https://github.com/stffn/declarative_authorization

 

Leave a Reply

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close