agent_spec.rb 24KB

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