docbrown/spec/requests/stories_index_spec.rb
Julianna Tetreault b12ebfa0e5
RFC #74, Pt. 2: Private Forem Landing Page (#13819)
* Adds landing_page checkbox for Landing Page

* Adds landing_page to page_params in Admin::Pages::Controller

* Adds landing_page to admin_manages_pages and admin_creates_pages specs

* Adjusts copy for Landing Page in pages/_form.html.erb

* Places #post_a_job before #privacy in Pages::Controller
  - Ensures that all page-related methods are properly
 alphabetized

* Alphabetizes remaining out-of-order page-related methods
  - Ensures that all page-related methods within the
 Pages::Controller are in alphabetical order, following
 the order of the other methods

* Adds landing_page check to ApplicationController#verify_private_forem

* Adds a single_landing_page validation to the Page model

* Adds a lock.svg to /images and uses it in the pages index

* Adjusts buttons and checkboxes to use crayons class in pages partial

* Ensures that pages/_form.html.erb destroy button uses crayons

* Adds a landing_page validation-related test to page_spec.rb

* Updates #single_landing_page validation and error message

* WIP: Adds JS modal upon landing_page error in pages/_form.html.erb

* Adds a working modal upon error in pages/_form.html.erb
- Removes redundant #single_landing_page validation from Page
- Adds #has_a_landing_page? in place of #single_landing_page
- Conditionally renders the landing_page checkbox in _form.html.erb
- Adds styles to locked screen/landing page modal
- Adds copy to locked screen/landing page modal
- Beings adding buttons to overwrite current landing_page

* Updates the data-modal-selectors and ids in pages/_form.html.erb

* Uses #landing_page_path within pages/_form.html.erb modal
- Adds a link_to to link to the current landing page in the modal
- Adds styles to buttons and link_to in modal

* Adds back single_landing_page validation to Page.rb

* Reverts changes and updates methods within Page.rb
 - Reverts changes to #single_landing_page
 - Refactors #has_a_landing_page? and #landing_page_path
 - Updates div id in pages/_form.html.erb

* Adjusts #single_landing_page error message slightly

* Adds a test for landing_pages in stories_index_spec.rb

* Adds an overwrite attribute to Pages for the modal in _form
  - Fixes anonymous spelling

* Refactors pages/_form.html.erb and cleans up spelling

* Adds a safe operator to landing_page.path in Page
  - Adjusts the placement of the hidden value in pages/_form.html.erb

* Updates styling for Current Locked Screen link

* Adds the ability to overwrite an active landing page on click
  - Adds Admin::PagesController#update_and_overwrite and refactors
AdminPages::Controller#udpate to work with new method
  - Adds a remote attribute, overwrite, to Admin::PagesController
via a hidden_field in pages/_form.html.erb and an attr_accessor in
the Page model
  - Adds a toggleOverwrite() function to pages/_form.html.erb to
toggle the (soon to be hidden) overwrite checkbox in the form --
the checkbox is toggled on button click

* Adds a guard clause to #landing_page_path in Page.rb

* Adds a safe naviagtor to Admin::PagesController#update_and_overwrite_landing_page

* Adds a type and return to Overwrite button to prevent submit

* Adds a hidden class to the overwrite checkbox field in _form.html.erb
  - Hides the overwrite checkbox since it is only used to overwrite
the landing_page boolean on click of the "Overwrite current locked
screen" button

* Adds Admin::PagesController#create_and_overwrite and removes hidden class

* Adds landing page-related tests to admin_manages_pages_spec.rb

* Remove messy commented out code and place it in GH comment instead

* PR Review Feedback: Renames remote attr, defines class methods, updates views
  - Renames remote attribute to overwrite_landing_page
  - Updates all necessary views, methods, etc. w/ new attr
  - Defines two class methods to make code more extensible
  - Updates all necessary views, methods, etc. w/ new methods

* Remove unnecessary comments and revert changes to ternary in _form.html.erb

* Adjustments for accessibility within _form and index
  - Adds a descriptive id to <p> in _form.html.erb
  - Adds an ariadescribedby field to _form.html.erb
  - Adjusts title within index.html.erb

