class TestUnicode(unittest.TestCase): def setUp(self): self.__base64_data = "AAIAVAAAAVZD345sAAABVkQZzNAAABwgMDEAAAFWRBnM0AAAAVZEGczQACBCREREMzVFN0FGNkMxOUJFRUFBMEE2NDJBNTBGMUZGMAAAAAAAAAAAeyJhZ3JlZWRUZXJtcyI6dHJ1ZSwiZGVmUGljIjoicHMwMC5wbmciLCJ2ZXJpZmllZCI6dHJ1ZSwid3JpdGluZ18xMDV0ZWFjaGVyIjp7ImlkIjoiNTQyZTcxMDYxMjI2OTc1NWJhN2QxMzQ5IiwicHJvZ3JhbXMiOlsid3JpdGluZ18xMDVfSkhTX2hvbWV3b3JrX1BDXzAiLCJ3cml0aW5nXzEwNV9KSFNfaG9tZXdvcmtfUENfMSJdfSwibG9naW5lZCI6dHJ1ZSwid3JpdGluZ18xMDVKSFNfcHJvZ3JhbV9uYW1lIjoiJUU1JTlDJThCJUU0JUI4JUFEJUU1JUFGJUFCJUU0JUJEJTlDJUU5JTgwJTlGJUU2JTg4JTkwIiwid3JpdGluZ18xMDV1c2VyX2lkIjoiNTVjMDY0Y2JlNGIwMDJhYjA1NWQ3ZmQzIiwibmFtZSI6InBldGVy5r2YIiwicmVjYXB0Y2hhIjoiMDc3NSIsIkpIU19wcm9ncmFtX3VybCI6Ii93cml0aW5nXzEwNS9sZWFybmluZy5odG1sJTNGc2Nob29sX3N5c3RlbSUzREpIUyIsIndyaXRpbmdfMTA1dXNlcl9uYW1lIjoicGV0ZXIlRTYlQkQlOTgiLCJ1c2VyIjoiNTVjMDY0Y2JlNGIwMDJhYjA1NWQ3ZmQzIiwiZW1haWwiOiJwZXRlcisyQGVoYW5saW4uY29tLnR3In0=" self.__transcoder = Transcoder() def __isNewVersion(self): return sys.version_info[0] > 2 def test_unicode(self): base64_data = self.__base64_data bin_session = base64.b64decode(base64_data) if self.__isNewVersion(): bin_session = bin_session.decode('iso-8859-1') buffers = list( map( ord, bin_session )) data = self.__transcoder.deserialize( buffers ) principalData = data.get('principalData') name = principalData.get('name') if not self.__isNewVersion(): name = principalData.get('name').encode('utf-8') self.assertEqual(name, 'peter潘')
class MemorySessionAPI( object ): def __init__( self, hosts = ["127.0.0.1"] ): self.__hosts = hosts self.__mc = pylibmc.Client( hosts, binary=True ) self.__transcoder = Transcoder() def create_session_data( self ): creationTime = int ( time.time() * 1000 ) session_data = dict() session_data["version"] = 2 session_data["sessionFieldsDataLength"] = 84 session_data["creationTime"] = creationTime session_data["lastAccessedTime"] = creationTime session_data["maxInactive"] = 7200 session_data["isNew"] = True session_data["isValid"] = True session_data["thisAccessedTime"] = creationTime session_data["lastBackupTime"] = creationTime session_data["id"] = uuid.uuid1().hex session_data["idLength"] = len( session_data["id"] ) session_data["authType"] = 0 session_data["principalDataLength"] = 0 session_data["savedRequestDataLength"] = 0 session_data['principalData'] = {} return session_data def get_data( self, session_id ): b64_session = self.__mc.get( session_id ) if b64_session: bin_session = base64.b64decode( b64_session ) if sys.version_info[0] > 2: bin_session = bin_session.decode('iso-8859-1') buffers = list( map( ord, bin_session )) data = self.__transcoder.deserialize( buffers ) else: data = self.create_session_data() #bin_data = ''.join( map( chr, self.__transcoder.serialize( data ) ) ) #output = base64.b64encode( bin_data ) return data def get_user_data( self, session_id ): data = self.get_data( session_id ) return data.get( "principalData" ) def update_user_data( self, session_id, user_data ): data = self.get_data( session_id ) data["principalData"] = user_data bin_data = ''.join( map( chr, self.__transcoder.serialize( data ) ) ) if sys.version_info[0] > 2: bin_data = bin_data.encode('iso-8859-1') output = base64.b64encode( bin_data ) self.__mc.set( session_id , output) def has_login( self, session_id ): principalData = self.get_user_data( session_id ) if principalData.get( "logined" ) is True: return True else: return False
class TestStringMethods(unittest.TestCase): def setUp(self): self.data = dict( authType = 0, lastBackupTime = 1461125781055, isNew = True, isValid = True, maxInactive = 7200, principalData = dict( logined = True, verified = True, user = '******', agreedTerms = True ), creationTime = 1461125781055, principalDataLength = 0, version = 2, savedPrincipalDataLength = 0, savedRequestDataLength = 0, idLength = 32, thisAccessedTime = 1461125781055, sessionFieldsDataLength = 84, id = 'a3065b6606ae11e682397831c1c31f4a', lastAccessedTime = 1461125781055 ) self.transcoder = Transcoder() def __assertItemsEqual(self, v1, v2, msg): if self.__isNewVersion(): self.assertCountEqual(v1, v2, msg) else: self.assertItemsEqual(v1, v2, msg) def __assertDictEqual(self, d1, d2, msg=None): if self.__isNewVersion(): items = d1.items() else: items = d1.iteritems() for k,v1 in items: self.assertIn(k, d2, msg) v2 = d2[k] if(isinstance(v1, collections.Iterable)): self.__assertItemsEqual(v1, v2, msg) else: self.assertEqual(v1, v2, msg) return True def __isNewVersion(self): return sys.version_info[0] > 2 def __getEncodingBData(self, encoding_data): text = ''.join( map( chr, encoding_data ) ) if self.__isNewVersion(): return text.encode('iso-8859-1') else: return text def __getDecodingBuffer(self, decoding_data): if self.__isNewVersion(): text = decoding_data.decode('iso-8859-1') else: text = decoding_data return map( ord, text ) def test_transcoder(self): data = self.data binary = self.transcoder.serialize(data) decode_data = self.transcoder.deserialize(binary) self.__assertDictEqual(data, decode_data) def test_base64(self): data = self.data encoding_data = self.transcoder.serialize( data ) encoding_base64_data = base64.b64encode( self.__getEncodingBData(encoding_data) ) decoding_base64_data = self.__getDecodingBuffer( base64.b64decode(encoding_base64_data) ) result = self.transcoder.deserialize( list(decoding_base64_data) ) self.__assertDictEqual( data, result )