Refactor articles_show_spec to Better Test JSON-LD (#7733)

* Refactor articles_show_spec.rb to better test JSON data
  * Adds additional tests around JSON-LD data
  * Removes original tests for JSON-LD from articles_show_spec

* Add org data to spec to test against actual JSON-LD data
  * Removes inline variables in favor of let declarations
  * Adds and tests organization structured data
  * Removes unnecessary objs and variables from spec

* Adjust org JSON-LD description in the Stories::Controller
  * Replaces user summary with org summary in org data structure

* Capitalize the S in RSpec in articles_show_spec.rb

* Remove OR from dateModified in favor of published_timestamp
This commit is contained in:
Julianna Tetreault 2020-05-08 10:16:41 -06:00 committed by GitHub
parent 911de37bc1
commit ab248edfe5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 26 deletions

View file

@ -423,7 +423,7 @@ class StoriesController < ApplicationController
"url": URL.organization(@organization),
"image": ProfileImage.new(@organization).get(width: 320),
"name": @organization.name,
"description": @user.summary.presence || "404 bio not found"
"description": @organization.summary.presence || "404 bio not found"
}
end

View file

@ -2,9 +2,11 @@ require "rails_helper"
RSpec.describe "ArticlesShow", type: :request do
let_it_be(:user) { create(:user) }
let_it_be(:article, reload: true) { create(:article, user: user, published: true, organization: organization) }
let_it_be(:organization) { create(:organization) }
let_it_be(:organization_article) { create(:article, organization_id: organization.id) }
let_it_be(:article, reload: true) { create(:article, user: user, published: true) }
let(:doc) { Nokogiri::HTML(response.body) }
let(:text) { doc.at('script[type="application/ld+json"]').text }
let(:response_json) { JSON.parse(text) }
describe "GET /:slug (articles)" do
before do
@ -15,32 +17,65 @@ RSpec.describe "ArticlesShow", type: :request do
expect(response).to have_http_status(:ok)
end
it "renders the proper title" do
expect(response.body).to include CGI.escapeHTML(article.title)
end
it "renders the proper published at date" do
expect(response.body).to include CGI.escapeHTML(article.readable_publish_date)
end
# This is a flakey spec. Due to how we are localizing date times, sometimes
# the date is off. See https://github.com/thepracticaldev/dev.to/issues/7086.
xit "renders the proper modified at date" do
article.update(edited_at: Time.zone.now)
get article.path
expect(response.body).to include GI.escapeHTML(article.edited_at.strftime("%b %d, %Y"))
end
it "renders the proper author" do
expect(response.body).to include CGI.escapeHTML(article.cached_user_username)
end
it "renders the proper organization for an article when one is present" do
get organization.path
expect(response.body).to include CGI.escapeHTML(organization_article.title)
# rubocop:disable RSpec/ExampleLength
it "renders the proper JSON-LD for an article" do
expect(response_json).to include(
"@context" => "http://schema.org",
"@type" => "Article",
"mainEntityOfPage" => {
"@type" => "WebPage",
"@id" => URL.article(article)
},
"url" => URL.article(article),
"image" => [
ApplicationController.helpers.article_social_image_url(article, width: 1080, height: 1080),
ApplicationController.helpers.article_social_image_url(article, width: 1280, height: 720),
ApplicationController.helpers.article_social_image_url(article, width: 1600, height: 900),
],
"publisher" => {
"@context" => "http://schema.org",
"@type" => "Organization",
"name" => "#{ApplicationConfig['COMMUNITY_NAME']} Community",
"logo" => {
"@context" => "http://schema.org",
"@type" => "ImageObject",
"url" => ApplicationController.helpers.cloudinary(SiteConfig.logo_png, 192, "png"),
"width" => "192",
"height" => "192"
}
},
"headline" => article.title,
"author" => {
"@context" => "http://schema.org",
"@type" => "Person",
"url" => URL.user(user),
"name" => user.name
},
"datePublished" => article.published_timestamp,
"dateModified" => article.published_timestamp,
)
end
end
it "renders the proper organization for an article when one is present" do
get organization.path
expect(response_json).to include(
{
"@context" => "http://schema.org",
"@type" => "Organization",
"mainEntityOfPage" => {
"@type" => "WebPage",
"@id" => URL.organization(organization)
},
"url" => URL.organization(organization),
"image" => ProfileImage.new(organization).get(width: 320),
"name" => organization.name,
"description" => organization.summary
},
)
end
# rubocop:enable RSpec/ExampleLength
context "when keywords are set up" do
it "shows keywords" do
SiteConfig.meta_keywords = { article: "hello, world" }