liquid_output_agent_spec.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. # encoding: utf-8
  2. require 'rails_helper'
  3. describe Agents::LiquidOutputAgent do
  4. let(:agent) do
  5. _agent = Agents::LiquidOutputAgent.new(:name => 'My Data Output Agent')
  6. _agent.options = _agent.default_options.merge('secret' => 'secret1', 'events_to_show' => 3)
  7. _agent.options['secret'] = "a secret"
  8. _agent.user = users(:bob)
  9. _agent.sources << agents(:bob_website_agent)
  10. _agent.save!
  11. _agent
  12. end
  13. describe "#working?" do
  14. it "checks if events have been received within expected receive period" do
  15. expect(agent).not_to be_working
  16. Agents::LiquidOutputAgent.async_receive agent.id, [events(:bob_website_agent_event).id]
  17. expect(agent.reload).to be_working
  18. two_days_from_now = 2.days.from_now
  19. stub(Time).now { two_days_from_now }
  20. expect(agent.reload).not_to be_working
  21. end
  22. end
  23. describe "validation" do
  24. before do
  25. expect(agent).to be_valid
  26. end
  27. it "should validate presence and length of secret" do
  28. agent.options[:secret] = ""
  29. expect(agent).not_to be_valid
  30. agent.options[:secret] = "foo"
  31. expect(agent).to be_valid
  32. agent.options[:secret] = "foo/bar"
  33. expect(agent).not_to be_valid
  34. agent.options[:secret] = "foo.xml"
  35. expect(agent).not_to be_valid
  36. agent.options[:secret] = false
  37. expect(agent).not_to be_valid
  38. agent.options[:secret] = []
  39. expect(agent).not_to be_valid
  40. agent.options[:secret] = ["foo.xml"]
  41. expect(agent).not_to be_valid
  42. agent.options[:secret] = ["hello", true]
  43. expect(agent).not_to be_valid
  44. agent.options[:secret] = ["hello"]
  45. expect(agent).not_to be_valid
  46. agent.options[:secret] = ["hello", "world"]
  47. expect(agent).not_to be_valid
  48. end
  49. it "should validate presence of expected_receive_period_in_days" do
  50. agent.options[:expected_receive_period_in_days] = ""
  51. expect(agent).not_to be_valid
  52. agent.options[:expected_receive_period_in_days] = 0
  53. expect(agent).not_to be_valid
  54. agent.options[:expected_receive_period_in_days] = -1
  55. expect(agent).not_to be_valid
  56. end
  57. it "should validate the event_limit" do
  58. agent.options[:event_limit] = ""
  59. expect(agent).to be_valid
  60. agent.options[:event_limit] = "1"
  61. expect(agent).to be_valid
  62. agent.options[:event_limit] = "1001"
  63. expect(agent).not_to be_valid
  64. agent.options[:event_limit] = "10000"
  65. expect(agent).not_to be_valid
  66. end
  67. it "should validate the event_limit with relative time" do
  68. agent.options[:event_limit] = "15 minutes"
  69. expect(agent).to be_valid
  70. agent.options[:event_limit] = "1 century"
  71. expect(agent).not_to be_valid
  72. end
  73. it "should not allow non-integer event limits" do
  74. agent.options[:event_limit] = "abc1234"
  75. expect(agent).not_to be_valid
  76. end
  77. end
  78. describe "#receive?" do
  79. let(:key) { SecureRandom.uuid }
  80. let(:value) { SecureRandom.uuid }
  81. let(:incoming_events) do
  82. last_payload = { key => value }
  83. [Struct.new(:payload).new( { key => SecureRandom.uuid } ),
  84. Struct.new(:payload).new( { key => SecureRandom.uuid } ),
  85. Struct.new(:payload).new(last_payload)]
  86. end
  87. describe "and the mode is last event in" do
  88. before { agent.options['mode'] = 'Last event in' }
  89. it "stores the last event in memory" do
  90. agent.receive incoming_events
  91. expect(agent.memory['last_event'][key]).to equal(value)
  92. end
  93. describe "but the casing is wrong" do
  94. before { agent.options['mode'] = 'LAST EVENT IN' }
  95. it "stores the last event in memory" do
  96. agent.receive incoming_events
  97. expect(agent.memory['last_event'][key]).to equal(value)
  98. end
  99. end
  100. end
  101. describe "but the mode is merge" do
  102. let(:second_key) { SecureRandom.uuid }
  103. let(:second_value) { SecureRandom.uuid }
  104. before { agent.options['mode'] = 'Merge events' }
  105. let(:incoming_events) do
  106. last_payload = { key => value }
  107. [Struct.new(:payload).new( { key => SecureRandom.uuid, second_key => second_value } ),
  108. Struct.new(:payload).new(last_payload)]
  109. end
  110. it "should merge all of the events passed to it" do
  111. agent.receive incoming_events
  112. expect(agent.memory['last_event'][key]).to equal(value)
  113. expect(agent.memory['last_event'][second_key]).to equal(second_value)
  114. end
  115. describe "but the casing on the mode is wrong" do
  116. before { agent.options['mode'] = 'MERGE EVENTS' }
  117. it "should merge all of the events passed to it" do
  118. agent.receive incoming_events
  119. expect(agent.memory['last_event'][key]).to equal(value)
  120. expect(agent.memory['last_event'][second_key]).to equal(second_value)
  121. end
  122. end
  123. end
  124. describe "but the mode is anything else" do
  125. before { agent.options['mode'] = SecureRandom.uuid }
  126. let(:incoming_events) do
  127. last_payload = { key => value }
  128. [Struct.new(:payload).new(last_payload)]
  129. end
  130. it "should do nothing" do
  131. agent.receive incoming_events
  132. expect(agent.memory.keys.count).to equal(0)
  133. end
  134. end
  135. end
  136. describe "#count_limit" do
  137. it "should have a default of 1000" do
  138. agent.options['event_limit'] = nil
  139. expect(agent.send(:count_limit)).to eq(1000)
  140. agent.options['event_limit'] = ''
  141. expect(agent.send(:count_limit)).to eq(1000)
  142. agent.options['event_limit'] = ' '
  143. expect(agent.send(:count_limit)).to eq(1000)
  144. end
  145. it "should convert string count limits to integers" do
  146. agent.options['event_limit'] = '1'
  147. expect(agent.send(:count_limit)).to eq(1)
  148. agent.options['event_limit'] = '2'
  149. expect(agent.send(:count_limit)).to eq(2)
  150. agent.options['event_limit'] = 3
  151. expect(agent.send(:count_limit)).to eq(3)
  152. end
  153. it "should default to 1000 with invalid values" do
  154. agent.options['event_limit'] = SecureRandom.uuid
  155. expect(agent.send(:count_limit)).to eq(1000)
  156. agent.options['event_limit'] = 'John Galt'
  157. expect(agent.send(:count_limit)).to eq(1000)
  158. end
  159. it "should not allow event limits above 1000" do
  160. agent.options['event_limit'] = '1001'
  161. expect(agent.send(:count_limit)).to eq(1000)
  162. agent.options['event_limit'] = '5000'
  163. expect(agent.send(:count_limit)).to eq(1000)
  164. end
  165. end
  166. describe "#receive_web_request?" do
  167. let(:secret) { SecureRandom.uuid }
  168. let(:params) { { 'secret' => secret } }
  169. let(:method) { nil }
  170. let(:format) { nil }
  171. let(:mime_type) { SecureRandom.uuid }
  172. let(:content) { "The key is {{#{key}}}." }
  173. let(:key) { SecureRandom.uuid }
  174. let(:value) { SecureRandom.uuid }
  175. before do
  176. agent.options['secret'] = secret
  177. agent.options['mime_type'] = mime_type
  178. agent.options['content'] = content
  179. agent.memory['last_event'] = { key => value }
  180. agents(:bob_website_agent).events.destroy_all
  181. end
  182. it 'should respond with custom response header if configured with `response_headers` option' do
  183. agent.options['response_headers'] = {"X-My-Custom-Header" => 'hello'}
  184. result = agent.receive_web_request params, method, format
  185. expect(result).to eq(["The key is #{value}.", 200, mime_type, {"X-My-Custom-Header" => "hello"}])
  186. end
  187. it 'should allow the usage custom liquid tags' do
  188. agent.options['content'] = "{% credential aws_secret %}"
  189. result = agent.receive_web_request params, method, format
  190. expect(result).to eq(["1111111111-bob", 200, mime_type, nil])
  191. end
  192. describe "and the mode is last event in" do
  193. before { agent.options['mode'] = 'Last event in' }
  194. it "should render the results as a liquid template from the last event in" do
  195. result = agent.receive_web_request params, method, format
  196. expect(result[0]).to eq("The key is #{value}.")
  197. expect(result[1]).to eq(200)
  198. expect(result[2]).to eq(mime_type)
  199. end
  200. describe "but the casing is wrong" do
  201. before { agent.options['mode'] = 'last event in' }
  202. it "should render the results as a liquid template from the last event in" do
  203. result = agent.receive_web_request params, method, format
  204. expect(result[0]).to eq("The key is #{value}.")
  205. expect(result[1]).to eq(200)
  206. expect(result[2]).to eq(mime_type)
  207. end
  208. end
  209. end
  210. describe "and the mode is merge events" do
  211. before { agent.options['mode'] = 'Merge events' }
  212. it "should render the results as a liquid template from the last event in" do
  213. result = agent.receive_web_request params, method, format
  214. expect(result[0]).to eq("The key is #{value}.")
  215. expect(result[1]).to eq(200)
  216. expect(result[2]).to eq(mime_type)
  217. end
  218. end
  219. describe "and the mode is last X events" do
  220. before do
  221. agent.options['mode'] = 'Last X events'
  222. agents(:bob_website_agent).create_event payload: {
  223. "name" => "Dagny Taggart",
  224. "book" => "Atlas Shrugged"
  225. }
  226. agents(:bob_website_agent).create_event payload: {
  227. "name" => "John Galt",
  228. "book" => "Atlas Shrugged"
  229. }
  230. agents(:bob_website_agent).create_event payload: {
  231. "name" => "Howard Roark",
  232. "book" => "The Fountainhead"
  233. }
  234. agent.options['content'] = <<EOF
  235. <table>
  236. {% for event in events %}
  237. <tr>
  238. <td>{{ event.name }}</td>
  239. <td>{{ event.book }}</td>
  240. </tr>
  241. {% endfor %}
  242. </table>
  243. EOF
  244. end
  245. it "should render the results as a liquid template from the last event in, limiting to 2" do
  246. agent.options['event_limit'] = 2
  247. result = agent.receive_web_request params, method, format
  248. expect(result[0]).to eq <<EOF
  249. <table>
  250. <tr>
  251. <td>Howard Roark</td>
  252. <td>The Fountainhead</td>
  253. </tr>
  254. <tr>
  255. <td>John Galt</td>
  256. <td>Atlas Shrugged</td>
  257. </tr>
  258. </table>
  259. EOF
  260. end
  261. it "should render the results as a liquid template from the last event in, limiting to 1" do
  262. agent.options['event_limit'] = 1
  263. result = agent.receive_web_request params, method, format
  264. expect(result[0]).to eq <<EOF
  265. <table>
  266. <tr>
  267. <td>Howard Roark</td>
  268. <td>The Fountainhead</td>
  269. </tr>
  270. </table>
  271. EOF
  272. end
  273. it "should render the results as a liquid template from the last event in, allowing no limit" do
  274. agent.options['event_limit'] = ''
  275. result = agent.receive_web_request params, method, format
  276. expect(result[0]).to eq <<EOF
  277. <table>
  278. <tr>
  279. <td>Howard Roark</td>
  280. <td>The Fountainhead</td>
  281. </tr>
  282. <tr>
  283. <td>John Galt</td>
  284. <td>Atlas Shrugged</td>
  285. </tr>
  286. <tr>
  287. <td>Dagny Taggart</td>
  288. <td>Atlas Shrugged</td>
  289. </tr>
  290. </table>
  291. EOF
  292. end
  293. it "should allow the limiting by time, as well" do
  294. one_event = agent.received_events.select { |x| x.payload['name'] == 'John Galt' }.first
  295. one_event.created_at = 2.days.ago
  296. one_event.save!
  297. agent.options['event_limit'] = '1 day'
  298. result = agent.receive_web_request params, method, format
  299. expect(result[0]).to eq <<EOF
  300. <table>
  301. <tr>
  302. <td>Howard Roark</td>
  303. <td>The Fountainhead</td>
  304. </tr>
  305. <tr>
  306. <td>Dagny Taggart</td>
  307. <td>Atlas Shrugged</td>
  308. </tr>
  309. </table>
  310. EOF
  311. end
  312. it "should not be case sensitive when limiting on time" do
  313. one_event = agent.received_events.select { |x| x.payload['name'] == 'John Galt' }.first
  314. one_event.created_at = 2.days.ago
  315. one_event.save!
  316. agent.options['event_limit'] = '1 DaY'
  317. result = agent.receive_web_request params, method, format
  318. expect(result[0]).to eq <<EOF
  319. <table>
  320. <tr>
  321. <td>Howard Roark</td>
  322. <td>The Fountainhead</td>
  323. </tr>
  324. <tr>
  325. <td>Dagny Taggart</td>
  326. <td>Atlas Shrugged</td>
  327. </tr>
  328. </table>
  329. EOF
  330. end
  331. it "it should continue to work when the event limit is wrong" do
  332. agent.options['event_limit'] = 'five days'
  333. result = agent.receive_web_request params, method, format
  334. expect(result[0].include?("Howard Roark")).to eq(true)
  335. expect(result[0].include?("Dagny Taggart")).to eq(true)
  336. expect(result[0].include?("John Galt")).to eq(true)
  337. agent.options['event_limit'] = '5 quibblequarks'
  338. result = agent.receive_web_request params, method, format
  339. expect(result[0].include?("Howard Roark")).to eq(true)
  340. expect(result[0].include?("Dagny Taggart")).to eq(true)
  341. expect(result[0].include?("John Galt")).to eq(true)
  342. end
  343. describe "but the mode was set to last X events with the wrong casing" do
  344. before { agent.options['mode'] = 'LAST X EVENTS' }
  345. it "should still work as last x events" do
  346. result = agent.receive_web_request params, method, format
  347. expect(result[0].include?("Howard Roark")).to eq(true)
  348. expect(result[0].include?("Dagny Taggart")).to eq(true)
  349. expect(result[0].include?("John Galt")).to eq(true)
  350. end
  351. end
  352. end
  353. describe "but the secret provided does not match" do
  354. before { params['secret'] = SecureRandom.uuid }
  355. it "should return a 401 response" do
  356. result = agent.receive_web_request params, method, format
  357. expect(result[0]).to eq("Not Authorized")
  358. expect(result[1]).to eq(401)
  359. end
  360. it "should return a 401 json response if the format is json" do
  361. result = agent.receive_web_request params, method, 'json'
  362. expect(result[0][:error]).to eq("Not Authorized")
  363. expect(result[1]).to eq(401)
  364. end
  365. end
  366. end
  367. end