agent_spec.rb 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. require 'spec_helper'
  2. describe Agent do
  3. it_behaves_like WorkingHelpers
  4. describe ".bulk_check" do
  5. before do
  6. @weather_agent_count = Agents::WeatherAgent.where(:schedule => "midnight", :disabled => false).count
  7. end
  8. it "should run all Agents with the given schedule" do
  9. mock(Agents::WeatherAgent).async_check(anything).times(@weather_agent_count)
  10. Agents::WeatherAgent.bulk_check("midnight")
  11. end
  12. it "should skip disabled Agents" do
  13. agents(:bob_weather_agent).update_attribute :disabled, true
  14. mock(Agents::WeatherAgent).async_check(anything).times(@weather_agent_count - 1)
  15. Agents::WeatherAgent.bulk_check("midnight")
  16. end
  17. end
  18. describe ".run_schedule" do
  19. before do
  20. expect(Agents::WeatherAgent.count).to be > 0
  21. expect(Agents::WebsiteAgent.count).to be > 0
  22. end
  23. it "runs agents with the given schedule" do
  24. weather_agent_ids = [agents(:bob_weather_agent), agents(:jane_weather_agent)].map(&:id)
  25. stub(Agents::WeatherAgent).async_check(anything) {|agent_id| weather_agent_ids.delete(agent_id) }
  26. stub(Agents::WebsiteAgent).async_check(agents(:bob_website_agent).id)
  27. Agent.run_schedule("midnight")
  28. expect(weather_agent_ids).to be_empty
  29. end
  30. it "groups agents by type" do
  31. mock(Agents::WeatherAgent).bulk_check("midnight").once
  32. mock(Agents::WebsiteAgent).bulk_check("midnight").once
  33. Agent.run_schedule("midnight")
  34. end
  35. it "only runs agents with the given schedule" do
  36. do_not_allow(Agents::WebsiteAgent).async_check
  37. Agent.run_schedule("blah")
  38. end
  39. it "will not run the 'never' schedule" do
  40. agents(:bob_weather_agent).update_attribute 'schedule', 'never'
  41. do_not_allow(Agents::WebsiteAgent).async_check
  42. Agent.run_schedule("never")
  43. end
  44. end
  45. describe "credential" do
  46. it "should return the value of the credential when credential is present" do
  47. expect(agents(:bob_weather_agent).credential("aws_secret")).to eq(user_credentials(:bob_aws_secret).credential_value)
  48. end
  49. it "should return nil when credential is not present" do
  50. expect(agents(:bob_weather_agent).credential("non_existing_credential")).to eq(nil)
  51. end
  52. it "should memoize the load" do
  53. mock.any_instance_of(UserCredential).credential_value.twice { "foo" }
  54. expect(agents(:bob_weather_agent).credential("aws_secret")).to eq("foo")
  55. expect(agents(:bob_weather_agent).credential("aws_secret")).to eq("foo")
  56. agents(:bob_weather_agent).reload
  57. expect(agents(:bob_weather_agent).credential("aws_secret")).to eq("foo")
  58. expect(agents(:bob_weather_agent).credential("aws_secret")).to eq("foo")
  59. end
  60. end
  61. describe "changes to type" do
  62. it "validates types" do
  63. source = Agent.new
  64. source.type = "Agents::WeatherAgent"
  65. expect(source).to have(0).errors_on(:type)
  66. source.type = "Agents::WebsiteAgent"
  67. expect(source).to have(0).errors_on(:type)
  68. source.type = "Agents::Fake"
  69. expect(source).to have(1).error_on(:type)
  70. end
  71. it "disallows changes to type once a record has been saved" do
  72. source = agents(:bob_website_agent)
  73. source.type = "Agents::WeatherAgent"
  74. expect(source).to have(1).error_on(:type)
  75. end
  76. it "should know about available types" do
  77. expect(Agent.types).to include(Agents::WeatherAgent, Agents::WebsiteAgent)
  78. end
  79. end
  80. describe "with an example Agent" do
  81. class Agents::SomethingSource < Agent
  82. default_schedule "2pm"
  83. def check
  84. create_event :payload => {}
  85. end
  86. def validate_options
  87. errors.add(:base, "bad is bad") if options[:bad]
  88. end
  89. end
  90. class Agents::CannotBeScheduled < Agent
  91. cannot_be_scheduled!
  92. def receive(events)
  93. events.each do |event|
  94. create_event :payload => { :events_received => 1 }
  95. end
  96. end
  97. end
  98. before do
  99. stub(Agents::SomethingSource).valid_type?("Agents::SomethingSource") { true }
  100. stub(Agents::CannotBeScheduled).valid_type?("Agents::CannotBeScheduled") { true }
  101. end
  102. describe Agents::SomethingSource do
  103. let(:new_instance) do
  104. agent = Agents::SomethingSource.new(:name => "some agent")
  105. agent.user = users(:bob)
  106. agent
  107. end
  108. it_behaves_like LiquidInterpolatable
  109. it_behaves_like HasGuid
  110. end
  111. describe ".short_type" do
  112. it "returns a short name without 'Agents::'" do
  113. expect(Agents::SomethingSource.new.short_type).to eq("SomethingSource")
  114. expect(Agents::CannotBeScheduled.new.short_type).to eq("CannotBeScheduled")
  115. end
  116. end
  117. describe ".default_schedule" do
  118. it "stores the default on the class" do
  119. expect(Agents::SomethingSource.default_schedule).to eq("2pm")
  120. expect(Agents::SomethingSource.new.default_schedule).to eq("2pm")
  121. end
  122. it "sets the default on new instances, allows setting new schedules, and prevents invalid schedules" do
  123. @checker = Agents::SomethingSource.new(:name => "something")
  124. @checker.user = users(:bob)
  125. expect(@checker.schedule).to eq("2pm")
  126. @checker.save!
  127. expect(@checker.reload.schedule).to eq("2pm")
  128. @checker.update_attribute :schedule, "5pm"
  129. expect(@checker.reload.schedule).to eq("5pm")
  130. expect(@checker.reload.schedule).to eq("5pm")
  131. @checker.schedule = "this_is_not_real"
  132. expect(@checker).to have(1).errors_on(:schedule)
  133. end
  134. it "should have an empty schedule if it cannot_be_scheduled" do
  135. @checker = Agents::CannotBeScheduled.new(:name => "something")
  136. @checker.user = users(:bob)
  137. expect(@checker.schedule).to be_nil
  138. expect(@checker).to be_valid
  139. @checker.schedule = "5pm"
  140. @checker.save!
  141. expect(@checker.schedule).to be_nil
  142. @checker.schedule = "5pm"
  143. expect(@checker).to have(0).errors_on(:schedule)
  144. expect(@checker.schedule).to be_nil
  145. end
  146. end
  147. describe "#create_event" do
  148. before do
  149. @checker = Agents::SomethingSource.new(:name => "something")
  150. @checker.user = users(:bob)
  151. @checker.save!
  152. end
  153. it "should use the checker's user" do
  154. @checker.check
  155. expect(Event.last.user).to eq(@checker.user)
  156. end
  157. it "should log an error if the Agent has been marked with 'cannot_create_events!'" do
  158. mock(@checker).can_create_events? { false }
  159. expect {
  160. @checker.check
  161. }.not_to change { Event.count }
  162. expect(@checker.logs.first.message).to match(/cannot create events/i)
  163. end
  164. end
  165. describe ".async_check" do
  166. before do
  167. @checker = Agents::SomethingSource.new(:name => "something")
  168. @checker.user = users(:bob)
  169. @checker.save!
  170. end
  171. it "records last_check_at and calls check on the given Agent" do
  172. mock(@checker).check.once {
  173. @checker.options[:new] = true
  174. }
  175. mock(Agent).find(@checker.id) { @checker }
  176. expect(@checker.last_check_at).to be_nil
  177. Agents::SomethingSource.async_check(@checker.id)
  178. expect(@checker.reload.last_check_at).to be_within(2).of(Time.now)
  179. expect(@checker.reload.options[:new]).to be_truthy # Show that we save options
  180. end
  181. it "should log exceptions" do
  182. mock(@checker).check.once {
  183. raise "foo"
  184. }
  185. mock(Agent).find(@checker.id) { @checker }
  186. expect {
  187. Agents::SomethingSource.async_check(@checker.id)
  188. }.to raise_error
  189. log = @checker.logs.first
  190. expect(log.message).to match(/Exception/)
  191. expect(log.level).to eq(4)
  192. end
  193. it "should not run disabled Agents" do
  194. mock(Agent).find(agents(:bob_weather_agent).id) { agents(:bob_weather_agent) }
  195. do_not_allow(agents(:bob_weather_agent)).check
  196. agents(:bob_weather_agent).update_attribute :disabled, true
  197. Agent.async_check(agents(:bob_weather_agent).id)
  198. end
  199. end
  200. describe ".receive!" do
  201. before do
  202. stub_request(:any, /wunderground/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/weather.json")), :status => 200)
  203. stub.any_instance_of(Agents::WeatherAgent).is_tomorrow?(anything) { true }
  204. end
  205. it "should use available events" do
  206. Agent.async_check(agents(:bob_weather_agent).id)
  207. mock(Agent).async_receive(agents(:bob_rain_notifier_agent).id, anything).times(1)
  208. Agent.receive!
  209. end
  210. it "should not propogate to disabled Agents" do
  211. Agent.async_check(agents(:bob_weather_agent).id)
  212. agents(:bob_rain_notifier_agent).update_attribute :disabled, true
  213. mock(Agent).async_receive(agents(:bob_rain_notifier_agent).id, anything).times(0)
  214. Agent.receive!
  215. end
  216. it "should log exceptions" do
  217. mock.any_instance_of(Agents::TriggerAgent).receive(anything).once {
  218. raise "foo"
  219. }
  220. Agent.async_check(agents(:bob_weather_agent).id)
  221. expect {
  222. Agent.async_receive(agents(:bob_rain_notifier_agent).id, [agents(:bob_weather_agent).events.last.id])
  223. }.to raise_error
  224. log = agents(:bob_rain_notifier_agent).logs.first
  225. expect(log.message).to match(/Exception/)
  226. expect(log.level).to eq(4)
  227. end
  228. it "should track when events have been seen and not received them again" do
  229. mock.any_instance_of(Agents::TriggerAgent).receive(anything).once
  230. Agent.async_check(agents(:bob_weather_agent).id)
  231. expect {
  232. Agent.receive!
  233. }.to change { agents(:bob_rain_notifier_agent).reload.last_checked_event_id }
  234. expect {
  235. Agent.receive!
  236. }.not_to change { agents(:bob_rain_notifier_agent).reload.last_checked_event_id }
  237. end
  238. it "should not run consumers that have nothing to do" do
  239. do_not_allow.any_instance_of(Agents::TriggerAgent).receive(anything)
  240. Agent.receive!
  241. end
  242. it "should group events" do
  243. mock.any_instance_of(Agents::TriggerAgent).receive(anything).twice { |events|
  244. expect(events.map(&:user).map(&:username).uniq.length).to eq(1)
  245. }
  246. Agent.async_check(agents(:bob_weather_agent).id)
  247. Agent.async_check(agents(:jane_weather_agent).id)
  248. Agent.receive!
  249. end
  250. it "should ignore events that were created before a particular Link" do
  251. agent2 = Agents::SomethingSource.new(:name => "something")
  252. agent2.user = users(:bob)
  253. agent2.save!
  254. agent2.check
  255. mock.any_instance_of(Agents::TriggerAgent).receive(anything).twice
  256. agents(:bob_weather_agent).check # bob_weather_agent makes an event
  257. expect {
  258. Agent.receive! # event gets propagated
  259. }.to change { agents(:bob_rain_notifier_agent).reload.last_checked_event_id }
  260. # This agent creates a few events before we link to it, but after our last check.
  261. agent2.check
  262. agent2.check
  263. # Now we link to it.
  264. agents(:bob_rain_notifier_agent).sources << agent2
  265. expect(agent2.links_as_source.first.event_id_at_creation).to eq(agent2.events.reorder("events.id desc").first.id)
  266. expect {
  267. Agent.receive! # but we don't receive those events because they're too old
  268. }.not_to change { agents(:bob_rain_notifier_agent).reload.last_checked_event_id }
  269. # Now a new event is created by agent2
  270. agent2.check
  271. expect {
  272. Agent.receive! # and we receive it
  273. }.to change { agents(:bob_rain_notifier_agent).reload.last_checked_event_id }
  274. end
  275. end
  276. describe ".async_receive" do
  277. it "should not run disabled Agents" do
  278. mock(Agent).find(agents(:bob_rain_notifier_agent).id) { agents(:bob_rain_notifier_agent) }
  279. do_not_allow(agents(:bob_rain_notifier_agent)).receive
  280. agents(:bob_rain_notifier_agent).update_attribute :disabled, true
  281. Agent.async_receive(agents(:bob_rain_notifier_agent).id, [1, 2, 3])
  282. end
  283. end
  284. describe "creating a new agent and then calling .receive!" do
  285. it "should not backfill events for a newly created agent" do
  286. Event.delete_all
  287. sender = Agents::SomethingSource.new(:name => "Sending Agent")
  288. sender.user = users(:bob)
  289. sender.save!
  290. sender.create_event :payload => {}
  291. sender.create_event :payload => {}
  292. expect(sender.events.count).to eq(2)
  293. receiver = Agents::CannotBeScheduled.new(:name => "Receiving Agent")
  294. receiver.user = users(:bob)
  295. receiver.sources << sender
  296. receiver.save!
  297. expect(receiver.events.count).to eq(0)
  298. Agent.receive!
  299. expect(receiver.events.count).to eq(0)
  300. sender.create_event :payload => {}
  301. Agent.receive!
  302. expect(receiver.events.count).to eq(1)
  303. end
  304. end
  305. describe "creating agents with propagate_immediately = true" do
  306. it "should schedule subagent events immediately" do
  307. Event.delete_all
  308. sender = Agents::SomethingSource.new(:name => "Sending Agent")
  309. sender.user = users(:bob)
  310. sender.save!
  311. receiver = Agents::CannotBeScheduled.new(
  312. :name => "Receiving Agent",
  313. )
  314. receiver.propagate_immediately = true
  315. receiver.user = users(:bob)
  316. receiver.sources << sender
  317. receiver.save!
  318. sender.create_event :payload => {"message" => "new payload"}
  319. expect(sender.events.count).to eq(1)
  320. expect(receiver.events.count).to eq(1)
  321. #should be true without calling Agent.receive!
  322. end
  323. it "should only schedule receiving agents that are set to propagate_immediately" do
  324. Event.delete_all
  325. sender = Agents::SomethingSource.new(:name => "Sending Agent")
  326. sender.user = users(:bob)
  327. sender.save!
  328. im_receiver = Agents::CannotBeScheduled.new(
  329. :name => "Immediate Receiving Agent",
  330. )
  331. im_receiver.propagate_immediately = true
  332. im_receiver.user = users(:bob)
  333. im_receiver.sources << sender
  334. im_receiver.save!
  335. slow_receiver = Agents::CannotBeScheduled.new(
  336. :name => "Slow Receiving Agent",
  337. )
  338. slow_receiver.user = users(:bob)
  339. slow_receiver.sources << sender
  340. slow_receiver.save!
  341. sender.create_event :payload => {"message" => "new payload"}
  342. expect(sender.events.count).to eq(1)
  343. expect(im_receiver.events.count).to eq(1)
  344. #we should get the quick one
  345. #but not the slow one
  346. expect(slow_receiver.events.count).to eq(0)
  347. Agent.receive!
  348. #now we should have one in both
  349. expect(im_receiver.events.count).to eq(1)
  350. expect(slow_receiver.events.count).to eq(1)
  351. end
  352. end
  353. describe "validations" do
  354. it "calls validate_options" do
  355. agent = Agents::SomethingSource.new(:name => "something")
  356. agent.user = users(:bob)
  357. agent.options[:bad] = true
  358. expect(agent).to have(1).error_on(:base)
  359. agent.options[:bad] = false
  360. expect(agent).to have(0).errors_on(:base)
  361. end
  362. it "makes options symbol-indifferent before validating" do
  363. agent = Agents::SomethingSource.new(:name => "something")
  364. agent.user = users(:bob)
  365. agent.options["bad"] = true
  366. expect(agent).to have(1).error_on(:base)
  367. agent.options["bad"] = false
  368. expect(agent).to have(0).errors_on(:base)
  369. end
  370. it "makes memory symbol-indifferent before validating" do
  371. agent = Agents::SomethingSource.new(:name => "something")
  372. agent.user = users(:bob)
  373. agent.memory["bad"] = 2
  374. agent.save
  375. expect(agent.memory[:bad]).to eq(2)
  376. end
  377. it "should work when assigned a hash or JSON string" do
  378. agent = Agents::SomethingSource.new(:name => "something")
  379. agent.memory = {}
  380. expect(agent.memory).to eq({})
  381. expect(agent.memory["foo"]).to be_nil
  382. agent.memory = ""
  383. expect(agent.memory["foo"]).to be_nil
  384. expect(agent.memory).to eq({})
  385. agent.memory = '{"hi": "there"}'
  386. expect(agent.memory).to eq({ "hi" => "there" })
  387. agent.memory = '{invalid}'
  388. expect(agent.memory).to eq({ "hi" => "there" })
  389. expect(agent).to have(1).errors_on(:memory)
  390. agent.memory = "{}"
  391. expect(agent.memory["foo"]).to be_nil
  392. expect(agent.memory).to eq({})
  393. expect(agent).to have(0).errors_on(:memory)
  394. agent.options = "{}"
  395. expect(agent.options["foo"]).to be_nil
  396. expect(agent.options).to eq({})
  397. expect(agent).to have(0).errors_on(:options)
  398. agent.options = '{"hi": 2}'
  399. expect(agent.options["hi"]).to eq(2)
  400. expect(agent).to have(0).errors_on(:options)
  401. agent.options = '{"hi": wut}'
  402. expect(agent.options["hi"]).to eq(2)
  403. expect(agent).to have(1).errors_on(:options)
  404. expect(agent.errors_on(:options)).to include("was assigned invalid JSON")
  405. agent.options = 5
  406. expect(agent.options["hi"]).to eq(2)
  407. expect(agent).to have(1).errors_on(:options)
  408. expect(agent.errors_on(:options)).to include("cannot be set to an instance of Fixnum")
  409. end
  410. it "should not allow source agents owned by other people" do
  411. agent = Agents::SomethingSource.new(:name => "something")
  412. agent.user = users(:bob)
  413. agent.source_ids = [agents(:bob_weather_agent).id]
  414. expect(agent).to have(0).errors_on(:sources)
  415. agent.source_ids = [agents(:jane_weather_agent).id]
  416. expect(agent).to have(1).errors_on(:sources)
  417. agent.user = users(:jane)
  418. expect(agent).to have(0).errors_on(:sources)
  419. end
  420. it "should not allow controller agents owned by other people" do
  421. agent = Agents::SomethingSource.new(:name => "something")
  422. agent.user = users(:bob)
  423. agent.controller_ids = [agents(:bob_weather_agent).id]
  424. expect(agent).to have(0).errors_on(:controllers)
  425. agent.controller_ids = [agents(:jane_weather_agent).id]
  426. expect(agent).to have(1).errors_on(:controllers)
  427. agent.user = users(:jane)
  428. expect(agent).to have(0).errors_on(:controllers)
  429. end
  430. it "should not allow control target agents owned by other people" do
  431. agent = Agents::CannotBeScheduled.new(:name => "something")
  432. agent.user = users(:bob)
  433. agent.control_target_ids = [agents(:bob_weather_agent).id]
  434. expect(agent).to have(0).errors_on(:control_targets)
  435. agent.control_target_ids = [agents(:jane_weather_agent).id]
  436. expect(agent).to have(1).errors_on(:control_targets)
  437. agent.user = users(:jane)
  438. expect(agent).to have(0).errors_on(:control_targets)
  439. end
  440. it "should not allow scenarios owned by other people" do
  441. agent = Agents::SomethingSource.new(:name => "something")
  442. agent.user = users(:bob)
  443. agent.scenario_ids = [scenarios(:bob_weather).id]
  444. expect(agent).to have(0).errors_on(:scenarios)
  445. agent.scenario_ids = [scenarios(:bob_weather).id, scenarios(:jane_weather).id]
  446. expect(agent).to have(1).errors_on(:scenarios)
  447. agent.scenario_ids = [scenarios(:jane_weather).id]
  448. expect(agent).to have(1).errors_on(:scenarios)
  449. agent.user = users(:jane)
  450. expect(agent).to have(0).errors_on(:scenarios)
  451. end
  452. it "validates keep_events_for" do
  453. agent = Agents::SomethingSource.new(:name => "something")
  454. agent.user = users(:bob)
  455. expect(agent).to be_valid
  456. agent.keep_events_for = nil
  457. expect(agent).to have(1).errors_on(:keep_events_for)
  458. agent.keep_events_for = 1000
  459. expect(agent).to have(1).errors_on(:keep_events_for)
  460. agent.keep_events_for = ""
  461. expect(agent).to have(1).errors_on(:keep_events_for)
  462. agent.keep_events_for = 5
  463. expect(agent).to be_valid
  464. agent.keep_events_for = 0
  465. expect(agent).to be_valid
  466. agent.keep_events_for = 365
  467. expect(agent).to be_valid
  468. # Rails seems to call to_i on the input. This guards against future changes to that behavior.
  469. agent.keep_events_for = "drop table;"
  470. expect(agent.keep_events_for).to eq(0)
  471. end
  472. end
  473. describe "cleaning up now-expired events" do
  474. before do
  475. @time = "2014-01-01 01:00:00 +00:00"
  476. time_travel_to @time do
  477. @agent = Agents::SomethingSource.new(:name => "something")
  478. @agent.keep_events_for = 5
  479. @agent.user = users(:bob)
  480. @agent.save!
  481. @event = @agent.create_event :payload => { "hello" => "world" }
  482. expect(@event.expires_at.to_i).to be_within(2).of(5.days.from_now.to_i)
  483. end
  484. end
  485. describe "when keep_events_for has not changed" do
  486. it "does nothing" do
  487. mock(@agent).update_event_expirations!.times(0)
  488. @agent.options[:foo] = "bar1"
  489. @agent.save!
  490. @agent.options[:foo] = "bar1"
  491. @agent.keep_events_for = 5
  492. @agent.save!
  493. end
  494. end
  495. describe "when keep_events_for is changed" do
  496. it "updates events' expires_at" do
  497. time_travel_to @time do
  498. expect {
  499. @agent.options[:foo] = "bar1"
  500. @agent.keep_events_for = 3
  501. @agent.save!
  502. }.to change { @event.reload.expires_at }
  503. expect(@event.expires_at.to_i).to be_within(2).of(3.days.from_now.to_i)
  504. end
  505. end
  506. it "updates events relative to their created_at" do
  507. @event.update_attribute :created_at, 2.days.ago
  508. expect(@event.reload.created_at.to_i).to be_within(2).of(2.days.ago.to_i)
  509. expect {
  510. @agent.options[:foo] = "bar2"
  511. @agent.keep_events_for = 3
  512. @agent.save!
  513. }.to change { @event.reload.expires_at }
  514. expect(@event.expires_at.to_i).to be_within(60 * 61).of(1.days.from_now.to_i) # The larger time is to deal with daylight savings
  515. end
  516. it "nulls out expires_at when keep_events_for is set to 0" do
  517. expect {
  518. @agent.options[:foo] = "bar"
  519. @agent.keep_events_for = 0
  520. @agent.save!
  521. }.to change { @event.reload.expires_at }.to(nil)
  522. end
  523. end
  524. end
  525. describe "Agent.build_clone" do
  526. before do
  527. Event.delete_all
  528. @sender = Agents::SomethingSource.new(
  529. name: 'Agent (2)',
  530. options: { foo: 'bar2' },
  531. schedule: '5pm')
  532. @sender.user = users(:bob)
  533. @sender.save!
  534. @sender.create_event :payload => {}
  535. @sender.create_event :payload => {}
  536. expect(@sender.events.count).to eq(2)
  537. @receiver = Agents::CannotBeScheduled.new(
  538. name: 'Agent',
  539. options: { foo: 'bar3' },
  540. keep_events_for: 3,
  541. propagate_immediately: true)
  542. @receiver.user = users(:bob)
  543. @receiver.sources << @sender
  544. @receiver.memory[:test] = 1
  545. @receiver.save!
  546. end
  547. it "should create a clone of a given agent for editing" do
  548. sender_clone = users(:bob).agents.build_clone(@sender)
  549. expect(sender_clone.attributes).to eq(Agent.new.attributes.
  550. update(@sender.slice(:user_id, :type,
  551. :options, :schedule, :keep_events_for, :propagate_immediately)).
  552. update('name' => 'Agent (2) (2)', 'options' => { 'foo' => 'bar2' }))
  553. expect(sender_clone.source_ids).to eq([])
  554. receiver_clone = users(:bob).agents.build_clone(@receiver)
  555. expect(receiver_clone.attributes).to eq(Agent.new.attributes.
  556. update(@receiver.slice(:user_id, :type,
  557. :options, :schedule, :keep_events_for, :propagate_immediately)).
  558. update('name' => 'Agent (3)', 'options' => { 'foo' => 'bar3' }))
  559. expect(receiver_clone.source_ids).to eq([@sender.id])
  560. end
  561. end
  562. end
  563. describe ".trigger_web_request" do
  564. class Agents::WebRequestReceiver < Agent
  565. cannot_be_scheduled!
  566. end
  567. before do
  568. stub(Agents::WebRequestReceiver).valid_type?("Agents::WebRequestReceiver") { true }
  569. end
  570. context "when .receive_web_request is defined" do
  571. before do
  572. @agent = Agents::WebRequestReceiver.new(:name => "something")
  573. @agent.user = users(:bob)
  574. @agent.save!
  575. def @agent.receive_web_request(params, method, format)
  576. memory['last_request'] = [params, method, format]
  577. ['Ok!', 200]
  578. end
  579. end
  580. it "calls the .receive_web_request hook, updates last_web_request_at, and saves" do
  581. @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html")
  582. expect(@agent.reload.memory['last_request']).to eq([ { "some_param" => "some_value" }, "post", "text/html" ])
  583. expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i)
  584. end
  585. end
  586. context "when .receive_webhook is defined" do
  587. before do
  588. @agent = Agents::WebRequestReceiver.new(:name => "something")
  589. @agent.user = users(:bob)
  590. @agent.save!
  591. def @agent.receive_webhook(params)
  592. memory['last_webhook_request'] = params
  593. ['Ok!', 200]
  594. end
  595. end
  596. it "outputs a deprecation warning and calls .receive_webhook with the params" do
  597. mock(Rails.logger).warn("DEPRECATED: The .receive_webhook method is deprecated, please switch your Agent to use .receive_web_request.")
  598. @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html")
  599. expect(@agent.reload.memory['last_webhook_request']).to eq({ "some_param" => "some_value" })
  600. expect(@agent.last_web_request_at.to_i).to be_within(1).of(Time.now.to_i)
  601. end
  602. end
  603. end
  604. describe "scopes" do
  605. describe "of_type" do
  606. it "should accept classes" do
  607. agents = Agent.of_type(Agents::WebsiteAgent)
  608. expect(agents).to include(agents(:bob_website_agent))
  609. expect(agents).to include(agents(:jane_website_agent))
  610. expect(agents).not_to include(agents(:bob_weather_agent))
  611. end
  612. it "should accept strings" do
  613. agents = Agent.of_type("Agents::WebsiteAgent")
  614. expect(agents).to include(agents(:bob_website_agent))
  615. expect(agents).to include(agents(:jane_website_agent))
  616. expect(agents).not_to include(agents(:bob_weather_agent))
  617. end
  618. it "should accept instances of an Agent" do
  619. agents = Agent.of_type(agents(:bob_website_agent))
  620. expect(agents).to include(agents(:bob_website_agent))
  621. expect(agents).to include(agents(:jane_website_agent))
  622. expect(agents).not_to include(agents(:bob_weather_agent))
  623. end
  624. end
  625. end
  626. describe "#create_event" do
  627. describe "when the agent has keep_events_for set" do
  628. before do
  629. expect(agents(:jane_weather_agent).keep_events_for).to be > 0
  630. end
  631. it "sets expires_at on created events" do
  632. event = agents(:jane_weather_agent).create_event :payload => { 'hi' => 'there' }
  633. expect(event.expires_at.to_i).to be_within(5).of(agents(:jane_weather_agent).keep_events_for.days.from_now.to_i)
  634. end
  635. end
  636. describe "when the agent does not have keep_events_for set" do
  637. before do
  638. expect(agents(:jane_website_agent).keep_events_for).to eq(0)
  639. end
  640. it "does not set expires_at on created events" do
  641. event = agents(:jane_website_agent).create_event :payload => { 'hi' => 'there' }
  642. expect(event.expires_at).to be_nil
  643. end
  644. end
  645. end
  646. describe '.last_checked_event_id' do
  647. it "should be updated by setting drop_pending_events to true" do
  648. agent = agents(:bob_rain_notifier_agent)
  649. agent.last_checked_event_id = nil
  650. agent.save!
  651. agent.update!(drop_pending_events: true)
  652. expect(agent.reload.last_checked_event_id).to eq(Event.maximum(:id))
  653. end
  654. it "should not affect a virtual attribute drop_pending_events" do
  655. agent = agents(:bob_rain_notifier_agent)
  656. agent.update!(drop_pending_events: true)
  657. expect(agent.reload.drop_pending_events).to eq(false)
  658. end
  659. end
  660. describe ".drop_pending_events" do
  661. before do
  662. stub_request(:any, /wunderground/).to_return(body: File.read(Rails.root.join("spec/data_fixtures/weather.json")), status: 200)
  663. stub.any_instance_of(Agents::WeatherAgent).is_tomorrow?(anything) { true }
  664. end
  665. it "should drop pending events while the agent was disabled when set to true" do
  666. agent1 = agents(:bob_weather_agent)
  667. agent2 = agents(:bob_rain_notifier_agent)
  668. expect {
  669. expect {
  670. Agent.async_check(agent1.id)
  671. Agent.receive!
  672. }.to change { agent1.events.count }.by(1)
  673. }.to change { agent2.events.count }.by(1)
  674. agent2.disabled = true
  675. agent2.save!
  676. expect {
  677. expect {
  678. Agent.async_check(agent1.id)
  679. Agent.receive!
  680. }.to change { agent1.events.count }.by(1)
  681. }.not_to change { agent2.events.count }
  682. agent2.disabled = false
  683. agent2.drop_pending_events = true
  684. agent2.save!
  685. expect {
  686. Agent.receive!
  687. }.not_to change { agent2.events.count }
  688. end
  689. end
  690. end
  691. describe AgentDrop do
  692. def interpolate(string, agent)
  693. agent.interpolate_string(string, "agent" => agent)
  694. end
  695. before do
  696. @wsa1 = Agents::WebsiteAgent.new(
  697. name: 'XKCD',
  698. options: {
  699. expected_update_period_in_days: 2,
  700. type: 'html',
  701. url: 'http://xkcd.com/',
  702. mode: 'on_change',
  703. extract: {
  704. url: { css: '#comic img', value: '@src' },
  705. title: { css: '#comic img', value: '@alt' },
  706. },
  707. },
  708. schedule: 'every_1h',
  709. keep_events_for: 2)
  710. @wsa1.user = users(:bob)
  711. @wsa1.save!
  712. @wsa2 = Agents::WebsiteAgent.new(
  713. name: 'Dilbert',
  714. options: {
  715. expected_update_period_in_days: 2,
  716. type: 'html',
  717. url: 'http://dilbert.com/',
  718. mode: 'on_change',
  719. extract: {
  720. url: { css: '[id^=strip_enlarged_] img', value: '@src' },
  721. title: { css: '.STR_DateStrip', value: './/text()' },
  722. },
  723. },
  724. schedule: 'every_12h',
  725. keep_events_for: 2)
  726. @wsa2.user = users(:bob)
  727. @wsa2.save!
  728. @efa = Agents::EventFormattingAgent.new(
  729. name: 'Formatter',
  730. options: {
  731. instructions: {
  732. message: '{{agent.name}}: {{title}} {{url}}',
  733. agent: '{{agent.type}}',
  734. },
  735. mode: 'clean',
  736. matchers: [],
  737. skip_created_at: 'false',
  738. },
  739. keep_events_for: 2,
  740. propagate_immediately: true)
  741. @efa.user = users(:bob)
  742. @efa.sources << @wsa1 << @wsa2
  743. @efa.memory[:test] = 1
  744. @efa.save!
  745. end
  746. it 'should be created via Agent#to_liquid' do
  747. expect(@wsa1.to_liquid.class).to be(AgentDrop)
  748. expect(@wsa2.to_liquid.class).to be(AgentDrop)
  749. expect(@efa.to_liquid.class).to be(AgentDrop)
  750. end
  751. it 'should have .type and .name' do
  752. t = '{{agent.type}}: {{agent.name}}'
  753. expect(interpolate(t, @wsa1)).to eq('WebsiteAgent: XKCD')
  754. expect(interpolate(t, @wsa2)).to eq('WebsiteAgent: Dilbert')
  755. expect(interpolate(t, @efa)).to eq('EventFormattingAgent: Formatter')
  756. end
  757. it 'should have .options' do
  758. t = '{{agent.options.url}}'
  759. expect(interpolate(t, @wsa1)).to eq('http://xkcd.com/')
  760. expect(interpolate(t, @wsa2)).to eq('http://dilbert.com/')
  761. expect(interpolate('{{agent.options.instructions.message}}',
  762. @efa)).to eq('{{agent.name}}: {{title}} {{url}}')
  763. end
  764. it 'should have .sources' do
  765. t = '{{agent.sources.size}}: {{agent.sources | map:"name" | join:", "}}'
  766. expect(interpolate(t, @wsa1)).to eq('0: ')
  767. expect(interpolate(t, @wsa2)).to eq('0: ')
  768. expect(interpolate(t, @efa)).to eq('2: XKCD, Dilbert')
  769. end
  770. it 'should have .receivers' do
  771. t = '{{agent.receivers.size}}: {{agent.receivers | map:"name" | join:", "}}'
  772. expect(interpolate(t, @wsa1)).to eq('1: Formatter')
  773. expect(interpolate(t, @wsa2)).to eq('1: Formatter')
  774. expect(interpolate(t, @efa)).to eq('0: ')
  775. end
  776. end