Fix error with articles API and unknown username (#2247)

Currently the articles API breaks the server when called with an unknown username. It should return nothing like when called with an unknown tag.
This commit is contained in:
rhymes 2019-03-31 16:47:48 +02:00 committed by Ben Halpern
parent f1048c3cb9
commit 54ccef4766
2 changed files with 13 additions and 5 deletions

View file

@ -1,8 +1,9 @@
class ArticleApiIndexService
attr_accessor :tag, :username, :page, :state, :top
def initialize(params)
@page = params[:page]
@tag = params[:tag]
@page = params[:page]
@tag = params[:tag]
@username = params[:username]
@state = params[:state]
@top = params[:top]
@ -18,8 +19,8 @@ class ArticleApiIndexService
else
base_articles
end
articles.
decorate
articles.decorate
end
private
@ -30,6 +31,7 @@ class ArticleApiIndexService
else
30
end
if (user = User.find_by(username: username))
user.articles.
where(published: true).
@ -45,7 +47,7 @@ class ArticleApiIndexService
page(page).
per(num)
else
not_found
Article.none
end
end

View file

@ -26,6 +26,12 @@ RSpec.describe "Api::V0::Articles", type: :request do
expect(JSON.parse(response.body).size).to eq(2)
end
it "returns no articles if username param is unknown" do
create(:article, user_id: user1.id)
get "/api/articles?username=foobar"
expect(JSON.parse(response.body).size).to eq(0)
end
it "returns organization articles if username param is present" do
org = create(:organization)
create(:article, user_id: user1.id)