Skip to content

Commit

Permalink
Validate public assets have public document URLs
Browse files Browse the repository at this point in the history
There are over 1000 assets in the Asset Manager database which are not drafts, but have a parent document URL that points to the draft stack.

We expect this may help us to diagnose an issue where at least some of those 1000+ assets were unexpectedly made public ahead of their parent document being published. If this validation had been in place we think it would have prevented the issue.
  • Loading branch information
ryanb-gds committed Sep 6, 2024
1 parent 31844f9 commit 87c0765
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/models/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,9 @@ def ensure_parent_document_url_is_valid
unless uri && %w[http https].include?(uri.scheme)
errors.add(:parent_document_url, "must be an http(s) URL")
end

if uri && uri.host.start_with?("draft-origin") && !draft?
errors.add(:parent_document_url, "must be a public GOV.UK URL")
end
end
end
4 changes: 2 additions & 2 deletions spec/controllers/assets_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@
end

it "stores parent_document_url on asset" do
attributes = valid_attributes.merge(parent_document_url: "parent-document-url")
attributes = valid_attributes.merge(parent_document_url: "http://parent-document-url")
post :create, params: { asset: attributes }

expect(assigns(:asset).parent_document_url).to eq("parent-document-url")
expect(assigns(:asset).parent_document_url).to eq("http://parent-document-url")
end

it "stores a specified content type" do
Expand Down
32 changes: 32 additions & 0 deletions spec/models/asset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,38 @@
expect(asset.errors[:parent_document_url]).to include(message)
end
end

context "and the URL points to the draft stack" do
before do
asset.parent_document_url = "https://draft-origin.publishing.service.gov.uk/government/news/test"
end

context "when the asset is a draft" do
before do
asset.draft = true
end

it "is valid" do
expect(asset).to be_valid
end
end

context "when the asset is published" do
before do
asset.draft = false
end

it "is invalid" do
expect(asset).not_to be_valid
end

it "has the expected error message" do
asset.valid?
message = "must be a public GOV.UK URL"
expect(asset.errors[:parent_document_url]).to include(message)
end
end
end
end

context "when content_type is not specified" do
Expand Down

0 comments on commit 87c0765

Please sign in to comment.