Skip to content

Commit

Permalink
Refactor assertFalse(True) code away from response tests
Browse files Browse the repository at this point in the history
  • Loading branch information
quantus committed Nov 15, 2016
1 parent 054f8e6 commit a9997f7
Showing 1 changed file with 28 additions and 56 deletions.
84 changes: 28 additions & 56 deletions tests/src/OneLogin/saml2_tests/response_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,17 @@ def testReturnNameId(self):

xml_4 = self.file_contents(join(self.data_path, 'responses', 'invalids', 'no_nameid.xml.base64'))
response_4 = OneLogin_Saml2_Response(settings, xml_4)
try:
with self.assertRaises(Exception) as context:
response_4.get_nameid()
self.assertTrue(False)
except Exception as e:
self.assertIn('Not NameID found in the assertion of the Response', str(e))
self.assertIn('Not NameID found in the assertion of the Response', str(context.exception))

json_settings['security']['wantNameId'] = True
settings = OneLogin_Saml2_Settings(json_settings)

response_5 = OneLogin_Saml2_Response(settings, xml_4)
try:
with self.assertRaises(Exception) as context:
response_5.get_nameid()
self.assertTrue(False)
except Exception as e:
self.assertIn('Not NameID found in the assertion of the Response', str(e))
self.assertIn('Not NameID found in the assertion of the Response', str(context.exception))

json_settings['security']['wantNameId'] = False
settings = OneLogin_Saml2_Settings(json_settings)
Expand All @@ -106,30 +102,24 @@ def testReturnNameId(self):
settings = OneLogin_Saml2_Settings(json_settings)

response_7 = OneLogin_Saml2_Response(settings, xml_4)
try:
with self.assertRaises(Exception) as context:
response_7.get_nameid()
self.assertTrue(False)
except Exception as e:
self.assertIn('Not NameID found in the assertion of the Response', str(e))
self.assertIn('Not NameID found in the assertion of the Response', str(context.exception))

json_settings['strict'] = True
settings = OneLogin_Saml2_Settings(json_settings)

xml_5 = self.file_contents(join(self.data_path, 'responses', 'invalids', 'wrong_spnamequalifier.xml.base64'))
response_8 = OneLogin_Saml2_Response(settings, xml_5)
try:
with self.assertRaises(Exception) as context:
response_8.get_nameid()
self.assertTrue(False)
except Exception as e:
self.assertIn('The SPNameQualifier value mistmatch the SP entityID value.', str(e))
self.assertIn('The SPNameQualifier value mistmatch the SP entityID value.', str(context.exception))

xml_6 = self.file_contents(join(self.data_path, 'responses', 'invalids', 'empty_nameid.xml.base64'))
response_9 = OneLogin_Saml2_Response(settings, xml_6)
try:
with self.assertRaises(Exception) as context:
response_9.get_nameid()
self.assertTrue(False)
except Exception as e:
self.assertIn('An empty NameID value found', str(e))
self.assertIn('An empty NameID value found', str(context.exception))

