def setUpClass(self): threading.Thread(target=reactor.run, kwargs={ 'installSignalHandlers': 0 }).start() services = [] self._local_service_identifier = HC.ServerServiceIdentifier( 'local file', HC.LOCAL_FILE) self._file_service_identifier = HC.ServerServiceIdentifier( 'file service', HC.FILE_REPOSITORY) self._tag_service_identifier = HC.ServerServiceIdentifier( 'tag service', HC.TAG_REPOSITORY) permissions = [ HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS, HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES ] account_id = 1 account_type = HC.AccountType('account', permissions, (None, None)) created = HC.GetNow() - 100000 expires = None used_data = (0, 0) self._account = HC.Account(account_id, account_type, created, expires, used_data) self._access_key = os.urandom(32) self._file_hash = os.urandom(32) def TWISTEDSetup(): reactor.listenTCP( HC.DEFAULT_SERVER_ADMIN_PORT, HydrusServer.HydrusServiceAdmin(HC.SERVER_ADMIN_IDENTIFIER, 'hello')) reactor.listenTCP( HC.DEFAULT_LOCAL_FILE_PORT, HydrusServer.HydrusServiceLocal(self._local_service_identifier, 'hello')) reactor.listenTCP( HC.DEFAULT_SERVICE_PORT, HydrusServer.HydrusServiceRepositoryFile( self._file_service_identifier, 'hello')) reactor.listenTCP( HC.DEFAULT_SERVICE_PORT + 1, HydrusServer.HydrusServiceRepositoryTag( self._tag_service_identifier, 'hello')) reactor.callFromThread(TWISTEDSetup) time.sleep(1)
def setUpClass(self): services = [] self._file_service = CC.Service(os.urandom(32), HC.FILE_REPOSITORY, 'file repo', {}) self._tag_service = CC.Service(os.urandom(32), HC.TAG_REPOSITORY, 'tag repo', {}) self._admin_service = CC.Service(os.urandom(32), HC.SERVER_ADMIN, 'server admin', {}) services_manager = HC.app.GetManager('services') services_manager._keys_to_services[ self._file_service.GetServiceKey()] = self._file_service services_manager._keys_to_services[ self._tag_service.GetServiceKey()] = self._tag_service services_manager._keys_to_services[ self._admin_service.GetServiceKey()] = self._admin_service permissions = [ HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS, HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES ] account_key = os.urandom(32) account_type = HC.AccountType('account', permissions, (None, None)) created = HC.GetNow() - 100000 expires = None used_bytes = 0 used_requests = 0 self._account = HC.Account(account_key, account_type, created, expires, used_bytes, used_requests) self._access_key = os.urandom(32) self._file_hash = os.urandom(32) def TWISTEDSetup(): reactor.listenTCP( HC.DEFAULT_SERVER_ADMIN_PORT, HydrusServer.HydrusServiceAdmin( self._admin_service.GetServiceKey(), HC.SERVER_ADMIN, 'hello')) reactor.listenTCP( HC.DEFAULT_LOCAL_FILE_PORT, HydrusServer.HydrusServiceLocal(HC.LOCAL_FILE_SERVICE_KEY, HC.LOCAL_FILE, 'hello')) reactor.listenTCP( HC.DEFAULT_LOCAL_BOORU_PORT, HydrusServer.HydrusServiceBooru(HC.LOCAL_BOORU_SERVICE_KEY, HC.LOCAL_BOORU, 'hello')) reactor.listenTCP( HC.DEFAULT_SERVICE_PORT, HydrusServer.HydrusServiceRepositoryFile( self._file_service.GetServiceKey(), HC.FILE_REPOSITORY, 'hello')) reactor.listenTCP( HC.DEFAULT_SERVICE_PORT + 1, HydrusServer.HydrusServiceRepositoryTag( self._tag_service.GetServiceKey(), HC.TAG_REPOSITORY, 'hello')) reactor.callFromThread(TWISTEDSetup) time.sleep(1)
def test_server(self): discard = HC.app.GetWrite( 'session') # just to discard gumph from testserver session_key_1 = os.urandom(32) service_key = os.urandom(32) permissions = [ HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS, HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES ] access_key = os.urandom(32) account_key = os.urandom(32) account_type = HC.AccountType('account', permissions, (None, None)) created = HC.GetNow() - 100000 expires = HC.GetNow() + 300 used_bytes = 0 used_requests = 0 account = HC.Account(account_key, account_type, created, expires, used_bytes, used_requests) expires = HC.GetNow() - 10 HC.app.SetRead('sessions', [(session_key_1, service_key, account, expires)]) session_manager = HydrusSessions.HydrusSessionManagerServer() with self.assertRaises(HydrusExceptions.SessionException): session_manager.GetAccount(service_key, session_key_1) # test fetching a session already in db, after bootup expires = HC.GetNow() + 300 HC.app.SetRead('sessions', [(session_key_1, service_key, account, expires)]) session_manager = HydrusSessions.HydrusSessionManagerServer() read_account = session_manager.GetAccount(service_key, session_key_1) self.assertIs(read_account, account) # test adding a session expires = HC.GetNow() + 300 account_key_2 = os.urandom(32) account_2 = HC.Account(account_key_2, account_type, created, expires, used_bytes, used_requests) HC.app.SetRead('account_key_from_access_key', account_key_2) HC.app.SetRead('account', account_2) (session_key_2, expires_2) = session_manager.AddSession(service_key, access_key) [(args, kwargs)] = HC.app.GetWrite('session') (written_session_key, written_service_key, written_account_key, written_expires) = args self.assertEqual( (session_key_2, service_key, account_key_2, expires_2), (written_session_key, written_service_key, written_account_key, written_expires)) read_account = session_manager.GetAccount(service_key, session_key_2) self.assertIs(read_account, account_2) # test adding a new session for an account already in the manager HC.app.SetRead('account_key_from_access_key', account_key) HC.app.SetRead('account', account) (session_key_3, expires_3) = session_manager.AddSession(service_key, access_key) [(args, kwargs)] = HC.app.GetWrite('session') (written_session_key, written_service_key, written_account_key, written_expires) = args self.assertEqual((session_key_3, service_key, account_key, expires_3), (written_session_key, written_service_key, written_account_key, written_expires)) read_account = session_manager.GetAccount(service_key, session_key_3) self.assertIs(read_account, account) read_account_original = session_manager.GetAccount( service_key, session_key_1) self.assertIs(read_account, read_account_original) # test individual account refresh expires = HC.GetNow() + 300 updated_account = HC.Account(account_key, account_type, created, expires, 1, 1) HC.app.SetRead('account', updated_account) session_manager.RefreshAccounts(service_key, [account_key]) read_account = session_manager.GetAccount(service_key, session_key_1) self.assertIs(read_account, updated_account) read_account = session_manager.GetAccount(service_key, session_key_3) self.assertIs(read_account, updated_account) # test all account refresh expires = HC.GetNow() + 300 updated_account_2 = HC.Account(account_key, account_type, created, expires, 2, 2) HC.app.SetRead( 'sessions', [(session_key_1, service_key, updated_account_2, expires), (session_key_2, service_key, account_2, expires), (session_key_3, service_key, updated_account_2, expires)]) session_manager.RefreshAllAccounts() read_account = session_manager.GetAccount(service_key, session_key_1) self.assertIs(read_account, updated_account_2) read_account = session_manager.GetAccount(service_key, session_key_2) self.assertIs(read_account, account_2) read_account = session_manager.GetAccount(service_key, session_key_3) self.assertIs(read_account, updated_account_2)