Skip to content

Commit 23a3887

Browse files
committed
Correct python3-incompatible assumptions
The s_utils tests reveal some incompatibilities with python2-specific behaviors being assumed.
1 parent 1dc8b80 commit 23a3887

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/saml2/saml.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ def set_text(self, val, base64encode=False):
193193
val = base64.encodestring(val)
194194
self.set_type("xs:base64Binary")
195195
else:
196+
if isinstance(val, six.binary_type):
197+
val = val.decode('utf-8')
196198
if isinstance(val, six.string_types):
197199
if not typ:
198200
self.set_type("xs:string")

tests/test_12_s_utils.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import base64
55

6+
import six
7+
68
from saml2 import s_utils as utils
79
from saml2 import saml
810
from saml2 import samlp
@@ -15,16 +17,20 @@
1517

1618
from pathutils import full_path
1719

18-
SUCCESS_STATUS = ('<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n'
20+
XML_HEADER = '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n'
21+
22+
SUCCESS_STATUS_NO_HEADER = (
1923
'<ns0:Status xmlns:ns0="urn:oasis:names:tc:SAML:2.0:protocol"><ns0:StatusCode '
2024
'Value="urn:oasis:names:tc:SAML:2.0:status:Success" /></ns0:Status>')
25+
SUCCESS_STATUS = '%s%s' % (XML_HEADER, SUCCESS_STATUS_NO_HEADER)
2126

22-
ERROR_STATUS = ('<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n'
27+
ERROR_STATUS_NO_HEADER = (
2328
'<ns0:Status xmlns:ns0="urn:oasis:names:tc:SAML:2.0:protocol"><ns0:StatusCode '
2429
'Value="urn:oasis:names:tc:SAML:2.0:status:Responder"><ns0:StatusCode '
2530
'Value="urn:oasis:names:tc:SAML:2.0:status:UnknownPrincipal" '
2631
'/></ns0:StatusCode><ns0:StatusMessage>Error resolving '
2732
'principal</ns0:StatusMessage></ns0:Status>')
33+
ERROR_STATUS = '%s%s' % (XML_HEADER, ERROR_STATUS_NO_HEADER)
2834

2935

3036
def _eq(l1, l2):
@@ -48,16 +54,20 @@ def test_inflate_then_deflate():
4854
txt = """Selma Lagerlöf (1858-1940) was born in Östra Emterwik, Värmland,
4955
Sweden. She was brought up on Mårbacka, the family estate, which she did
5056
not leave until 1881, when she went to a teachers' college at Stockholm"""
57+
if not isinstance(txt, six.binary_type):
58+
txt = txt.encode('utf-8')
5159

5260
interm = utils.deflate_and_base64_encode(txt)
5361
bis = utils.decode_base64_and_inflate(interm)
62+
if not isinstance(bis, six.binary_type):
63+
bis = bis.encode('utf-8')
5464
assert bis == txt
5565

5666

5767
def test_status_success():
5868
status = utils.success_status_factory()
5969
status_text = "%s" % status
60-
assert status_text == SUCCESS_STATUS
70+
assert status_text in (SUCCESS_STATUS_NO_HEADER, SUCCESS_STATUS)
6171
assert status.status_code.value == samlp.STATUS_SUCCESS
6272

6373

@@ -68,15 +78,15 @@ def test_error_status():
6878

6979
status_text = "%s" % status
7080
print(status_text)
71-
assert status_text == ERROR_STATUS
81+
assert status_text in (ERROR_STATUS_NO_HEADER, ERROR_STATUS)
7282

7383

7484
def test_status_from_exception():
7585
e = utils.UnknownPrincipal("Error resolving principal")
7686
stat = utils.error_status_factory(e)
7787
status_text = "%s" % stat
7888
print(status_text)
79-
assert status_text == ERROR_STATUS
89+
assert status_text in (ERROR_STATUS_NO_HEADER, ERROR_STATUS)
8090

8191

8292
def test_attribute_sn():
@@ -117,7 +127,10 @@ def test_attribute_onoff():
117127

118128

119129
def test_attribute_base64():
120-
b64sl = base64.b64encode("Selma Lagerlöf")
130+
txt = "Selma Lagerlöf"
131+
if not isinstance(txt, six.binary_type):
132+
txt = txt.encode("utf-8")
133+
b64sl = base64.b64encode(txt).decode('ascii')
121134
attr = utils.do_attributes({"name": (b64sl, "xs:base64Binary")})
122135

123136
assert len(attr) == 1

0 commit comments

Comments
 (0)