1
0

liquid_output_agent_spec.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 should not allow non-integer event limits" do
  68. agent.options[:event_limit] = "abc1234"
  69. expect(agent).not_to be_valid
  70. end
  71. end
  72. describe "#receive?" do
  73. let(:key) { SecureRandom.uuid }
  74. let(:value) { SecureRandom.uuid }
  75. let(:incoming_events) do
  76. last_payload = { key => value }
  77. [Struct.new(:payload).new( { key => SecureRandom.uuid } ),
  78. Struct.new(:payload).new( { key => SecureRandom.uuid } ),
  79. Struct.new(:payload).new(last_payload)]
  80. end
  81. describe "and the mode is last event in" do
  82. before { agent.options['mode'] = 'Last event in' }
  83. it "stores the last event in memory" do
  84. agent.receive incoming_events
  85. expect(agent.memory['last_event'][key]).to equal(value)
  86. end
  87. describe "but the casing is wrong" 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. end
  94. end
  95. describe "but the mode is merge" do
  96. let(:second_key) { SecureRandom.uuid }
  97. let(:second_value) { SecureRandom.uuid }
  98. before { agent.options['mode'] = 'Merge events' }
  99. let(:incoming_events) do
  100. last_payload = { key => value }
  101. [Struct.new(:payload).new( { key => SecureRandom.uuid, second_key => second_value } ),
  102. Struct.new(:payload).new(last_payload)]
  103. end
  104. it "should merge all of the events passed to it" do
  105. agent.receive incoming_events
  106. expect(agent.memory['last_event'][key]).to equal(value)
  107. expect(agent.memory['last_event'][second_key]).to equal(second_value)
  108. end
  109. describe "but the casing on the mode is wrong" do
  110. before { agent.options['mode'] = 'MERGE EVENTS' }
  111. it "should merge all of the events passed to it" do
  112. agent.receive incoming_events
  113. expect(agent.memory['last_event'][key]).to equal(value)
  114. expect(agent.memory['last_event'][second_key]).to equal(second_value)
  115. end
  116. end
  117. end
  118. describe "but the mode is anything else" do
  119. before { agent.options['mode'] = SecureRandom.uuid }
  120. let(:incoming_events) do
  121. last_payload = { key => value }
  122. [Struct.new(:payload).new(last_payload)]
  123. end
  124. it "should do nothing" do
  125. agent.receive incoming_events
  126. expect(agent.memory.keys.count).to equal(0)
  127. end
  128. end
  129. end
  130. describe "#count_limit" do
  131. it "should have a default of 1000" do
  132. agent.options['event_limit'] = nil
  133. expect(agent.send(:count_limit)).to eq(1000)
  134. agent.options['event_limit'] = ''
  135. expect(agent.send(:count_limit)).to eq(1000)
  136. agent.options['event_limit'] = ' '
  137. expect(agent.send(:count_limit)).to eq(1000)
  138. end
  139. it "should convert string count limits to integers" do
  140. agent.options['event_limit'] = '1'
  141. expect(agent.send(:count_limit)).to eq(1)
  142. agent.options['event_limit'] = '2'
  143. expect(agent.send(:count_limit)).to eq(2)
  144. agent.options['event_limit'] = 3
  145. expect(agent.send(:count_limit)).to eq(3)
  146. end
  147. it "should default to 1000 with invalid values" do
  148. agent.options['event_limit'] = SecureRandom.uuid
  149. expect(agent.send(:count_limit)).to eq(1000)
  150. agent.options['event_limit'] = 'John Galt'
  151. expect(agent.send(:count_limit)).to eq(1000)
  152. end
  153. it "should not allow event limits above 1000" do
  154. agent.options['event_limit'] = '1001'
  155. expect(agent.send(:count_limit)).to eq(1000)
  156. agent.options['event_limit'] = '5000'
  157. expect(agent.send(:count_limit)).to eq(1000)
  158. end
  159. end
  160. describe "#receive_web_request?" do
  161. let(:secret) { SecureRandom.uuid }
  162. let(:params) { { 'secret' => secret } }
  163. let(:method) { nil }
  164. let(:format) { nil }
  165. let(:mime_type) { SecureRandom.uuid }
  166. let(:content) { "The key is {{#{key}}}." }
  167. let(:key) { SecureRandom.uuid }
  168. let(:value) { SecureRandom.uuid }
  169. before do
  170. agent.options['secret'] = secret
  171. agent.options['mime_type'] = mime_type
  172. agent.options['content'] = content
  173. agent.memory['last_event'] = { key => value }
  174. agents(:bob_website_agent).events.destroy_all
  175. end
  176. it 'should respond with custom response header if configured with `response_headers` option' do
  177. agent.options['response_headers'] = {"X-My-Custom-Header" => 'hello'}
  178. result = agent.receive_web_request params, method, format
  179. expect(result).to eq(["The key is #{value}.", 200, mime_type, {"X-My-Custom-Header" => "hello"}])
  180. end
  181. it 'should allow the usage custom liquid tags' do
  182. agent.options['content'] = "{% credential aws_secret %}"
  183. result = agent.receive_web_request params, method, format
  184. expect(result).to eq(["1111111111-bob", 200, mime_type, nil])
  185. end
  186. describe "and the mode is last event in" do
  187. before { agent.options['mode'] = 'Last event in' }
  188. it "should render the results as a liquid template from the last event in" do
  189. result = agent.receive_web_request params, method, format
  190. expect(result[0]).to eq("The key is #{value}.")
  191. expect(result[1]).to eq(200)
  192. expect(result[2]).to eq(mime_type)
  193. end
  194. describe "but the casing is wrong" do
  195. before { agent.options['mode'] = 'last event in' }
  196. it "should render the results as a liquid template from the last event in" do
  197. result = agent.receive_web_request params, method, format
  198. expect(result[0]).to eq("The key is #{value}.")
  199. expect(result[1]).to eq(200)
  200. expect(result[2]).to eq(mime_type)
  201. end
  202. end
  203. end
  204. describe "and the mode is merge events" do
  205. before { agent.options['mode'] = 'Merge events' }
  206. it "should render the results as a liquid template from the last event in" do
  207. result = agent.receive_web_request params, method, format
  208. expect(result[0]).to eq("The key is #{value}.")
  209. expect(result[1]).to eq(200)
  210. expect(result[2]).to eq(mime_type)
  211. end
  212. end
  213. describe "and the mode is last X events" do
  214. before do
  215. agent.options['mode'] = 'Last X events'
  216. agents(:bob_website_agent).create_event payload: {
  217. "name" => "Dagny Taggart",
  218. "book" => "Atlas Shrugged"
  219. }
  220. agents(:bob_website_agent).create_event payload: {
  221. "name" => "John Galt",
  222. "book" => "Atlas Shrugged"
  223. }
  224. agents(:bob_website_agent).create_event payload: {
  225. "name" => "Howard Roark",
  226. "book" => "The Fountainhead"
  227. }
  228. agent.options['content'] = <<EOF
  229. <table>
  230. {% for event in events %}
  231. <tr>
  232. <td>{{ event.name }}</td>
  233. <td>{{ event.book }}</td>
  234. </tr>
  235. {% endfor %}
  236. </table>
  237. EOF
  238. end
  239. it "should render the results as a liquid template from the last event in, limiting to 2" do
  240. agent.options['event_limit'] = 2
  241. result = agent.receive_web_request params, method, format
  242. expect(result[0]).to eq <<EOF
  243. <table>
  244. <tr>
  245. <td>Howard Roark</td>
  246. <td>The Fountainhead</td>
  247. </tr>
  248. <tr>
  249. <td>John Galt</td>
  250. <td>Atlas Shrugged</td>
  251. </tr>
  252. </table>
  253. EOF
  254. end
  255. it "should render the results as a liquid template from the last event in, limiting to 1" do
  256. agent.options['event_limit'] = 1
  257. result = agent.receive_web_request params, method, format
  258. expect(result[0]).to eq <<EOF
  259. <table>
  260. <tr>
  261. <td>Howard Roark</td>
  262. <td>The Fountainhead</td>
  263. </tr>
  264. </table>
  265. EOF
  266. end
  267. it "should render the results as a liquid template from the last event in, allowing no limit" do
  268. agent.options['event_limit'] = ''
  269. result = agent.receive_web_request params, method, format
  270. expect(result[0]).to eq <<EOF
  271. <table>
  272. <tr>
  273. <td>Howard Roark</td>
  274. <td>The Fountainhead</td>
  275. </tr>
  276. <tr>
  277. <td>John Galt</td>
  278. <td>Atlas Shrugged</td>
  279. </tr>
  280. <tr>
  281. <td>Dagny Taggart</td>
  282. <td>Atlas Shrugged</td>
  283. </tr>
  284. </table>
  285. EOF
  286. end
  287. it "should allow the limiting by time, as well" do
  288. one_event = agent.received_events.select { |x| x.payload['name'] == 'John Galt' }.first
  289. one_event.created_at = 2.days.ago
  290. one_event.save!
  291. agent.options['event_limit'] = '1 day'
  292. result = agent.receive_web_request params, method, format
  293. expect(result[0]).to eq <<EOF
  294. <table>
  295. <tr>
  296. <td>Howard Roark</td>
  297. <td>The Fountainhead</td>
  298. </tr>
  299. <tr>
  300. <td>Dagny Taggart</td>
  301. <td>Atlas Shrugged</td>
  302. </tr>
  303. </table>
  304. EOF
  305. end
  306. it "should not be case sensitive when limiting on time" do
  307. one_event = agent.received_events.select { |x| x.payload['name'] == 'John Galt' }.first
  308. one_event.created_at = 2.days.ago
  309. one_event.save!
  310. agent.options['event_limit'] = '1 DaY'
  311. result = agent.receive_web_request params, method, format
  312. expect(result[0]).to eq <<EOF
  313. <table>
  314. <tr>
  315. <td>Howard Roark</td>
  316. <td>The Fountainhead</td>
  317. </tr>
  318. <tr>
  319. <td>Dagny Taggart</td>
  320. <td>Atlas Shrugged</td>
  321. </tr>
  322. </table>
  323. EOF
  324. end
  325. it "it should continue to work when the event limit is wrong" do
  326. agent.options['event_limit'] = 'five days'
  327. result = agent.receive_web_request params, method, format
  328. expect(result[0].include?("Howard Roark")).to eq(true)
  329. expect(result[0].include?("Dagny Taggart")).to eq(true)
  330. expect(result[0].include?("John Galt")).to eq(true)
  331. agent.options['event_limit'] = '5 quibblequarks'
  332. result = agent.receive_web_request params, method, format
  333. expect(result[0].include?("Howard Roark")).to eq(true)
  334. expect(result[0].include?("Dagny Taggart")).to eq(true)
  335. expect(result[0].include?("John Galt")).to eq(true)
  336. end
  337. describe "but the mode was set to last X events with the wrong casing" do
  338. before { agent.options['mode'] = 'LAST X EVENTS' }
  339. it "should still work as last x events" do
  340. result = agent.receive_web_request params, method, format
  341. expect(result[0].include?("Howard Roark")).to eq(true)
  342. expect(result[0].include?("Dagny Taggart")).to eq(true)
  343. expect(result[0].include?("John Galt")).to eq(true)
  344. end
  345. end
  346. end
  347. describe "but the secret provided does not match" do
  348. before { params['secret'] = SecureRandom.uuid }
  349. it "should return a 401 response" do
  350. result = agent.receive_web_request params, method, format
  351. expect(result[0]).to eq("Not Authorized")
  352. expect(result[1]).to eq(401)
  353. end
  354. it "should return a 401 json response if the format is json" do
  355. result = agent.receive_web_request params, method, 'json'
  356. expect(result[0][:error]).to eq("Not Authorized")
  357. expect(result[1]).to eq(401)
  358. end
  359. end
  360. end
  361. end