Skip to content

Commit 7bc045a

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 7bc045a

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

app/models/document_collection_non_whitehall_link/govuk_url.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def content_id
4141
toplevel_path_segment = parsed_url.path.split("/").second
4242
@content_id = Services.publishing_api.lookup_content_id(base_path: "/#{toplevel_path_segment}", with_drafts: true)
4343
if @content_id.blank?
44-
raise GdsApi::HTTPNotFound, 404
44+
@content_id = content_id_from_content_store
4545
else
4646
unless content_item["document_type"] == "guide"
4747
raise GdsApi::HTTPNotFound, 404
@@ -51,4 +51,10 @@ def content_id
5151

5252
@content_id
5353
end
54+
55+
def content_id_from_content_store
56+
Services.content_store.content_item(parsed_url.path)["content_id"]
57+
rescue GdsApi::ContentStore::ItemNotFound
58+
raise GdsApi::HTTPNotFound, 404
59+
end
5460
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)