Commit a5b254d

mokha <mo@mokhan.ca>
2019-04-20 17:33:30
raise an error when an encryption certificate is not provided
1 parent cdfb6ff
Changed files (3)
lib/xml/kit/templatable.rb
@@ -67,6 +67,10 @@ module Xml
       # @abstract
       # @since 0.3.0
       def asymmetric_cipher(algorithm: Crypto::RsaCipher::ALGORITHM)
+        unless encryption_certificate
+          raise Xml::Kit::Error, 'encryption_certificate is not specified.'
+        end
+
         @asymmetric_cipher ||= Crypto.cipher_for(
           algorithm,
           encryption_certificate.public_key
lib/xml/kit.rb
@@ -35,6 +35,8 @@ require 'xml/kit/version'
 
 module Xml
   module Kit
+    class Error < StandardError; end
+
     class << self
       def logger
         @logger ||= Logger.new(STDOUT)
spec/xml/kit/key_info_spec.rb
@@ -4,6 +4,9 @@ RSpec.describe Xml::Kit::KeyInfo do
   subject { described_class.new }
 
   describe '#to_xml' do
+    specify { expect(Hash.from_xml(subject.to_xml)).not_to be_empty }
+    specify { expect { subject.asymmetric_cipher }.to raise_error(/encryption_certificate is not specified/) }
+
     context 'with encrypted key' do
       let(:encrypted_key) { ::Xml::Kit::EncryptedKey.new(id: id, asymmetric_cipher: asymmetric_cipher, symmetric_cipher: symmetric_cipher) }
       let(:symmetric_cipher) { ::Xml::Kit::Crypto::SymmetricCipher.new }