post_agent_spec.rb 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. require 'rails_helper'
  2. require 'ostruct'
  3. describe Agents::PostAgent do
  4. before do
  5. @valid_options = {
  6. 'post_url' => "http://www.example.com",
  7. 'expected_receive_period_in_days' => 1,
  8. 'payload' => {
  9. 'default' => 'value'
  10. }
  11. }
  12. @valid_params = {
  13. name: "somename",
  14. options: @valid_options
  15. }
  16. @checker = Agents::PostAgent.new(@valid_params)
  17. @checker.user = users(:jane)
  18. @checker.save!
  19. @event = Event.new
  20. @event.agent = agents(:jane_weather_agent)
  21. @event.payload = {
  22. 'somekey' => 'somevalue',
  23. 'someotherkey' => {
  24. 'somekey' => 'value'
  25. }
  26. }
  27. @requests = 0
  28. @sent_requests = Hash.new { |hash, method| hash[method] = [] }
  29. stub_request(:any, /:/).to_return { |request|
  30. method = request.method
  31. @requests += 1
  32. @sent_requests[method] << req = OpenStruct.new(uri: request.uri)
  33. case method
  34. when :get, :delete
  35. req.data = request.uri.query
  36. else
  37. content_type = request.headers['Content-Type'][/\A[^;\s]+/]
  38. case content_type
  39. when 'application/x-www-form-urlencoded'
  40. req.data = request.body
  41. when 'application/json'
  42. req.data = ActiveSupport::JSON.decode(request.body)
  43. when 'text/xml'
  44. req.data = Hash.from_xml(request.body)
  45. when Agents::PostAgent::MIME_RE
  46. req.data = request.body
  47. else
  48. raise "unexpected Content-Type: #{content_type}"
  49. end
  50. end
  51. { status: 200, body: "<html>a webpage!</html>", headers: { 'Content-type' => 'text/html' } }
  52. }
  53. end
  54. it_behaves_like WebRequestConcern
  55. describe "making requests" do
  56. it "can make requests of each type" do
  57. %w[get put post patch delete].each.with_index(1) do |verb, index|
  58. @checker.options['method'] = verb
  59. expect(@checker).to be_valid
  60. @checker.check
  61. expect(@requests).to eq(index)
  62. expect(@sent_requests[verb.to_sym].length).to eq(1)
  63. end
  64. end
  65. end
  66. describe "#receive" do
  67. it "can handle multiple events and merge the payloads with options['payload']" do
  68. event1 = Event.new
  69. event1.agent = agents(:bob_weather_agent)
  70. event1.payload = {
  71. 'xyz' => 'value1',
  72. 'message' => 'value2',
  73. 'default' => 'value2'
  74. }
  75. expect {
  76. expect {
  77. @checker.receive([@event, event1])
  78. }.to change { @sent_requests[:post].length }.by(2)
  79. }.not_to change { @sent_requests[:get].length }
  80. expect(@sent_requests[:post][0].data).to eq(@event.payload.merge('default' => 'value').to_query)
  81. expect(@sent_requests[:post][1].data).to eq(event1.payload.to_query)
  82. end
  83. it "can make GET requests" do
  84. @checker.options['method'] = 'get'
  85. expect {
  86. expect {
  87. @checker.receive([@event])
  88. }.to change { @sent_requests[:get].length }.by(1)
  89. }.not_to change { @sent_requests[:post].length }
  90. expect(@sent_requests[:get][0].data).to eq(@event.payload.merge('default' => 'value').to_query)
  91. end
  92. it "can make a GET request merging params in post_url, payload and event" do
  93. @checker.options['method'] = 'get'
  94. @checker.options['post_url'] = "http://example.com/a/path?existing_param=existing_value"
  95. @event.payload = {
  96. "some_param" => "some_value",
  97. "another_param" => "another_value"
  98. }
  99. @checker.receive([@event])
  100. uri = @sent_requests[:get].first.uri
  101. # parameters are alphabetically sorted by Faraday
  102. expect(uri.request_uri).to eq("/a/path?another_param=another_value&default=value&existing_param=existing_value&some_param=some_value")
  103. end
  104. it "can skip merging the incoming event when no_merge is set, but it still interpolates" do
  105. @checker.options['no_merge'] = 'true'
  106. @checker.options['payload'] = {
  107. 'key' => 'it said: {{ someotherkey.somekey }}'
  108. }
  109. @checker.receive([@event])
  110. expect(@sent_requests[:post].first.data).to eq({ 'key' => 'it said: value' }.to_query)
  111. end
  112. it "interpolates when receiving a payload" do
  113. @checker.options['post_url'] = "https://{{ domain }}/{{ variable }}?existing_param=existing_value"
  114. @event.payload = {
  115. 'domain' => 'google.com',
  116. 'variable' => 'a_variable'
  117. }
  118. @checker.receive([@event])
  119. uri = @sent_requests[:post].first.uri
  120. expect(uri.scheme).to eq('https')
  121. expect(uri.host).to eq('google.com')
  122. expect(uri.path).to eq('/a_variable')
  123. expect(uri.query).to eq("existing_param=existing_value")
  124. end
  125. end
  126. describe "#check" do
  127. it "sends options['payload'] as a POST request" do
  128. expect {
  129. @checker.check
  130. }.to change { @sent_requests[:post].length }.by(1)
  131. expect(@sent_requests[:post][0].data).to eq(@checker.options['payload'].to_query)
  132. end
  133. it "sends options['payload'] as JSON as a POST request" do
  134. @checker.options['content_type'] = 'json'
  135. expect {
  136. @checker.check
  137. }.to change { @sent_requests[:post].length }.by(1)
  138. expect(@sent_requests[:post][0].data).to eq(@checker.options['payload'])
  139. end
  140. it "sends options['payload'] as XML as a POST request" do
  141. @checker.options['content_type'] = 'xml'
  142. expect {
  143. @checker.check
  144. }.to change { @sent_requests[:post].length }.by(1)
  145. expect(@sent_requests[:post][0].data.keys).to eq([ 'post' ])
  146. expect(@sent_requests[:post][0].data['post']).to eq(@checker.options['payload'])
  147. end
  148. it "sends options['payload'] as XML with custom root element name, as a POST request" do
  149. @checker.options['content_type'] = 'xml'
  150. @checker.options['xml_root'] = 'foobar'
  151. expect {
  152. @checker.check
  153. }.to change { @sent_requests[:post].length }.by(1)
  154. expect(@sent_requests[:post][0].data.keys).to eq([ 'foobar' ])
  155. expect(@sent_requests[:post][0].data['foobar']).to eq(@checker.options['payload'])
  156. end
  157. it "sends options['payload'] as a GET request" do
  158. @checker.options['method'] = 'get'
  159. expect {
  160. expect {
  161. @checker.check
  162. }.to change { @sent_requests[:get].length }.by(1)
  163. }.not_to change { @sent_requests[:post].length }
  164. expect(@sent_requests[:get][0].data).to eq(@checker.options['payload'].to_query)
  165. end
  166. it "sends options['payload'] as a string POST request when content-type continas a MIME type" do
  167. @checker.options['payload'] = '<test>hello</test>'
  168. @checker.options['content_type'] = 'application/xml'
  169. expect {
  170. @checker.check
  171. }.to change { @sent_requests[:post].length }.by(1)
  172. expect(@sent_requests[:post][0].data).to eq('<test>hello</test>')
  173. end
  174. describe "emitting events" do
  175. context "when emit_events is not set to true" do
  176. it "does not emit events" do
  177. expect {
  178. @checker.check
  179. }.not_to change { @checker.events.count }
  180. end
  181. end
  182. context "when emit_events is set to true" do
  183. before do
  184. @checker.options['emit_events'] = 'true'
  185. @checker.save!
  186. end
  187. it "emits the response status" do
  188. expect {
  189. @checker.check
  190. }.to change { @checker.events.count }.by(1)
  191. expect(@checker.events.last.payload['status']).to eq 200
  192. end
  193. it "emits the body" do
  194. @checker.check
  195. expect(@checker.events.last.payload['body']).to eq '<html>a webpage!</html>'
  196. end
  197. it "emits the response headers capitalized by default" do
  198. @checker.check
  199. expect(@checker.events.last.payload['headers']).to eq({ 'Content-Type' => 'text/html' })
  200. end
  201. it "emits the response headers capitalized" do
  202. @checker.options['event_headers_style'] = 'capitalized'
  203. @checker.check
  204. expect(@checker.events.last.payload['headers']).to eq({ 'Content-Type' => 'text/html' })
  205. end
  206. it "emits the response headers downcased" do
  207. @checker.options['event_headers_style'] = 'downcased'
  208. @checker.check
  209. expect(@checker.events.last.payload['headers']).to eq({ 'content-type' => 'text/html' })
  210. end
  211. it "emits the response headers snakecased" do
  212. @checker.options['event_headers_style'] = 'snakecased'
  213. @checker.check
  214. expect(@checker.events.last.payload['headers']).to eq({ 'content_type' => 'text/html' })
  215. end
  216. end
  217. end
  218. end
  219. describe "#working?" do
  220. it "checks if events have been received within expected receive period" do
  221. expect(@checker).not_to be_working
  222. Agents::PostAgent.async_receive @checker.id, [@event.id]
  223. expect(@checker.reload).to be_working
  224. two_days_from_now = 2.days.from_now
  225. stub(Time).now { two_days_from_now }
  226. expect(@checker.reload).not_to be_working
  227. end
  228. end
  229. describe "validation" do
  230. before do
  231. expect(@checker).to be_valid
  232. end
  233. it "should validate presence of post_url" do
  234. @checker.options['post_url'] = ""
  235. expect(@checker).not_to be_valid
  236. end
  237. it "should validate presence of expected_receive_period_in_days" do
  238. @checker.options['expected_receive_period_in_days'] = ""
  239. expect(@checker).not_to be_valid
  240. end
  241. it "should validate method as post, get, put, patch, or delete, defaulting to post" do
  242. @checker.options['method'] = ""
  243. expect(@checker.method).to eq("post")
  244. expect(@checker).to be_valid
  245. @checker.options['method'] = "POST"
  246. expect(@checker.method).to eq("post")
  247. expect(@checker).to be_valid
  248. @checker.options['method'] = "get"
  249. expect(@checker.method).to eq("get")
  250. expect(@checker).to be_valid
  251. @checker.options['method'] = "patch"
  252. expect(@checker.method).to eq("patch")
  253. expect(@checker).to be_valid
  254. @checker.options['method'] = "wut"
  255. expect(@checker.method).to eq("wut")
  256. expect(@checker).not_to be_valid
  257. end
  258. it "should validate that no_merge is 'true' or 'false', if present" do
  259. @checker.options['no_merge'] = ""
  260. expect(@checker).to be_valid
  261. @checker.options['no_merge'] = "true"
  262. expect(@checker).to be_valid
  263. @checker.options['no_merge'] = "false"
  264. expect(@checker).to be_valid
  265. @checker.options['no_merge'] = false
  266. expect(@checker).to be_valid
  267. @checker.options['no_merge'] = true
  268. expect(@checker).to be_valid
  269. @checker.options['no_merge'] = 'blarg'
  270. expect(@checker).not_to be_valid
  271. end
  272. it "should validate payload as a hash, if present" do
  273. @checker.options['payload'] = ""
  274. expect(@checker).to be_valid
  275. @checker.options['payload'] = "hello"
  276. expect(@checker).not_to be_valid
  277. @checker.options['payload'] = ["foo", "bar"]
  278. expect(@checker).not_to be_valid
  279. @checker.options['payload'] = { 'this' => 'that' }
  280. expect(@checker).to be_valid
  281. end
  282. it "should not validate payload as a hash if content_type includes a MIME type and method is not get or delete" do
  283. @checker.options['no_merge'] = 'true'
  284. @checker.options['content_type'] = 'text/xml'
  285. @checker.options['payload'] = "test"
  286. expect(@checker).to be_valid
  287. @checker.options['method'] = 'get'
  288. expect(@checker).not_to be_valid
  289. @checker.options['method'] = 'delete'
  290. expect(@checker).not_to be_valid
  291. end
  292. it "requires `no_merge` to be set to true when content_type contains a MIME type" do
  293. @checker.options['content_type'] = 'text/xml'
  294. @checker.options['payload'] = "test"
  295. expect(@checker).not_to be_valid
  296. end
  297. it "requires headers to be a hash, if present" do
  298. @checker.options['headers'] = [1,2,3]
  299. expect(@checker).not_to be_valid
  300. @checker.options['headers'] = "hello world"
  301. expect(@checker).not_to be_valid
  302. @checker.options['headers'] = ""
  303. expect(@checker).to be_valid
  304. @checker.options['headers'] = {}
  305. expect(@checker).to be_valid
  306. @checker.options['headers'] = { "Authorization" => "foo bar" }
  307. expect(@checker).to be_valid
  308. end
  309. it "requires emit_events to be true or false" do
  310. @checker.options['emit_events'] = 'what?'
  311. expect(@checker).not_to be_valid
  312. @checker.options.delete('emit_events')
  313. expect(@checker).to be_valid
  314. @checker.options['emit_events'] = 'true'
  315. expect(@checker).to be_valid
  316. @checker.options['emit_events'] = 'false'
  317. expect(@checker).to be_valid
  318. @checker.options['emit_events'] = true
  319. expect(@checker).to be_valid
  320. end
  321. end
  322. end