docbrown/spec/requests/stories_show_spec.rb
Julianna Tetreault c87974b7ac
[deploy] Add co_author_ids to Articles (#10339)
* Replaces second_user_id and third_user_id with co_author_ids on the Articles table

* Adjusts migration that adds co_author_ids to also add array: true

* Replaces second_user_id and third_user_id with co_author_ids in all applicable files
  - Use co_author_ids in _individual_article view
  - Use co_author_ids in articles and stories controllers
  - Use co_author_ids in article and podcast_episode models
  - Use co_author_ids in article_dashboard
  - Use co_author_ids in comments_helper
  - Use co_author_ids in articles_spec

* Replaces second_user_id and third_user_id with co_author_ids in article.rb model

* Remove unused #assign_co_authors method from the Stories::Controller

* Add #co_authors and #co_author_name_and_path to article_decorator.rb

* Adds a descriptive form field for co_author_ids for Admins in /admin/articles

* Adds a conditional in article/show.html.erb to render co_author names on an Article in a human-readable way

* Replaces get_co_author_name_and_path with proper method name, co_author_name_and_path in show.html.erb

* Adds latest schema to (hopefully) resolve Travis conflicts

* Adds the safe operator to show.html.erb

* Adjusts articles_spec.rb to use an array in the first test

* Replaces unless &.empty? with if ./present? in articles/show.html.erb

* Replaces second_user_id with co_author_ids in stories_show_spec.rb test

* Replaces elsif with else in articles/show.html.erb

* Cleans up show.html.erb by removing conditional in favor of decorator method

* Refactors splitting of params in Articles::Controller and optimizes query in #co_authors

* Reverts removal of second_user_id and third_user_id and migration file

* Adds a data_update script to update co_author_ids with existing second and third user_ids

* Adds validations to co_authors and flash_messages to indicate whether an update was successful or not
  - Adds to methods to article.rb to validate the IDs of co_authors and authors
  - Adds flash messages to the Admin::Articles::Controller and a redirect to the show page
  - Removes the JS highlighting upon submit when updating an Article in Admin
  - Refactors #update action in Admin::Articles::Controller

* Adds tests to article_spec.rb around co_author_id validations in article.rb

* Adjusts #validate_co_authors_must_not_be_the_same to use .include? instead

* Uses Field::Select.with_options in article_dashboard.rb to properly display co_author_ids

* Reverts removal of assigning co_author_ids in the Stories::Controller

* Adjusts error message and adjusts return logic in article.rb (thanks, Fernando!)

* Fixes failing article_spec.rb test that checks for co_author_ids uniqueness

* Adds a default array to co_author_ids and checks if they are .blank?

* Refactor data_update script to use a single SQL statement (thank you, Michael)

* Preserve array order of co_author_ids in article_decorator.rb

* Add db file for default: []

* Add validation to fix bug related to text inputs and invalid users when adding co_authors

* Adds tests to ensure that co_author_ids are both an integer and an integer > 0

* Updates admin/articles_spec.rb to default [] instead of nil

* Adjusts validations in article.rb to be DRY-er and more consistent

* Consolidates validations further

* Refactors validations in article.rb to use procs

* Refactors data_update script to remove nil values from array
2020-10-02 11:06:11 -04:00

219 lines
8.3 KiB
Ruby

require "rails_helper"
RSpec.describe "StoriesShow", type: :request do
let(:user) { create(:user) }
let(:org) { create(:organization) }
let(:article) { create(:article, user: user) }
describe "GET /:username/:slug (articles)" do
it "renders proper title" do
get article.path
expect(response.body).to include CGI.escapeHTML(article.title)
end
it "redirects to appropriate if article belongs to org and user visits user version" do
old_path = article.path
article.update(organization: org)
get old_path
expect(response.body).to redirect_to article.path
expect(response).to have_http_status(:moved_permanently)
end
## Title tag
it "renders signed-in title tag for signed-in user" do
sign_in user
get article.path
expected_title = "<title>#{CGI.escapeHTML(article.title)} - #{community_qualified_name} 👩‍💻👨‍💻</title>"
expect(response.body).to include(expected_title)
end
it "renders signed-out title tag for signed-out user" do
get article.path
expect(response.body).to include "<title>#{CGI.escapeHTML(article.title)} - #{community_name}</title>"
end
# search_optimized_title_preamble
it "renders title tag with search_optimized_title_preamble if set and not signed in" do
article.update_column(:search_optimized_title_preamble, "Hey this is a test")
get article.reload.path
expected_title = "<title>Hey this is a test: #{CGI.escapeHTML(article.title)} - #{community_name}</title>"
expect(response.body).to include(expected_title)
end
it "does not render title tag with search_optimized_title_preamble if set and not signed in" do
sign_in user
article.update_column(:search_optimized_title_preamble, "Hey this is a test")
get article.path
expected_title = "<title>#{CGI.escapeHTML(article.title)} - #{community_qualified_name} 👩‍💻👨‍💻</title>"
expect(response.body).to include(expected_title)
end
it "does not render preamble with search_optimized_title_preamble not signed in but not set" do
get article.path
expect(response.body).to include("#{CGI.escapeHTML(article.title)} - #{community_name}</title>")
end
it "renders title preamble with search_optimized_title_preamble if set and not signed in" do
article.update_column(:search_optimized_title_preamble, "Hey this is a test")
get article.reload.path
expect(response.body).to include("<span class=\"fs-xl color-base-70 block\">Hey this is a test</span>")
end
it "does not render preamble with search_optimized_title_preamble if set and signed in" do
sign_in user
article.update_column(:search_optimized_title_preamble, "Hey this is a test")
get article.path
expect(response.body).not_to include("<span class=\"fs-xl color-base-70 block\">Hey this is a test</span>")
end
it "does not render title tag with search_optimized_title_preamble not signed in but not set" do
get article.path
expect(response.body).not_to include("<span class=\"fs-xl color-base-70 block\">Hey this is a test</span>")
end
###
it "renders date-no-year if article published this year" do
get article.path
expect(response.body).to include "date-no-year"
end
it "renders date with year if article published last year" do
article.update_column(:published_at, 1.year.ago)
get article.path
expect(response.body).not_to include "date-no-year"
end
it "renders user payment pointer if set" do
article.user.update_column(:payment_pointer, "this-is-a-pointer")
get article.path
expect(response.body).to include "author-payment-pointer"
expect(response.body).to include "this-is-a-pointer"
end
it "does not render payment pointer if not set" do
get article.path
expect(response.body).not_to include "author-payment-pointer"
end
it "renders second and third users if present" do
# 3rd user doesn't seem to get rendered for some reason
user2 = create(:user)
article.update(co_author_ids: [user2.id])
get article.path
expect(response.body).to include "<em>with <b><a href=\"#{user2.path}\">"
end
it "renders articles of long length without breaking" do
# This is a pretty weak test, just to exercise different lengths with no breakage
article.update(title: (0...75).map { rand(65..90).chr }.join)
get article.path
article.update(title: (0...100).map { rand(65..90).chr }.join)
get article.path
article.update(title: (0...118).map { rand(65..90).chr }.join)
get article.path
expect(response.body).to include "title"
end
it "redirects to appropriate page if user changes username" do
old_username = user.username
user.update(username: "new_hotness_#{rand(10_000)}")
get "/#{old_username}/#{article.slug}"
expect(response.body).to redirect_to("/#{user.username}/#{article.slug}")
expect(response).to have_http_status(:moved_permanently)
end
it "redirects to appropriate page if user changes username twice" do
old_username = user.username
user.update(username: "new_hotness_#{rand(10_000)}")
user.update(username: "new_new_username_#{rand(10_000)}")
get "/#{old_username}/#{article.slug}"
expect(response.body).to redirect_to("/#{user.username}/#{article.slug}")
expect(response).to have_http_status(:moved_permanently)
end
it "redirects to appropriate page if user changes username twice and go to middle username" do
user.update(username: "new_hotness_#{rand(10_000)}")
middle_username = user.username
user.update(username: "new_new_username_#{rand(10_000)}")
get "/#{middle_username}/#{article.slug}"
expect(response.body).to redirect_to("/#{user.username}/#{article.slug}")
expect(response).to have_http_status(:moved_permanently)
end
it "renders canonical url when exists" do
article = create(:article, with_canonical_url: true)
get article.path
expect(response.body).to include(%("canonical" href="#{article.canonical_url}"))
end
it "does not render canonical url when not on article model" do
article = create(:article, with_canonical_url: false)
get article.path
expect(response.body).not_to include(%("canonical" href="#{article.canonical_url}"))
end
it "handles invalid slug characters" do
allow(Article).to receive(:find_by).and_raise(ArgumentError)
get article.path
expect(response.status).to be(400)
end
it "has noindex if article has low score" do
article = create(:article, score: -5)
get article.path
expect(response.body).to include("noindex")
end
it "has noindex if article has low score even with <code>" do
article = create(:article, score: -5)
article.update_column(:processed_html, "<code>hello</code>")
get article.path
expect(response.body).to include("noindex")
end
it "does not have noindex if article has high score" do
article = create(:article, score: 6)
get article.path
expect(response.body).not_to include("noindex")
end
it "does not have noindex if article intermediate score and <code>" do
article = create(:article, score: 3)
article.update_column(:processed_html, "<code>hello</code>")
get article.path
expect(response.body).not_to include("noindex")
end
it "does not have noindex if article w/ intermediate score w/ 1 comment " do
article = create(:article, score: 3)
article.user.update_column(:comments_count, 1)
get article.path
expect(response.body).not_to include("noindex")
end
end
describe "GET /:username (org)" do
it "redirects to the appropriate page if given an organization's old slug" do
original_slug = org.slug
org.update(slug: "somethingnew")
get "/#{original_slug}"
expect(response.body).to redirect_to org.path
expect(response).to have_http_status(:moved_permanently)
end
it "redirects to the appropriate page if given an organization's old old slug" do
original_slug = org.slug
org.update(slug: "somethingnew")
org.update(slug: "anothernewslug")
get "/#{original_slug}"
expect(response.body).to redirect_to org.path
expect(response).to have_http_status(:moved_permanently)
end
end
end