PageRenderTime 30ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/features/step_definitions/api_steps.rb

https://github.com/coggy/RapidFTR
Ruby | 173 lines | 133 code | 33 blank | 7 comment | 70 complexity | 052bf5c738b6b10680028f58f3662288 MD5 | raw file
  1. When /^I make a request for (.+)$/ do |resource|
  2. visit path_to(resource)
  3. end
  4. And /^that JSON list of elements has these properties:$/ do |properties_table|
  5. json_response = JSON.parse(response_body)
  6. json_response.each do |item|
  7. item.keys.length.should == properties_table.rows.count
  8. properties_table.rows.each do |property|
  9. lambda {item.has_key? property}.should be_true
  10. end
  11. end
  12. end
  13. And /^that JSON hash of elements has these properties:$/ do |properties_table|
  14. json_response = JSON.parse(response_body)
  15. properties_table.rows.each do |property|
  16. lambda {json_response.has_key? property}.should be_true
  17. end
  18. end
  19. And /^that JSON response should be composed of items like (.+)$/ do |json_expectation_string|
  20. json_expectation = JSON.parse(json_expectation_string)
  21. json_response = JSON.parse(response_body)
  22. json_response.each do |item|
  23. json_expectation.keys.each do |expectation_key|
  24. item_valid(item, json_expectation, expectation_key)
  25. end
  26. end
  27. end
  28. Then /^that JSON response should be an item like$/ do |json_expectation_string|
  29. json_expectation = JSON.parse(json_expectation_string)
  30. json_expectation.keys.each do |expectation_key|
  31. item_valid(JSON.parse(response_body), json_expectation, expectation_key)
  32. end
  33. end
  34. def item_valid(item, json_expectation, expectation_key)
  35. lambda {item.has_key? expectation_key}.should be_true
  36. match_value(item[expectation_key], json_expectation[expectation_key])
  37. end
  38. And /^that JSON response should be composed of items with body$/ do |json_expectation_string|
  39. json_expectation = JSON.parse(json_expectation_string)
  40. json_response = JSON.parse(response_body)
  41. json_response.each do |item|
  42. json_expectation.keys.each do |expectation_key|
  43. lambda {item.has_key? expectation_key}.should be_true
  44. match_value(item[expectation_key], json_expectation[expectation_key])
  45. end
  46. end
  47. end
  48. Then /^I receive a JSON hash$/ do
  49. json_response = JSON.parse(response_body)
  50. json_response.class.should == Hash
  51. end
  52. Then /^I receive a JSON array$/ do
  53. json_response = JSON.parse(response_body)
  54. json_response.class.should == Array
  55. end
  56. And /^that (.+) should be composed of (.+) elements$/ do |type, num_elements|
  57. json_response = JSON.parse(response_body)
  58. json_response.count.should == num_elements.to_i
  59. end
  60. Then /^I receive a JSON hash of (.+) elements$/ do |num_elements|
  61. json_response = JSON.parse(response_body)
  62. json_response.class.should == Hash
  63. json_response.count.should == num_elements.to_i
  64. end
  65. Then /^I receive a JSON response:$/ do |table|
  66. expected = table.hashes.first
  67. json_response = JSON.parse(response_body)
  68. json_response.class.should == Hash
  69. expected.each {|key,value| json_response[key].should == value}
  70. end
  71. When /^I login with user (.+):(.+) for device with imei (.+)$/ do |user, password, imei|
  72. post(sessions_path, {:imei => imei, :user_name => user, :password => password, :mobile_number => "123456", :format => 'json'})
  73. end
  74. def check_field_validity(input_field)
  75. return_val = true
  76. return_val = return_val && (input_field.has_key? "name") && match_value(input_field["name"], "%SOME_STRING%")
  77. return_val = return_val && (input_field.has_key? "enabled") && match_value(input_field["enabled"], "%SOME_BOOL%")
  78. return_val = return_val && (input_field.has_key? "editable") && match_value(input_field["editable"], "%SOME_BOOL%")
  79. return_val = return_val && (input_field.has_key? "type") && match_value(input_field["type"], "%SOME_FIELD_TYPE%")
  80. if (input_field["type"] == "select_box")
  81. return_val = return_val && (input_field.has_key? "option_strings") && (input_field["option_strings"].class == Array)
  82. end
  83. return_val = return_val && (input_field.has_key? "display_name") && match_value(input_field["display_name"], "%SOME_STRING%")
  84. return return_val
  85. end
  86. def check_field_array_validity (input_field_array)
  87. input_field_array.each{|field| if check_field_validity(field) == false then return false end}
  88. return true
  89. end
  90. def match_value (input, match_text)
  91. # puts input
  92. # puts match_text
  93. #check simple string match?
  94. checkval = (input == match_text || match_text == "%SOME_STRING%" )
  95. #check for boolean?
  96. checkval = checkval || (match_text == "%SOME_BOOL%" && (input == true || input == false))
  97. #check for integer?
  98. checkval = checkval || (match_text == "%SOME_INTEGER%" && !!(input.to_s() =~ /^[-+]?[0-9]+$/))
  99. #check for fields array?
  100. checkval = checkval || (match_text == "%SOME_FIELD_ARRAY%" && input.class.should == Array && check_field_array_validity(input))
  101. #check for field type?
  102. checkval = checkval || (match_text == "%SOME_FIELD_TYPE%" && (input == "text_field" || input == "select_box" || input == "textarea" || input == "photo_upload_box" || input == "audio_upload_box" || input == "radio_button" || input == "check_box" || input == "numeric_field" || input == "date_field"))
  103. checkval.should be_true
  104. end
  105. Then /^should be kill response for imei "(.+)"$/ do |imei|
  106. response.status.should =~ /403/
  107. response_body.should == imei
  108. end
  109. Then /^should be successful login$/ do
  110. response_body.should_not =~ /sessions\/new/
  111. end
  112. And /^I am using device with imei "(.+)"$/ do |imei|
  113. session = Session.for_user(@user, nil)
  114. session.imei = imei
  115. session.save!
  116. end
  117. When /^I create the following child:$/ do |table|
  118. params={}
  119. params[:format] ||= 'json'
  120. visit children_path(params), :post, {:child => table.rows_hash}
  121. end
  122. When /^I edit the following child:$/ do |input_json|
  123. input_child_hash = JSON.parse(input_json)
  124. visit "/children/" + input_child_hash["_id"], :put, {:child => input_child_hash, :format => 'json'}
  125. end
  126. When /^I request for child with ID (\d+)$/ do |id|
  127. visit "/children/" + id, :get, {:format => 'json'}
  128. end
  129. Then /^I should get back a response saying null$/ do
  130. response_body.should == "null"
  131. end
  132. Then /^the following child should be returned:$/ do |table|
  133. json_response = JSON.parse(response_body)
  134. table.rows_hash.each do |key,value|
  135. json_response[key].should == value
  136. end
  137. end
  138. When /^I request for the picture of the child with ID (\d+) and square dimensions of (\d+) pixels$/ do |id, dimensions|
  139. visit "/children/"+id+"/resized_photo/" + dimensions, :get, nil
  140. end