post_agent_spec.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. else
  46. raise "unexpected Content-Type: #{content_type}"
  47. end
  48. end
  49. { status: 200, body: "<html>a webpage!</html>", headers: { 'Content-Type' => 'text/html' } }
  50. }
  51. end
  52. it_behaves_like WebRequestConcern
  53. describe "making requests" do
  54. it "can make requests of each type" do
  55. %w[get put post patch delete].each.with_index(1) do |verb, index|
  56. @checker.options['method'] = verb
  57. expect(@checker).to be_valid
  58. @checker.check
  59. expect(@requests).to eq(index)
  60. expect(@sent_requests[verb.to_sym].length).to eq(1)
  61. end
  62. end
  63. end
  64. describe "#receive" do
  65. it "can handle multiple events and merge the payloads with options['payload']" do
  66. event1 = Event.new
  67. event1.agent = agents(:bob_weather_agent)
  68. event1.payload = {
  69. 'xyz' => 'value1',
  70. 'message' => 'value2',
  71. 'default' => 'value2'
  72. }
  73. expect {
  74. expect {
  75. @checker.receive([@event, event1])
  76. }.to change { @sent_requests[:post].length }.by(2)
  77. }.not_to change { @sent_requests[:get].length }
  78. expect(@sent_requests[:post][0].data).to eq(@event.payload.merge('default' => 'value').to_query)
  79. expect(@sent_requests[:post][1].data).to eq(event1.payload.to_query)
  80. end
  81. it "can make GET requests" do
  82. @checker.options['method'] = 'get'
  83. expect {
  84. expect {
  85. @checker.receive([@event])
  86. }.to change { @sent_requests[:get].length }.by(1)
  87. }.not_to change { @sent_requests[:post].length }
  88. expect(@sent_requests[:get][0].data).to eq(@event.payload.merge('default' => 'value').to_query)
  89. end
  90. it "can make a GET request merging params in post_url, payload and event" do
  91. @checker.options['method'] = 'get'
  92. @checker.options['post_url'] = "http://example.com/a/path?existing_param=existing_value"
  93. @event.payload = {
  94. "some_param" => "some_value",
  95. "another_param" => "another_value"
  96. }
  97. @checker.receive([@event])
  98. uri = @sent_requests[:get].first.uri
  99. # parameters are alphabetically sorted by Faraday
  100. expect(uri.request_uri).to eq("/a/path?another_param=another_value&default=value&existing_param=existing_value&some_param=some_value")
  101. end
  102. it "can skip merging the incoming event when no_merge is set, but it still interpolates" do
  103. @checker.options['no_merge'] = 'true'
  104. @checker.options['payload'] = {
  105. 'key' => 'it said: {{ someotherkey.somekey }}'
  106. }
  107. @checker.receive([@event])
  108. expect(@sent_requests[:post].first.data).to eq({ 'key' => 'it said: value' }.to_query)
  109. end
  110. it "interpolates when receiving a payload" do
  111. @checker.options['post_url'] = "https://{{ domain }}/{{ variable }}?existing_param=existing_value"
  112. @event.payload = {
  113. 'domain' => 'google.com',
  114. 'variable' => 'a_variable'
  115. }
  116. @checker.receive([@event])
  117. uri = @sent_requests[:post].first.uri
  118. expect(uri.scheme).to eq('https')
  119. expect(uri.host).to eq('google.com')
  120. expect(uri.path).to eq('/a_variable')
  121. expect(uri.query).to eq("existing_param=existing_value")
  122. end
  123. end
  124. describe "#check" do
  125. it "sends options['payload'] as a POST request" do
  126. expect {
  127. @checker.check
  128. }.to change { @sent_requests[:post].length }.by(1)
  129. expect(@sent_requests[:post][0].data).to eq(@checker.options['payload'].to_query)
  130. end
  131. it "sends options['payload'] as JSON as a POST request" do
  132. @checker.options['content_type'] = 'json'
  133. expect {
  134. @checker.check
  135. }.to change { @sent_requests[:post].length }.by(1)
  136. expect(@sent_requests[:post][0].data).to eq(@checker.options['payload'])
  137. end
  138. it "sends options['payload'] as XML as a POST request" do
  139. @checker.options['content_type'] = 'xml'
  140. expect {
  141. @checker.check
  142. }.to change { @sent_requests[:post].length }.by(1)
  143. expect(@sent_requests[:post][0].data.keys).to eq([ 'post' ])
  144. expect(@sent_requests[:post][0].data['post']).to eq(@checker.options['payload'])
  145. end
  146. it "sends options['payload'] as XML with custom root element name, as a POST request" do
  147. @checker.options['content_type'] = 'xml'
  148. @checker.options['xml_root'] = 'foobar'
  149. expect {
  150. @checker.check
  151. }.to change { @sent_requests[:post].length }.by(1)
  152. expect(@sent_requests[:post][0].data.keys).to eq([ 'foobar' ])
  153. expect(@sent_requests[:post][0].data['foobar']).to eq(@checker.options['payload'])
  154. end
  155. it "sends options['payload'] as a GET request" do
  156. @checker.options['method'] = 'get'
  157. expect {
  158. expect {
  159. @checker.check
  160. }.to change { @sent_requests[:get].length }.by(1)
  161. }.not_to change { @sent_requests[:post].length }
  162. expect(@sent_requests[:get][0].data).to eq(@checker.options['payload'].to_query)
  163. end
  164. describe "emitting events" do
  165. context "when emit_events is not set to true" do
  166. it "does not emit events" do
  167. expect {
  168. @checker.check
  169. }.not_to change { @checker.events.count }
  170. end
  171. end
  172. context "when emit_events is set to true" do
  173. before do
  174. @checker.options['emit_events'] = 'true'
  175. @checker.save!
  176. end
  177. it "emits the response status" do
  178. expect {
  179. @checker.check
  180. }.to change { @checker.events.count }.by(1)
  181. expect(@checker.events.last.payload['status']).to eq 200
  182. end
  183. it "emits the body" do
  184. @checker.check
  185. expect(@checker.events.last.payload['body']).to eq '<html>a webpage!</html>'
  186. end
  187. it "emits the response headers" do
  188. @checker.check
  189. expect(@checker.events.last.payload['headers']).to eq({ 'Content-Type' => 'text/html' })
  190. end
  191. end
  192. end
  193. end
  194. describe "#working?" do
  195. it "checks if events have been received within expected receive period" do
  196. expect(@checker).not_to be_working
  197. Agents::PostAgent.async_receive @checker.id, [@event.id]
  198. expect(@checker.reload).to be_working
  199. two_days_from_now = 2.days.from_now
  200. stub(Time).now { two_days_from_now }
  201. expect(@checker.reload).not_to be_working
  202. end
  203. end
  204. describe "validation" do
  205. before do
  206. expect(@checker).to be_valid
  207. end
  208. it "should validate presence of post_url" do
  209. @checker.options['post_url'] = ""
  210. expect(@checker).not_to be_valid
  211. end
  212. it "should validate presence of expected_receive_period_in_days" do
  213. @checker.options['expected_receive_period_in_days'] = ""
  214. expect(@checker).not_to be_valid
  215. end
  216. it "should validate method as post, get, put, patch, or delete, defaulting to post" do
  217. @checker.options['method'] = ""
  218. expect(@checker.method).to eq("post")
  219. expect(@checker).to be_valid
  220. @checker.options['method'] = "POST"
  221. expect(@checker.method).to eq("post")
  222. expect(@checker).to be_valid
  223. @checker.options['method'] = "get"
  224. expect(@checker.method).to eq("get")
  225. expect(@checker).to be_valid
  226. @checker.options['method'] = "patch"
  227. expect(@checker.method).to eq("patch")
  228. expect(@checker).to be_valid
  229. @checker.options['method'] = "wut"
  230. expect(@checker.method).to eq("wut")
  231. expect(@checker).not_to be_valid
  232. end
  233. it "should validate that no_merge is 'true' or 'false', if present" do
  234. @checker.options['no_merge'] = ""
  235. expect(@checker).to be_valid
  236. @checker.options['no_merge'] = "true"
  237. expect(@checker).to be_valid
  238. @checker.options['no_merge'] = "false"
  239. expect(@checker).to be_valid
  240. @checker.options['no_merge'] = false
  241. expect(@checker).to be_valid
  242. @checker.options['no_merge'] = true
  243. expect(@checker).to be_valid
  244. @checker.options['no_merge'] = 'blarg'
  245. expect(@checker).not_to be_valid
  246. end
  247. it "should validate payload as a hash, if present" do
  248. @checker.options['payload'] = ""
  249. expect(@checker).to be_valid
  250. @checker.options['payload'] = "hello"
  251. expect(@checker).not_to be_valid
  252. @checker.options['payload'] = ["foo", "bar"]
  253. expect(@checker).not_to be_valid
  254. @checker.options['payload'] = { 'this' => 'that' }
  255. expect(@checker).to be_valid
  256. end
  257. it "requires headers to be a hash, if present" do
  258. @checker.options['headers'] = [1,2,3]
  259. expect(@checker).not_to be_valid
  260. @checker.options['headers'] = "hello world"
  261. expect(@checker).not_to be_valid
  262. @checker.options['headers'] = ""
  263. expect(@checker).to be_valid
  264. @checker.options['headers'] = {}
  265. expect(@checker).to be_valid
  266. @checker.options['headers'] = { "Authorization" => "foo bar" }
  267. expect(@checker).to be_valid
  268. end
  269. it "requires emit_events to be true or false" do
  270. @checker.options['emit_events'] = 'what?'
  271. expect(@checker).not_to be_valid
  272. @checker.options.delete('emit_events')
  273. expect(@checker).to be_valid
  274. @checker.options['emit_events'] = 'true'
  275. expect(@checker).to be_valid
  276. @checker.options['emit_events'] = 'false'
  277. expect(@checker).to be_valid
  278. @checker.options['emit_events'] = true
  279. expect(@checker).to be_valid
  280. end
  281. end
  282. end