rss_agent_spec.rb 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. require 'spec_helper'
  2. describe Agents::RssAgent do
  3. before do
  4. @valid_options = {
  5. 'expected_update_period_in_days' => "2",
  6. 'url' => "https://github.com/cantino/huginn/commits/master.atom",
  7. }
  8. stub_request(:any, /github.com/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/github_rss.atom")), :status => 200)
  9. end
  10. let(:agent) do
  11. _agent = Agents::RssAgent.new(:name => "github rss feed", :options => @valid_options)
  12. _agent.user = users(:bob)
  13. _agent.save!
  14. _agent
  15. end
  16. it_behaves_like WebRequestConcern
  17. describe "validations" do
  18. it "should validate the presence of url" do
  19. agent.options['url'] = "http://google.com"
  20. agent.should be_valid
  21. agent.options['url'] = ""
  22. agent.should_not be_valid
  23. agent.options['url'] = nil
  24. agent.should_not be_valid
  25. end
  26. it "should validate the presence and numericality of expected_update_period_in_days" do
  27. agent.options['expected_update_period_in_days'] = "5"
  28. agent.should be_valid
  29. agent.options['expected_update_period_in_days'] = "wut?"
  30. agent.should_not be_valid
  31. agent.options['expected_update_period_in_days'] = 0
  32. agent.should_not be_valid
  33. agent.options['expected_update_period_in_days'] = nil
  34. agent.should_not be_valid
  35. agent.options['expected_update_period_in_days'] = ""
  36. agent.should_not be_valid
  37. end
  38. end
  39. describe "emitting RSS events" do
  40. it "should emit items as events" do
  41. lambda {
  42. agent.check
  43. }.should change { agent.events.count }.by(20)
  44. end
  45. it "should track ids and not re-emit the same item when seen again" do
  46. agent.check
  47. agent.memory['seen_ids'].should == agent.events.map {|e| e.payload['id'] }
  48. newest_id = agent.memory['seen_ids'][0]
  49. agent.events.first.payload['id'].should == newest_id
  50. agent.memory['seen_ids'] = agent.memory['seen_ids'][1..-1] # forget the newest id
  51. lambda {
  52. agent.check
  53. }.should change { agent.events.count }.by(1)
  54. agent.events.first.payload['id'].should == newest_id
  55. agent.memory['seen_ids'][0].should == newest_id
  56. end
  57. it "should truncate the seen_ids in memory at 500 items" do
  58. agent.memory['seen_ids'] = ['x'] * 490
  59. agent.check
  60. agent.memory['seen_ids'].length.should == 500
  61. end
  62. end
  63. end