Skip to content

Commit 0726c4f

Browse files
committed
code style, fix deprecated warning on python3 test
1 parent edae690 commit 0726c4f

10 files changed

Lines changed: 180 additions & 101 deletions

File tree

src/onelogin/saml2/idp_metadata_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ def parse(
121121
sso_nodes = OneLogin_Saml2_XML.query(
122122
idp_descriptor_node,
123123
"./md:SingleSignOnService[@Binding='%s']" % required_sso_binding
124-
)
124+
)
125125

126126
if len(sso_nodes) > 0:
127127
idp_sso_url = sso_nodes[0].get('Location', None)
128128

129129
slo_nodes = OneLogin_Saml2_XML.query(
130130
idp_descriptor_node,
131131
"./md:SingleLogoutService[@Binding='%s']" % required_slo_binding
132-
)
132+
)
133133

134134
if len(slo_nodes) > 0:
135135
idp_slo_url = slo_nodes[0].get('Location', None)

tests/src/OneLogin/saml2_tests/auth_test.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ def testProcessNoResponse(self):
143143
Case No Response, An exception is throw
144144
"""
145145
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=self.loadSettingsJSON())
146-
self.assertRaisesRegexp(Exception, 'SAML Response not found', auth.process_response)
146+
with self.assertRaises(Exception) as context:
147+
auth.process_response()
148+
exception = context.exception
149+
self.assertIn("SAML Response not found", str(exception))
147150
self.assertEqual(auth.get_errors(), ['invalid_binding'])
148151

149152
def testProcessResponseInvalid(self):
@@ -256,7 +259,10 @@ def testProcessNoSLO(self):
256259
Case No Message, An exception is throw
257260
"""
258261
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=self.loadSettingsJSON())
259-
self.assertRaisesRegexp(Exception, 'SAML LogoutRequest/LogoutResponse not found', auth.process_slo, True)
262+
with self.assertRaises(Exception) as context:
263+
auth.process_slo(True)
264+
exception = context.exception
265+
self.assertIn("SAML LogoutRequest/LogoutResponse not found", str(exception))
260266
self.assertEqual(auth.get_errors(), ['invalid_binding'])
261267

262268
def testProcessSLOResponseInvalid(self):
@@ -767,8 +773,11 @@ def testLogoutNoSLO(self):
767773
del settings_info['idp']['singleLogoutService']
768774
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
769775
# The Header of the redirect produces an Exception
770-
self.assertRaisesRegexp(Exception, 'The IdP does not support Single Log Out',
771-
auth.logout, 'http://example.com/returnto')
776+
with self.assertRaises(Exception) as context:
777+
auth.logout('http://example.com/returnto')
778+
exception = context.exception
779+
self.assertIn("The IdP does not support Single Log Out", str(exception))
780+
772781

773782
def testLogoutNameIDandSessionIndex(self):
774783
"""
@@ -855,8 +864,10 @@ def testBuildRequestSignature(self):
855864
settings['sp']['privateKey'] = ''
856865
settings['custom_base_path'] = u'invalid/path/'
857866
auth2 = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings)
858-
self.assertRaisesRegexp(Exception, "Trying to sign the SAMLRequest but can't load the SP private key",
859-
auth2.add_request_signature, parameters)
867+
with self.assertRaises(Exception) as context:
868+
auth2.add_request_signature(parameters)
869+
exception = context.exception
870+
self.assertIn("Trying to sign the SAMLRequest but can't load the SP private key", str(exception))
860871

861872
def testBuildResponseSignature(self):
862873
"""
@@ -876,8 +887,10 @@ def testBuildResponseSignature(self):
876887
settings['sp']['privateKey'] = ''
877888
settings['custom_base_path'] = u'invalid/path/'
878889
auth2 = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings)
879-
self.assertRaisesRegexp(Exception, "Trying to sign the SAMLResponse but can't load the SP private key",
880-
auth2.add_response_signature, parameters)
890+
with self.assertRaises(Exception) as context:
891+
auth2.add_response_signature(parameters)
892+
exception = context.exception
893+
self.assertIn("Trying to sign the SAMLResponse but can't load the SP private key", str(exception))
881894

