55 :license: MIT, see LICENSE for more details.
66"""
77import io
8+ import os
89from unittest import mock as mock
910import requests
1011
@@ -317,43 +318,52 @@ def test_authenticate_with_password(self, _call):
317318
318319class EmployeeClientTests (testing .TestCase ):
319320
321+
320322 @staticmethod
321- def failed_log ():
323+ def setup_response (filename , status_code = 200 , total_items = 1 ):
324+ basepath = os .path .dirname (__file__ )
325+ body = b''
326+ print (f"Base Path: { basepath } " )
327+ with open (f"{ basepath } /../SoftLayer/fixtures/xmlrpc/{ filename } .xml" , 'rb' ) as fixture :
328+ body = fixture .read ()
322329 response = requests .Response ()
323- list_body = b'''<?xml version="1.0" encoding="iso-8859-1"?>
324- <methodResponse>
325- <fault>
326- <value>
327- <struct>
328- <member>
329- <name>faultCode</name>
330- <value>
331- <string>SoftLayer_Exception_Public</string>
332- </value>
333- </member>
334- <member>
335- <name>faultString</name>
336- <value>
337- <string>Invalid username/password</string>
338- </value>
339- </member>
340- </struct>
341- </value>
342- </fault>
343- </methodResponse>'''
330+ list_body = body
344331 response .raw = io .BytesIO (list_body )
345- response .status_code = 200
332+ response .headers ['SoftLayer-Total-Items' ] = total_items
333+ response .status_code = status_code
346334 return response
347335
336+
348337 def set_up (self ):
349338 self .client = SoftLayer .API .EmployeeClient (config_file = './tests/testconfig' )
350339
351340 @mock .patch ('SoftLayer.transports.xmlrpc.requests.Session.request' )
352- def test_auth_with_pass (self , api_response ):
353- api_response .return_value = self .failed_log ( )
341+ def test_auth_with_pass_failure (self , api_response ):
342+ api_response .return_value = self .setup_response ( 'invalidLogin' )
354343 exception = self .assertRaises (
355344 exceptions .SoftLayerAPIError ,
356345 self .client .authenticate_with_password , 'testUser' , 'testPassword' , '123456' )
346+ self .assertEqual (exception .faultCode , "SoftLayer_Exception_Public" )
357347
348+ @mock .patch ('SoftLayer.transports.xmlrpc.requests.Session.request' )
349+ def test_auth_with_pass_success (self , api_response ):
350+ api_response .return_value = self .setup_response ('successLogin' )
351+ result = self .client .authenticate_with_password ('testUser' , 'testPassword' , '123456' )
352+ print (result )
353+ self .assertEqual (result ['userId' ], 1234 )
354+ self .assertEqual (self .client .settings ['softlayer' ]['userid' ], '1234' )
355+ self .assertIn ('x' * 200 , self .client .settings ['softlayer' ]['access_token' ])
356+
357+ def test_auth_with_hash (self ):
358+ self .client .auth = None
359+ self .client .authenticate_with_hash (5555 , 'abcdefg' )
360+ self .assertEqual (self .client .auth .user_id , 5555 )
361+ self .assertEqual (self .client .auth .hash , 'abcdefg' )
358362
359- self .assertEqual (exception .faultCode , "SoftLayer_Exception_Public" )
363+ @mock .patch ('SoftLayer.transports.xmlrpc.requests.Session.request' )
364+ def test_refresh_token (self , api_response ):
365+ api_response .return_value = self .setup_response ('refreshSuccess' )
366+ result = self .client .refresh_token (9999 , 'qweasdzxcqweasdzxcqweasdzxc' )
367+ self .assertEqual (self .client .auth .user_id , 9999 )
368+ self .assertIn ('REFRESHEDTOKENaaaa' , self .client .auth .hash )
369+
0 commit comments