def test_get_certificates_wrong_XML(self): """ The incoming value is valid XML but not the correct XML. It should handle that! """ result = get_certificates('<xml/>') self.assertEqual(set(), result)
def test_get_certificates_not_XML(self): """ Since we're parsing a value from an external third party, ensure we recover gracefully from crap-in. """ # Mocking the log ensures we know the exception has been handled. with mock.patch('ckanext.adfs.metadata.log.error') as mock_logger: result = get_certificates('not xml') self.assertEqual(3, mock_logger.call_count) self.assertEqual(set(), result)
def test_get_certificates(self): """ We should get two certificates given the content of METADATA. """ result = get_certificates(METADATA.replace(u'\ufeff', '')) self.assertEqual(2, len(result)) cert_list = list(result) for cert in cert_list: # Check they're actually the certificates we're interested in. self.assertTrue(cert.startswith('MII'))
import inspect import unittest import mock import lxml import lxml.etree as ET from ckanext.adfs import validation from ckanext.adfs.metadata import get_certificates # WTF are we..? PATH_TO_FILES = os.path.dirname(inspect.getfile(inspect.currentframe())) # There is also a response.old.xml that used to be used. VALID_SAML = open('{}/response.new.xml'.format(PATH_TO_FILES), 'rb').read() # There is also a FederationMetadata.old.xml that used to be used. X509 = get_certificates( open('{}/FederationMetadata.new.xml'.format(PATH_TO_FILES), 'rb').read()) class TestValidation(unittest.TestCase): """ Tests the various functions within the validation module. """ def test_get_tag(self): """ Ensures we're able to get a tag while ignoring namespaces. """ doc = ET.fromstring( '<xml><foo xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust"><bar>baz</bar></foo></xml>' ) result = validation.get_tag(doc, 'bar') self.assertEqual('bar', result.tag)
import lxml import lxml.etree as ET from ckanext.adfs import validation from ckanext.adfs.metadata import get_certificates # WTF are we..? PATH_TO_FILES = os.path.dirname(inspect.getfile(inspect.currentframe())) # There is also a response.old.xml that used to be used. VALID_SAML = open('{}/response.new.xml'.format(PATH_TO_FILES), 'rb').read() # There is also a FederationMetadata.old.xml that used to be used. X509 = get_certificates(open('{}/FederationMetadata.new.xml'.format(PATH_TO_FILES), 'rb').read()) class TestValidation(unittest.TestCase): """ Tests the various functions within the validation module. """ def test_get_tag(self): """ Ensures we're able to get a tag while ignoring namespaces. """ doc = ET.fromstring('<xml><foo xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust"><bar>baz</bar></foo></xml>') result = validation.get_tag(doc, 'bar') self.assertEqual('bar', result.tag) self.assertIsInstance(result, ET._Element)