Module: ActsAsQA::AAQA

Defined in:
lib/acts_as_qa/acts_as_qa.rb

Constant Summary collapse

@@result =
{}

Instance Method Summary collapse

Instance Method Details

#check_response(response, parameters, method, path, p) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/acts_as_qa/acts_as_qa.rb', line 246

def check_response(response, parameters, method, path, p)
  if(response.class.ancestors.include?(Net::HTTPOK) || response.class.ancestors.include?(Net::HTTPFound))
    request_add_to_result(parameters[:controller], parameters[:action], method, true)
    puts(ActsAsQA::Display.colorize("#{method}: #{path} [OK] if parameters are #{p}", 32))
  elsif(response.class.ancestors.include?(Net::HTTPNotFound))
    request_add_to_result(parameters[:controller], parameters[:action], method, true)
    puts(ActsAsQA::Display.colorize("#{method}: #{path} [NOTFOUND] if parameters are #{p}", 36))
  else
    request_add_to_result(parameters[:controller], parameters[:action], method, false)
    puts(ActsAsQA::Display.colorize("#{method}: #{path} [FAIL] FAILS WITH STATUS #{response.class}]  if parameters are #{p}", 31))
  end
end

#display_error(path) ⇒ Object

Error message if path doesn’t exixt.



320
321
322
# File 'lib/acts_as_qa/acts_as_qa.rb', line 320

def display_error(path)
   puts ActsAsQA::Display.colorize("PATH DOES NOT EXIST IN ROUTES #{path.inspect}", 31)
end

#fetch_controller_parametersObject

Fetch all the QA parameters defined in the controller and generates the path.



170
171
172
173
174
175
176
177
178
# File 'lib/acts_as_qa/acts_as_qa.rb', line 170

def fetch_controller_parameters
  generate_routes_file
  controller_files, paths_to_hit = fetch_controllers, []
  controller_files.each do |file_name|
    file, controller = File.expand_path(file_name, "#{Rails.root}/app/controllers"), file_name.split('_controller').first
    paths_to_hit+=read_file_and_find_paths(file, controller)
  end  # end of controller_files iterator
  paths_to_hit
end

#form_request(method, uri, p, data) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/acts_as_qa/acts_as_qa.rb', line 272

def form_request(method, uri, p, data)
  case method
    when 'POST'
      request = Net::HTTP::Post.new(uri.path)
      request.set_form_data(p)
    when 'PUT'
      request = Net::HTTP::Put.new(uri.path)
      request.set_form_data(p)
    when 'GET'
      request = Net::HTTP::Get.new(uri.path+"?"+data)
    when 'DELETE'
      request = Net::HTTP::Delete.new(uri.path+"?"+data)
  end
  request
end

#generate_parameters_and_hit_path(root_url, path) ⇒ Object



210
211
212
213
214
215
# File 'lib/acts_as_qa/acts_as_qa.rb', line 210

def generate_parameters_and_hit_path(root_url, path)
  parameters_list = ActsAsQA::Generate.instance_eval(path.chomp)
  parameters_list.each do |parameters|
    validate_parameters_and_hit_path(parameters, root_url, path)
  end
end

#generate_routes_fileObject

Generates a routes file so that it can compare the routes with actual routes.



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/acts_as_qa/acts_as_qa.rb', line 139

def generate_routes_file
  home_directory = File.expand_path(".acts_as_qa", Rails.root)
  unless Dir.exists?(home_directory)
    status = Dir.mkdir('.acts_as_qa', 0777)
    status == 0 ? puts("Home Directory created") : puts("Home Directory could not be created.")
  else
    puts "Home directory exists."       
  end 
  filename = File.expand_path('routes.txt', "#{Rails.root}/.acts_as_qa")
  status=system("rake routes > #{filename}") ? puts('Routes Loaded') : puts("Something went wrong")
end

#get_response(specifications, parameters, root_url, method) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/acts_as_qa/acts_as_qa.rb', line 259

def get_response(specifications, parameters, root_url, method)
  p = path = nil
  specifications.length <= 4 ? path = specifications[1] : path = specifications[2]
  p, data = (parameters[:parameters] || []), []
  p.each{|k,v| path.match(":#{k}") ? (path.gsub!(":#{k}", v.to_s)) : data << "#{k}=#{v}"}
  data, path = data.join('&'), path.split("(.:format")[0].gsub(/\(|\)/, '')
  uri = URI.parse("#{root_url}#{path}")
  http = Net::HTTP.new(uri.host, uri.port) 
  request = form_request(method, uri, p, data)
  response = http.request(request)
  [response, path, p]
end

#hit_path(root_url, repeat_global) ⇒ Object

Finds the parameters and send the request and catch the request.



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/acts_as_qa/acts_as_qa.rb', line 196

