a small gem that allows you to easily hammer your site with requests to all your routes, to give you confidence that the site is not breaking anywhere. It is not a load test, nor is it meant to do functional or unit testing, instead, it's sole focus is to ensure that there are no rails errors anywhere on your site. It is very easy to work with a…
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
rails
.gitignore
MIT-LICENSE
README.md
Rakefile
acts_as_qa.gemspec

README.md

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

Fallowing 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 datatypes 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

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 funcationality 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 datacase.
  • 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 parametes (*3 or *4 in parameters) then it overwrites the 'repete' for that action

Result

Response from each action has the following format:

GET: /users/confirmation [OK] if parameters are {:id=>""}
GET: /users/confirmation [OK] if parameters are {:id=>0.8721885707014159}
GET: /users/confirmation [OK] if parameters are {:id=>true}
GET: /crib_sheets [NOTFOUND] if parameters are {}
GET: /crib_sheets/3 [NOTFOUND] if parameters are {:id=>3}
GET: /crib_sheets/16542 [NOTFOUND] if parameters are {:id=>16542}
GET: /crib_sheets/0.7283873076508682 [NOTFOUND] if parameters are {:id=>0.7283873076508682}
GET: /crib_sheets/3 [NOTFOUND] if parameters are {:id=>3}
GET: /crib_sheets/ [NOTFOUND] if parameters are {:id=>nil}
GET: /crib_sheets/ [NOTFOUND] if parameters are {:id=>nil}
GET: /dashboard.js [FAIL] FAILS WITH STATUS Net::HTTPUnauthorized]  if parameters are {:id=>0.4012952405196911, :format=>"js"}
GET: /dashboard.js [FAIL] FAILS WITH STATUS Net::HTTPUnauthorized]  if parameters are {:id=>"J50h3RQQxVJmRBkw", :format=>"js"}
GET: /dashboard.js [FAIL] FAILS WITH STATUS Net::HTTPUnauthorized]  if parameters are {:id=>false, :format=>"js"}
GET: /dashboard.html [OK] if parameters are {:id=>0.14420009413931612, :format=>"html"}
GET: /dashboard.html [OK] if parameters are {:id=>nil, :format=>"html"}
GET: /dashboard.html [OK] if parameters are {:id=>false, :format=>"html"}
GET: /about_us [OK] if parameters are {} 

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