diff --git a/app/controllers/api/v1/pages_controller.rb b/app/controllers/api/v1/pages_controller.rb index 3d4aa4091..c8f840c6c 100644 --- a/app/controllers/api/v1/pages_controller.rb +++ b/app/controllers/api/v1/pages_controller.rb @@ -39,8 +39,11 @@ module Api end def permitted_params + if params[:social_image].present? && params[:social_image][:url].present? + params[:remote_social_image_url] = params[:social_image][:url] + end params.permit(*%i[title slug description is_top_level_path - body_json body_markdown body_html body_css social_image template]) + body_json body_markdown body_html body_css remote_social_image_url template]) end end end diff --git a/spec/requests/api/v1/pages_spec.rb b/spec/requests/api/v1/pages_spec.rb index 0bc84ea1b..044746bca 100644 --- a/spec/requests/api/v1/pages_spec.rb +++ b/spec/requests/api/v1/pages_spec.rb @@ -153,6 +153,29 @@ RSpec.describe "Api::V1::Pages" do expect(response).to have_http_status(:success) expect(Page).not_to exist(page.id) end + + context "when providing a social image url" do + let(:image_url) { "https://example.com/image.jpg" } + let(:mocked_image_file) { Images::ProfileImageGenerator.call } + # forced binary encoding to avoid encoding issues + let(:mocked_image_data) { mocked_image_file.read.force_encoding("BINARY") } + + before do + stub_request(:get, image_url).to_return(body: mocked_image_data, headers: { "Content-Type" => "image/png" }) + end + + it "creates a page with a social image" do + post_params[:social_image] = { url: image_url } + post api_pages_path, params: post_params.to_json, headers: auth_header + expect(response).to have_http_status(:success) + + page = Page.find_by(title: post_params[:title]) + expect(page.social_image).to be_present + + uploaded_image_data = page.social_image.read.force_encoding("BINARY") + expect(uploaded_image_data).to eq(mocked_image_data) + end + end end it "retrieves all pages and renders the collection as json" do