From 78c6d5d9a0970d21396751ce120e0a61c3b888fe Mon Sep 17 00:00:00 2001 From: Anna Buianova Date: Wed, 29 Nov 2023 21:43:55 +0300 Subject: [PATCH] Create/update pages via api with body_html (#20390) --- app/controllers/admin/pages_controller.rb | 3 +- app/controllers/api/v1/pages_controller.rb | 2 +- spec/requests/api/v1/pages_spec.rb | 39 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index e13a7cfda..f02a45bff 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -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 diff --git a/app/controllers/api/v1/pages_controller.rb b/app/controllers/api/v1/pages_controller.rb index 566506a15..6b523ab46 100644 --- a/app/controllers/api/v1/pages_controller.rb +++ b/app/controllers/api/v1/pages_controller.rb @@ -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 diff --git a/spec/requests/api/v1/pages_spec.rb b/spec/requests/api/v1/pages_spec.rb index cc64d5003..93d3e3388 100644 --- a/spec/requests/api/v1/pages_spec.rb +++ b/spec/requests/api/v1/pages_spec.rb @@ -70,18 +70,57 @@ RSpec.describe "Api::V1::Pages" do let(:post_params) do attributes_for(:page) end + let(:body_html) { "
hi, folks
" } 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)