* Refactors landing_page-related code per PR review requests
  - Adds a landing_page_modal_controller.js that handles
and further customizes the dismiss and overwrite button, and
handles opening the modal
  - Adds a _landing_page_modal partial to extract modal-specific
html outside of the _form.html.erb partial
  - Refactors _form.html.erb to account for the _landing_page_modal
partial and the landing_page_modal_controller.js
  - Refactors the overwrite checkbox to be a hidden_field rather than
a hidden checkbox and removes existing hidden_field to accomodate it
  - Loads the @landing_page within Admin::PagesController#edit for
use by the _landing_page_modal.html.erb partial
  - Refactors Page#single_landing_page to be more readable
  - Removes useless JS from _form.html.erb

* Removes #landing_page_exists? class method since it isnt used now

* Uses the #landing_page class method within ApplicationController

* Removes pending status from test in admin_manages_pages_spec.rb

* Adds an e2e test for landing pages and adds to seeds_e2e
  - Adds a landingPage.spec.js to test landing pages
  - Adds a Page to seeds_e2e.rb for landingPage.spec.js

* Removes references to articles from e2e test and adds clickable link

* Adds another Page to seeds_e2e.rb and updates link names in test

* Adds the Current Locked Screen title to modal and opens in new link

* Renames Dismiss to Cancel and updates all necessary code

* Adjusts landingPage.spec.js

* check for existing landing page before showing modal

* Update Cypress tests and fix bug in LandingPageModalController

* Apply suggestions from code review

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* Orders Pages by created_at desc in /admin/pages/index.html.erb for consistency

* Targets specific edit button in landingPage.spec.js

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>
Co-authored-by: rhymes <github@rhymes.dev>
2021-06-10 11:46:24 -06:00

302 lines
11 KiB
Ruby

