Пример #1
0
 def getAnonymousSession( self , key = None ):
     """
     @return: an anonymous session. If key is None create a new anonymous session
     @rtype: Session object
     @param key: the key to identify a anonymous session
     @type key: string
     @raise SessionError: if key is specified and doesn't match any session.
     """
     anonymousSessionAllowed = self.cfg.anonymousSession()
     if anonymousSessionAllowed== 'no':
         self.log.error("SessionFactory/can't create anonymous session ANONYMOUS_SESSION is set to \"no\" in Local/Config/Config.py")          
         raise SessionError , "can't create anonymous session: permission denied"
     try:
         session = self.__sessions[ key ]
     except KeyError: 
         if key :
             sessionDir = self.__getSessionDir( key )
             
             if os.path.exists( sessionDir ):
                 self.log.debug( "SessionFactory.getAnonymousSession( key= %s )/ the dir exist I 'll return this session" % key)
                 session = AnonymousSession( self.cfg , key )
             else: 
                 self.log.error( "can't retrieve anonymous session, the Key: %s doesn't match with any Session" % key )
                 raise SessionError , "wrong Key: %s" % key
                
         else: #new session
             session = AnonymousSession( self.cfg )
             self.log.debug( "SessionFactory.getAnonymousSession( key= %s ) / a new anonymous has been created . I 'll return this session" % key)
             
         self.__sessions[session.getKey()] = session  
         
     self.log.debug( "SessionFactory.getAnonymousSession( key= %s ) I return this anonymous session :key=" + str( session.getKey() )) 
     return session
 def testAnonymousSession(self):
     ## Create a new session
     session = AnonymousSession(self.cfg)
     ## Fetch an existing session
     session_key = session.getKey()
     session = AnonymousSession(self.cfg, session_key)
     self.assertEqual(session.getKey(), session_key)
     ## Creation should fail if missing
     shutil.rmtree(session.getDir())
     self.assertRaises(MobyleError, AnonymousSession, self.cfg, session_key)
     ## Creation should fail if disabled
     self.cfg._anonymous_session = "no"
     self.assertRaises(MobyleError, AnonymousSession, self.cfg)
 def testMergeWith(self):
     ## Create an authenticated session
     auth_session = AuthenticatedSession(self.cfg, self.email, passwd=self.passwd)
     ## Merging a session with itself should fail
     self.assertRaises(SessionError, auth_session.mergeWith, auth_session)
     ## Create an anonymous session
     self.cfg._anonymous_session = "yes"
     key = "anonymous_01"
     self._makeFakeSession(key)
     anno_session = AnonymousSession(self.cfg, key=key)
     jobs = anno_session.getAllJobs()
     datas = anno_session.getAllData()
     ## Merge sessions
     auth_session.mergeWith(anno_session)
     newJobs = auth_session.getAllJobs()
     newDatas = auth_session.getAllData()
     self.assertEqual(jobs, newJobs)
     self.assertEqual(datas, newDatas)
 def testisAuthenticated(self):
     session = AnonymousSession(self.cfg)
     self.assertFalse(session.isAuthenticated())