2424class OneLogin_Saml2_Auth_Test (unittest .TestCase ):
2525 data_path = join (dirname (__file__ ), '..' , '..' , '..' , 'data' )
2626
27- def loadSettingsJSON (self ):
28- filename = join (dirname (__file__ ), '..' , '..' , '..' , 'settings' , 'settings1.json' )
27+ def loadSettingsJSON (self , filename = None ):
28+ if filename :
29+ filename = join (dirname (__file__ ), '..' , '..' , '..' , 'settings' , filename )
30+ else :
31+ filename = join (dirname (__file__ ), '..' , '..' , '..' , 'settings' , 'settings1.json' )
2932 if exists (filename ):
3033 stream = open (filename , 'r' )
3134 settings = json .load (stream )
3235 stream .close ()
3336 return settings
34- else :
35- raise Exception ('Settings json file does not exist' )
3637
3738 def file_contents (self , filename ):
3839 f = open (filename , 'r' )
@@ -810,14 +811,29 @@ def testLogoutNameID(self):
810811 auth .process_response ()
811812
812813 name_id_from_response = auth .get_nameid ()
814+ name_id_format_from_response = auth .get_nameid_format ()
813815
814816 target_url = auth .logout ()
815817 parsed_query = parse_qs (urlparse (target_url )[4 ])
816818 self .assertIn ('SAMLRequest' , parsed_query )
817819 logout_request = OneLogin_Saml2_Utils .decode_base64_and_inflate (parsed_query ['SAMLRequest' ][0 ])
818820
819821 name_id_from_request = OneLogin_Saml2_Logout_Request .get_nameid (logout_request )
822+ name_id_format_from_request = OneLogin_Saml2_Logout_Request .get_nameid_format (logout_request )
820823 self .assertEqual (name_id_from_response , name_id_from_request )
824+ self .assertEqual (name_id_format_from_response , name_id_format_from_request )
825+
826+ new_name_id = "new_name_id"
827+ new_name_id_format = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
828+ target_url_2 = auth .logout (name_id = new_name_id , name_id_format = new_name_id_format )
829+ parsed_query = parse_qs (urlparse (target_url_2 )[4 ])
830+ self .assertIn ('SAMLRequest' , parsed_query )
831+ logout_request = OneLogin_Saml2_Utils .decode_base64_and_inflate (parsed_query ['SAMLRequest' ][0 ])
832+
833+ name_id_from_request = OneLogin_Saml2_Logout_Request .get_nameid (logout_request )
834+ name_id_format_from_request = OneLogin_Saml2_Logout_Request .get_nameid_format (logout_request )
835+ self .assertEqual (new_name_id , name_id_from_request )
836+ self .assertEqual (new_name_id_format , name_id_format_from_request )
821837
822838 def testSetStrict (self ):
823839 """
@@ -840,6 +856,98 @@ def testSetStrict(self):
840856
841857 self .assertRaises (AssertionError , auth .set_strict , '42' )
842858
859+ def testIsAuthenticated (self ):
860+ """
861+ Tests the is_authenticated method of the OneLogin_Saml2_Auth
862+ """
863+ request_data = self .get_request ()
864+ del request_data ['get_data' ]
865+ message = self .file_contents (join (self .data_path , 'responses' , 'response1.xml.base64' ))
866+ request_data ['post_data' ] = {
867+ 'SAMLResponse' : message
868+ }
869+ auth = OneLogin_Saml2_Auth (request_data , old_settings = self .loadSettingsJSON ())
870+ auth .process_response ()
871+ self .assertFalse (auth .is_authenticated ())
872+
873+ message = self .file_contents (join (self .data_path , 'responses' , 'valid_response.xml.base64' ))
874+ request_data ['post_data' ] = {
875+ 'SAMLResponse' : message
876+ }
877+ auth = OneLogin_Saml2_Auth (request_data , old_settings = self .loadSettingsJSON ())
878+ auth .process_response ()
879+ self .assertTrue (auth .is_authenticated ())
880+
881+ def testGetNameId (self ):
882+ """
883+ Tests the get_nameid method of the OneLogin_Saml2_Auth
884+ """
885+ settings = self .loadSettingsJSON ()
886+ request_data = self .get_request ()
887+ del request_data ['get_data' ]
888+ message = self .file_contents (join (self .data_path , 'responses' , 'response1.xml.base64' ))
889+ request_data ['post_data' ] = {
890+ 'SAMLResponse' : message
891+ }
892+ auth = OneLogin_Saml2_Auth (request_data , old_settings = settings )
893+ auth .process_response ()
894+ self .assertFalse (auth .is_authenticated ())
895+ self .assertEqual (auth .get_nameid (), None )
896+
897+ message = self .file_contents (join (self .data_path , 'responses' , 'valid_response.xml.base64' ))
898+ request_data ['post_data' ] = {
899+ 'SAMLResponse' : message
900+ }
901+ auth = OneLogin_Saml2_Auth (request_data , old_settings = settings )
902+ auth .process_response ()
903+ self .assertTrue (auth .is_authenticated ())
904+ self .assertEqual ("492882615acf31c8096b627245d76ae53036c090" , auth .get_nameid ())
905+
906+ settings_2 = self .loadSettingsJSON ('settings2.json' )
907+ message = self .file_contents (join (self .data_path , 'responses' , 'signed_message_encrypted_assertion2.xml.base64' ))
908+ request_data ['post_data' ] = {
909+ 'SAMLResponse' : message
910+ }
911+ auth = OneLogin_Saml2_Auth (request_data , old_settings = settings_2 )
912+ auth .process_response ()
913+ self .assertTrue (auth .is_authenticated ())
914+ self .assertEqual ("25ddd7d34a7d79db69167625cda56a320adf2876" , auth .get_nameid ())
915+
916+ def testGetNameIdFormat (self ):
917+ """
918+ Tests the get_nameid_format method of the OneLogin_Saml2_Auth
919+ """
920+ settings = self .loadSettingsJSON ()
921+ request_data = self .get_request ()
922+ del request_data ['get_data' ]
923+ message = self .file_contents (join (self .data_path , 'responses' , 'response1.xml.base64' ))
924+ request_data ['post_data' ] = {
925+ 'SAMLResponse' : message
926+ }
927+ auth = OneLogin_Saml2_Auth (request_data , old_settings = settings )
928+ auth .process_response ()
929+ self .assertFalse (auth .is_authenticated ())
930+ self .assertEqual (auth .get_nameid_format (), None )
931+
932+ message = self .file_contents (join (self .data_path , 'responses' , 'valid_response.xml.base64' ))
933+ request_data ['post_data' ] = {
934+ 'SAMLResponse' : message
935+ }
936+ auth = OneLogin_Saml2_Auth (request_data , old_settings = settings )
937+ auth .process_response ()
938+ self .assertTrue (auth .is_authenticated ())
939+ self .assertEqual ("urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress" , auth .get_nameid_format ())
940+
941+ settings_2 = self .loadSettingsJSON ('settings2.json' )
942+ message = self .file_contents (join (self .data_path , 'responses' , 'signed_message_encrypted_assertion2.xml.base64' ))
943+ request_data ['post_data' ] = {
944+ 'SAMLResponse' : message
945+ }
946+ auth = OneLogin_Saml2_Auth (request_data , old_settings = settings_2 )
947+ auth .process_response ()
948+ self .assertTrue (auth .is_authenticated ())
949+ self .assertEqual ("urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified" , auth .get_nameid_format ())
950+
843951 def testBuildRequestSignature (self ):
844952 """
845953 Tests the build_request_signature method of the OneLogin_Saml2_Auth
0 commit comments