Direct listing links only show listing if published (#3645)

* displayed_classified_listing should be published

* remove redundant line

* Update classified_listings_controller.rb

* update get request tests
This commit is contained in:
Mario See 2019-08-13 10:53:53 -04:00 committed by Ben Halpern
parent 4f94889524
commit 657d88ca13
2 changed files with 42 additions and 2 deletions

View file

@ -6,14 +6,15 @@ class ClassifiedListingsController < ApplicationController
before_action :authenticate_user!, only: %i[edit update new dashboard]
def index
@displayed_classified_listing = ClassifiedListing.find_by!(slug: params[:slug]) if params[:slug]
published_listings = ClassifiedListing.where(published: true)
@displayed_classified_listing = published_listings.find_by!(slug: params[:slug]) if params[:slug]
if params[:view] == "moderate"
return redirect_to "/internal/listings/#{@displayed_classified_listing.id}/edit"
end
@classified_listings = if params[:category].blank?
ClassifiedListing.where(published: true).
published_listings.
order("bumped_at DESC").
includes(:user, :organization, :taggings).
limit(12)

View file

@ -14,6 +14,45 @@ RSpec.describe "ClassifiedListings", type: :request do
}
end
describe "GET /listings" do
let(:listing) { create(:classified_listing, user: user) }
let(:expired_listing) { create(:classified_listing, user: user) }
before do
sign_in user
create_list(:credit, 25, user: user)
end
it "returns text/html and has status 200" do
get "/listings"
expect(response.content_type).to eq("text/html")
expect(response).to have_http_status(:ok)
end
context "when the user has no params" do
it "shows all active listings" do
get "/listings"
expect(response.body).to include("classifieds-container")
end
end
context "when the user has category and slug params for active listing" do
it "shows that direct listing" do
get "/listings", params: { category: `#{listing.category}`, slug: `#{listing.slug}` }
expect(response.body).to include(listing.title)
end
end
context "when the user has category and slug params for expired listing" do
it "shows only active listings from that category" do
expired_listing.published = false
expired_listing.save
get "/listings", params: { category: `#{expired_listing.category}`, slug: `#{expired_listing.slug}` }
expect(response.body).not_to include(expired_listing.title)
end
end
end
describe "GET /listings/new" do
before { sign_in user }