Skip to content

Commit

Permalink
Add controller spec to ensure lead image markdown isn't in body
Browse files Browse the repository at this point in the history
This does 2 things:

1. updates the vaidation that checks for the markdown for a lead image
not being in the body to use the edition_lead_image rather than the
lead_image association. The lead image association returns nil until
persisted. We need to make sure the built edition_lead_image is valid.
2. adds a controller spec to ensure that update endpoint fails with
a relevant error message when a user tries to make an image the lead
image with it's markdown in the body.
  • Loading branch information
davidgisbey committed Nov 6, 2023
1 parent 06da2ff commit 8256b38
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/models/edition/custom_lead_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def remove_lead_image
end

def body_does_not_contain_lead_image
return if lead_image.blank? || images.none?
return if edition_lead_image.blank? || images.none?

html = Whitehall::GovspeakRenderer.new.govspeak_edition_to_html(self)
doc = Nokogiri::HTML::DocumentFragment.parse(html)

if doc.css("img").any? { |img| img[:src] == lead_image.url }
if doc.css("img").any? { |img| img[:src] == edition_lead_image.image.url }
errors.add(:body, "cannot have a reference to the lead image in the text")
end
end
Expand Down
20 changes: 19 additions & 1 deletion test/functional/admin/edition_lead_images_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Admin::EditionLeadImagesControllerTest < ActionController::TestCase
assert_equal "Lead image updated to minister-of-funk.960x640.jpg", flash[:notice]
end

test "PATCH :update does not update the lead image when the edition is invalid " do
test "PATCH :update does not update the lead image when the edition is invalid" do
login_as :writer

published_edition = create(:published_case_study)
Expand All @@ -41,6 +41,24 @@ class Admin::EditionLeadImagesControllerTest < ActionController::TestCase
assert_equal "This edition is invalid: Change note can't be blank", flash[:alert]
end

test "PATCH :update does not update the lead image when edition's body contains the images markdown" do
login_as :writer

published_edition = create(:published_case_study)
image = build(:image)
edition = create(:draft_case_study, images: [image], document: published_edition.document, body: "!!1")

Whitehall::PublishingApi
.expects(:save_draft)
.never

get :update, params: { edition_id: edition.id, id: image.id }

assert_nil edition.reload.lead_image
assert_redirected_to admin_edition_images_path(edition)
assert_equal "This edition is invalid: Body cannot have a reference to the lead image in the text", flash[:alert]
end

test "PATCH :update forbids unauthorised users" do
login_as :world_editor
image = build(:image)
Expand Down

0 comments on commit 8256b38

Please sign in to comment.