agent_spec.rb 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. Agents::WeatherAgent.count.should > 0
  21. Agents::WebsiteAgent.count.should > 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. weather_agent_ids.should 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. agents(:bob_weather_agent).credential("aws_secret").should == user_credentials(:bob_aws_secret).credential_value
  48. end
  49. it "should return nil when credential is not present" do
  50. agents(:bob_weather_agent).credential("non_existing_credential").should == nil
  51. end
  52. it "should memoize the load" do
  53. mock.any_instance_of(UserCredential).credential_value.twice { "foo" }
  54. agents(:bob_weather_agent).credential("aws_secret").should == "foo"
  55. agents(:bob_weather_agent).credential("aws_secret").should == "foo"
  56. agents(:bob_weather_agent).reload
  57. agents(:bob_weather_agent).credential("aws_secret").should == "foo"
  58. agents(:bob_weather_agent).credential("aws_secret").should == "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. source.should have(0).errors_on(:type)
  66. source.type = "Agents::WebsiteAgent"
  67. source.should have(0).errors_on(:type)
  68. source.type = "Agents::Fake"
  69. source.should 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. source.should have(1).error_on(:type)
  75. end
  76. it "should know about available types" do
  77. Agent.types.should 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 ".default_schedule" do
  112. it "stores the default on the class" do
  113. Agents::SomethingSource.default_schedule.should == "2pm"
  114. Agents::SomethingSource.new.default_schedule.should == "2pm"
  115. end
  116. it "sets the default on new instances, allows setting new schedules, and prevents invalid schedules" do
  117. @checker = Agents::SomethingSource.new(:name => "something")
  118. @checker.user = users(:bob)
  119. @checker.schedule.should == "2pm"
  120. @checker.save!
  121. @checker.reload.schedule.should == "2pm"
  122. @checker.update_attribute :schedule, "5pm"
  123. @checker.reload.schedule.should == "5pm"
  124. @checker.reload.schedule.should == "5pm"
  125. @checker.schedule = "this_is_not_real"
  126. @checker.should have(1).errors_on(:schedule)
  127. end
  128. it "should have an empty schedule if it cannot_be_scheduled" do
  129. @checker = Agents::CannotBeScheduled.new(:name => "something")
  130. @checker.user = users(:bob)
  131. @checker.schedule.should be_nil
  132. @checker.should be_valid
  133. @checker.schedule = "5pm"
  134. @checker.save!
  135. @checker.schedule.should be_nil
  136. @checker.schedule = "5pm"
  137. @checker.should have(0).errors_on(:schedule)
  138. @checker.schedule.should be_nil
  139. end
  140. end
  141. describe "#create_event" do
  142. before do
  143. @checker = Agents::SomethingSource.new(:name => "something")
  144. @checker.user = users(:bob)
  145. @checker.save!
  146. end
  147. it "should use the checker's user" do
  148. @checker.check
  149. Event.last.user.should == @checker.user
  150. end
  151. it "should log an error if the Agent has been marked with 'cannot_create_events!'" do
  152. mock(@checker).can_create_events? { false }
  153. lambda {
  154. @checker.check
  155. }.should_not change { Event.count }
  156. @checker.logs.first.message.should =~ /cannot create events/i
  157. end
  158. end
  159. describe ".async_check" do
  160. before do
  161. @checker = Agents::SomethingSource.new(:name => "something")
  162. @checker.user = users(:bob)
  163. @checker.save!
  164. end
  165. it "records last_check_at and calls check on the given Agent" do
  166. mock(@checker).check.once {
  167. @checker.options[:new] = true
  168. }
  169. mock(Agent).find(@checker.id) { @checker }
  170. @checker.last_check_at.should be_nil
  171. Agents::SomethingSource.async_check(@checker.id)
  172. @checker.reload.last_check_at.should be_within(2).of(Time.now)
  173. @checker.reload.options[:new].should be_true # Show that we save options
  174. end
  175. it "should log exceptions" do
  176. mock(@checker).check.once {
  177. raise "foo"
  178. }
  179. mock(Agent).find(@checker.id) { @checker }
  180. lambda {
  181. Agents::SomethingSource.async_check(@checker.id)
  182. }.should raise_error
  183. log = @checker.logs.first
  184. log.message.should =~ /Exception/
  185. log.level.should == 4
  186. end
  187. it "should not run disabled Agents" do
  188. mock(Agent).find(agents(:bob_weather_agent).id) { agents(:bob_weather_agent) }
  189. do_not_allow(agents(:bob_weather_agent)).check
  190. agents(:bob_weather_agent).update_attribute :disabled, true
  191. Agent.async_check(agents(:bob_weather_agent).id)
  192. end
  193. end
  194. describe ".receive!" do
  195. before do
  196. stub_request(:any, /wunderground/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/weather.json")), :status => 200)
  197. stub.any_instance_of(Agents::WeatherAgent).is_tomorrow?(anything) { true }
  198. end
  199. it "should use available events" do
  200. Agent.async_check(agents(:bob_weather_agent).id)
  201. mock(Agent).async_receive(agents(:bob_rain_notifier_agent).id, anything).times(1)
  202. Agent.receive!
  203. end
  204. it "should not propogate to disabled Agents" do
  205. Agent.async_check(agents(:bob_weather_agent).id)
  206. agents(:bob_rain_notifier_agent).update_attribute :disabled, true
  207. mock(Agent).async_receive(agents(:bob_rain_notifier_agent).id, anything).times(0)
  208. Agent.receive!
  209. end
  210. it "should log exceptions" do
  211. mock.any_instance_of(Agents::TriggerAgent).receive(anything).once {
  212. raise "foo"
  213. }
  214. Agent.async_check(agents(:bob_weather_agent).id)
  215. lambda {
  216. Agent.async_receive(agents(:bob_rain_notifier_agent).id, [agents(:bob_weather_agent).events.last.id])
  217. }.should raise_error
  218. log = agents(:bob_rain_notifier_agent).logs.first
  219. log.message.should =~ /Exception/
  220. log.level.should == 4
  221. end
  222. it "should track when events have been seen and not received them again" do
  223. mock.any_instance_of(Agents::TriggerAgent).receive(anything).once
  224. Agent.async_check(agents(:bob_weather_agent).id)
  225. lambda {
  226. Agent.receive!
  227. }.should change { agents(:bob_rain_notifier_agent).reload.last_checked_event_id }
  228. lambda {
  229. Agent.receive!
  230. }.should_not change { agents(:bob_rain_notifier_agent).reload.last_checked_event_id }
  231. end
  232. it "should not run consumers that have nothing to do" do
  233. do_not_allow.any_instance_of(Agents::TriggerAgent).receive(anything)
  234. Agent.receive!
  235. end
  236. it "should group events" do
  237. mock.any_instance_of(Agents::TriggerAgent).receive(anything).twice { |events|
  238. events.map(&:user).map(&:username).uniq.length.should == 1
  239. }
  240. Agent.async_check(agents(:bob_weather_agent).id)
  241. Agent.async_check(agents(:jane_weather_agent).id)
  242. Agent.receive!
  243. end
  244. it "should ignore events that were created before a particular Link" do
  245. agent2 = Agents::SomethingSource.new(:name => "something")
  246. agent2.user = users(:bob)
  247. agent2.save!
  248. agent2.check
  249. mock.any_instance_of(Agents::TriggerAgent).receive(anything).twice
  250. agents(:bob_weather_agent).check # bob_weather_agent makes an event
  251. lambda {
  252. Agent.receive! # event gets propagated
  253. }.should change { agents(:bob_rain_notifier_agent).reload.last_checked_event_id }
  254. # This agent creates a few events before we link to it, but after our last check.
  255. agent2.check
  256. agent2.check
  257. # Now we link to it.
  258. agents(:bob_rain_notifier_agent).sources << agent2
  259. agent2.links_as_source.first.event_id_at_creation.should == agent2.events.reorder("events.id desc").first.id
  260. lambda {
  261. Agent.receive! # but we don't receive those events because they're too old
  262. }.should_not change { agents(:bob_rain_notifier_agent).reload.last_checked_event_id }
  263. # Now a new event is created by agent2
  264. agent2.check
  265. lambda {
  266. Agent.receive! # and we receive it
  267. }.should change { agents(:bob_rain_notifier_agent).reload.last_checked_event_id }
  268. end
  269. end
  270. describe ".async_receive" do
  271. it "should not run disabled Agents" do
  272. mock(Agent).find(agents(:bob_rain_notifier_agent).id) { agents(:bob_rain_notifier_agent) }
  273. do_not_allow(agents(:bob_rain_notifier_agent)).receive
  274. agents(:bob_rain_notifier_agent).update_attribute :disabled, true
  275. Agent.async_receive(agents(:bob_rain_notifier_agent).id, [1, 2, 3])
  276. end
  277. end
  278. describe "creating a new agent and then calling .receive!" do
  279. it "should not backfill events for a newly created agent" do
  280. Event.delete_all
  281. sender = Agents::SomethingSource.new(:name => "Sending Agent")
  282. sender.user = users(:bob)
  283. sender.save!
  284. sender.create_event :payload => {}
  285. sender.create_event :payload => {}
  286. sender.events.count.should == 2
  287. receiver = Agents::CannotBeScheduled.new(:name => "Receiving Agent")
  288. receiver.user = users(:bob)
  289. receiver.sources << sender
  290. receiver.save!
  291. receiver.events.count.should == 0
  292. Agent.receive!
  293. receiver.events.count.should == 0
  294. sender.create_event :payload => {}
  295. Agent.receive!
  296. receiver.events.count.should == 1
  297. end
  298. end
  299. describe "creating agents with propagate_immediately = true" do
  300. it "should schedule subagent events immediately" do
  301. Event.delete_all
  302. sender = Agents::SomethingSource.new(:name => "Sending Agent")
  303. sender.user = users(:bob)
  304. sender.save!
  305. receiver = Agents::CannotBeScheduled.new(
  306. :name => "Receiving Agent",
  307. )
  308. receiver.propagate_immediately = true
  309. receiver.user = users(:bob)
  310. receiver.sources << sender
  311. receiver.save!
  312. sender.create_event :payload => {"message" => "new payload"}
  313. sender.events.count.should == 1
  314. receiver.events.count.should == 1
  315. #should be true without calling Agent.receive!
  316. end
  317. it "should only schedule receiving agents that are set to propagate_immediately" do
  318. Event.delete_all
  319. sender = Agents::SomethingSource.new(:name => "Sending Agent")
  320. sender.user = users(:bob)
  321. sender.save!
  322. im_receiver = Agents::CannotBeScheduled.new(
  323. :name => "Immediate Receiving Agent",
  324. )
  325. im_receiver.propagate_immediately = true
  326. im_receiver.user = users(:bob)
  327. im_receiver.sources << sender
  328. im_receiver.save!
  329. slow_receiver = Agents::CannotBeScheduled.new(
  330. :name => "Slow Receiving Agent",
  331. )
  332. slow_receiver.user = users(:bob)
  333. slow_receiver.sources << sender
  334. slow_receiver.save!
  335. sender.create_event :payload => {"message" => "new payload"}
  336. sender.events.count.should == 1
  337. im_receiver.events.count.should == 1
  338. #we should get the quick one
  339. #but not the slow one
  340. slow_receiver.events.count.should == 0
  341. Agent.receive!
  342. #now we should have one in both
  343. im_receiver.events.count.should == 1
  344. slow_receiver.events.count.should == 1
  345. end
  346. end
  347. describe "validations" do
  348. it "calls validate_options" do
  349. agent = Agents::SomethingSource.new(:name => "something")
  350. agent.user = users(:bob)
  351. agent.options[:bad] = true
  352. agent.should have(1).error_on(:base)
  353. agent.options[:bad] = false
  354. agent.should have(0).errors_on(:base)
  355. end
  356. it "makes options symbol-indifferent before validating" do
  357. agent = Agents::SomethingSource.new(:name => "something")
  358. agent.user = users(:bob)
  359. agent.options["bad"] = true
  360. agent.should have(1).error_on(:base)
  361. agent.options["bad"] = false
  362. agent.should have(0).errors_on(:base)
  363. end
  364. it "makes memory symbol-indifferent before validating" do
  365. agent = Agents::SomethingSource.new(:name => "something")
  366. agent.user = users(:bob)
  367. agent.memory["bad"] = 2
  368. agent.save
  369. agent.memory[:bad].should == 2
  370. end
  371. it "should work when assigned a hash or JSON string" do
  372. agent = Agents::SomethingSource.new(:name => "something")
  373. agent.memory = {}
  374. agent.memory.should == {}
  375. agent.memory["foo"].should be_nil
  376. agent.memory = ""
  377. agent.memory["foo"].should be_nil
  378. agent.memory.should == {}
  379. agent.memory = '{"hi": "there"}'
  380. agent.memory.should == { "hi" => "there" }
  381. agent.memory = '{invalid}'
  382. agent.memory.should == { "hi" => "there" }
  383. agent.should have(1).errors_on(:memory)
  384. agent.memory = "{}"
  385. agent.memory["foo"].should be_nil
  386. agent.memory.should == {}
  387. agent.should have(0).errors_on(:memory)
  388. agent.options = "{}"
  389. agent.options["foo"].should be_nil
  390. agent.options.should == {}
  391. agent.should have(0).errors_on(:options)
  392. agent.options = '{"hi": 2}'
  393. agent.options["hi"].should == 2
  394. agent.should have(0).errors_on(:options)
  395. agent.options = '{"hi": wut}'
  396. agent.options["hi"].should == 2
  397. agent.should have(1).errors_on(:options)
  398. agent.errors_on(:options).should include("was assigned invalid JSON")
  399. agent.options = 5
  400. agent.options["hi"].should == 2
  401. agent.should have(1).errors_on(:options)
  402. agent.errors_on(:options).should include("cannot be set to an instance of Fixnum")
  403. end
  404. it "should not allow agents owned by other people" do
  405. agent = Agents::SomethingSource.new(:name => "something")
  406. agent.user = users(:bob)
  407. agent.source_ids = [agents(:bob_weather_agent).id]
  408. agent.should have(0).errors_on(:sources)
  409. agent.source_ids = [agents(:jane_weather_agent).id]
  410. agent.should have(1).errors_on(:sources)
  411. agent.user = users(:jane)
  412. agent.should have(0).errors_on(:sources)
  413. end
  414. it "should not allow scenarios owned by other people" do
  415. agent = Agents::SomethingSource.new(:name => "something")
  416. agent.user = users(:bob)
  417. agent.scenario_ids = [scenarios(:bob_weather).id]
  418. agent.should have(0).errors_on(:scenarios)
  419. agent.scenario_ids = [scenarios(:bob_weather).id, scenarios(:jane_weather).id]
  420. agent.should have(1).errors_on(:scenarios)
  421. agent.scenario_ids = [scenarios(:jane_weather).id]
  422. agent.should have(1).errors_on(:scenarios)
  423. agent.user = users(:jane)
  424. agent.should have(0).errors_on(:scenarios)
  425. end
  426. it "validates keep_events_for" do
  427. agent = Agents::SomethingSource.new(:name => "something")
  428. agent.user = users(:bob)
  429. agent.should be_valid
  430. agent.keep_events_for = nil
  431. agent.should have(1).errors_on(:keep_events_for)
  432. agent.keep_events_for = 1000
  433. agent.should have(1).errors_on(:keep_events_for)
  434. agent.keep_events_for = ""
  435. agent.should have(1).errors_on(:keep_events_for)
  436. agent.keep_events_for = 5
  437. agent.should be_valid
  438. agent.keep_events_for = 0
  439. agent.should be_valid
  440. agent.keep_events_for = 365
  441. agent.should be_valid
  442. # Rails seems to call to_i on the input. This guards against future changes to that behavior.
  443. agent.keep_events_for = "drop table;"
  444. agent.keep_events_for.should == 0
  445. end
  446. end
  447. describe "cleaning up now-expired events" do
  448. before do
  449. @agent = Agents::SomethingSource.new(:name => "something")
  450. @agent.keep_events_for = 5
  451. @agent.user = users(:bob)
  452. @agent.save!
  453. @event = @agent.create_event :payload => { "hello" => "world" }
  454. @event.expires_at.to_i.should be_within(2).of(5.days.from_now.to_i)
  455. end
  456. describe "when keep_events_for has not changed" do
  457. it "does nothing" do
  458. mock(@agent).update_event_expirations!.times(0)
  459. @agent.options[:foo] = "bar1"
  460. @agent.save!
  461. @agent.options[:foo] = "bar1"
  462. @agent.keep_events_for = 5
  463. @agent.save!
  464. end
  465. end
  466. describe "when keep_events_for is changed" do
  467. it "updates events' expires_at" do
  468. lambda {
  469. @agent.options[:foo] = "bar1"
  470. @agent.keep_events_for = 3
  471. @agent.save!
  472. }.should change { @event.reload.expires_at }
  473. @event.expires_at.to_i.should be_within(2).of(3.days.from_now.to_i)
  474. end
  475. it "updates events relative to their created_at" do
  476. @event.update_attribute :created_at, 2.days.ago
  477. @event.reload.created_at.to_i.should be_within(2).of(2.days.ago.to_i)
  478. lambda {
  479. @agent.options[:foo] = "bar2"
  480. @agent.keep_events_for = 3
  481. @agent.save!
  482. }.should change { @event.reload.expires_at }
  483. @event.expires_at.to_i.should be_within(60 * 61).of(1.days.from_now.to_i) # The larger time is to deal with daylight savings
  484. end
  485. it "nulls out expires_at when keep_events_for is set to 0" do
  486. lambda {
  487. @agent.options[:foo] = "bar"
  488. @agent.keep_events_for = 0
  489. @agent.save!
  490. }.should change { @event.reload.expires_at }.to(nil)
  491. end
  492. end
  493. end
  494. describe "Agent.build_clone" do
  495. before do
  496. Event.delete_all
  497. @sender = Agents::SomethingSource.new(
  498. name: 'Agent (2)',
  499. options: { foo: 'bar2' },
  500. schedule: '5pm')
  501. @sender.user = users(:bob)
  502. @sender.save!
  503. @sender.create_event :payload => {}
  504. @sender.create_event :payload => {}
  505. @sender.events.count.should == 2
  506. @receiver = Agents::CannotBeScheduled.new(
  507. name: 'Agent',
  508. options: { foo: 'bar3' },
  509. keep_events_for: 3,
  510. propagate_immediately: true)
  511. @receiver.user = users(:bob)
  512. @receiver.sources << @sender
  513. @receiver.memory[:test] = 1
  514. @receiver.save!
  515. end
  516. it "should create a clone of a given agent for editing" do
  517. sender_clone = users(:bob).agents.build_clone(@sender)
  518. sender_clone.attributes.should == Agent.new.attributes.
  519. update(@sender.slice(:user_id, :type,
  520. :options, :schedule, :keep_events_for, :propagate_immediately)).
  521. update('name' => 'Agent (2) (2)', 'options' => { 'foo' => 'bar2' })
  522. sender_clone.source_ids.should == []
  523. receiver_clone = users(:bob).agents.build_clone(@receiver)
  524. receiver_clone.attributes.should == Agent.new.attributes.
  525. update(@receiver.slice(:user_id, :type,
  526. :options, :schedule, :keep_events_for, :propagate_immediately)).
  527. update('name' => 'Agent (3)', 'options' => { 'foo' => 'bar3' })
  528. receiver_clone.source_ids.should == [@sender.id]
  529. end
  530. end
  531. end
  532. describe ".trigger_web_request" do
  533. class Agents::WebRequestReceiver < Agent
  534. cannot_be_scheduled!
  535. end
  536. before do
  537. stub(Agents::WebRequestReceiver).valid_type?("Agents::WebRequestReceiver") { true }
  538. end
  539. context "when .receive_web_request is defined" do
  540. before do
  541. @agent = Agents::WebRequestReceiver.new(:name => "something")
  542. @agent.user = users(:bob)
  543. @agent.save!
  544. def @agent.receive_web_request(params, method, format)
  545. memory['last_request'] = [params, method, format]
  546. ['Ok!', 200]
  547. end
  548. end
  549. it "calls the .receive_web_request hook, updates last_web_request_at, and saves" do
  550. @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html")
  551. @agent.reload.memory['last_request'].should == [ { "some_param" => "some_value" }, "post", "text/html" ]
  552. @agent.last_web_request_at.to_i.should be_within(1).of(Time.now.to_i)
  553. end
  554. end
  555. context "when .receive_webhook is defined" do
  556. before do
  557. @agent = Agents::WebRequestReceiver.new(:name => "something")
  558. @agent.user = users(:bob)
  559. @agent.save!
  560. def @agent.receive_webhook(params)
  561. memory['last_webhook_request'] = params
  562. ['Ok!', 200]
  563. end
  564. end
  565. it "outputs a deprecation warning and calls .receive_webhook with the params" do
  566. mock(Rails.logger).warn("DEPRECATED: The .receive_webhook method is deprecated, please switch your Agent to use .receive_web_request.")
  567. @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html")
  568. @agent.reload.memory['last_webhook_request'].should == { "some_param" => "some_value" }
  569. @agent.last_web_request_at.to_i.should be_within(1).of(Time.now.to_i)
  570. end
  571. end
  572. end
  573. describe "scopes" do
  574. describe "of_type" do
  575. it "should accept classes" do
  576. agents = Agent.of_type(Agents::WebsiteAgent)
  577. agents.should include(agents(:bob_website_agent))
  578. agents.should include(agents(:jane_website_agent))
  579. agents.should_not include(agents(:bob_weather_agent))
  580. end
  581. it "should accept strings" do
  582. agents = Agent.of_type("Agents::WebsiteAgent")
  583. agents.should include(agents(:bob_website_agent))
  584. agents.should include(agents(:jane_website_agent))
  585. agents.should_not include(agents(:bob_weather_agent))
  586. end
  587. it "should accept instances of an Agent" do
  588. agents = Agent.of_type(agents(:bob_website_agent))
  589. agents.should include(agents(:bob_website_agent))
  590. agents.should include(agents(:jane_website_agent))
  591. agents.should_not include(agents(:bob_weather_agent))
  592. end
  593. end
  594. end
  595. describe "#create_event" do
  596. describe "when the agent has keep_events_for set" do
  597. before do
  598. agents(:jane_weather_agent).keep_events_for.should > 0
  599. end
  600. it "sets expires_at on created events" do
  601. event = agents(:jane_weather_agent).create_event :payload => { 'hi' => 'there' }
  602. event.expires_at.to_i.should be_within(5).of(agents(:jane_weather_agent).keep_events_for.days.from_now.to_i)
  603. end
  604. end
  605. describe "when the agent does not have keep_events_for set" do
  606. before do
  607. agents(:jane_website_agent).keep_events_for.should == 0
  608. end
  609. it "does not set expires_at on created events" do
  610. event = agents(:jane_website_agent).create_event :payload => { 'hi' => 'there' }
  611. event.expires_at.should be_nil
  612. end
  613. end
  614. end
  615. end