Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def content_id
def content_item_from_content_store
path = parsed_url.path

item = Services.content_store.content_item(path).to_h
item = content_item_for(path)

if item["base_path"] != path && item["document_type"] != "guide"
raise GdsApi::HTTPNotFound, 404
Expand All @@ -51,4 +51,11 @@ def content_item_from_content_store
rescue GdsApi::ContentStore::ItemNotFound
raise GdsApi::HTTPNotFound, 404
end

def content_item_for(path)
Services.content_store.content_item(path).to_h
rescue GdsApi::ContentStore::ItemNotFound
# Fall back to the draft content store for unpublished content
Services.draft_content_store.content_item(path).to_h
end
end
7 changes: 7 additions & 0 deletions lib/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ def self.content_store
@content_store ||= GdsApi::ContentStore.new(Plek.find("content-store"))
end

def self.draft_content_store
@draft_content_store ||= GdsApi::ContentStore.new(
Plek.find("draft-content-store"),
bearer_token: ENV.fetch("DRAFT_CONTENT_STORE_BEARER_TOKEN", "example"),
)
end

def self.asset_manager
@asset_manager ||= GdsApi::AssetManager.new(
Plek.find("asset-manager"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,27 @@ class DocumentCollectionNonWhitehallLink::GovukUrlTest < ActiveSupport::TestCase
assert url.valid?
end

test "should be invalid when content store returns a 404" do
test "should be valid for an unpublished URL found only in the draft content store" do
draft_content_id = SecureRandom.uuid
Services.content_store.stubs(:content_item).with("/unpublished").raises(GdsApi::ContentStore::ItemNotFound.new(404))
Services.draft_content_store.stubs(:content_item).with("/unpublished").returns(
"content_id" => draft_content_id,
"title" => "Unpublished",
"base_path" => "/unpublished",
"publishing_app" => "whitehall",
)

url = DocumentCollectionNonWhitehallLink::GovukUrl.new(
url: "https://www.gov.uk/unpublished",
document_collection_group: build(:document_collection_group),
)

assert url.valid?
end

test "should be invalid when neither content store has the path" do
Services.content_store.stubs(:content_item).raises(GdsApi::ContentStore::ItemNotFound.new(404))
Services.draft_content_store.stubs(:content_item).raises(GdsApi::ContentStore::ItemNotFound.new(404))

url = DocumentCollectionNonWhitehallLink::GovukUrl.new(
url: "https://www.gov.uk/test",
Expand Down