Create/update pages via api with body_html (#20390)

This commit is contained in:
Anna Buianova 2023-11-29 21:43:55 +03:00 committed by GitHub
parent 6c45945b11
commit 78c6d5d9a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 3 deletions

View file

@ -8,7 +8,7 @@ module Admin
].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)
@ -43,7 +43,6 @@ module Admin
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

View file

@ -40,7 +40,7 @@ module Api
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

View file

@ -70,18 +70,57 @@ RSpec.describe "Api::V1::Pages" do
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)