require "rails_helper"
RSpec.shared_examples "redirects to the lowercase route" do
context "when a path contains uppercase characters" do
it "redirects to the lowercase route" do
get path
expect(response).to have_http_status(:moved_permanently)
expect(response).to redirect_to(path.downcase)
end
end
end
RSpec.describe "StoriesIndex", type: :request do
describe "GET stories index" do
let(:user) { create(:user) }
it "renders page with article list and proper attributes", :aggregate_failures do
article = create(:article, featured: true)
navigation_link = create(:navigation_link)
get "/"
expect(response.body).to include(CGI.escapeHTML(article.title))
renders_ga_tracking_data
renders_proper_description
renders_min_read_time
renders_proper_sidebar(navigation_link)
end
def renders_proper_description
expect(response.body).to include(Settings::Community.community_description)
end
def renders_min_read_time
expect(response.body).to include("min read")
end
def renders_proper_sidebar(navigation_link)
expect(response.body).to include(CGI.escapeHTML(navigation_link.name))
end
def renders_ga_tracking_data
expect(response.body).to include("data-ga-tracking=\"#{Settings::General.ga_tracking_id}\"")
end
it "renders registration page if the Forem instance is private" do
allow(Settings::UserExperience).to receive(:public).and_return(false)
get root_path
expect(response.body).to include("Continue with")
end
it "renders a landing page if one is active and if the site config is set to private" do
allow(Settings::UserExperience).to receive(:public).and_return(false)
create(:page, title: "This is a landing page!", landing_page: true)
get root_path
expect(response.body).to include("This is a landing page!")
end
it "renders all display_ads when published and approved" do
org = create(:organization)
ad = create(:display_ad, published: true, approved: true, organization: org)
right_ad = create(:display_ad, published: true, approved: true, placement_area: "sidebar_right",
organization: org)
get "/"
expect(response.body).to include(ad.processed_html)
expect(response.body).to include(right_ad.processed_html)
end
it "does not render display_ads when not approved" do
org = create(:organization)
ad = create(:display_ad, published: true, approved: false, organization: org)
right_ad = create(:display_ad, published: true, approved: false, placement_area: "sidebar_right",
organization: org)
get "/"
expect(response.body).not_to include(ad.processed_html)
expect(response.body).not_to include(right_ad.processed_html)
end
it "displays correct sponsors", :aggregate_failures do
org = create(:organization)
gold_sponsorship = create(:sponsorship, level: "gold", tagline: "GOLD!!!", status: "live", organization: org)
silver_sponsorship = create(:sponsorship, level: "silver", tagline: "SILVER!!!", status: "live",
organization: org)
non_live_gold_sponsorship = create(:sponsorship, level: "gold", tagline: "NOT LIVE GOLD!!!", status: "pending",
organization: org)
get "/"
displays_gold_sponsors(gold_sponsorship)
does_not_display_silver_sponsors(silver_sponsorship)
does_not_display_non_live_gold_sponsors(non_live_gold_sponsorship)
end
def displays_gold_sponsors(sponsorship)
expect(response.body).to include(sponsorship.tagline)
end
def does_not_display_silver_sponsors(sponsorship)
expect(response.body).not_to include(sponsorship.tagline)
end
def does_not_display_non_live_gold_sponsors(sponsorship)
expect(response.body).not_to include(sponsorship.tagline)
end
it "shows listings" do
user = create(:user)
listing = create(:listing, user_id: user.id)
get "/"
expect(response.body).to include(CGI.escapeHTML(listing.title))
end
it "does not set cache-related headers if private" do
allow(Settings::UserExperience).to receive(:public).and_return(false)
get "/"
expect(response.status).to eq(200)
expect(response.headers["X-Accel-Expires"]).to eq(nil)
expect(response.headers["Cache-Control"]).not_to eq("public, no-cache")
expect(response.headers["Surrogate-Key"]).to eq(nil)
end
it "sets correct cache headers", :aggregate_failures do
get "/"
expect(response.status).to eq(200)
sets_fastly_headers
sets_nginx_headers
end
def sets_fastly_headers
expected_surrogate_key_headers = %w[main_app_home_page]
expect(response.headers["Surrogate-Key"].split(", ")).to match_array(expected_surrogate_key_headers)
end
def sets_nginx_headers
expect(response.headers["X-Accel-Expires"]).to eq("600")
end
it "shows default meta keywords if set" do
allow(Settings::General).to receive(:meta_keywords).and_return({ default: "cool developers, civil engineers" })
get "/"
expect(response.body).to include("<meta name=\"keywords\" content=\"cool developers, civil engineers\">")
end
it "does not show default meta keywords if not set" do
allow(Settings::General).to receive(:meta_keywords).and_return({ default: "" })
get "/"
expect(response.body).not_to include(
"<meta name=\"keywords\" content=\"cool developers, civil engineers\">",
)
end
it "shows only one cover if basic feed style" do
create_list(:article, 3, featured: true, score: 20, main_image: "https://example.com/image.jpg")
allow(Settings::UserExperience).to receive(:feed_style).and_return("basic")
get "/"
expect(response.body.scan(/(?=class="crayons-story__cover crayons-story__cover__image)/).count).to be 1
end
it "shows multiple cover images if rich feed style" do
create_list(:article, 3, featured: true, score: 20, main_image: "https://example.com/image.jpg")
allow(Settings::UserExperience).to receive(:feed_style).and_return("rich")
get "/"
expect(response.body.scan(/(?=class="crayons-story__cover crayons-story__cover__image)/).count).to be > 1
end
context "with campaign hero" do
let!(:hero_html) do
create(
:html_variant,
group: "campaign",
name: "hero",
html: "<em>#{Faker::Book.title}'s</em>",
published: true,
approved: true,
)
end
it "displays hero html when it exists and is set in config" do
allow(Settings::Campaign).to receive(:hero_html_variant_name).and_return("hero")
get root_path
expect(response.body).to include(hero_html.html)
end
it "doesn't display when hero_html_variant_name is not set" do
allow(Settings::Campaign).to receive(:hero_html_variant_name).and_return("")
get root_path
expect(response.body).not_to include(hero_html.html)
end
it "doesn't display when hero html is not approved" do
allow(Settings::Campaign).to receive(:hero_html_variant_name).and_return("hero")
hero_html.update_column(:approved, false)
get root_path
expect(response.body).not_to include(hero_html.html)
end
end
context "with campaign_sidebar" do
before do
allow(Settings::Campaign).to receive(:featured_tags).and_return("mytag,yourtag")
allow(Settings::UserExperience).to receive(:home_feed_minimum_score).and_return(7)
a_body = "---\ntitle: Super-sheep#{rand(1000)}\npublished: true\ntags: heyheyhey,mytag\n---\n\nHello"
create(:article, approved: true, body_markdown: a_body, score: 1)
u_body = "---\ntitle: Unapproved-post#{rand(1000)}\npublished: true\ntags: heyheyhey,mytag\n---\n\nHello"
create(:article, approved: false, body_markdown: u_body, score: 1)
end
it "doesn't display posts with the campaign tags when sidebar is disabled" do
allow(Settings::Campaign).to receive(:sidebar_enabled).and_return(false)
get "/"
expect(response.body).not_to include(CGI.escapeHTML("Super-sheep"))
end
it "doesn't display low-score posts" do
allow(Settings::Campaign).to receive(:sidebar_enabled).and_return(true)
allow(Settings::Campaign).to receive(:articles_require_approval).and_return(true)
get "/"
expect(response.body).not_to include(CGI.escapeHTML("Unapproved-post"))
end
it "doesn't display unapproved posts" do
allow(Settings::Campaign).to receive(:sidebar_enabled).and_return(true)
allow(Settings::Campaign).to receive(:sidebar_image).and_return("https://example.com/image.png")
allow(Settings::Campaign).to receive(:articles_require_approval).and_return(true)
Article.last.update_column(:score, -2)
get "/"
expect(response.body).not_to include(CGI.escapeHTML("Unapproved-post"))
end
it "displays unapproved post if approval is not required" do
allow(Settings::Campaign).to receive(:sidebar_enabled).and_return(true)
allow(Settings::Campaign).to receive(:sidebar_image).and_return("https://example.com/image.png")
allow(Settings::Campaign).to receive(:articles_require_approval).and_return(false)
get "/"
expect(response.body).to include(CGI.escapeHTML("Unapproved-post"))
end
it "displays only approved posts with the campaign tags" do
allow(Settings::Campaign).to receive(:sidebar_enabled).and_return(false)
get "/"
expect(response.body).not_to include(CGI.escapeHTML("Super-puper"))
end
it "displays sidebar url if url is set" do
allow(Settings::Campaign).to receive(:sidebar_enabled).and_return(true)
allow(Settings::Campaign).to receive(:url).and_return("https://campaign-lander.com")
allow(Settings::Campaign).to receive(:sidebar_image).and_return("https://example.com/image.png")
get "/"
expect(response.body).to include('<a href="https://campaign-lander.com"')
end
it "does not display sidebar url if image is not present is set" do
allow(Settings::Campaign).to receive(:sidebar_enabled).and_return(true)
allow(Settings::Campaign).to receive(:url).and_return("https://campaign-lander.com")
get "/"
expect(response.body).not_to include('<a href="https://campaign-lander.com"')
end
end
end
describe "GET query page" do
it "renders page with proper header" do
get "/search?q=hello"
expect(response.body).to include("=> Search Results")
end
end
describe "GET podcast index" do
include_examples "redirects to the lowercase route" do
let(:path) { "/#{build(:podcast).slug.upcase}" }
end
it "renders page with proper header" do
podcast = create(:podcast)
create(:podcast_episode, podcast: podcast)
get "/#{podcast.slug}"
expect(response.body).to include(podcast.title)
end
end
describe "GET user_path" do
include_examples "redirects to the lowercase route" do
let(:path) { "/#{build(:user).username.upcase}" }
end
end
describe "GET organization_path" do
include_examples "redirects to the lowercase route" do
let(:path) { "/#{build(:organization).slug.upcase}" }
end
end
end