Keine Beschreibung http://j1x-huginn.herokuapp.com

post_agent_spec.rb 7.3KB

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