Skip to content

Commit

Permalink
Create a ContentIdAlias on PUT Content
Browse files Browse the repository at this point in the history
A `ContentIdAlias` will be created when making a request to this
endpoint when the API consumer submits the
`[:details][:content_id_alias]` parameter, as long as they're updating
the correct document type (currently only possible for email addresses
and postal addresses).
  • Loading branch information
Gweaton committed Oct 10, 2024
1 parent 7dfed83 commit 913d66e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/commands/v2/put_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def call
edition = create_or_update_edition
set_timestamps(edition)

create_content_id_alias
update_content_dependencies(edition)

orphaned_links = link_diff_between(
Expand Down Expand Up @@ -98,6 +99,17 @@ def create_links(edition)
end
end

def create_content_id_alias
content_id_alias = payload.dig(:details, :content_id_alias)
return unless content_id_alias && EmbeddedContentFinderService::SUPPORTED_DOCUMENT_TYPES.include?(payload[:document_type])

if ContentIdAlias.find_by(name: content_id_alias)
raise CommandError.new(code: 422, message: "ContentIdAlias with that name already exists")
end

ContentIdAlias.create!(name: content_id_alias, content_id: payload[:content_id])
end

def fetch_embedded_content(edition)
return [] if edition[:details].nil?

Expand Down
73 changes: 73 additions & 0 deletions spec/commands/v2/put_content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,79 @@
)
end

describe "linking a content_id to a human readable alias" do
context "when updating a content block type" do
let(:content_block_payload) do
{
content_id:,
locale: "en",
schema_name: "content_block_email_address",
document_type: "content_block_email_address",
title: "Government Digital Service - General contact",
description: "General contact email address for Government Digital Service",
details: {
content_id_alias: "gds-general",
email_address: "[email protected]",
},
publishing_app: "whitehall",
}
end

context "and including a content_id_alias" do
it "creates a ContentIdAlias" do
expect(ContentIdAlias).to receive(:create!).with(name: content_block_payload[:details][:content_id_alias], content_id:)
described_class.call(content_block_payload)
end

context "but a matching ContentIdAlias already exists" do
before do
create(:content_id_alias, content_id:, name: content_block_payload[:details][:content_id_alias])
end

it "raises an error" do
expect {
described_class.call(content_block_payload)
}.to raise_error(CommandError) { |error|
expect(error.code).to eq(422)
}
end
end
end

context "and not including a content_id_alias" do
before do
validator = double(:validator, validate: true)
allow(Commands::V2::PutContentValidator).to receive(:new)
.and_return(validator)
end

it "does not create a ContentIdAlias" do
content_block_payload = {
content_id:,
locale: "en",
schema_name: "content_block_email_address",
document_type: "content_block_email_address",
title: "Government Digital Service - General contact",
description: "General contact email address for Government Digital Service",
details: {
email_address: "[email protected]",
},
publishing_app: "whitehall",
}
expect(ContentIdAlias).not_to receive(:create!)
described_class.call(content_block_payload)
end
end
end

context "when updating any other type" do
it "does not create a ContentIdAlias" do
expect(ContentIdAlias).not_to receive(:create!)
described_class.call(payload)
end
end
end

context "when the 'downstream' parameter is false" do
it "does not send to the downstream draft worker" do
expect(DownstreamDraftJob).not_to receive(:perform_async_in_queue)
Expand Down

0 comments on commit 913d66e

Please sign in to comment.