Do not render or link to empty rss feeds (#6429) [deploy]

* Do not render or link to empty rss feeds

* Fix tests up
This commit is contained in:
Ben Halpern 2020-03-03 21:37:51 -05:00 committed by GitHub
parent 832f59ff6d
commit 883264c8ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 15 deletions

View file

@ -19,9 +19,8 @@ class ArticlesController < ApplicationController
@articles.where(featured: true).includes(:user)
end
unless @articles
render body: nil
return
unless @articles&.any?
not_found
end
set_surrogate_key_header "feed"

View file

@ -154,4 +154,8 @@ module ApplicationHelper
"#{path}-#{heroku_slug_commit}"
end
def app_protocol_and_domain
"#{ApplicationConfig["APP_PROTOCOL"]}#{ApplicationConfig["APP_DOMAIN"]}"
end
end

View file

@ -5,12 +5,12 @@
content_for: true,
) %>
<link rel="canonical" href="https://dev.to<%= request.path %>" />
<link rel="canonical" href="<%= app_protocol_and_domain %><%= request.path %>" />
<meta name="description" content="Where programmers share ideas and help each other grow.">
<meta name="keywords" content="software development,engineering,rails,javascript,ruby">
<meta property="og:type" content="website" />
<meta property="og:url" content="https://dev.to<%= request.path %>" />
<meta property="og:url" content="<%= app_protocol_and_domain %><%= request.path %>" />
<meta property="og:title" content="<%= title_with_timeframe(page_title: community_qualified_name, timeframe: params[:timeframe]) %>" />
<meta property="og:image" content="<%= SiteConfig.main_social_image %>">
<meta property="og:description" content="Where programmers share ideas and help each other grow." />
@ -21,7 +21,7 @@
<meta name="twitter:description" content="Where programmers share ideas, experiences, and help each other grow.">
<meta name="twitter:image:src" content="<%= SiteConfig.main_social_image %>">
<meta name="twitter:card" content="summary_large_image">
<%= auto_discovery_link_tag(:rss, "https://dev.to/feed", title: "#{ApplicationConfig['COMMUNITY_NAME']} RSS Feed") %>
<%= auto_discovery_link_tag(:rss, "#{app_protocol_and_domain}/feed", title: "#{ApplicationConfig['COMMUNITY_NAME']} RSS Feed") %>
<% end %>
<%= javascript_pack_tag "homePage", defer: true %>

View file

@ -1,9 +1,9 @@
<link rel="canonical" href="https://dev.to/<%= @user.username %>" />
<link rel="canonical" href="<%= app_protocol_and_domain %>/<%= @user.username %>" />
<meta name="description" content="<%= @user.summary %>">
<meta name="keywords" content="software development,engineering,rails,javascript,ruby">
<meta property="og:type" content="website" />
<meta property="og:url" content="https://dev.to/<%= @user.username %>" />
<meta property="og:url" content="<%= app_protocol_and_domain %>/<%= @user.username %>" />
<meta property="og:title" content="<%= @user.name %> — DEV Profile" />
<meta property="og:image" content="<%= user_social_image_url(@user) %>">
<meta property="og:description" content="<%= @user.summary %>" />
@ -15,7 +15,9 @@
<meta name="twitter:title" content="<%= @user.name %> — DEV Profile">
<meta name="twitter:description" content="<%= @user.summary %>">
<meta name="twitter:image:src" content="<%= user_social_image_url(@user) %>">
<%= auto_discovery_link_tag(:rss, "https://dev.to/feed/#{@user.username}", title: "The Practical Dev RSS Feed") %>
<% if @stories.any? %>
<%= auto_discovery_link_tag(:rss, "#{app_protocol_and_domain}/feed/#{@user.username}", title: "The Practical Dev RSS Feed") %>
<% end %>
<% if @user.banned %>
<meta name="googlebot" content="noindex">

View file

@ -6,11 +6,18 @@ RSpec.describe "Articles", type: :request do
describe "GET /feed" do
it "returns rss+xml content" do
create(:article, featured: true)
get "/feed"
expect(response.status).to eq(200)
expect(response.content_type).to eq("application/rss+xml")
end
it "returns not found if no articles" do
expect { get "/feed" }.to raise_error(ActiveRecord::RecordNotFound)
expect { get "/feed/#{user.username}" }.to raise_error(ActiveRecord::RecordNotFound)
expect { get "/feed/#{tag.name}" }.to raise_error(ActiveRecord::RecordNotFound)
end
context "when :username param is not given" do
let!(:featured_article) { create(:article, featured: true) }
let!(:not_featured_article) { create(:article, featured: false) }
@ -51,14 +58,12 @@ RSpec.describe "Articles", type: :request do
context "when :username param is given but it belongs to nither user nor organization" do
include_context "when user/organization articles exist"
before { get "/feed", params: { username: "unknown" } }
it("renders empty body") { expect(response.body).to be_empty }
it("renders empty body") { expect { get "/feed", params: { username: "unknown" } }.to raise_error(ActiveRecord::RecordNotFound) }
end
context "when format is invalid" do
it "returns a 404 response" do
expect { get "/feed.zip" }.to raise_error(ActionController::RoutingError)
expect { get "/feed.zip" }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
@ -91,9 +96,8 @@ RSpec.describe "Articles", type: :request do
context "when :tag param is given and tag does not exist" do
include_context "when tagged articles exist"
before { get "/feed/tag/unknown" }
it("renders empty body") { expect(response.body).to be_empty }
it("renders empty body") { expect { get "/feed/tag/unknown" }.to raise_error(ActiveRecord::RecordNotFound) }
end
end

View file

@ -22,6 +22,7 @@ RSpec.describe "RssFeed", type: :request do
end
it "renders organization feed" do
create(:article, organization_id: organization.id)
get "/feed/#{organization.slug}"
expect(response.body).to include("<link>https://dev.to/#{organization.slug}</link>")
end

View file

@ -57,6 +57,18 @@ RSpec.describe "UserProfiles", type: :request do
expect(response.body).not_to include("<meta name=\"googlebot\" content=\"noindex\">")
end
it "renders rss feed link if any stories" do
create(:article, user_id: user.id)
get "/#{user.username}"
expect(response.body).to include("/feed/#{user.username}")
end
it "does not render feed link if no stories" do
get "/#{user.username}"
expect(response.body).not_to include("/feed/#{user.username}")
end
context "when organization" do
it "renders organization page if org" do
get organization.path
@ -103,6 +115,17 @@ RSpec.describe "UserProfiles", type: :request do
get organization.path
expect(response.body).to include(ActionController::Base.helpers.sanitize(organization.location))
end
it "renders rss feed link if any stories" do
create(:article, organization_id: organization.id)
get organization.path
expect(response.body).to include("/feed/#{organization.slug}")
end
it "does not render feed link if no stories" do
get organization.path
expect(response.body).not_to include("/feed/#{organization.slug}")
end
end
context "when github repo" do