def testGetNameIdData(self):
"""
Expand Down Expand Up @@ -168,21 +158,17 @@ def testGetNameIdData(self):

xml_4 = self.file_contents(join(self.data_path, 'responses', 'invalids', 'no_nameid.xml.base64'))
response_4 = OneLogin_Saml2_Response(settings, xml_4)
try:
with self.assertRaises(Exception) as context:
response_4.get_nameid_data()
self.assertTrue(False)
except Exception as e:
self.assertIn('Not NameID found in the assertion of the Response', str(e))
self.assertIn('Not NameID found in the assertion of the Response', str(context.exception))

json_settings['security']['wantNameId'] = True
settings = OneLogin_Saml2_Settings(json_settings)

response_5 = OneLogin_Saml2_Response(settings, xml_4)
try:
with self.assertRaises(Exception) as context:
response_5.get_nameid_data()
self.assertTrue(False)
except Exception as e:
self.assertIn('Not NameID found in the assertion of the Response', str(e))
self.assertIn('Not NameID found in the assertion of the Response', str(context.exception))

json_settings['security']['wantNameId'] = False
settings = OneLogin_Saml2_Settings(json_settings)
Expand All @@ -195,11 +181,9 @@ def testGetNameIdData(self):
settings = OneLogin_Saml2_Settings(json_settings)

response_7 = OneLogin_Saml2_Response(settings, xml_4)
try:
with self.assertRaises(Exception) as context:
response_7.get_nameid_data()
self.assertTrue(False)
except Exception as e:
self.assertIn('Not NameID found in the assertion of the Response', str(e))
self.assertIn('Not NameID found in the assertion of the Response', str(context.exception))

json_settings['security']['wantNameId'] = False
settings = OneLogin_Saml2_Settings(json_settings)
Expand All @@ -212,30 +196,24 @@ def testGetNameIdData(self):
settings = OneLogin_Saml2_Settings(json_settings)

response_7 = OneLogin_Saml2_Response(settings, xml_4)
try:
with self.assertRaises(Exception) as context:
response_7.get_nameid_data()
self.assertTrue(False)
except Exception as e:
self.assertIn('Not NameID found in the assertion of the Response', str(e))
self.assertIn('Not NameID found in the assertion of the Response', str(context.exception))

json_settings['strict'] = True
settings = OneLogin_Saml2_Settings(json_settings)

xml_5 = self.file_contents(join(self.data_path, 'responses', 'invalids', 'wrong_spnamequalifier.xml.base64'))
response_8 = OneLogin_Saml2_Response(settings, xml_5)
try:
with self.assertRaises(Exception) as context:
response_8.get_nameid_data()
self.assertTrue(False)
except Exception as e:
self.assertIn('The SPNameQualifier value mistmatch the SP entityID value.', str(e))
self.assertIn('The SPNameQualifier value mistmatch the SP entityID value.', str(context.exception))

xml_6 = self.file_contents(join(self.data_path, 'responses', 'invalids', 'empty_nameid.xml.base64'))
response_9 = OneLogin_Saml2_Response(settings, xml_6)
try:
with self.assertRaises(Exception) as context:
response_9.get_nameid_data()
self.assertTrue(False)
except Exception as e:
self.assertIn('An empty NameID value found', str(e))
self.assertIn('An empty NameID value found', str(context.exception))

def testCheckStatus(self):
"""
Expand All @@ -252,19 +230,15 @@ def testCheckStatus(self):

xml_2 = self.file_contents(join(self.data_path, 'responses', 'invalids', 'status_code_responder.xml.base64'))
response_2 = OneLogin_Saml2_Response(settings, xml_2)
try:
with self.assertRaises(Exception) as context:
response_2.check_status()
self.assertTrue(False)
except Exception as e:
self.assertIn('The status code of the Response was not Success, was Responder', str(e))
self.assertIn('The status code of the Response was not Success, was Responder', str(context.exception))

xml_3 = self.file_contents(join(self.data_path, 'responses', 'invalids', 'status_code_responer_and_msg.xml.base64'))
response_3 = OneLogin_Saml2_Response(settings, xml_3)
try:
with self.assertRaises(Exception) as context:
response_3.check_status()
self.assertTrue(False)
except Exception as e:
self.assertIn('The status code of the Response was not Success, was Responder -> something_is_wrong', str(e))
self.assertIn('The status code of the Response was not Success, was Responder -> something_is_wrong', str(context.exception))

def testCheckOneCondition(self):
"""
Expand Down Expand Up @@ -691,11 +665,9 @@ def testIsInValidDuplicatedAttrs(self):
settings = OneLogin_Saml2_Settings(self.loadSettingsJSON())
xml = self.file_contents(join(self.data_path, 'responses', 'invalids', 'duplicated_attributes.xml.base64'))
response = OneLogin_Saml2_Response(settings, xml)
try:
with self.assertRaises(Exception) as context:
response.get_attributes()
self.assertFalse(True)
except Exception as e:
self.assertEqual('Found an Attribute element with duplicated Name', str(e))
self.assertEqual('Found an Attribute element with duplicated Name', str(context.exception))

def testIsInValidDestination(self):
"""
Expand Down

0 comments on commit a9997f7

Please sign in to comment.