2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-12-31 14:37:43 -05:00
|
|
|
|
|
|
|
describe Jobs::PollFeed do
|
|
|
|
let(:poller) { Jobs::PollFeed.new }
|
|
|
|
|
|
|
|
context "execute" do
|
|
|
|
let(:url) { "http://eviltrout.com" }
|
|
|
|
|
2017-05-10 14:28:16 -04:00
|
|
|
before do
|
|
|
|
$redis.del("feed-polled-recently")
|
|
|
|
end
|
|
|
|
|
2013-12-31 14:37:43 -05:00
|
|
|
it "requires feed_polling_enabled?" do
|
2017-05-10 14:28:16 -04:00
|
|
|
SiteSetting.feed_polling_enabled = true
|
|
|
|
SiteSetting.feed_polling_url = nil
|
|
|
|
poller.expects(:poll_feed).never
|
|
|
|
poller.execute({})
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "requires feed_polling_url" do
|
2017-05-10 14:28:16 -04:00
|
|
|
SiteSetting.feed_polling_enabled = false
|
|
|
|
SiteSetting.feed_polling_url = nil
|
|
|
|
poller.expects(:poll_feed).never
|
|
|
|
poller.execute({})
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "delegates to poll_feed" do
|
2015-06-03 06:14:00 -04:00
|
|
|
SiteSetting.feed_polling_enabled = true
|
|
|
|
SiteSetting.feed_polling_url = url
|
2013-12-31 14:37:43 -05:00
|
|
|
poller.expects(:poll_feed).once
|
|
|
|
poller.execute({})
|
|
|
|
end
|
2017-05-10 14:28:16 -04:00
|
|
|
|
|
|
|
it "won't poll if it has polled recently" do
|
|
|
|
SiteSetting.feed_polling_enabled = true
|
|
|
|
SiteSetting.feed_polling_url = url
|
|
|
|
poller.expects(:poll_feed).once
|
|
|
|
poller.execute({})
|
|
|
|
poller.execute({})
|
|
|
|
end
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|
|
|
|
|
2017-09-20 21:14:39 -04:00
|
|
|
describe '#poll_feed' do
|
|
|
|
let(:embed_by_username) { 'eviltrout' }
|
2017-12-05 18:45:09 -05:00
|
|
|
let(:embed_username_key_from_feed) { 'discourse:username' }
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:default_user) { Fabricate(:evil_trout) }
|
|
|
|
fab!(:feed_author) { Fabricate(:user, username: 'xrav3nz', email: 'hi@bye.com') }
|
2017-09-20 21:14:39 -04:00
|
|
|
|
2017-12-05 18:45:09 -05:00
|
|
|
shared_examples 'topic creation based on the the feed' do
|
|
|
|
describe 'author username parsing' do
|
|
|
|
context 'when neither embed_by_username nor embed_username_key_from_feed is set' do
|
|
|
|
before do
|
|
|
|
SiteSetting.embed_by_username = ""
|
|
|
|
SiteSetting.embed_username_key_from_feed = ""
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not import topics' do
|
|
|
|
expect { poller.poll_feed }.not_to change { Topic.count }
|
|
|
|
end
|
2017-09-20 21:14:39 -04:00
|
|
|
end
|
|
|
|
|
2017-12-05 18:45:09 -05:00
|
|
|
context 'when embed_by_username is set' do
|
|
|
|
before do
|
|
|
|
SiteSetting.embed_by_username = embed_by_username
|
|
|
|
SiteSetting.embed_username_key_from_feed = ""
|
|
|
|
end
|
2017-09-20 21:14:39 -04:00
|
|
|
|
2017-12-05 18:45:09 -05:00
|
|
|
it 'creates the new topics under embed_by_username' do
|
|
|
|
expect { poller.poll_feed }.to change { Topic.count }.by(1)
|
|
|
|
expect(Topic.last.user).to eq(default_user)
|
|
|
|
end
|
2017-09-20 21:14:39 -04:00
|
|
|
end
|
|
|
|
|
2017-12-05 18:45:09 -05:00
|
|
|
context 'when embed_username_key_from_feed is set' do
|
|
|
|
before do
|
|
|
|
SiteSetting.embed_username_key_from_feed = embed_username_key_from_feed
|
|
|
|
end
|
2017-09-20 21:14:39 -04:00
|
|
|
|
2017-12-05 18:45:09 -05:00
|
|
|
it 'creates the new topics under the username found' do
|
|
|
|
expect { poller.poll_feed }.to change { Topic.count }.by(1)
|
|
|
|
expect(Topic.last.user).to eq(feed_author)
|
|
|
|
end
|
2018-04-18 15:22:43 -04:00
|
|
|
|
|
|
|
it "updates the post if it had been polled" do
|
|
|
|
embed_url = 'https://blog.discourse.org/2017/09/poll-feed-spec-fixture'
|
|
|
|
post = TopicEmbed.import(Fabricate(:user), embed_url, 'old title', 'old content')
|
|
|
|
|
|
|
|
expect { poller.poll_feed }.to_not change { Topic.count }
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
expect(post.raw).to include('<p>This is the body & content. </p>')
|
|
|
|
expect(post.user).to eq(feed_author)
|
|
|
|
end
|
2017-09-20 21:14:39 -04:00
|
|
|
end
|
2017-12-05 18:45:09 -05:00
|
|
|
end
|
2017-09-20 21:14:39 -04:00
|
|
|
|
2017-12-05 18:45:09 -05:00
|
|
|
it 'parses creates a new post correctly' do
|
|
|
|
expect { poller.poll_feed }.to change { Topic.count }.by(1)
|
|
|
|
expect(Topic.last.title).to eq('Poll Feed Spec Fixture')
|
|
|
|
expect(Topic.last.first_post.raw).to include('<p>This is the body & content. </p>')
|
|
|
|
expect(Topic.last.topic_embed.embed_url).to eq('https://blog.discourse.org/2017/09/poll-feed-spec-fixture')
|
2017-09-20 21:14:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-05 18:45:09 -05:00
|
|
|
context 'when parsing RSS feed' do
|
|
|
|
before do
|
|
|
|
SiteSetting.feed_polling_enabled = true
|
|
|
|
SiteSetting.feed_polling_url = 'https://blog.discourse.org/feed/'
|
|
|
|
SiteSetting.embed_by_username = embed_by_username
|
|
|
|
|
2018-02-24 06:35:57 -05:00
|
|
|
stub_request(:head, SiteSetting.feed_polling_url)
|
2017-12-05 18:45:09 -05:00
|
|
|
stub_request(:get, SiteSetting.feed_polling_url).to_return(
|
|
|
|
body: file_from_fixtures('feed.rss', 'feed').read,
|
|
|
|
headers: { "Content-Type" => "application/rss+xml" }
|
|
|
|
)
|
|
|
|
end
|
2017-09-20 21:14:39 -04:00
|
|
|
|
2017-12-05 18:45:09 -05:00
|
|
|
include_examples 'topic creation based on the the feed'
|
2017-09-20 21:14:39 -04:00
|
|
|
end
|
|
|
|
|
2017-12-05 18:45:09 -05:00
|
|
|
context 'when parsing ATOM feed' do
|
|
|
|
before do
|
|
|
|
SiteSetting.feed_polling_enabled = true
|
|
|
|
SiteSetting.feed_polling_url = 'https://blog.discourse.org/feed/atom/'
|
|
|
|
SiteSetting.embed_by_username = embed_by_username
|
|
|
|
|
2018-02-24 06:35:57 -05:00
|
|
|
stub_request(:head, SiteSetting.feed_polling_url)
|
2017-12-05 18:45:09 -05:00
|
|
|
stub_request(:get, SiteSetting.feed_polling_url).to_return(
|
|
|
|
body: file_from_fixtures('feed.atom', 'feed').read,
|
|
|
|
headers: { "Content-Type" => "application/atom+xml" }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'topic creation based on the the feed'
|
2017-09-20 21:14:39 -04:00
|
|
|
end
|
2018-07-27 13:41:53 -04:00
|
|
|
|
2018-08-10 12:37:14 -04:00
|
|
|
it "aborts when it can't fetch the feed" do
|
|
|
|
SiteSetting.feed_polling_enabled = true
|
|
|
|
SiteSetting.feed_polling_url = 'https://blog.discourse.org/feed/atom/'
|
|
|
|
SiteSetting.embed_by_username = 'eviltrout'
|
|
|
|
|
|
|
|
stub_request(:head, SiteSetting.feed_polling_url).to_return(status: 404)
|
|
|
|
stub_request(:get, SiteSetting.feed_polling_url).to_return(status: 404)
|
|
|
|
|
|
|
|
expect { poller.poll_feed }.to_not change { Topic.count }
|
|
|
|
end
|
|
|
|
|
2018-07-27 13:41:53 -04:00
|
|
|
context 'encodings' do
|
|
|
|
before do
|
|
|
|
SiteSetting.feed_polling_enabled = true
|
|
|
|
SiteSetting.feed_polling_url = 'https://blog.discourse.org/feed/atom/'
|
|
|
|
SiteSetting.embed_by_username = 'eviltrout'
|
|
|
|
|
|
|
|
stub_request(:head, SiteSetting.feed_polling_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'works with encodings other than UTF-8' do
|
|
|
|
stub_request(:get, SiteSetting.feed_polling_url).to_return(
|
|
|
|
body: file_from_fixtures('utf-16le-feed.rss', 'feed').read,
|
|
|
|
headers: { "Content-Type" => "application/rss+xml" }
|
|
|
|
)
|
|
|
|
|
|
|
|
expect { poller.poll_feed }.to change { Topic.count }.by(1)
|
|
|
|
expect(Topic.last.first_post.raw).to include('<p>This is the body & content. </p>')
|
|
|
|
end
|
2018-07-27 13:43:20 -04:00
|
|
|
|
|
|
|
it 'respects the charset in the Content-Type header' do
|
|
|
|
stub_request(:get, SiteSetting.feed_polling_url).to_return(
|
|
|
|
body: file_from_fixtures('iso-8859-15-feed.rss', 'feed').read,
|
|
|
|
headers: { "Content-Type" => "application/rss+xml; charset=ISO-8859-15" }
|
|
|
|
)
|
|
|
|
|
|
|
|
expect { poller.poll_feed }.to change { Topic.count }.by(1)
|
|
|
|
expect(Topic.last.first_post.raw).to include('<p>This is the body & content. 100€ </p>')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'works when the charset in the Content-Type header is unknown' do
|
|
|
|
stub_request(:get, SiteSetting.feed_polling_url).to_return(
|
|
|
|
body: file_from_fixtures('feed.rss', 'feed').read,
|
|
|
|
headers: { "Content-Type" => "application/rss+xml; charset=foo" }
|
|
|
|
)
|
|
|
|
|
|
|
|
expect { poller.poll_feed }.to change { Topic.count }.by(1)
|
|
|
|
expect(Topic.last.first_post.raw).to include('<p>This is the body & content. </p>')
|
|
|
|
end
|
2018-07-27 13:41:53 -04:00
|
|
|
end
|
2017-09-20 21:14:39 -04:00
|
|
|
end
|
2013-12-31 14:37:43 -05:00
|
|
|
end
|