Skip to content

Commit

Permalink
Merge pull request #1475 from alphagov/validate-public-assets-have-pu…
Browse files Browse the repository at this point in the history
…blic-document-urls

Validate public assets have public document URLs
  • Loading branch information
ryanb-gds committed Sep 9, 2024
2 parents 31844f9 + 87c0765 commit a3bf7a1
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 a3bf7a1

Please sign in to comment.