From 778a782c762e68033a78e83912562285831d11c9 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Fri, 25 Sep 2015 23:31:13 +0200 Subject: [PATCH] add exceptions for salmon stuff and move all to one file --- lib/diaspora_federation/salmon.rb | 1 + lib/diaspora_federation/salmon/exceptions.rb | 50 +++++++++++++++++++ lib/diaspora_federation/salmon/xml_payload.rb | 20 ++------ .../salmon/xml_payload_spec.rb | 10 ++-- 4 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 lib/diaspora_federation/salmon/exceptions.rb diff --git a/lib/diaspora_federation/salmon.rb b/lib/diaspora_federation/salmon.rb index f7b2c47..ec46941 100644 --- a/lib/diaspora_federation/salmon.rb +++ b/lib/diaspora_federation/salmon.rb @@ -5,4 +5,5 @@ module DiasporaFederation end end +require "diaspora_federation/salmon/exceptions" require "diaspora_federation/salmon/xml_payload" diff --git a/lib/diaspora_federation/salmon/exceptions.rb b/lib/diaspora_federation/salmon/exceptions.rb new file mode 100644 index 0000000..44ae542 --- /dev/null +++ b/lib/diaspora_federation/salmon/exceptions.rb @@ -0,0 +1,50 @@ +module DiasporaFederation + module Salmon + # Raised, if the element containing the Magic Envelope is missing from the XML + class MissingMagicEnvelope < RuntimeError + end + + # Raised, if the element containing the author is empty. + class MissingAuthor < RuntimeError + end + + # Raised, if the element containing the header is missing from the XML + class MissingHeader < RuntimeError + end + + # Raised if the decrypted header has an unexpected XML structure + class InvalidHeader < RuntimeError + end + + # Raised, if the Magic Envelope XML structure is malformed. + class InvalidEnvelope < RuntimeError + end + + # Raised, if the calculated signature doesn't match the one contained in the + # Magic Envelope. + class InvalidSignature < RuntimeError + end + + # Raised, if the parsed Magic Envelope specifies an unhandled algorithm. + class InvalidAlgorithm < RuntimeError + end + + # Raised, if the parsed Magic Envelope specifies an unhandled encoding. + class InvalidEncoding < RuntimeError + end + + # Raised, if the XML structure of the parsed document doesn't resemble the + # expected structure. + class InvalidStructure < RuntimeError + end + + # Raised, if the entity name in the XML is invalid + class InvalidEntityName < RuntimeError + end + + # Raised, if the entity contained within the XML cannot be mapped to a + # defined {Entity} subclass. + class UnknownEntity < RuntimeError + end + end +end diff --git a/lib/diaspora_federation/salmon/xml_payload.rb b/lib/diaspora_federation/salmon/xml_payload.rb index 8d2b54d..48f8653 100644 --- a/lib/diaspora_federation/salmon/xml_payload.rb +++ b/lib/diaspora_federation/salmon/xml_payload.rb @@ -44,11 +44,11 @@ module DiasporaFederation # XML can't be found def self.unpack(xml) raise ArgumentError, "only Nokogiri::XML::Element allowed" unless xml.instance_of?(Nokogiri::XML::Element) - raise InvalidStructure unless wrap_valid?(xml) + raise Salmon::InvalidStructure unless wrap_valid?(xml) data = xml.at_xpath("post/*[1]") klass_name = entity_class_name(data.name) - raise UnknownEntity, "'#{klass_name}' not found" unless Entities.const_defined?(klass_name) + raise Salmon::UnknownEntity, "'#{klass_name}' not found" unless Entities.const_defined?(klass_name) klass = Entities.const_get(klass_name) populate_entity(klass, data) @@ -70,7 +70,7 @@ module DiasporaFederation # @return [String] "CamelCase" class name def self.entity_class_name(term) term.to_s.tap do |string| - raise InvalidEntityName, "'#{string}' is invalid" unless string =~ /^[a-z]*(_[a-z]*)*$/ + raise Salmon::InvalidEntityName, "'#{string}' is invalid" unless string =~ /^[a-z]*(_[a-z]*)*$/ string.sub!(/^[a-z]/, &:upcase) string.gsub!(/_([a-z])/) { Regexp.last_match[1].upcase } end @@ -126,20 +126,6 @@ module DiasporaFederation n.map {|child| populate_entity(type.first, child) } end private_class_method :parse_array_from_node - - # Raised, if the XML structure of the parsed document doesn't resemble the - # expected structure. - class InvalidStructure < RuntimeError - end - - # Raised, if the entity name in the XML is invalid - class InvalidEntityName < RuntimeError - end - - # Raised, if the entity contained within the XML cannot be mapped to a - # defined {Entity} subclass. - class UnknownEntity < RuntimeError - end end end end diff --git a/spec/lib/diaspora_federation/salmon/xml_payload_spec.rb b/spec/lib/diaspora_federation/salmon/xml_payload_spec.rb index b555220..a8d7558 100644 --- a/spec/lib/diaspora_federation/salmon/xml_payload_spec.rb +++ b/spec/lib/diaspora_federation/salmon/xml_payload_spec.rb @@ -77,7 +77,7 @@ XML XML expect { Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root) - }.to raise_error Salmon::XmlPayload::UnknownEntity, "'UnknownEntity' not found" + }.to raise_error Salmon::UnknownEntity, "'UnknownEntity' not found" end it "raises an error when the entity is not found" do @@ -88,7 +88,7 @@ XML XML expect { Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root) - }.to raise_error Salmon::XmlPayload::InvalidStructure + }.to raise_error Salmon::InvalidStructure end end @@ -136,19 +136,19 @@ XML it "raises an error when the entity name contains special characters" do expect { Salmon::XmlPayload.send(:entity_class_name, "te.st-enti/ty") - }.to raise_error Salmon::XmlPayload::InvalidEntityName, "'te.st-enti/ty' is invalid" + }.to raise_error Salmon::InvalidEntityName, "'te.st-enti/ty' is invalid" end it "raises an error when the entity name contains upper case letters" do expect { Salmon::XmlPayload.send(:entity_class_name, "TestEntity") - }.to raise_error Salmon::XmlPayload::InvalidEntityName, "'TestEntity' is invalid" + }.to raise_error Salmon::InvalidEntityName, "'TestEntity' is invalid" end it "raises an error when the entity name contains numbers" do expect { Salmon::XmlPayload.send(:entity_class_name, "te5t_ent1ty_w1th_number5") - }.to raise_error Salmon::XmlPayload::InvalidEntityName, "'te5t_ent1ty_w1th_number5' is invalid" + }.to raise_error Salmon::InvalidEntityName, "'te5t_ent1ty_w1th_number5' is invalid" end end end