Skip to content

Commit 1cabc1c

Browse files
committed
Allow Welsh-language mainstream URLs to be added to document collection groups
The Publishing API's lookup_content_id endpoint does not index translated (Welsh) base paths, causing a false "must reference a GOV.UK page" error when users tried to add Welsh mainstream URLs to a document collection. Fall back to the Content Store when both Publishing API lookups return no content ID, as the Content Store resolves content items by any translated base path. If the Content Store also returns a 404, the original error is still raised. Adds Services.content_store using GdsApi::ContentStore, and updates tests to cover the Welsh URL case and to stub the Content Store in the existing "not found" test.
1 parent 849f908 commit 1cabc1c

3 files changed

Lines changed: 42 additions & 5 deletions

File tree

app/models/document_collection_non_whitehall_link/govuk_url.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ def parsed_url
3131
end
3232

3333
def content_item
34-
@content_item ||= Services.publishing_api.get_content(content_id).to_h
34+
content_id
35+
@content_item ||= begin
36+
Services.publishing_api.get_content(@content_id).to_h
37+
rescue GdsApi::HTTPNotFound
38+
content_item_from_content_store
39+
end
3540
end
3641

3742
def content_id
@@ -40,15 +45,21 @@ def content_id
4045
if @content_id.blank?
4146
toplevel_path_segment = parsed_url.path.split("/").second
4247
@content_id = Services.publishing_api.lookup_content_id(base_path: "/#{toplevel_path_segment}", with_drafts: true)
48+
4349
if @content_id.blank?
44-
raise GdsApi::HTTPNotFound, 404
50+
@content_id = content_item_from_content_store["content_id"]
51+
raise GdsApi::HTTPNotFound, 404 if @content_id.blank?
4552
else
46-
unless content_item["document_type"] == "guide"
47-
raise GdsApi::HTTPNotFound, 404
48-
end
53+
raise GdsApi::HTTPNotFound, 404 unless content_item["document_type"] == "guide"
4954
end
5055
end
5156

5257
@content_id
5358
end
59+
60+
def content_item_from_content_store
61+
Services.content_store.content_item(parsed_url.path).to_h
62+
rescue GdsApi::ContentStore::ItemNotFound
63+
raise GdsApi::HTTPNotFound, 404
64+
end
5465
end

lib/services.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require "gds_api/publishing_api"
22
require "gds_api/asset_manager"
3+
require "gds_api/content_store"
34
require "gds_api/search"
45

56
module Services
@@ -11,6 +12,10 @@ def self.publishing_api_with_huge_timeout
1112
@publishing_api_with_huge_timeout ||= publishing_api_client_with_timeout(60)
1213
end
1314

15+
def self.content_store
16+
@content_store ||= GdsApi::ContentStore.new(Plek.find("content-store"))
17+
end
18+
1419
def self.asset_manager
1520
@asset_manager ||= GdsApi::AssetManager.new(
1621
Plek.find("asset-manager"),

test/unit/app/models/document_collection_non_whitehall_link/govuk_url_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,31 @@ class DocumentCollectionNonWhitehallLink::GovukUrlTest < ActiveSupport::TestCase
9191
document_collection_group: build(:document_collection_group),
9292
)
9393

94+
Services.content_store.stubs(:content_item).raises(GdsApi::ContentStore::ItemNotFound.new(404))
95+
9496
assert_not url.valid?
9597
assert url.errors.full_messages.include?("Url must reference a GOV.UK page")
9698
end
9799

100+
test "should be valid when a Welsh-language GOV.UK URL is used that is not in the Publishing API path reservations" do
101+
welsh_content_id = SecureRandom.uuid
102+
stub_publishing_api_has_item(
103+
content_id: welsh_content_id,
104+
title: "Talu cosb hunanasesiad",
105+
base_path: "/talu-cosb-hunanasesiad",
106+
publishing_app: "publisher",
107+
)
108+
content_store_response = { "content_id" => welsh_content_id, "title" => "Talu cosb hunanasesiad", "base_path" => "/talu-cosb-hunanasesiad", "publishing_app" => "publisher" }
109+
Services.content_store.stubs(:content_item).with("/talu-cosb-hunanasesiad").returns(content_store_response)
110+
111+
url = DocumentCollectionNonWhitehallLink::GovukUrl.new(
112+
url: "https://www.gov.uk/talu-cosb-hunanasesiad",
113+
document_collection_group: build(:document_collection_group),
114+
)
115+
116+
assert url.valid?
117+
end
118+
98119
test "should be invalid when Publishing API returns a 404" do
99120
stub_any_publishing_api_call_to_return_not_found
100121

0 commit comments

Comments
 (0)