rss_agent_spec.rb 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. require 'rails_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. stub_request(:any, /onethingwell.org/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/onethingwell.atom")), :status => 200)
  11. end
  12. let(:agent) do
  13. _agent = Agents::RssAgent.new(:name => "rss feed", :options => @valid_options)
  14. _agent.user = users(:bob)
  15. _agent.save!
  16. _agent
  17. end
  18. it_behaves_like WebRequestConcern
  19. describe "validations" do
  20. it "should validate the presence of url" do
  21. agent.options['url'] = "http://google.com"
  22. expect(agent).to be_valid
  23. agent.options['url'] = ["http://google.com", "http://yahoo.com"]
  24. expect(agent).to be_valid
  25. agent.options['url'] = ""
  26. expect(agent).not_to be_valid
  27. agent.options['url'] = nil
  28. expect(agent).not_to be_valid
  29. end
  30. it "should validate the presence and numericality of expected_update_period_in_days" do
  31. agent.options['expected_update_period_in_days'] = "5"
  32. expect(agent).to be_valid
  33. agent.options['expected_update_period_in_days'] = "wut?"
  34. expect(agent).not_to be_valid
  35. agent.options['expected_update_period_in_days'] = 0
  36. expect(agent).not_to be_valid
  37. agent.options['expected_update_period_in_days'] = nil
  38. expect(agent).not_to be_valid
  39. agent.options['expected_update_period_in_days'] = ""
  40. expect(agent).not_to be_valid
  41. end
  42. end
  43. describe "emitting RSS events" do
  44. it "should emit items as events for an Atom feed" do
  45. agent.options['include_feed_info'] = true
  46. expect {
  47. agent.check
  48. }.to change { agent.events.count }.by(20)
  49. first, *, last = agent.events.last(20)
  50. [first, last].each do |event|
  51. expect(first.payload['feed']).to include({
  52. "type" => "atom",
  53. "title" => "Recent Commits to huginn:master",
  54. "url" => "https://github.com/cantino/huginn/commits/master",
  55. "links" => [
  56. {
  57. "type" => "text/html",
  58. "rel" => "alternate",
  59. "href" => "https://github.com/cantino/huginn/commits/master",
  60. },
  61. {
  62. "type" => "application/atom+xml",
  63. "rel" => "self",
  64. "href" => "https://github.com/cantino/huginn/commits/master.atom",
  65. },
  66. ],
  67. })
  68. end
  69. expect(first.payload['url']).to eq("https://github.com/cantino/huginn/commit/d0a844662846cf3c83b94c637c1803f03db5a5b0")
  70. expect(first.payload['urls']).to eq(["https://github.com/cantino/huginn/commit/d0a844662846cf3c83b94c637c1803f03db5a5b0"])
  71. expect(first.payload['links']).to eq([
  72. {
  73. "href" => "https://github.com/cantino/huginn/commit/d0a844662846cf3c83b94c637c1803f03db5a5b0",
  74. "rel" => "alternate",
  75. "type" => "text/html",
  76. }
  77. ])
  78. expect(first.payload['authors']).to eq(["cantino (https://github.com/cantino)"])
  79. expect(first.payload['date_published']).to be_nil
  80. expect(first.payload['last_updated']).to eq("2014-07-16T22:26:22-07:00")
  81. expect(last.payload['url']).to eq("https://github.com/cantino/huginn/commit/d465158f77dcd9078697e6167b50abbfdfa8b1af")
  82. expect(last.payload['urls']).to eq(["https://github.com/cantino/huginn/commit/d465158f77dcd9078697e6167b50abbfdfa8b1af"])
  83. expect(last.payload['links']).to eq([
  84. {
  85. "href" => "https://github.com/cantino/huginn/commit/d465158f77dcd9078697e6167b50abbfdfa8b1af",
  86. "rel" => "alternate",
  87. "type" => "text/html",
  88. }
  89. ])
  90. expect(last.payload['authors']).to eq(["CloCkWeRX (https://github.com/CloCkWeRX)"])
  91. expect(last.payload['date_published']).to be_nil
  92. expect(last.payload['last_updated']).to eq("2014-07-01T16:37:47+09:30")
  93. end
  94. it "should emit items as events in the order specified in the events_order option" do
  95. expect {
  96. agent.options['events_order'] = ['{{title | replace_regex: "^[[:space:]]+", "" }}']
  97. agent.check
  98. }.to change { agent.events.count }.by(20)
  99. first, *, last = agent.events.last(20)
  100. expect(first.payload['title'].strip).to eq('upgrade rails and gems')
  101. expect(first.payload['url']).to eq("https://github.com/cantino/huginn/commit/87a7abda23a82305d7050ac0bb400ce36c863d01")
  102. expect(first.payload['urls']).to eq(["https://github.com/cantino/huginn/commit/87a7abda23a82305d7050ac0bb400ce36c863d01"])
  103. expect(last.payload['title'].strip).to eq('Dashed line in a diagram indicates propagate_immediately being false.')
  104. expect(last.payload['url']).to eq("https://github.com/cantino/huginn/commit/0e80f5341587aace2c023b06eb9265b776ac4535")
  105. expect(last.payload['urls']).to eq(["https://github.com/cantino/huginn/commit/0e80f5341587aace2c023b06eb9265b776ac4535"])
  106. end
  107. it "should emit items as events for a FeedBurner RSS 2.0 feed" do
  108. agent.options['url'] = "http://feeds.feedburner.com/SlickdealsnetFP?format=atom" # This is actually RSS 2.0 w/ Atom extension
  109. agent.options['include_feed_info'] = true
  110. agent.save!
  111. expect {
  112. agent.check
  113. }.to change { agent.events.count }.by(79)
  114. first, *, last = agent.events.last(79)
  115. expect(first.payload['feed']).to include({
  116. "type" => "rss",
  117. "title" => "SlickDeals.net",
  118. "description" => "Slick online shopping deals.",
  119. "url" => "http://slickdeals.net/",
  120. })
  121. # Feedjira extracts feedburner:origLink
  122. expect(first.payload['url']).to eq("http://slickdeals.net/permadeal/130160/green-man-gaming---pc-games-tomb-raider-game-of-the-year-6-hitman-absolution-elite-edition")
  123. expect(last.payload['feed']).to include({
  124. "type" => "rss",
  125. "title" => "SlickDeals.net",
  126. "description" => "Slick online shopping deals.",
  127. "url" => "http://slickdeals.net/",
  128. })
  129. expect(last.payload['url']).to eq("http://slickdeals.net/permadeal/129980/amazon---rearth-ringke-fusion-bumper-hybrid-case-for-iphone-6")
  130. end
  131. it "should track ids and not re-emit the same item when seen again" do
  132. agent.check
  133. expect(agent.memory['seen_ids']).to eq(agent.events.map {|e| e.payload['id'] })
  134. newest_id = agent.memory['seen_ids'][0]
  135. expect(agent.events.first.payload['id']).to eq(newest_id)
  136. agent.memory['seen_ids'] = agent.memory['seen_ids'][1..-1] # forget the newest id
  137. expect {
  138. agent.check
  139. }.to change { agent.events.count }.by(1)
  140. expect(agent.events.first.payload['id']).to eq(newest_id)
  141. expect(agent.memory['seen_ids'][0]).to eq(newest_id)
  142. end
  143. it "should truncate the seen_ids in memory at 500 items" do
  144. agent.memory['seen_ids'] = ['x'] * 490
  145. agent.check
  146. expect(agent.memory['seen_ids'].length).to eq(500)
  147. end
  148. it "should support an array of URLs" do
  149. agent.options['url'] = ["https://github.com/cantino/huginn/commits/master.atom", "http://feeds.feedburner.com/SlickdealsnetFP?format=atom"]
  150. agent.save!
  151. expect {
  152. agent.check
  153. }.to change { agent.events.count }.by(20 + 79)
  154. end
  155. it "should fetch one event per run" do
  156. agent.options['url'] = ["https://github.com/cantino/huginn/commits/master.atom"]
  157. agent.options['max_events_per_run'] = 1
  158. agent.check
  159. expect(agent.events.count).to eq(1)
  160. end
  161. it "should fetch all events per run" do
  162. agent.options['url'] = ["https://github.com/cantino/huginn/commits/master.atom"]
  163. # <= 0 should ignore option and get all
  164. agent.options['max_events_per_run'] = 0
  165. agent.check
  166. expect(agent.events.count).to eq(20)
  167. agent.options['max_events_per_run'] = -1
  168. expect {
  169. agent.check
  170. }.to_not change { agent.events.count }
  171. end
  172. end
  173. context "when no ids are available" do
  174. before do
  175. @valid_options['url'] = 'http://feeds.feedburner.com/SlickdealsnetFP?format=atom'
  176. end
  177. it "calculates content MD5 sums" do
  178. expect {
  179. agent.check
  180. }.to change { agent.events.count }.by(79)
  181. expect(agent.memory['seen_ids']).to eq(agent.events.map {|e| Digest::MD5.hexdigest(e.payload['content']) })
  182. end
  183. end
  184. context "parsing feeds" do
  185. before do
  186. @valid_options['url'] = 'http://onethingwell.org/rss'
  187. end
  188. it "captures timestamps normalized in the ISO 8601 format" do
  189. agent.check
  190. first, *, third = agent.events.take(3)
  191. expect(first.payload['date_published']).to eq('2015-08-20T17:00:10+01:00')
  192. expect(third.payload['date_published']).to eq('2015-08-20T13:00:07+01:00')
  193. end
  194. it "captures multiple categories" do
  195. agent.check
  196. first, *, third = agent.events.take(3)
  197. expect(first.payload['categories']).to eq(["csv", "crossplatform", "utilities"])
  198. expect(third.payload['categories']).to eq(["web"])
  199. end
  200. it "sanitizes HTML content" do
  201. agent.options['clean'] = true
  202. agent.check
  203. event = agent.events.last
  204. expect(event.payload['content']).to eq('<a href="http://showgoers.tv/">Showgoers</a>: <blockquote> <p>Showgoers is a Chrome browser extension to synchronize your Netflix player with someone else so that you can co-watch the same movie on different computers with no hassle. Syncing up your player is as easy as sharing a URL.</p> </blockquote>')
  205. expect(event.payload['description']).to eq('<a href="http://showgoers.tv/">Showgoers</a>: <blockquote> <p>Showgoers is a Chrome browser extension to synchronize your Netflix player with someone else so that you can co-watch the same movie on different computers with no hassle. Syncing up your player is as easy as sharing a URL.</p> </blockquote>')
  206. end
  207. it "captures an enclosure" do
  208. agent.check
  209. event = agent.events.fourth
  210. expect(event.payload['enclosure']).to eq({ "url" => "http://c.1tw.org/images/2015/itsy.png", "type" => "image/png", "length" => "48249" })
  211. expect(event.payload['image']).to eq("http://c.1tw.org/images/2015/itsy.png")
  212. end
  213. end
  214. describe 'logging errors with the feed url' do
  215. it 'includes the feed URL when an exception is raised' do
  216. mock(Feedjira::Feed).parse(anything) { raise StandardError.new("Some error!") }
  217. expect(lambda {
  218. agent.check
  219. }).not_to raise_error
  220. expect(agent.logs.last.message).to match(%r[Failed to fetch https://github.com])
  221. end
  222. end
  223. end