gracefully handle missing xml elements of relayables

parent-fetching failed, if the xml didn't contain the information needed
for it.

fixes NameError: uninitialized constant DiasporaFederation::Entities::Like::PARENT_TYPE
This commit is contained in:
Benjamin Neff 2016-07-12 22:18:14 +02:00
parent 2aaff56e14
commit 90970973a5
3 changed files with 55 additions and 2 deletions

View file

@ -203,8 +203,16 @@ module DiasporaFederation
end
def fetch_parent(data)
type = data[:parent_type] || self::PARENT_TYPE
guid = data[:parent_guid]
type = data.fetch(:parent_type) {
if const_defined?(:PARENT_TYPE)
self::PARENT_TYPE
else
raise DiasporaFederation::Entity::ValidationError, "invalid #{self}! missing 'parent_type'."
end
}
guid = data.fetch(:parent_guid) {
raise DiasporaFederation::Entity::ValidationError, "invalid #{self}! missing 'parent_guid'."
}
data[:parent] = DiasporaFederation.callbacks.trigger(:fetch_related_entity, type, guid)

View file

@ -32,5 +32,35 @@ XML
it_behaves_like "an XML Entity"
it_behaves_like "a relayable Entity"
context "invalid XML" do
it "raises a ValidationError if the parent_type is missing" do
broken_xml = <<-XML
<like>
<parent_guid>#{parent.guid}</parent_guid>
<author_signature>#{data[:author_signature]}</author_signature>
<parent_author_signature>#{data[:parent_author_signature]}</parent_author_signature>
</like>
XML
expect {
DiasporaFederation::Entities::Like.from_xml(Nokogiri::XML::Document.parse(broken_xml).root)
}.to raise_error Entity::ValidationError, "invalid DiasporaFederation::Entities::Like! missing 'parent_type'."
end
it "raises a ValidationError if the parent_guid is missing" do
broken_xml = <<-XML
<like>
<target_type>#{parent.entity_type}</target_type>
<author_signature>#{data[:author_signature]}</author_signature>
<parent_author_signature>#{data[:parent_author_signature]}</parent_author_signature>
</like>
XML
expect {
DiasporaFederation::Entities::Like.from_xml(Nokogiri::XML::Document.parse(broken_xml).root)
}.to raise_error Entity::ValidationError, "invalid DiasporaFederation::Entities::Like! missing 'parent_guid'."
end
end
end
end

View file

@ -295,6 +295,21 @@ XML
end
end
context "parse invalid XML" do
it "raises a ValidationError if the parent_guid is missing" do
broken_xml = <<-XML
<some_relayable>
<author_signature/>
<parent_author_signature/>
</some_relayable>
XML
expect {
SomeRelayable.from_xml(Nokogiri::XML::Document.parse(broken_xml).root)
}.to raise_error Entity::ValidationError, "invalid DiasporaFederation::SomeRelayable! missing 'parent_guid'."
end
end
context "fetch parent" do
before do
expect_callback(:fetch_public_key, author).and_return(author_pkey.public_key)