agent_spec.rb 33 KB

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