1
0

post_agent_spec.rb 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. require 'rails_helper'
  2. require 'ostruct'
  3. describe Agents::PostAgent do
  4. let(:mocked_response) do
  5. {
  6. status: 200,
  7. body: "<html>a webpage!</html>",
  8. headers: {
  9. 'Content-type' => 'text/html',
  10. 'X-Foo-Bar' => 'baz',
  11. }
  12. }
  13. end
  14. before do
  15. @valid_options = {
  16. 'post_url' => "http://www.example.com",
  17. 'expected_receive_period_in_days' => 1,
  18. 'payload' => {
  19. 'default' => 'value'
  20. }
  21. }
  22. @valid_params = {
  23. name: "somename",
  24. options: @valid_options
  25. }
  26. @checker = Agents::PostAgent.new(@valid_params)
  27. @checker.user = users(:jane)
  28. @checker.save!
  29. @event = Event.new
  30. @event.agent = agents(:jane_weather_agent)
  31. @event.payload = {
  32. 'somekey' => 'somevalue',
  33. 'someotherkey' => {
  34. 'somekey' => 'value'
  35. }
  36. }
  37. @requests = 0
  38. @sent_requests = Hash.new { |hash, method| hash[method] = [] }
  39. stub_request(:any, /:/).to_return { |request|
  40. method = request.method
  41. @requests += 1
  42. @sent_requests[method] << req = OpenStruct.new(uri: request.uri, headers: request.headers)
  43. case method
  44. when :get, :delete
  45. req.data = request.uri.query
  46. else
  47. content_type = request.headers['Content-Type'][/\A[^;\s]+/]
  48. case content_type
  49. when 'application/x-www-form-urlencoded'
  50. req.data = request.body
  51. when 'application/json'
  52. req.data = ActiveSupport::JSON.decode(request.body)
  53. when 'text/xml'
  54. req.data = Hash.from_xml(request.body)
  55. when Agents::PostAgent::MIME_RE
  56. req.data = request.body
  57. else
  58. raise "unexpected Content-Type: #{content_type}"
  59. end
  60. end
  61. mocked_response
  62. }
  63. end
  64. it_behaves_like WebRequestConcern
  65. it_behaves_like 'FileHandlingConsumer'
  66. it 'renders the description markdown without errors' do
  67. expect { @checker.description }.not_to raise_error
  68. end
  69. describe "making requests" do
  70. it "can make requests of each type" do
  71. %w[get put post patch delete].each.with_index(1) do |verb, index|
  72. @checker.options['method'] = verb
  73. expect(@checker).to be_valid
  74. @checker.check
  75. expect(@requests).to eq(index)
  76. expect(@sent_requests[verb.to_sym].length).to eq(1)
  77. end
  78. end
  79. end
  80. describe "#receive" do
  81. it "can handle multiple events and merge the payloads with options['payload']" do
  82. event1 = Event.new
  83. event1.agent = agents(:bob_weather_agent)
  84. event1.payload = {
  85. 'xyz' => 'value1',
  86. 'message' => 'value2',
  87. 'default' => 'value2'
  88. }
  89. expect {
  90. expect {
  91. @checker.receive([@event, event1])
  92. }.to change { @sent_requests[:post].length }.by(2)
  93. }.not_to(change { @sent_requests[:get].length })
  94. expect(@sent_requests[:post][0].data).to eq(@event.payload.merge('default' => 'value').to_query)
  95. expect(@sent_requests[:post][1].data).to eq(event1.payload.to_query)
  96. end
  97. it "can make GET requests" do
  98. @checker.options['method'] = 'get'
  99. expect {
  100. expect {
  101. @checker.receive([@event])
  102. }.to change { @sent_requests[:get].length }.by(1)
  103. }.not_to(change { @sent_requests[:post].length })
  104. expect(@sent_requests[:get][0].data).to eq(@event.payload.merge('default' => 'value').to_query)
  105. end
  106. it "can make a GET request merging params in post_url, payload and event" do
  107. @checker.options['method'] = 'get'
  108. @checker.options['post_url'] = "http://example.com/a/path?existing_param=existing_value"
  109. @event.payload = {
  110. "some_param" => "some_value",
  111. "another_param" => "another_value"
  112. }
  113. @checker.receive([@event])
  114. uri = @sent_requests[:get].first.uri
  115. # parameters are alphabetically sorted by Faraday
  116. expect(uri.request_uri).to eq("/a/path?another_param=another_value&default=value&existing_param=existing_value&some_param=some_value")
  117. end
  118. it "can skip merging the incoming event when no_merge is set, but it still interpolates" do
  119. @checker.options['no_merge'] = 'true'
  120. @checker.options['payload'] = {
  121. 'key' => 'it said: {{ someotherkey.somekey }}'
  122. }
  123. @checker.receive([@event])
  124. expect(@sent_requests[:post].first.data).to eq({ 'key' => 'it said: value' }.to_query)
  125. end
  126. it "interpolates when receiving a payload" do
  127. @checker.options['post_url'] = "https://{{ domain }}/{{ variable }}?existing_param=existing_value"
  128. @event.payload = {
  129. 'domain' => 'google.com',
  130. 'variable' => 'a_variable'
  131. }
  132. @checker.receive([@event])
  133. uri = @sent_requests[:post].first.uri
  134. expect(uri.scheme).to eq('https')
  135. expect(uri.host).to eq('google.com')
  136. expect(uri.path).to eq('/a_variable')
  137. expect(uri.query).to eq("existing_param=existing_value")
  138. end
  139. it "interpolates outgoing headers with the event payload" do
  140. @checker.options['headers'] = {
  141. "Foo" => "{{ variable }}"
  142. }
  143. @event.payload = {
  144. 'variable' => 'a_variable'
  145. }
  146. @checker.receive([@event])
  147. headers = @sent_requests[:post].first.headers
  148. expect(headers["Foo"]).to eq("a_variable")
  149. end
  150. it 'makes a multipart request when receiving a file_pointer' do
  151. WebMock.reset!
  152. stub_request(:post, "http://www.example.com/")
  153. .with(headers: {
  154. 'Accept-Encoding' => 'gzip,deflate',
  155. 'Content-Type' => /\Amultipart\/form-data; boundary=/,
  156. 'User-Agent' => 'Huginn - https://github.com/huginn/huginn'
  157. }) { |request|
  158. qboundary = Regexp.quote(request.headers['Content-Type'][/ boundary=(.+)/, 1])
  159. /\A--#{qboundary}\r\nContent-Disposition: form-data; name="default"\r\n\r\nvalue\r\n--#{qboundary}\r\nContent-Disposition: form-data; name="file"; filename="local.path"\r\nContent-Length: 8\r\nContent-Type: \r\nContent-Transfer-Encoding: binary\r\n\r\ntestdata\r\n--#{qboundary}--\r\n\z/ === request.body
  160. }.to_return(status: 200, body: "", headers: {})
  161. event = Event.new(payload: { file_pointer: { agent_id: 111, file: 'test' } })
  162. io_mock = double
  163. expect(@checker).to receive(:get_io).with(event) { StringIO.new("testdata") }
  164. @checker.options['no_merge'] = true
  165. @checker.receive([event])
  166. end
  167. end
  168. describe "#check" do
  169. it "sends options['payload'] as a POST request" do
  170. expect {
  171. @checker.check
  172. }.to change { @sent_requests[:post].length }.by(1)
  173. expect(@sent_requests[:post][0].data).to eq(@checker.options['payload'].to_query)
  174. end
  175. it "sends options['payload'] as JSON as a POST request" do
  176. @checker.options['content_type'] = 'json'
  177. expect {
  178. @checker.check
  179. }.to change { @sent_requests[:post].length }.by(1)
  180. expect(@sent_requests[:post][0].data).to eq(@checker.options['payload'])
  181. end
  182. it "sends options['payload'] as XML as a POST request" do
  183. @checker.options['content_type'] = 'xml'
  184. expect {
  185. @checker.check
  186. }.to change { @sent_requests[:post].length }.by(1)
  187. expect(@sent_requests[:post][0].data.keys).to eq(['post'])
  188. expect(@sent_requests[:post][0].data['post']).to eq(@checker.options['payload'])
  189. end
  190. it "sends options['payload'] as XML with custom root element name, as a POST request" do
  191. @checker.options['content_type'] = 'xml'
  192. @checker.options['xml_root'] = 'foobar'
  193. expect {
  194. @checker.check
  195. }.to change { @sent_requests[:post].length }.by(1)
  196. expect(@sent_requests[:post][0].data.keys).to eq(['foobar'])
  197. expect(@sent_requests[:post][0].data['foobar']).to eq(@checker.options['payload'])
  198. end
  199. it "sends options['payload'] as a GET request" do
  200. @checker.options['method'] = 'get'
  201. expect {
  202. expect {
  203. @checker.check
  204. }.to change { @sent_requests[:get].length }.by(1)
  205. }.not_to(change { @sent_requests[:post].length })
  206. expect(@sent_requests[:get][0].data).to eq(@checker.options['payload'].to_query)
  207. end
  208. it "sends options['payload'] as a string POST request when content-type continas a MIME type" do
  209. @checker.options['payload'] = '<test>hello</test>'
  210. @checker.options['content_type'] = 'application/xml'
  211. expect {
  212. @checker.check
  213. }.to change { @sent_requests[:post].length }.by(1)
  214. expect(@sent_requests[:post][0].data).to eq('<test>hello</test>')
  215. end
  216. it "interpolates outgoing headers" do
  217. @checker.options['headers'] = {
  218. "Foo" => "{% credential aws_key %}"
  219. }
  220. @checker.check
  221. headers = @sent_requests[:post].first.headers
  222. expect(headers["Foo"]).to eq("2222222222-jane")
  223. end
  224. describe "emitting events" do
  225. context "when emit_events is not set to true" do
  226. it "does not emit events" do
  227. expect {
  228. @checker.check
  229. }.not_to(change { @checker.events.count })
  230. end
  231. end
  232. context "when emit_events is set to true" do
  233. before do
  234. @checker.options['emit_events'] = 'true'
  235. @checker.save!
  236. end
  237. it "emits the response status" do
  238. expect {
  239. @checker.check
  240. }.to change { @checker.events.count }.by(1)
  241. expect(@checker.events.last.payload['status']).to eq 200
  242. end
  243. it "emits the body" do
  244. @checker.check
  245. expect(@checker.events.last.payload['body']).to eq '<html>a webpage!</html>'
  246. end
  247. context "and the response is in JSON" do
  248. let(:json_data) {
  249. { "foo" => 123, "bar" => 456 }
  250. }
  251. let(:mocked_response) do
  252. {
  253. status: 200,
  254. body: json_data.to_json,
  255. headers: {
  256. 'Content-type' => 'application/json',
  257. 'X-Foo-Bar' => 'baz',
  258. }
  259. }
  260. end
  261. it "emits the unparsed JSON body" do
  262. @checker.check
  263. expect(@checker.events.last.payload['body']).to eq json_data.to_json
  264. end
  265. it "emits the parsed JSON body when parse_body is true" do
  266. @checker.options['parse_body'] = 'true'
  267. @checker.save!
  268. @checker.check
  269. expect(@checker.events.last.payload['body']).to eq json_data
  270. end
  271. end
  272. it "emits the response headers capitalized by default" do
  273. @checker.check
  274. expect(@checker.events.last.payload['headers']).to eq({ 'Content-Type' => 'text/html', 'X-Foo-Bar' => 'baz' })
  275. end
  276. it "emits the response headers capitalized" do
  277. @checker.options['event_headers_style'] = 'capitalized'
  278. @checker.check
  279. expect(@checker.events.last.payload['headers']).to eq({ 'Content-Type' => 'text/html', 'X-Foo-Bar' => 'baz' })
  280. end
  281. it "emits the response headers downcased" do
  282. @checker.options['event_headers_style'] = 'downcased'
  283. @checker.check
  284. expect(@checker.events.last.payload['headers']).to eq({ 'content-type' => 'text/html', 'x-foo-bar' => 'baz' })
  285. end
  286. it "emits the response headers snakecased" do
  287. @checker.options['event_headers_style'] = 'snakecased'
  288. @checker.check
  289. expect(@checker.events.last.payload['headers']).to eq({ 'content_type' => 'text/html', 'x_foo_bar' => 'baz' })
  290. end
  291. it "emits the response headers only including those specified by event_headers" do
  292. @checker.options['event_headers_style'] = 'snakecased'
  293. @checker.options['event_headers'] = 'content-type'
  294. @checker.check
  295. expect(@checker.events.last.payload['headers']).to eq({ 'content_type' => 'text/html' })
  296. end
  297. context "when output_mode is set to 'merge'" do
  298. before do
  299. @checker.options['output_mode'] = 'merge'
  300. @checker.save!
  301. end
  302. it "emits the received event" do
  303. @checker.receive([@event])
  304. @checker.check
  305. expect(@checker.events.last.payload['somekey']).to eq('somevalue')
  306. expect(@checker.events.last.payload['someotherkey']).to eq({ 'somekey' => 'value' })
  307. end
  308. end
  309. end
  310. end
  311. end
  312. describe "#working?" do
  313. it "checks if there was an error" do
  314. @checker.error("error")
  315. expect(@checker.logs.count).to eq(1)
  316. expect(@checker.reload).not_to be_working
  317. end
  318. it "checks if 'expected_receive_period_in_days' was not set" do
  319. expect(@checker.logs.count).to eq(0)
  320. @checker.options.delete('expected_receive_period_in_days')
  321. expect(@checker).to be_working
  322. end
  323. it "checks if no event has been received" do
  324. expect(@checker.logs.count).to eq(0)
  325. expect(@checker.last_receive_at).to be_nil
  326. expect(@checker.reload).not_to be_working
  327. end
  328. it "checks if events have been received within expected receive period" do
  329. expect(@checker).not_to be_working
  330. Agents::PostAgent.async_receive @checker.id, [@event.id]
  331. expect(@checker.reload).to be_working
  332. two_days_from_now = 2.days.from_now
  333. allow(Time).to receive(:now) { two_days_from_now }
  334. expect(@checker.reload).not_to be_working
  335. end
  336. end
  337. describe "validation" do
  338. before do
  339. expect(@checker).to be_valid
  340. end
  341. it "should validate presence of post_url" do
  342. @checker.options['post_url'] = ""
  343. expect(@checker).not_to be_valid
  344. end
  345. it "should validate absence of expected_receive_period_in_days is allowed" do
  346. @checker.options['expected_receive_period_in_days'] = ""
  347. expect(@checker).to be_valid
  348. end
  349. it "should validate method as post, get, put, patch, or delete, defaulting to post" do
  350. @checker.options['method'] = ""
  351. expect(@checker.method).to eq("post")
  352. expect(@checker).to be_valid
  353. @checker.options['method'] = "POST"
  354. expect(@checker.method).to eq("post")
  355. expect(@checker).to be_valid
  356. @checker.options['method'] = "get"
  357. expect(@checker.method).to eq("get")
  358. expect(@checker).to be_valid
  359. @checker.options['method'] = "patch"
  360. expect(@checker.method).to eq("patch")
  361. expect(@checker).to be_valid
  362. @checker.options['method'] = "wut"
  363. expect(@checker.method).to eq("wut")
  364. expect(@checker).not_to be_valid
  365. end
  366. it "should validate that no_merge is 'true' or 'false', if present" do
  367. @checker.options['no_merge'] = ""
  368. expect(@checker).to be_valid
  369. @checker.options['no_merge'] = "true"
  370. expect(@checker).to be_valid
  371. @checker.options['no_merge'] = "false"
  372. expect(@checker).to be_valid
  373. @checker.options['no_merge'] = false
  374. expect(@checker).to be_valid
  375. @checker.options['no_merge'] = true
  376. expect(@checker).to be_valid
  377. @checker.options['no_merge'] = 'blarg'
  378. expect(@checker).not_to be_valid
  379. end
  380. it "should validate payload as a hash, if present" do
  381. @checker.options['payload'] = ""
  382. expect(@checker).to be_valid
  383. @checker.options['payload'] = ["foo", "bar"]
  384. expect(@checker).to be_valid
  385. @checker.options['payload'] = "hello"
  386. expect(@checker).not_to be_valid
  387. @checker.options['payload'] = { 'this' => 'that' }
  388. expect(@checker).to be_valid
  389. end
  390. it "should not validate payload as a hash or an array if content_type includes a MIME type and method is not get or delete" do
  391. @checker.options['no_merge'] = 'true'
  392. @checker.options['content_type'] = 'text/xml'
  393. @checker.options['payload'] = "test"
  394. expect(@checker).to be_valid
  395. @checker.options['method'] = 'get'
  396. expect(@checker).not_to be_valid
  397. @checker.options['method'] = 'delete'
  398. expect(@checker).not_to be_valid
  399. end
  400. it "requires `no_merge` to be set to true when content_type contains a MIME type" do
  401. @checker.options['content_type'] = 'text/xml'
  402. @checker.options['payload'] = "test"
  403. expect(@checker).not_to be_valid
  404. end
  405. it "requires headers to be a hash, if present" do
  406. @checker.options['headers'] = [1, 2, 3]
  407. expect(@checker).not_to be_valid
  408. @checker.options['headers'] = "hello world"
  409. expect(@checker).not_to be_valid
  410. @checker.options['headers'] = ""
  411. expect(@checker).to be_valid
  412. @checker.options['headers'] = {}
  413. expect(@checker).to be_valid
  414. @checker.options['headers'] = { "Authorization" => "foo bar" }
  415. expect(@checker).to be_valid
  416. end
  417. it "requires emit_events to be true or false" do
  418. @checker.options['emit_events'] = 'what?'
  419. expect(@checker).not_to be_valid
  420. @checker.options.delete('emit_events')
  421. expect(@checker).to be_valid
  422. @checker.options['emit_events'] = 'true'
  423. expect(@checker).to be_valid
  424. @checker.options['emit_events'] = 'false'
  425. expect(@checker).to be_valid
  426. @checker.options['emit_events'] = true
  427. expect(@checker).to be_valid
  428. end
  429. it "requires output_mode to be 'clean' or 'merge', if present" do
  430. @checker.options['output_mode'] = 'what?'
  431. expect(@checker).not_to be_valid
  432. @checker.options.delete('output_mode')
  433. expect(@checker).to be_valid
  434. @checker.options['output_mode'] = 'clean'
  435. expect(@checker).to be_valid
  436. @checker.options['output_mode'] = 'merge'
  437. expect(@checker).to be_valid
  438. @checker.options['output_mode'] = :clean
  439. expect(@checker).to be_valid
  440. @checker.options['output_mode'] = :merge
  441. expect(@checker).to be_valid
  442. @checker.options['output_mode'] = '{{somekey}}'
  443. expect(@checker).to be_valid
  444. @checker.options['output_mode'] = "{% if key == 'foo' %}merge{% else %}clean{% endif %}"
  445. expect(@checker).to be_valid
  446. end
  447. end
  448. end