post_agent_spec.rb 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. require 'spec_helper'
  2. describe Agents::PostAgent do
  3. before do
  4. @valid_options = {
  5. 'post_url' => "http://www.example.com",
  6. 'expected_receive_period_in_days' => 1,
  7. 'payload' => {
  8. 'default' => 'value'
  9. }
  10. }
  11. @valid_params = {
  12. name: "somename",
  13. options: @valid_options
  14. }
  15. @checker = Agents::PostAgent.new(@valid_params)
  16. @checker.user = users(:jane)
  17. @checker.save!
  18. @event = Event.new
  19. @event.agent = agents(:jane_weather_agent)
  20. @event.payload = {
  21. 'somekey' => 'somevalue',
  22. 'someotherkey' => {
  23. 'somekey' => 'value'
  24. }
  25. }
  26. @requests = 0
  27. @sent_requests = Hash.new { |hash, method| hash[method] = [] }
  28. stub_request(:any, /:/).to_return { |request|
  29. method = request.method
  30. @requests += 1
  31. case method
  32. when :get, :delete
  33. data = request.uri.query
  34. else
  35. if data = request.body
  36. case request.headers['Content-Type'][/\A[^;\s]+/]
  37. when 'application/x-www-form-urlencoded'
  38. # ok
  39. when 'application/json'
  40. data = ActiveSupport::JSON.decode(data)
  41. else
  42. raise "unexpected Content-Type: #{content_type}"
  43. end
  44. end
  45. end
  46. @sent_requests[method] << data
  47. { status: 200, body: "ok" }
  48. }
  49. end
  50. it_behaves_like WebRequestConcern
  51. describe "making requests" do
  52. it "can make requests of each type" do
  53. %w[get put post patch delete].each.with_index(1) do |verb, index|
  54. @checker.options['method'] = verb
  55. @checker.should be_valid
  56. @checker.check
  57. @requests.should == index
  58. @sent_requests[verb.to_sym].length.should == 1
  59. end
  60. end
  61. end
  62. describe "#receive" do
  63. it "can handle multiple events and merge the payloads with options['payload']" do
  64. event1 = Event.new
  65. event1.agent = agents(:bob_weather_agent)
  66. event1.payload = {
  67. 'xyz' => 'value1',
  68. 'message' => 'value2',
  69. 'default' => 'value2'
  70. }
  71. lambda {
  72. lambda {
  73. @checker.receive([@event, event1])
  74. }.should change { @sent_requests[:post].length }.by(2)
  75. }.should_not change { @sent_requests[:get].length }
  76. @sent_requests[:post][0].should == @event.payload.merge('default' => 'value').to_query
  77. @sent_requests[:post][1].should == event1.payload.to_query
  78. end
  79. it "can make GET requests" do
  80. @checker.options['method'] = 'get'
  81. lambda {
  82. lambda {
  83. @checker.receive([@event])
  84. }.should change { @sent_requests[:get].length }.by(1)
  85. }.should_not change { @sent_requests[:post].length }
  86. @sent_requests[:get][0].should == @event.payload.merge('default' => 'value').to_query
  87. end
  88. it "can skip merging the incoming event when no_merge is set, but it still interpolates" do
  89. @checker.options['no_merge'] = 'true'
  90. @checker.options['payload'] = {
  91. 'key' => 'it said: {{ someotherkey.somekey }}'
  92. }
  93. @checker.receive([@event])
  94. @sent_requests[:post].first.should == { 'key' => 'it said: value' }.to_query
  95. end
  96. end
  97. describe "#check" do
  98. it "sends options['payload'] as a POST request" do
  99. lambda {
  100. @checker.check
  101. }.should change { @sent_requests[:post].length }.by(1)
  102. @sent_requests[:post][0].should == @checker.options['payload'].to_query
  103. end
  104. it "sends options['payload'] as JSON as a POST request" do
  105. @checker.options['content_type'] = 'json'
  106. lambda {
  107. @checker.check
  108. }.should change { @sent_requests[:post].length }.by(1)
  109. @sent_requests[:post][0].should == @checker.options['payload']
  110. end
  111. it "sends options['payload'] as a GET request" do
  112. @checker.options['method'] = 'get'
  113. lambda {
  114. lambda {
  115. @checker.check
  116. }.should change { @sent_requests[:get].length }.by(1)
  117. }.should_not change { @sent_requests[:post].length }
  118. @sent_requests[:get][0].should == @checker.options['payload'].to_query
  119. end
  120. end
  121. describe "#working?" do
  122. it "checks if events have been received within expected receive period" do
  123. @checker.should_not be_working
  124. Agents::PostAgent.async_receive @checker.id, [@event.id]
  125. @checker.reload.should be_working
  126. two_days_from_now = 2.days.from_now
  127. stub(Time).now { two_days_from_now }
  128. @checker.reload.should_not be_working
  129. end
  130. end
  131. describe "validation" do
  132. before do
  133. @checker.should be_valid
  134. end
  135. it "should validate presence of post_url" do
  136. @checker.options['post_url'] = ""
  137. @checker.should_not be_valid
  138. end
  139. it "should validate presence of expected_receive_period_in_days" do
  140. @checker.options['expected_receive_period_in_days'] = ""
  141. @checker.should_not be_valid
  142. end
  143. it "should validate method as post, get, put, patch, or delete, defaulting to post" do
  144. @checker.options['method'] = ""
  145. @checker.method.should == "post"
  146. @checker.should be_valid
  147. @checker.options['method'] = "POST"
  148. @checker.method.should == "post"
  149. @checker.should be_valid
  150. @checker.options['method'] = "get"
  151. @checker.method.should == "get"
  152. @checker.should be_valid
  153. @checker.options['method'] = "patch"
  154. @checker.method.should == "patch"
  155. @checker.should be_valid
  156. @checker.options['method'] = "wut"
  157. @checker.method.should == "wut"
  158. @checker.should_not be_valid
  159. end
  160. it "should validate that no_merge is 'true' or 'false', if present" do
  161. @checker.options['no_merge'] = ""
  162. @checker.should be_valid
  163. @checker.options['no_merge'] = "true"
  164. @checker.should be_valid
  165. @checker.options['no_merge'] = "false"
  166. @checker.should be_valid
  167. @checker.options['no_merge'] = false
  168. @checker.should be_valid
  169. @checker.options['no_merge'] = true
  170. @checker.should be_valid
  171. @checker.options['no_merge'] = 'blarg'
  172. @checker.should_not be_valid
  173. end
  174. it "should validate payload as a hash, if present" do
  175. @checker.options['payload'] = ""
  176. @checker.should be_valid
  177. @checker.options['payload'] = "hello"
  178. @checker.should_not be_valid
  179. @checker.options['payload'] = ["foo", "bar"]
  180. @checker.should_not be_valid
  181. @checker.options['payload'] = { 'this' => 'that' }
  182. @checker.should be_valid
  183. end
  184. it "requires headers to be a hash, if present" do
  185. @checker.options['headers'] = [1,2,3]
  186. @checker.should_not be_valid
  187. @checker.options['headers'] = "hello world"
  188. @checker.should_not be_valid
  189. @checker.options['headers'] = ""
  190. @checker.should be_valid
  191. @checker.options['headers'] = {}
  192. @checker.should be_valid
  193. @checker.options['headers'] = { "Authorization" => "foo bar" }
  194. @checker.should be_valid
  195. end
  196. end
  197. end