def hit_path(root_url, repeat_global)
  @@repeat_global=repeat_global
  puts "please enter root_url (example: http://localhost)" and return unless root_url 
  paths_to_hit = fetch_controller_parameters
  paths_to_hit.each do |path|
    begin
      generate_parameters_and_hit_path(root_url, path)
    rescue Exception => e
      puts ActsAsQA::Display.colorize("Wrong parameters for #{path}. Error: #{e}", 31)
    end
  end # end of paths to hit
  show_result
end

#hit_valid_paths(path_details, parameters, root_url, path) ⇒ Object



231
232
233
234
235
236
# File 'lib/acts_as_qa/acts_as_qa.rb', line 231

def hit_valid_paths(path_details, parameters, root_url, path)
  path_details.each do |path_detail|
    path_specifications =  path_detail.split().inspect unless path_detail.blank?
    send_request(JSON.parse(path_specifications), parameters, root_url)
  end
end

#read_file_and_find_paths(file, controller) ⇒ Object

end of method



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/acts_as_qa/acts_as_qa.rb', line 180

def read_file_and_find_paths(file, controller)
  paths_to_hit = []
  File.open(file, 'r') do |f|
    action=''
    while(line = f.gets)
      action = line.split('def').last.split(';').first.strip if line.include?('def')
      if line.include?('#QA')
        parameters, times = line.split('#QA ').last.strip.split('*')
        paths_to_hit << "qa({:controller => '#{controller}', :action => '#{action}', :parameters => {#{parameters}}}, #{times||@@repeat_global})" 
      end
    end # end of while
  end # end of file open
  paths_to_hit
end

#request_add_to_result(controller, action, method, pass) ⇒ Object

creates no of requests, pass/fail result set for all paths.



289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/acts_as_qa/acts_as_qa.rb', line 289

def request_add_to_result(controller, action, method, pass)
  if !@@result[controller]     
    @@result.merge!({controller => {action => {method => {:requests => 1, :pass => (pass ? 1 : 0), :fail => (pass ? 0 : 1)}}}})
  elsif !@@result[controller][action]
    @@result[controller].merge!({action => {method => {:requests => 1, :pass => (pass ? 1 : 0), :fail => (pass ? 0 : 1)}}})
  elsif !@@result[controller][action][method]
    @@result[controller][action].merge!({method => {:requests => 1, :pass => (pass ? 1 : 0), :fail => (pass ? 0 : 1)}}) 
  else
    @@result[controller][action][method][:requests] += 1
    @@result[controller][action][method][pass ? :pass : :fail] += 1
  end
end

#send_request(specifications, parameters, root_url) ⇒ Object

Send the request



239
240
241
242
243
244
# File 'lib/acts_as_qa/acts_as_qa.rb', line 239

def send_request(specifications, parameters, root_url)
  puts "please enter root_url (example: http://localhost)" and return unless root_url
  method = (specifications.length <= 4 ? 'GET' : specifications[1])
  response, path, p = get_response(specifications, parameters, root_url, method)
  check_response(response, parameters, method, path, p)
end

#show_resultObject

Show the result.



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/acts_as_qa/acts_as_qa.rb', line 303

def show_result
  puts "-"*145+"\n Controller"+" "*40+"Action"+" "*24+"Method"+" "*14+"Total Requests"+" "*6+"Pass Requests"+" "*7+"Fail Requests\n"+"-"*145
  total=pass=fail=0
  @@result.each do |controller, x|
    x.each do |action, y|
      y.each do |method, z|
        total+=z[:requests]
        pass+=z[:pass]
        fail+=z[:fail]
        puts controller+" "*(50-controller.length)+action+" "*(30-action.length)+method+" "*(20-method.length)+z[:requests].to_s+" "*(20-z[:requests].to_s.length)+z[:pass].to_s+" "*(20-z[:pass].to_s.length)+z[:fail].to_s
      end
    end
  end
  puts "-"*145+"\n"+" "*100+total.to_s+" "*(20-total.to_s.length)+pass.to_s+" "*(20-pass.to_s.length)+fail.to_s+" "*(20-fail.to_s.length)+"\n"+"-"*145
end

#validate_parameters_and_hit_path(parameters, root_url, path) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/acts_as_qa/acts_as_qa.rb', line 217

def validate_parameters_and_hit_path(parameters, root_url, path)
  File.open("#{Rails.root}/.acts_as_qa/routes.txt", 'r') do |f|
    path_details = []
    while(line = f.gets)
      path_details << line if (line.include?(":controller=>\"#{parameters[:controller]}\"") && line.include?(":action=>\"#{parameters[:action]}\""))
    end # end of while
    if path_details.empty?
      display_error(path)
      next
    end
    hit_valid_paths(path_details, parameters, root_url, path)
  end# end of file open
end