Skip to content

Commit

Permalink
Create/update pages via api with body_html (forem#20390)
Browse files Browse the repository at this point in the history
  • Loading branch information
lightalloy authored Nov 29, 2023
1 parent 6c45945 commit 78c6d5d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
3 changes: 1 addition & 2 deletions app/controllers/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class PagesController < Admin::ApplicationController
].freeze

def index
@pages = Page.all.order(created_at: :desc)
@pages = Page.order(created_at: :desc)
@code_of_conduct = Page.find_by(slug: Page::CODE_OF_CONDUCT_SLUG)
@privacy = Page.find_by(slug: Page::PRIVACY_SLUG)
@terms = Page.find_by(slug: Page::TERMS_SLUG)
Expand Down Expand Up @@ -43,7 +43,6 @@ def create

def update
@page = Page.find(params[:id])

if @page.update(page_params)
flash[:success] = I18n.t("admin.pages_controller.updated")
redirect_to admin_pages_path
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def require_admin

def permitted_params
params.permit(*%i[title slug description is_top_level_path
body_json body_markdown social_image template])
body_json body_markdown body_html social_image template])
end
end
end
Expand Down
39 changes: 39 additions & 0 deletions spec/requests/api/v1/pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,57 @@
let(:post_params) do
attributes_for(:page)
end
let(:body_html) { "<div>hi, folks</div>" }

it "can create a new page via post" do
post api_pages_path, params: post_params.to_json, headers: auth_header
expect(response).to have_http_status(:success)
end

it "creates a page with body_html" do
post_params[:body_html] = body_html
post_params[:body_markdown] = ""
post api_pages_path, params: post_params.to_json, headers: auth_header
page = Page.find_by(title: post_params[:title])
expect(page.processed_html).to eq(body_html)
end

it "creates a page when both body_html and markdown are passed" do
post_params[:body_html] = body_html
post_params[:body_markdown] = "other"
post api_pages_path, params: post_params.to_json, headers: auth_header
page = Page.find_by(title: post_params[:title])
expect(page.processed_html).to include("other")
end

it "doesn't create a page when no html or md are passed" do
post_params[:body_html] = nil
post_params[:body_markdown] = nil
post api_pages_path, params: post_params.to_json, headers: auth_header
page = Page.find_by(title: post_params[:title])
expect(page).to be_nil
end

it "can update an existing page via put" do
put api_page_path(page.id), params: page.attributes.merge(title: "Brand New Title").to_json, headers: auth_header
expect(response).to have_http_status(:success)
expect(page.reload.title).to eq("Brand New Title")
end

it "updates an existing page via put with body_html" do
post_params = page.attributes.merge(body_html: body_html, body_markdown: nil)
put api_page_path(page.id), params: post_params.to_json, headers: auth_header
expect(response).to have_http_status(:success)
expect(page.reload.processed_html).to eq(body_html)
end

it "updates an existing page via put when both body_html and body_markdown are passed" do
post_params = page.attributes.merge(body_html: body_html, body_markdown: "other")
put api_page_path(page.id), params: post_params.to_json, headers: auth_header
expect(response).to have_http_status(:success)
expect(page.reload.processed_html).to include("other")
end

it "can destroy an existing page via delete" do
delete api_page_path(page.id), headers: auth_header
expect(response).to have_http_status(:success)
Expand Down

0 comments on commit 78c6d5d

Please sign in to comment.