ActsAsQA
ActsAsQa checks all the actions defined in the application and their responses. It returns success if response is success, redirect or page not found otherwise it returns false.
Requirement
Rails 3
Install as Gem
Open 'Gemfile' and add the following line:
gem 'acts_as_qa'
Now run:
bundle install
Install as plugin
To install as plugin run:
rails plugin install git@github.com:sapnasolutions/sapna-acts_as_qa.git
Getting started
Run the rake task
rake acts_as_qa:setup
This adds parameters for each action in a controller. This rake task also creates a new environment 'acts_as_qa'. this task also adds a before_filter in application_controller.rb to create a logged in user. Open each controller and supply parameters according to requirement of action.
Formats of Parameters
(This step can be skipped initially)
Following are the formats of the parameters:
#QA :a => 'b'
so for show action for posts controller:
#QA :id => Post.first.id
or
#QA :id => :post
or
#QA :id => :integer
When a symbol is passed like ':post' or ':integer', it automatically generates values for parameter. when the symbol is a name of model it first generates attribute of some random model instance. For Example if its ':author_id => :post' then it finds some random but existing 'author_id' from 'Post' Model and then it also generates some random integer, date string, float etc. By this way valid and invalid values can be passed to action which helps to test different type conditions. The possible data types are:
:boolean
:date
:datetime
:decimal
:float
:integer
:string
When an array is passed in parameters it hits the action one by one using each value in array.
#QA :name => ["foo", "bar"]
In the above example it hits the action 2 times with ":name => 'foo'" and ":name => 'bar'". Similarly To test a action for user logged in and logged out add :user in parameters.
#QA :user => [:user, :admin, nil]
so the values are ':user' when normal user is logged in ':admin' when an administrator is logged in and 'nil' when user is not logged in. To make this work add logic inside 'set_current_user_for_qa' filter in application_controller.rb, generated by ActsAsQa.
Add ':format' in parameters to test different formats of a action
#QA :format => ['js', 'json', 'xml']
Also to hit the action multiple times with different random data add '*repeat'. For example to repeat 3 times add '*3' at the end of parameters.
#QA :a => 'b', c => 'd' *3
Create Logged in user
(This step can be skipped initially)
To test the conditions when user is logged in a user must be set in session before hitting the desired action. To do so open 'application_controller.rb' where following code is automatically generated by the gem:
#Generated by acts as qa
before_filter :set_current_user_for_qa
def set_current_user_for_qa
if Rails.env=='acts_as_qa'
#session[:user_id]=1
end
end
Add logic to create a user session before hitting the action. Modify this code according to functionality of user login. For example:
def set_current_user_for_qa
session[:user_id]=1 params[:user]=='admin'
session[:user_id]=2 params[:user]=='user'
end
or
def set_current_user_for_qa
session[:user]=User.find(1) params[:user]=='admin'
session[:user]=User.find(2) params[:user]=='user'
end
Where ':user_id' or ':user' should exist in database. Similarly if devise gem is used for authentication Add following line in the code:
session["warden.user.user.key"] = ["User", [15], "$2a$10$RSVEtVgr4UGwwnbGNPn9se"]
Initialize Database
WARNING : ActsAsQa can edit/delete/update records of database
As ActsAsQa can edit/delete/update records of database so there are two option to proceed.
OPTION 1:
- Take a dump of development database.
- Run server in 'development' environment.
- Run 'rake acts_as_qa:hit_paths[ROOT_URL,repeat]'
- Copy back the old database.
OPTION 2:
- 'rake acts_as_qa:setup' task creates 'acts_as_qa' environment and 'YOUR_APP_NAME_acts_as_qa' database. Run 'rake db:migrate RATLS_ENV=acts_as_qa'
- Add Records to 'YOUR_APP_NAME_acts_as_qa' database by coping data from fixtures or development database
- Run server in 'acts_as_qa' environment
- Run 'rake acts_as_qa:hit_paths[ROOT_URL,repeat]'
Where ROOT_URL can be 'http://localhost:3000' and repeat is a integer 1,2,3.... Which is no of times a action should be hit by ActsAsQa. If number of times to hit a action is already supplied in parameters (*3 or *4 in parameters) then it overwrites the 'repeat' for that action
Result
Response from each action has the following format:
GET: /admin/lessons/:lesson_id/videos/new [OK] if parameters are {}
GET: /admin/lessons/:lesson_id/videos [OK] if parameters are {}
GET: /admin/lessons/:lesson_id/videos/11 [OK] if parameters are {:id=>11}
GET: /admin/lessons/:lesson_id/videos/112 [OK] if parameters are {:id=>112}
POST: /admin/lessons/:lesson_id/videos/sort [OK] if parameters are {}
GET: /annotations [NOTFOUND] if parameters are {}
GET: /users/confirmation/new [FAIL] FAILS WITH STATUS Net::HTTPBadGateway] if parameters are {}
POST: /users/confirmation [FAIL] FAILS WITH STATUS Net::HTTPBadGateway] if parameters are {}
GET: /users/confirmation [FAIL] FAILS WITH STATUS Net::HTTPBadGateway] if parameters are {:id=>""}
GET: /crib_sheets [NOTFOUND] if parameters are {}
GET: /crib_sheets/24 [NOTFOUND] if parameters are {:id=>24}
GET: /crib_sheets/16 [NOTFOUND] if parameters are {:id=>16}
Uninstall
To uninstall first run:
rake acts_as_qa:remove
This task removes all the parameters added in controller, 'acts_as_qa' environment and the filter added in application_controller.rb Remove the following line in 'Gemfile'
gem 'acts_as_qa'
or remove the plugin from '/vendor/plugins/'
Note
Add necessary logic in set_current_user_for_qa filter if server is in development mode as set_current_user_for_qa is a before filter it overwrites the user session. One solution is to pass ":acts_as_qa => 'true'" in parameters and set user session only if 'params[:acts_as_qa]' is true. So...
def set_current_user_for_qa
if params[:acts_as_qa]==true
session[:user_id]=1 params[:user]=='admin'
session[:user_id]=2 params[:user]=='user'
end
end