Skip to content

Commit

Permalink
Join to page version in PagesController
Browse files Browse the repository at this point in the history
When accessing an unpublished page as an admin, the previous code would
result in the API returning a 200 response and a page with no elements.
It is more correct to return a 404. By joining the pages to their
version, we make sure this happens.
  • Loading branch information
mamhoff committed Mar 27, 2021
1 parent 3baa217 commit 15e9112
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/controllers/alchemy/json_api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ def api_page(page)
def base_page_scope
# cancancan is not able to merge our complex AR scopes for logged in users
if can?(:edit_content, ::Alchemy::Page)
Alchemy::Page.all
Alchemy::Page.all.joins(page_version)
else
Alchemy::Page.published
Alchemy::Page.published.joins(page_version)
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/requests/alchemy/json_api/layout_pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@
end
end

it "finds the page" do
it "does not find the page" do
get alchemy_json_api.layout_page_path(page.urlname)
expect(response).to have_http_status(200)
expect(response).to have_http_status(404)
end
end
end
Expand Down Expand Up @@ -101,11 +101,11 @@
end
end

it "returns all layout pages" do
it "returns all published layout pages" do
get alchemy_json_api.layout_pages_path
document = JSON.parse(response.body)
expect(document["data"]).to include(have_id(layoutpage.id.to_s))
expect(document["data"]).to include(have_id(non_public_layout_page.id.to_s))
expect(document["data"]).not_to include(have_id(non_public_layout_page.id.to_s))
expect(document["data"]).not_to include(have_id(public_page.id.to_s))
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/requests/alchemy/json_api/pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@
end
end

it "finds the page" do
it "does not find the page" do
get alchemy_json_api.page_path(page.urlname)
expect(response).to have_http_status(200)
expect(response).to have_http_status(404)
end
end
end
Expand Down Expand Up @@ -121,11 +121,11 @@
end
end

it "returns all content pages" do
it "returns all published content pages" do
get alchemy_json_api.pages_path
document = JSON.parse(response.body)
expect(document["data"]).not_to include(have_id(layoutpage.id.to_s))
expect(document["data"]).to include(have_id(non_public_page.id.to_s))
expect(document["data"]).not_to include(have_id(non_public_page.id.to_s))
expect(document["data"]).to include(have_id(public_page.id.to_s))
end
end
Expand Down

0 comments on commit 15e9112

Please sign in to comment.