882895
def testIsInValidLogoutResponseSign(self):
883896
"""

tests/src/OneLogin/saml2_tests/authn_request_test.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222

2323

2424
class OneLogin_Saml2_Authn_Request_Test(unittest.TestCase):
25+
26+
# assertRegexpMatches deprecated on python3
27+
def assertRegex(self, text, regexp, msg=None):
28+
if hasattr(unittest.TestCase, 'assertRegex'):
29+
return super(OneLogin_Saml2_Authn_Request_Test, self).assertRegex(text, regexp, msg)
30+
else:
31+
return self.assertRegexpMatches(text, regexp, msg)
32+
2533
def loadSettingsJSON(self, filename='settings1.json'):
2634
filename = join(dirname(__file__), '..', '..', '..', 'settings', filename)
2735
if exists(filename):
@@ -53,7 +61,7 @@ def testCreateRequest(self):
5361
authn_request = OneLogin_Saml2_Authn_Request(settings)
5462
authn_request_encoded = authn_request.get_request()
5563
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded))
56-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
64+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
5765
self.assertNotIn('ProviderName="SP test"', inflated)
5866

5967
saml_settings['organization'] = {}
@@ -62,7 +70,7 @@ def testCreateRequest(self):
6270
authn_request = OneLogin_Saml2_Authn_Request(settings)
6371
authn_request_encoded = authn_request.get_request()
6472
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded))
65-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
73+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
6674
self.assertNotIn('ProviderName="SP test"', inflated)
6775

6876
def testCreateRequestAuthContext(self):
@@ -75,7 +83,7 @@ def testCreateRequestAuthContext(self):
7583
authn_request = OneLogin_Saml2_Authn_Request(settings)
7684
authn_request_encoded = authn_request.get_request()
7785
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded))
78-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
86+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
7987
self.assertIn(OneLogin_Saml2_Constants.AC_PASSWORD, inflated)
8088
self.assertNotIn(OneLogin_Saml2_Constants.AC_X509, inflated)
8189

@@ -84,7 +92,7 @@ def testCreateRequestAuthContext(self):
8492
authn_request = OneLogin_Saml2_Authn_Request(settings)
8593
authn_request_encoded = authn_request.get_request()
8694
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded))
87-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
95+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
8896
self.assertIn(OneLogin_Saml2_Constants.AC_PASSWORD_PROTECTED, inflated)
8997
self.assertNotIn(OneLogin_Saml2_Constants.AC_X509, inflated)
9098

@@ -93,7 +101,7 @@ def testCreateRequestAuthContext(self):
93101
authn_request = OneLogin_Saml2_Authn_Request(settings)
94102
authn_request_encoded = authn_request.get_request()
95103
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded))
96-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
104+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
97105
self.assertIn(OneLogin_Saml2_Constants.AC_PASSWORD_PROTECTED, inflated)
98106
self.assertNotIn(OneLogin_Saml2_Constants.AC_X509, inflated)
99107

@@ -102,7 +110,7 @@ def testCreateRequestAuthContext(self):
102110
authn_request = OneLogin_Saml2_Authn_Request(settings)
103111
authn_request_encoded = authn_request.get_request()
104112
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded))
105-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
113+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
106114
self.assertNotIn(OneLogin_Saml2_Constants.AC_PASSWORD_PROTECTED, inflated)
107115
self.assertNotIn(OneLogin_Saml2_Constants.AC_X509, inflated)
108116

@@ -111,7 +119,7 @@ def testCreateRequestAuthContext(self):
111119
authn_request = OneLogin_Saml2_Authn_Request(settings)
112120
authn_request_encoded = authn_request.get_request()
113121
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded))
114-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
122+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
115123
self.assertIn(OneLogin_Saml2_Constants.AC_PASSWORD_PROTECTED, inflated)
116124
self.assertIn(OneLogin_Saml2_Constants.AC_X509, inflated)
117125

@@ -125,7 +133,7 @@ def testCreateRequestAuthContextComparision(self):
125133
authn_request = OneLogin_Saml2_Authn_Request(settings)
126134
authn_request_encoded = authn_request.get_request()
127135
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded))
128-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
136+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
129137
self.assertIn(OneLogin_Saml2_Constants.AC_PASSWORD, inflated)
130138
self.assertNotIn(OneLogin_Saml2_Constants.AC_X509, inflated)
131139

@@ -134,15 +142,15 @@ def testCreateRequestAuthContextComparision(self):
134142
authn_request = OneLogin_Saml2_Authn_Request(settings)
135143
authn_request_encoded = authn_request.get_request()
136144
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded))
137-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
145+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
138146
self.assertIn('RequestedAuthnContext Comparison="exact"', inflated)
139147

140148
saml_settings['security']['requestedAuthnContextComparison'] = 'minimun'
141149
settings = OneLogin_Saml2_Settings(saml_settings)
142150
authn_request = OneLogin_Saml2_Authn_Request(settings)
143151
authn_request_encoded = authn_request.get_request()
144152
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded))
145-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
153+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
146154
self.assertIn('RequestedAuthnContext Comparison="minimun"', inflated)
147155

148156
def testCreateRequestForceAuthN(self):
@@ -155,19 +163,19 @@ def testCreateRequestForceAuthN(self):
155163
authn_request = OneLogin_Saml2_Authn_Request(settings)
156164
authn_request_encoded = authn_request.get_request()
157165
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded))
158-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
166+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
159167
self.assertNotIn('ForceAuthn="true"', inflated)
160168

161169
authn_request_2 = OneLogin_Saml2_Authn_Request(settings, False, False)
162170
authn_request_encoded_2 = authn_request_2.get_request()
163171
inflated_2 = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded_2))
164-
self.assertRegexpMatches(inflated_2, '^<samlp:AuthnRequest')
172+
self.assertRegex(inflated_2, '^<samlp:AuthnRequest')
165173
self.assertNotIn('ForceAuthn="true"', inflated_2)
166174

167175
authn_request_3 = OneLogin_Saml2_Authn_Request(settings, True, False)
168176
authn_request_encoded_3 = authn_request_3.get_request()
169177
inflated_3 = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded_3))
170-
self.assertRegexpMatches(inflated_3, '^<samlp:AuthnRequest')
178+
self.assertRegex(inflated_3, '^<samlp:AuthnRequest')
171179
self.assertIn('ForceAuthn="true"', inflated_3)
172180

173181
def testCreateRequestIsPassive(self):
@@ -180,19 +188,19 @@ def testCreateRequestIsPassive(self):
180188
authn_request = OneLogin_Saml2_Authn_Request(settings)
181189
authn_request_encoded = authn_request.get_request()
182190
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded))
183-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
191+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
184192
self.assertNotIn('IsPassive="true"', inflated)
185193

186194
authn_request_2 = OneLogin_Saml2_Authn_Request(settings, False, False)
187195
authn_request_encoded_2 = authn_request_2.get_request()
188196
inflated_2 = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded_2))
189-
self.assertRegexpMatches(inflated_2, '^<samlp:AuthnRequest')
197+
self.assertRegex(inflated_2, '^<samlp:AuthnRequest')
190198
self.assertNotIn('IsPassive="true"', inflated_2)
191199

192200
authn_request_3 = OneLogin_Saml2_Authn_Request(settings, False, True)
193201
authn_request_encoded_3 = authn_request_3.get_request()
194202
inflated_3 = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded_3))
195-
self.assertRegexpMatches(inflated_3, '^<samlp:AuthnRequest')
203+
self.assertRegex(inflated_3, '^<samlp:AuthnRequest')
196204
self.assertIn('IsPassive="true"', inflated_3)
197205

198206
def testCreateRequestSetNameIDPolicy(self):
@@ -205,19 +213,19 @@ def testCreateRequestSetNameIDPolicy(self):
205213
authn_request = OneLogin_Saml2_Authn_Request(settings)
206214
authn_request_encoded = authn_request.get_request()
207215
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded))
208-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
216+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
209217
self.assertIn('<samlp:NameIDPolicy', inflated)
210218

211219
authn_request_2 = OneLogin_Saml2_Authn_Request(settings, False, False, True)
212220
authn_request_encoded_2 = authn_request_2.get_request()
213221
inflated_2 = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded_2))
214-
self.assertRegexpMatches(inflated_2, '^<samlp:AuthnRequest')
222+
self.assertRegex(inflated_2, '^<samlp:AuthnRequest')
215223
self.assertIn('<samlp:NameIDPolicy', inflated_2)
216224

217225
authn_request_3 = OneLogin_Saml2_Authn_Request(settings, False, False, False)
218226
authn_request_encoded_3 = authn_request_3.get_request()
219227
inflated_3 = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded_3))
220-
self.assertRegexpMatches(inflated_3, '^<samlp:AuthnRequest')
228+
self.assertRegex(inflated_3, '^<samlp:AuthnRequest')
221229
self.assertNotIn('<samlp:NameIDPolicy', inflated_3)
222230

223231
def testCreateDeflatedSAMLRequestURLParameter(self):
@@ -230,12 +238,12 @@ def testCreateDeflatedSAMLRequestURLParameter(self):
230238
'SAMLRequest': authn_request.get_request()
231239
}
232240
auth_url = OneLogin_Saml2_Utils.redirect('http://idp.example.com/SSOService.php', parameters, True)
233-
self.assertRegexpMatches(auth_url, '^http://idp\.example\.com\/SSOService\.php\?SAMLRequest=')
241+
self.assertRegex(auth_url, '^http://idp\.example\.com\/SSOService\.php\?SAMLRequest=')
234242
exploded = urlparse(auth_url)
235243
exploded = parse_qs(exploded[4])
236244
payload = exploded['SAMLRequest'][0]
237245
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(payload))
238-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
246+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
239247

240248
def testCreateEncSAMLRequest(self):
241249
"""
@@ -258,16 +266,16 @@ def testCreateEncSAMLRequest(self):
258266
'SAMLRequest': authn_request.get_request()
259267
}
260268
auth_url = OneLogin_Saml2_Utils.redirect('http://idp.example.com/SSOService.php', parameters, True)
261-
self.assertRegexpMatches(auth_url, '^http://idp\.example\.com\/SSOService\.php\?SAMLRequest=')
269+
self.assertRegex(auth_url, '^http://idp\.example\.com\/SSOService\.php\?SAMLRequest=')
262270
exploded = urlparse(auth_url)
263271
exploded = parse_qs(exploded[4])
264272
payload = exploded['SAMLRequest'][0]
265273
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(payload))
266-
self.assertRegexpMatches(inflated, '^<samlp:AuthnRequest')
267-
self.assertRegexpMatches(inflated, 'AssertionConsumerServiceURL="http://stuff.com/endpoints/endpoints/acs.php">')
268-
self.assertRegexpMatches(inflated, '<saml:Issuer>http://stuff.com/endpoints/metadata.php</saml:Issuer>')
269-
self.assertRegexpMatches(inflated, 'Format="urn:oasis:names:tc:SAML:2.0:nameid-format:encrypted"')
270-
self.assertRegexpMatches(inflated, 'ProviderName="SP prueba"')
274+
self.assertRegex(inflated, '^<samlp:AuthnRequest')
275+
self.assertRegex(inflated, 'AssertionConsumerServiceURL="http://stuff.com/endpoints/endpoints/acs.php">')
276+
self.assertRegex(inflated, '<saml:Issuer>http://stuff.com/endpoints/metadata.php</saml:Issuer>')
277+
self.assertRegex(inflated, 'Format="urn:oasis:names:tc:SAML:2.0:nameid-format:encrypted"')
278+
self.assertRegex(inflated, 'ProviderName="SP prueba"')
271279

272280
def testGetID(self):
273281
"""
@@ -300,4 +308,5 @@ def testAttributeConsumingService(self):
300308
authn_request = OneLogin_Saml2_Authn_Request(settings)
301309
authn_request_encoded = authn_request.get_request()
302310
inflated = compat.to_string(OneLogin_Saml2_Utils.decode_base64_and_inflate(authn_request_encoded))
303-
self.assertRegexpMatches(inflated, 'AttributeConsumingServiceIndex="1"')
311+
312+
self.assertRegex(inflated, 'AttributeConsumingServiceIndex="1"')

0 commit comments

Comments
 (0)