Нет описания http://j1x-huginn.herokuapp.com

post_agent_spec.rb 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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" do
  198. @checker.check
  199. expect(@checker.events.last.payload['headers']).to eq({ 'Content-Type' => 'text/html' })
  200. end
  201. end
  202. end
  203. end
  204. describe "#working?" do
  205. it "checks if events have been received within expected receive period" do
  206. expect(@checker).not_to be_working
  207. Agents::PostAgent.async_receive @checker.id, [@event.id]
  208. expect(@checker.reload).to be_working
  209. two_days_from_now = 2.days.from_now
  210. stub(Time).now { two_days_from_now }
  211. expect(@checker.reload).not_to be_working
  212. end
  213. end
  214. describe "validation" do
  215. before do
  216. expect(@checker).to be_valid
  217. end
  218. it "should validate presence of post_url" do
  219. @checker.options['post_url'] = ""
  220. expect(@checker).not_to be_valid
  221. end
  222. it "should validate presence of expected_receive_period_in_days" do
  223. @checker.options['expected_receive_period_in_days'] = ""
  224. expect(@checker).not_to be_valid
  225. end
  226. it "should validate method as post, get, put, patch, or delete, defaulting to post" do
  227. @checker.options['method'] = ""
  228. expect(@checker.method).to eq("post")
  229. expect(@checker).to be_valid
  230. @checker.options['method'] = "POST"
  231. expect(@checker.method).to eq("post")
  232. expect(@checker).to be_valid
  233. @checker.options['method'] = "get"
  234. expect(@checker.method).to eq("get")
  235. expect(@checker).to be_valid
  236. @checker.options['method'] = "patch"
  237. expect(@checker.method).to eq("patch")
  238. expect(@checker).to be_valid
  239. @checker.options['method'] = "wut"
  240. expect(@checker.method).to eq("wut")
  241. expect(@checker).not_to be_valid
  242. end
  243. it "should validate that no_merge is 'true' or 'false', if present" do
  244. @checker.options['no_merge'] = ""
  245. expect(@checker).to be_valid
  246. @checker.options['no_merge'] = "true"
  247. expect(@checker).to be_valid
  248. @checker.options['no_merge'] = "false"
  249. expect(@checker).to be_valid
  250. @checker.options['no_merge'] = false
  251. expect(@checker).to be_valid
  252. @checker.options['no_merge'] = true
  253. expect(@checker).to be_valid
  254. @checker.options['no_merge'] = 'blarg'
  255. expect(@checker).not_to be_valid
  256. end
  257. it "should validate payload as a hash, if present" do
  258. @checker.options['payload'] = ""
  259. expect(@checker).to be_valid
  260. @checker.options['payload'] = "hello"
  261. expect(@checker).not_to be_valid
  262. @checker.options['payload'] = ["foo", "bar"]
  263. expect(@checker).not_to be_valid
  264. @checker.options['payload'] = { 'this' => 'that' }
  265. expect(@checker).to be_valid
  266. end
  267. it "should not validate payload as a hash if content_type includes a MIME type and method is not get or delete" do
  268. @checker.options['no_merge'] = 'true'
  269. @checker.options['content_type'] = 'text/xml'
  270. @checker.options['payload'] = "test"
  271. expect(@checker).to be_valid
  272. @checker.options['method'] = 'get'
  273. expect(@checker).not_to be_valid
  274. @checker.options['method'] = 'delete'
  275. expect(@checker).not_to be_valid
  276. end
  277. it "requires `no_merge` to be set to true when content_type contains a MIME type" do
  278. @checker.options['content_type'] = 'text/xml'
  279. @checker.options['payload'] = "test"
  280. expect(@checker).not_to be_valid
  281. end
  282. it "requires headers to be a hash, if present" do
  283. @checker.options['headers'] = [1,2,3]
  284. expect(@checker).not_to be_valid
  285. @checker.options['headers'] = "hello world"
  286. expect(@checker).not_to be_valid
  287. @checker.options['headers'] = ""
  288. expect(@checker).to be_valid
  289. @checker.options['headers'] = {}
  290. expect(@checker).to be_valid
  291. @checker.options['headers'] = { "Authorization" => "foo bar" }
  292. expect(@checker).to be_valid
  293. end
  294. it "requires emit_events to be true or false" do
  295. @checker.options['emit_events'] = 'what?'
  296. expect(@checker).not_to be_valid
  297. @checker.options.delete('emit_events')
  298. expect(@checker).to be_valid
  299. @checker.options['emit_events'] = 'true'
  300. expect(@checker).to be_valid
  301. @checker.options['emit_events'] = 'false'
  302. expect(@checker).to be_valid
  303. @checker.options['emit_events'] = true
  304. expect(@checker).to be_valid
  305. end
  306. end
  307. end