agent_spec.rb 24KB

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