rss_agent_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. event = agent.events.last
  46. expect(event.payload['url']).to eq("https://github.com/cantino/huginn/commit/d0a844662846cf3c83b94c637c1803f03db5a5b0")
  47. expect(event.payload['urls']).to eq(["https://github.com/cantino/huginn/commit/d0a844662846cf3c83b94c637c1803f03db5a5b0"])
  48. end
  49. it "should track ids and not re-emit the same item when seen again" do
  50. agent.check
  51. expect(agent.memory['seen_ids']).to eq(agent.events.map {|e| e.payload['id'] })
  52. newest_id = agent.memory['seen_ids'][0]
  53. expect(agent.events.first.payload['id']).to eq(newest_id)
  54. agent.memory['seen_ids'] = agent.memory['seen_ids'][1..-1] # forget the newest id
  55. expect {
  56. agent.check
  57. }.to change { agent.events.count }.by(1)
  58. expect(agent.events.first.payload['id']).to eq(newest_id)
  59. expect(agent.memory['seen_ids'][0]).to eq(newest_id)
  60. end
  61. it "should truncate the seen_ids in memory at 500 items" do
  62. agent.memory['seen_ids'] = ['x'] * 490
  63. agent.check
  64. expect(agent.memory['seen_ids'].length).to eq(500)
  65. end
  66. end
  67. context "when no ids are available" do
  68. before do
  69. @valid_options['url'] = 'http://feeds.feedburner.com/SlickdealsnetFP?format=atom'
  70. end
  71. it "calculates content MD5 sums" do
  72. expect {
  73. agent.check
  74. }.to change { agent.events.count }.by(79)
  75. expect(agent.memory['seen_ids']).to eq(agent.events.map {|e| Digest::MD5.hexdigest(e.payload['content']) })
  76. end
  77. end
  78. end