Fix articles feed not filtering articles by user/organization (#1941)

* Fix articles feed not filtering articles by user/organization
fix #1940

* add specs for articles fetching in articles#feed controller action
This commit is contained in:
Dmitrii Krasnov 2019-03-02 18:48:55 +03:00 committed by Ben Halpern
parent bcce614a80
commit 9375d2436f
2 changed files with 50 additions and 3 deletions

View file

@ -16,15 +16,15 @@ class ArticlesController < ApplicationController
if params[:username]
if @user = User.find_by_username(params[:username])
@articles.where(user_id: @user.id)
@articles = @articles.where(user_id: @user.id)
elsif @user = Organization.find_by_slug(params[:username])
@articles.where(organization_id: @user.id).includes(:user)
@articles = @articles.where(organization_id: @user.id).includes(:user)
else
render body: nil
return
end
else
@articles.where(featured: true).includes(:user)
@articles = @articles.where(featured: true).includes(:user)
end
set_surrogate_key_header "feed", @articles.map(&:record_key)

View file

@ -6,10 +6,57 @@ RSpec.describe ArticlesController, type: :controller do
let(:user) { create(:user) }
describe "GET #feed" do
render_views
it "works" do
get :feed, format: :rss
expect(response.status).to eq(200)
end
context "when :username param is not given" do
let!(:featured_article) { create(:article, featured: true) }
let!(:not_featured_article) { create(:article, featured: false) }
before { get :feed, format: :rss }
it "returns only featured articles" do
expect(response.body).to include(featured_article.title)
expect(response.body).not_to include(not_featured_article.title)
end
end
shared_context "when user/organization articles exist" do
let(:organization) { create(:organization) }
let!(:user_article) { create(:article, user_id: user.id) }
let!(:organization_article) { create(:article, organization_id: organization.id) }
end
context "when :username param is given and belongs to a user" do
include_context "when user/organization articles exist"
before { get :feed, params: { username: user.username }, format: :rss }
it "returns only articles for that user" do
expect(response.body).to include(user_article.title)
expect(response.body).not_to include(organization_article.title)
end
end
context "when :username param is given and belongs to an organization" do
include_context "when user/organization articles exist"
before { get :feed, params: { username: organization.slug }, format: :rss }
it "returns only articles for that organization" do
expect(response.body).not_to include(user_article.title)
expect(response.body).to include(organization_article.title)
end
end
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" }, format: :rss }
it("renders empty body") { expect(response.body).to be_empty }
end
end
describe "GET #new" do