From ac666ddf17e6cada37e4a6e934bf3232fb4c7108 Mon Sep 17 00:00:00 2001 From: Kyle Zhao Date: Sun, 17 Sep 2017 00:50:32 -0400 Subject: [PATCH 1/2] PollFeed: check 'content:encoded' for content first --- app/jobs/scheduled/poll_feed.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/jobs/scheduled/poll_feed.rb b/app/jobs/scheduled/poll_feed.rb index fcd0243aae2..798cbff7892 100644 --- a/app/jobs/scheduled/poll_feed.rb +++ b/app/jobs/scheduled/poll_feed.rb @@ -101,7 +101,9 @@ module Jobs end def content - @article_rss_item.content.try(:force_encoding, "UTF-8").try(:scrub) || @article_rss_item.description.try(:force_encoding, "UTF-8").try(:scrub) + @article_rss_item.content_encoded&.force_encoding("UTF-8")&.scrub || + @article_rss_item.content&.force_encoding("UTF-8")&.scrub || + @article_rss_item.description&.force_encoding("UTF-8")&.scrub end def title From 15cd3b78aec7f4716b1a5c9e36686b415a2edf79 Mon Sep 17 00:00:00 2001 From: Kyle Zhao Date: Wed, 20 Sep 2017 21:14:39 -0400 Subject: [PATCH 2/2] integration test for PollFeed job --- spec/fixtures/feed/feed.rss | 30 ++++++++++++++++ spec/jobs/poll_feed_spec.rb | 69 +++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 spec/fixtures/feed/feed.rss diff --git a/spec/fixtures/feed/feed.rss b/spec/fixtures/feed/feed.rss new file mode 100644 index 00000000000..2de7185c438 --- /dev/null +++ b/spec/fixtures/feed/feed.rss @@ -0,0 +1,30 @@ + + + Discourse + + https://blog.discourse.org + Official blog for the open source Discourse project + Thu, 14 Sep 2017 15:22:33 +0000 + en-US + hourly + 1 + https://wordpress.org/?v=4.8.1 + + Poll Feed Spec Fixture + https://blog.discourse.org/2017/09/poll-feed-spec-fixture/ + Thu, 14 Sep 2017 15:22:33 +0000 + + + https://blog.discourse.org/?p=pollfeedspec + + This is the body & content.

]]>
+
+
+
diff --git a/spec/jobs/poll_feed_spec.rb b/spec/jobs/poll_feed_spec.rb index 0fcfa7d02cb..27f108b3ee8 100644 --- a/spec/jobs/poll_feed_spec.rb +++ b/spec/jobs/poll_feed_spec.rb @@ -43,4 +43,73 @@ describe Jobs::PollFeed do end + describe '#poll_feed' do + let(:embed_by_username) { 'eviltrout' } + let(:embed_username_key_from_feed) { 'dc_creator' } + let!(:default_user) { Fabricate(:evil_trout) } + let!(:feed_author) { Fabricate(:user, username: 'xrav3nz', email: 'hi@bye.com') } + + before do + SiteSetting.feed_polling_enabled = true + SiteSetting.feed_polling_url = 'https://blog.discourse.org/feed/' + SiteSetting.embed_by_username = embed_by_username + + stub_request(:get, SiteSetting.feed_polling_url).to_return( + status: 200, + body: file_from_fixtures('feed.rss', 'feed').read, + headers: { "Content-Type" => "application/rss+xml" } + ) + end + + 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 + end + + context 'when embed_by_username is set' do + before do + SiteSetting.embed_by_username = embed_by_username + SiteSetting.embed_username_key_from_feed = "" + end + + 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 + end + + context 'when embed_username_key_from_feed is set' do + before do + SiteSetting.embed_username_key_from_feed = embed_username_key_from_feed + end + + 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 + end + end + + it 'parses the title correctly' do + expect { poller.poll_feed }.to change { Topic.count }.by(1) + expect(Topic.last.title).to eq('Poll Feed Spec Fixture') + end + + it 'parses the content correctly' do + expect { poller.poll_feed }.to change { Topic.count }.by(1) + expect(Topic.last.first_post.raw).to include('

This is the body & content.

') + end + + it 'parses the link correctly' do + expect { poller.poll_feed }.to change { Topic.count }.by(1) + expect(Topic.last.topic_embed.embed_url).to eq('https://blog.discourse.org/2017/09/poll-feed-spec-fixture') + end + end end