rss_agent_spec.rb 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. stub_request(:any, /SlickdealsnetFP/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/slickdeals.atom")), :status => 200)
  10. end
  11. let(:agent) do
  12. _agent = Agents::RssAgent.new(:name => "rss feed", :options => @valid_options)
  13. _agent.user = users(:bob)
  14. _agent.save!
  15. _agent
  16. end
  17. it_behaves_like WebRequestConcern
  18. describe "validations" do
  19. it "should validate the presence of url" do
  20. agent.options['url'] = "http://google.com"
  21. expect(agent).to be_valid
  22. agent.options['url'] = ""
  23. expect(agent).not_to be_valid
  24. agent.options['url'] = nil
  25. expect(agent).not_to be_valid
  26. end
  27. it "should validate the presence and numericality of expected_update_period_in_days" do
  28. agent.options['expected_update_period_in_days'] = "5"
  29. expect(agent).to be_valid
  30. agent.options['expected_update_period_in_days'] = "wut?"
  31. expect(agent).not_to be_valid
  32. agent.options['expected_update_period_in_days'] = 0
  33. expect(agent).not_to be_valid
  34. agent.options['expected_update_period_in_days'] = nil
  35. expect(agent).not_to be_valid
  36. agent.options['expected_update_period_in_days'] = ""
  37. expect(agent).not_to be_valid
  38. end
  39. end
  40. describe "emitting RSS events" do
  41. it "should emit items as events" do
  42. expect {
  43. agent.check
  44. }.to change { agent.events.count }.by(20)
  45. end
  46. it "should track ids and not re-emit the same item when seen again" do
  47. agent.check
  48. expect(agent.memory['seen_ids']).to eq(agent.events.map {|e| e.payload['id'] })
  49. newest_id = agent.memory['seen_ids'][0]
  50. expect(agent.events.first.payload['id']).to eq(newest_id)
  51. agent.memory['seen_ids'] = agent.memory['seen_ids'][1..-1] # forget the newest id
  52. expect {
  53. agent.check
  54. }.to change { agent.events.count }.by(1)
  55. expect(agent.events.first.payload['id']).to eq(newest_id)
  56. expect(agent.memory['seen_ids'][0]).to eq(newest_id)
  57. end
  58. it "should truncate the seen_ids in memory at 500 items" do
  59. agent.memory['seen_ids'] = ['x'] * 490
  60. agent.check
  61. expect(agent.memory['seen_ids'].length).to eq(500)
  62. end
  63. end
  64. context "when no ids are available" do
  65. before do
  66. @valid_options['url'] = 'http://feeds.feedburner.com/SlickdealsnetFP?format=atom'
  67. end
  68. it "calculates content MD5 sums" do
  69. expect {
  70. agent.check
  71. }.to change { agent.events.count }.by(79)
  72. expect(agent.memory['seen_ids']).to eq(agent.events.map {|e| Digest::MD5.hexdigest(e.payload['content']) })
  73. end
  74. end
  75. end