-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2840 from alphagov/content-modelling/support-emai…
…l-address-content-blocks Support email address content blocks
- Loading branch information
Showing
4 changed files
with
98 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,14 @@ | |
:edition, | ||
document:, | ||
details: details.deep_stringify_keys, | ||
links_hash: { | ||
embed: [embedded_content_id], | ||
}, | ||
links_hash:, | ||
) | ||
end | ||
let(:links_hash) do | ||
{ | ||
embed: [embedded_content_id], | ||
} | ||
end | ||
let(:details) { {} } | ||
|
||
before do | ||
|
@@ -97,5 +100,35 @@ | |
end | ||
end | ||
end | ||
|
||
context "when the document is an email address" do | ||
let(:embedded_document) { create(:document) } | ||
let(:links_hash) do | ||
{ | ||
embed: [embedded_document.content_id], | ||
} | ||
end | ||
|
||
before do | ||
create( | ||
:edition, | ||
document: embedded_document, | ||
state: "published", | ||
content_store: "live", | ||
document_type: "content_block_email_address", | ||
details: { | ||
email_address: "[email protected]", | ||
}, | ||
) | ||
end | ||
|
||
let(:details) { { body: "some string with a reference: {{embed:content_block_email_address:#{embedded_document.content_id}}}" } } | ||
|
||
it "returns an email address" do | ||
expect(described_class.new(edition).render_embedded_content(details)).to eq({ | ||
body: "some string with a reference: [email protected]", | ||
}) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,76 @@ | ||
RSpec.describe EmbeddedContentFinderService do | ||
let(:contacts) do | ||
[ | ||
create(:edition, | ||
state: "published", | ||
document_type: "contact", | ||
content_store: "live", | ||
details: { title: "Some Title" }), | ||
RSpec.shared_examples "finds references" do |document_type| | ||
describe "when content is a #{document_type}" do | ||
let(:editions) do | ||
[ | ||
create(:edition, | ||
state: "published", | ||
document_type:, | ||
content_store: "live", | ||
details: { title: "Some Title" }), | ||
create(:edition, | ||
state: "published", | ||
document_type:, | ||
content_store: "live", | ||
details: { title: "Some other Title" }), | ||
] | ||
end | ||
|
||
let(:draft_edition) do | ||
create(:edition, | ||
state: "published", | ||
document_type: "contact", | ||
state: "draft", | ||
document_type:, | ||
content_store: "live", | ||
details: { title: "Some other Title" }), | ||
] | ||
end | ||
let(:draft_contact) do | ||
create(:edition, | ||
state: "draft", | ||
document_type: "contact", | ||
content_store: "live", | ||
details: { title: "Some Title" }) | ||
end | ||
details: { title: "Some Title" }) | ||
end | ||
|
||
describe ".fetch_linked_content_ids" do | ||
it "returns an empty hash where there are no embeds" do | ||
body = "Hello world!" | ||
it "finds content references" do | ||
body = "{{embed:#{document_type}:#{editions[0].content_id}}} {{embed:#{document_type}:#{editions[1].content_id}}}" | ||
|
||
links = EmbeddedContentFinderService.new.fetch_linked_content_ids(body, Edition::DEFAULT_LOCALE) | ||
|
||
expect(links).to eq([]) | ||
expect(links).to eq([editions[0].content_id, editions[1].content_id]) | ||
end | ||
|
||
it "finds contact references" do | ||
body = "{{embed:contact:#{contacts[0].content_id}}} {{embed:contact:#{contacts[1].content_id}}}" | ||
it "finds content references when body is an array of hashes" do | ||
body = [{ "content" => "{{embed:#{document_type}:#{editions[0].content_id}}} {{embed:#{document_type}:#{editions[1].content_id}}}" }] | ||
|
||
links = EmbeddedContentFinderService.new.fetch_linked_content_ids(body, Edition::DEFAULT_LOCALE) | ||
|
||
expect(links).to eq([contacts[0].content_id, contacts[1].content_id]) | ||
expect(links).to eq([editions[0].content_id, editions[1].content_id]) | ||
end | ||
|
||
it "finds contact references when body is an array of hashes" do | ||
body = [{ "content" => "{{embed:contact:#{contacts[0].content_id}}} {{embed:contact:#{contacts[1].content_id}}}" }] | ||
|
||
links = EmbeddedContentFinderService.new.fetch_linked_content_ids(body, Edition::DEFAULT_LOCALE) | ||
it "errors when given a content ID that is still draft" do | ||
body = "{{embed:#{document_type}:#{draft_edition.content_id}}}" | ||
|
||
expect(links).to eq([contacts[0].content_id, contacts[1].content_id]) | ||
expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(body, Edition::DEFAULT_LOCALE) }.to raise_error(CommandError) | ||
end | ||
|
||
it "errors when given a content ID that has no live editions" do | ||
body = "{{embed:contact:00000000-0000-0000-0000-000000000000}}" | ||
it "errors when given a live content ID that is not available in the current locale" do | ||
body = "{{embed:#{document_type}:#{editions[0].content_id}}}" | ||
|
||
expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(body, Edition::DEFAULT_LOCALE) }.to raise_error(CommandError) | ||
expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(body, "foo") }.to raise_error(CommandError) | ||
end | ||
end | ||
end | ||
|
||
it "errors when given a content ID that is still draft" do | ||
body = "{{embed:contact:#{draft_contact.content_id}}}" | ||
RSpec.describe EmbeddedContentFinderService do | ||
describe ".fetch_linked_content_ids" do | ||
EmbeddedContentFinderService::SUPPORTED_DOCUMENT_TYPES.each do |document_type| | ||
include_examples "finds references", document_type | ||
end | ||
|
||
expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(body, Edition::DEFAULT_LOCALE) }.to raise_error(CommandError) | ||
it "returns an empty hash where there are no embeds" do | ||
body = "Hello world!" | ||
|
||
links = EmbeddedContentFinderService.new.fetch_linked_content_ids(body, Edition::DEFAULT_LOCALE) | ||
|
||
expect(links).to eq([]) | ||
end | ||
|
||
it "errors when given a live content ID that is not available in the current locale" do | ||
body = "{{embed:contact:#{contacts[0].content_id}}}" | ||
it "errors when given a content ID that has no live editions" do | ||
body = "{{embed:contact:00000000-0000-0000-0000-000000000000}}" | ||
|
||
expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(body, "foo") }.to raise_error(CommandError) | ||
expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(body, Edition::DEFAULT_LOCALE) }.to raise_error(CommandError) | ||
end | ||
end | ||
end |