Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #189 from italia/hotfix/issue-146
Browse files Browse the repository at this point in the history
Fix xml declaration handling. Fix issue #146
  • Loading branch information
alranel committed Sep 20, 2018
2 parents f40a6c3 + f9f920d commit 99016fb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions testenv/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _decode_saml_request(self, saml_request):
def _convert_saml_request(saml_request):
saml_request = b64decode(saml_request)
saml_request = zlib.decompress(saml_request, -15)
return saml_request.decode('utf-8')
return saml_request

def _parse_relay_state(self):
try:
Expand Down Expand Up @@ -167,7 +167,7 @@ def _decode_saml_request(self, saml_request):
@staticmethod
def _convert_saml_request(saml_request):
saml_request = b64decode(saml_request)
return saml_request.decode('utf-8')
return saml_request

def _parse_relay_state(self):
try:
Expand Down
5 changes: 3 additions & 2 deletions testenv/spmetadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from testenv.utils import saml_to_dict
from testenv.validators import ServiceProviderMetadataXMLSchemaValidator, ValidatorGroup, XMLMetadataFormatValidator

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

ENTITYDESCRIPTOR = EntityDescriptor.tag()
Expand Down Expand Up @@ -59,7 +60,7 @@ def _load(self):

def _read_file_text(self):
path = self._config
with open(path, 'r') as fp:
with open(path, 'rb') as fp:
return fp.read()


Expand All @@ -77,7 +78,7 @@ def _load(self):
def _make_request(self):
response = requests.get(self._config.get('url'))
response.raise_for_status()
return bytes(response.text, 'utf-8')
return response.content


class ServiceProviderMetadata(object):
Expand Down
4 changes: 2 additions & 2 deletions testenv/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def setUp(self):
def test_valid_request(self):
parser = HTTPRedirectRequestParser(self.querystring)
parsed = parser.parse()
self.assertEqual(parsed.saml_request, 'saml_request')
self.assertEqual(parsed.saml_request, b'saml_request')
self.assertEqual(parsed.sig_alg, 'sig_alg')
self.assertEqual(parsed.relay_state, 'relay_state')
self.assertEqual(parsed.signature, b'signature')
Expand Down Expand Up @@ -119,7 +119,7 @@ def setUp(self):
def test_valid_request(self):
parser = HTTPPostRequestParser(self.form)
parsed = parser.parse()
self.assertEqual(parsed.saml_request, 'saml_request')
self.assertEqual(parsed.saml_request, b'saml_request')
self.assertEqual(parsed.relay_state, 'relay_state')

def test_relay_state_is_optional(self):
Expand Down
15 changes: 10 additions & 5 deletions testenv/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ class XMLFormatValidatorTestCase(unittest.TestCase):

def test_valid_request(self):
validator = XMLFormatValidator(translator=FakeTranslator())
request = FakeRequest('<a></a>')
request = FakeRequest(b'<a></a>')
self.assertIsNone(validator.validate(request))

def test_empty_request(self):
validator = XMLFormatValidator(translator=FakeTranslator())
request = FakeRequest('')
request = FakeRequest(b'')
with pytest.raises(XMLFormatValidationError) as excinfo:
validator.validate(request)
exc = excinfo.value
Expand All @@ -98,7 +98,7 @@ def test_empty_request(self):

def test_not_xml(self):
validator = XMLFormatValidator(translator=FakeTranslator())
request = FakeRequest('{"this": "is JSON"}')
request = FakeRequest(b'{"this": "is JSON"}')
with pytest.raises(XMLFormatValidationError) as excinfo:
validator.validate(request)
exc = excinfo.value
Expand All @@ -108,7 +108,7 @@ def test_not_xml(self):

def test_tag_mismatch(self):
validator = XMLFormatValidator(translator=FakeTranslator())
request = FakeRequest('<a></b>')
request = FakeRequest(b'<a></b>')
with pytest.raises(XMLFormatValidationError) as excinfo:
validator.validate(request)
exc = excinfo.value
Expand All @@ -120,13 +120,18 @@ def test_tag_mismatch(self):

def test_duplicate_attribute(self):
validator = XMLFormatValidator(translator=FakeTranslator())
request = FakeRequest('<a attr="value" attr="value"></a>')
request = FakeRequest(b'<a attr="value" attr="value"></a>')
with pytest.raises(XMLFormatValidationError) as excinfo:
validator.validate(request)
exc = excinfo.value
self.assertEqual(len(exc.details), 1)
self.assertIn('Attribute attr redefined', exc.details[0].message)

def test_xml_with_declarations(self):
validator = XMLFormatValidator(translator=FakeTranslator())
request = FakeRequest(b'<?xml version="1.0" encoding="utf-8" ?><a></a>')
validator.validate(request)


class AuthnRequestXMLSchemaValidatorTestCase(unittest.TestCase):

Expand Down

0 comments on commit 99016fb

Please sign in to comment.