diff --git a/app/models/asset.rb b/app/models/asset.rb index bb0b60f4..2454b228 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -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 diff --git a/spec/controllers/assets_controller_spec.rb b/spec/controllers/assets_controller_spec.rb index 372510dc..dedb21eb 100644 --- a/spec/controllers/assets_controller_spec.rb +++ b/spec/controllers/assets_controller_spec.rb @@ -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 diff --git a/spec/models/asset_spec.rb b/spec/models/asset_spec.rb index 15b88776..9e9c3172 100644 --- a/spec/models/asset_spec.rb +++ b/spec/models/asset_spec.rb @@ -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