def _test_service_creation( self ): self._tag_service_key = HydrusData.GenerateKey() self._file_service_key = HydrusData.GenerateKey() current_services = self._read( 'services' ) self._tag_service = HydrusNetwork.GenerateService( self._tag_service_key, HC.TAG_REPOSITORY, 'tag repo', 100 ) self._file_service = HydrusNetwork.GenerateService( self._file_service_key, HC.FILE_REPOSITORY, 'file repo', 101 ) new_services = list( current_services ) new_services.append( self._tag_service ) new_services.append( self._file_service ) service_keys_to_access_keys = self._write( 'services', self._admin_account, new_services ) self.assertEqual( set( service_keys_to_access_keys.keys() ), { self._tag_service_key, self._file_service_key } ) self._tag_service_access_key = service_keys_to_access_keys[ self._tag_service_key ] self._file_service_access_key = service_keys_to_access_keys[ self._file_service_key ] self._tag_service_account_key = self._read( 'account_key_from_access_key', self._tag_service_key, self._tag_service_access_key ) self._file_service_account_key = self._read( 'account_key_from_access_key', self._file_service_key, self._file_service_access_key ) self._tag_service_account = self._read( 'account', self._tag_service_key, self._tag_service_account_key ) self._file_service_account = self._read( 'account', self._file_service_key, self._file_service_account_key ) self.assertEqual( self._tag_service_account.GetAccountKey(), self._tag_service_account_key ) self.assertEqual( self._file_service_account.GetAccountKey(), self._file_service_account_key )
def _Add(self, service_type): service_key = HydrusData.GenerateKey() port = self._GetNextPort() name = 'new service' dictionary = HydrusNetwork.GenerateDefaultServiceDictionary( service_type) service = HydrusNetwork.GenerateService(service_key, service_type, name, port, dictionary) with ClientGUITopLevelWindowsPanels.DialogEdit( self, 'edit serverside service') as dlg_edit: panel = EditServersideService(dlg_edit, service) dlg_edit.SetPanel(panel) if dlg_edit.exec() == QW.QDialog.Accepted: new_service = panel.GetValue() self._services_listctrl.SetNonDupeName(new_service) self._SetNonDupePort(new_service) self._services_listctrl.AddDatas((new_service, ))
def ParseHydrusNetworkGETArgs(requests_args): args = ParseTwistedRequestGETArgs(requests_args, INT_PARAMS, BYTE_PARAMS, STRING_PARAMS, JSON_PARAMS, JSON_BYTE_LIST_PARAMS) if 'subject_account_key' in args: args['subject_identifier'] = HydrusNetwork.AccountIdentifier( account_key=args['subject_account_key']) elif 'subject_hash' in args: hash = args['subject_hash'] if 'subject_tag' in args: tag = args['subject_tag'] content = HydrusNetwork.Content(HC.CONTENT_TYPE_MAPPING, (tag, hash)) else: content = HydrusNetwork.Content(HC.CONTENT_TYPE_FILES, [hash]) args['subject_identifier'] = HydrusNetwork.AccountIdentifier( content=content) return args
def _test_content_creation(self): tag = 'character:samus aran' hash = HydrusData.GenerateKey() mappings_content = HydrusNetwork.Content(HC.CONTENT_TYPE_MAPPINGS, (tag, (hash, ))) mapping_content = HydrusNetwork.Content(HC.CONTENT_TYPE_MAPPING, (tag, hash)) client_to_server_update = HydrusNetwork.ClientToServerUpdate() client_to_server_update.AddContent(HC.CONTENT_UPDATE_PEND, mappings_content) self._write('update', self._tag_service_key, self._tag_service_regular_account, client_to_server_update, HydrusData.GetNow()) # can extend this to generate and fetch an actual update given a timespan # result = self._read('account_from_content', self._tag_service_key, mapping_content) self.assertEqual(result.GetAccountKey(), self._tag_service_regular_account.GetAccountKey())
def GetValue( self ) -> HydrusNetwork.AccountType: title = self._title.text() permissions = {} for permission_control in self._permission_controls: ( content_type, action ) = permission_control.GetValue() if action is not None: permissions[ content_type ] = action bandwidth_rules = self._bandwidth_rules_control.GetValue() auto_creation_velocity = self._auto_create_velocity_control.GetValue() return HydrusNetwork.AccountType( account_type_key = self._account_type_key, title = title, permissions = permissions, bandwidth_rules = bandwidth_rules, auto_creation_velocity = auto_creation_velocity, auto_creation_history = self._auto_create_history )
def work_callable(): account_errors = set() account_keys_to_accounts = {} account_keys_to_account_info = {} for account_identifier in account_identifiers: try: result = service.Request( HC.GET, 'other_account', {'subject_identifier': account_identifier}) except Exception as e: account_errors.add(str(e)) continue if 'account' in result: account = result['account'] account_key = account.GetAccountKey() if account_key in account_keys_to_accounts: continue account_keys_to_accounts[account_key] = account try: response = self._service.Request( HC.GET, 'account_info', { 'subject_identifier': HydrusNetwork.AccountIdentifier( account_key=account_key) }) except Exception as e: HydrusData.PrintException(e) continue account_string = str(response['account_info']) account_keys_to_account_info[account_key] = account_string return (account_keys_to_accounts, account_keys_to_account_info, account_errors)
def ConvertToNewAccountType(account_type_key, title, dictionary_string) -> HydrusNetwork.AccountType: dictionary = HydrusSerialisable.CreateFromString(dictionary_string) permissions = dict(dictionary['permissions']) bandwidth_rules = dictionary['bandwidth_rules'] return HydrusNetwork.AccountType(account_type_key=account_type_key, title=title, permissions=permissions, bandwidth_rules=bandwidth_rules)
def work_callable(): for subject_account_key in subject_account_keys: service.Request( HC.POST, 'modify_account_unban', { 'subject_identifier': HydrusNetwork.AccountIdentifier( account_key=subject_account_key) }) return 1
def _test_tag_repo(self, service): # account from tag test_tag = 'character:samus aran' test_hash = HydrusData.GenerateKey() subject_content = HydrusNetwork.Content( content_type=HC.CONTENT_TYPE_MAPPING, content_data=(test_tag, test_hash)) subject_account_identifier = HydrusNetwork.AccountIdentifier( content=subject_content) HG.test_controller.SetRead('account', self._account) response = service.Request( HC.GET, 'other_account', {'subject_identifier': subject_account_identifier}) self.assertEqual(repr(response['account']), repr(self._account))
def _ModifyAccounts( self ): accounts = self._account_list.GetData( only_selected = True ) if len( accounts ) > 0: subject_account_identifiers = [ HydrusNetwork.AccountIdentifier( account_key = account.GetAccountKey() ) for account in accounts ] frame = ClientGUITopLevelWindowsPanels.FrameThatTakesScrollablePanel( self.window().parentWidget(), 'manage accounts' ) panel = ModifyAccountsPanel( frame, self._service_key, subject_account_identifiers ) frame.SetPanel( panel )
def work_callable(): for (subject_account_key, new_expires) in subject_account_keys_and_new_expires: service.Request( HC.POST, 'modify_account_expires', { 'subject_identifier': HydrusNetwork.AccountIdentifier( account_key=subject_account_key), 'expires': new_expires }) return 1
def work_callable(): for subject_account_key in subject_account_keys: service.Request( HC.POST, 'modify_account_ban', { 'subject_identifier': HydrusNetwork.AccountIdentifier( account_key=subject_account_key), 'reason': reason, 'expires': expires }) return 1
def GetValue( self ) -> HydrusNetwork.ServerService: ( name, port, dictionary_part ) = self._service_panel.GetValue() dictionary = self._dictionary.Duplicate() dictionary.update( dictionary_part ) for panel in self._panels: dictionary_part = panel.GetValue() dictionary.update( dictionary_part ) return HydrusNetwork.GenerateService( self._service_key, self._service_type, name, port, dictionary )
def _GeneratePermissionChoices( self, service_type ): possible_permissions = HydrusNetwork.GetPossiblePermissions( service_type ) permission_choices = [] for ( content_type, possible_actions ) in possible_permissions: choices = [] for action in possible_actions: choices.append( ( HC.permission_pair_string_lookup[ ( content_type, action ) ], action ) ) permission_choices.append( ( content_type, choices ) ) return permission_choices
def _test_restricted(self, service): # access_key registration_key = HydrusData.GenerateKey() HG.test_controller.SetRead('access_key', self._access_key) response = service.Request(HC.GET, 'access_key', {'registration_key': registration_key}) self.assertEqual(response['access_key'], self._access_key) # set up session last_error = 0 account = self._account HG.test_controller.SetRead('service', service) HG.test_controller.SetRead('account_key_from_access_key', HydrusData.GenerateKey()) HG.test_controller.SetRead('account', self._account) # account response = service.Request(HC.GET, 'account') self.assertEqual(repr(response['account']), repr(self._account)) # account from access key HG.test_controller.SetRead('account', self._account) subject_account_identifier = HydrusNetwork.AccountIdentifier( account_key=self._account.GetAccountKey()) response = service.Request( HC.GET, 'other_account', {'subject_identifier': subject_account_identifier}) self.assertEqual(repr(response['account']), repr(self._account)) # account from file HG.test_controller.SetRead('account_from_content', self._account) content = HydrusNetwork.Content( content_type=HC.CONTENT_TYPE_FILES, content_data=(HydrusData.GenerateKey(), )) subject_account_identifier = HydrusNetwork.AccountIdentifier( content=content) response = service.Request( HC.GET, 'other_account', {'subject_identifier': subject_account_identifier}) self.assertEqual(repr(response['account']), repr(self._account)) # account from mapping HG.test_controller.SetRead('account_from_content', self._account) content = HydrusNetwork.Content( content_type=HC.CONTENT_TYPE_MAPPING, content_data=('hello', HydrusData.GenerateKey())) subject_account_identifier = HydrusNetwork.AccountIdentifier( content=content) response = service.Request( HC.GET, 'other_account', {'subject_identifier': subject_account_identifier}) self.assertEqual(repr(response['account']), repr(self._account)) # account_info account_info = {'message': 'hello'} HG.test_controller.SetRead('account_info', account_info) subject_account_identifier = HydrusNetwork.AccountIdentifier( account_key=HydrusData.GenerateKey()) response = service.Request( HC.GET, 'account_info', {'subject_identifier': subject_account_identifier}) self.assertEqual(response['account_info'], account_info) # content = HydrusNetwork.Content( content_type=HC.CONTENT_TYPE_FILES, content_data=(HydrusData.GenerateKey(), )) subject_account_identifier = HydrusNetwork.AccountIdentifier( content=content) with self.assertRaises(HydrusExceptions.BadRequestException): # can only do it with an account id response = service.Request( HC.GET, 'account_info', {'subject_identifier': subject_account_identifier}) content = HydrusNetwork.Content( content_type=HC.CONTENT_TYPE_MAPPING, content_data=('hello', HydrusData.GenerateKey())) subject_account_identifier = HydrusNetwork.AccountIdentifier( content=content) with self.assertRaises(HydrusExceptions.BadRequestException): # can only do it with an account id response = service.Request( HC.GET, 'account_info', {'subject_identifier': subject_account_identifier}) # account_types account_types = [ HydrusNetwork.AccountType.GenerateAdminAccountType( service.GetServiceType()) ] HG.test_controller.SetRead('account_types', account_types) HG.test_controller.ClearWrites('account_types') response = service.Request(HC.GET, 'account_types') self.assertEqual(response['account_types'][0].GetAccountTypeKey(), account_types[0].GetAccountTypeKey()) empty_account_type = HydrusNetwork.AccountType.GenerateNewAccountType( 'empty account', {}, HydrusNetworking.BandwidthRules()) account_types.append(empty_account_type) service.Request( HC.POST, 'account_types', { 'account_types': account_types, 'deletee_account_type_keys_to_new_account_type_keys': {} }) written = HG.test_controller.GetWrite('account_types') [(args, kwargs)] = written (written_service_key, written_account, written_account_types, written_deletee_account_type_keys_to_new_account_type_keys) = args self.assertEqual( {wat.GetAccountTypeKey() for wat in written_account_types}, {at.GetAccountTypeKey() for at in account_types}) self.assertEqual( written_deletee_account_type_keys_to_new_account_type_keys, {}) # registration_keys registration_key = HydrusData.GenerateKey() HG.test_controller.SetRead('registration_keys', [registration_key]) response = service.Request( HC.GET, 'registration_keys', { 'num': 1, 'account_type_key': os.urandom(32), 'expires': HydrusData.GetNow() + 1200 }) self.assertEqual(response['registration_keys'], [registration_key])
def setUpClass(cls): cls._access_key = HydrusData.GenerateKey() cls._serverside_file_service = HydrusNetwork.GenerateService( HydrusData.GenerateKey(), HC.FILE_REPOSITORY, 'file repo', HC.DEFAULT_SERVICE_PORT + 1) cls._serverside_tag_service = HydrusNetwork.GenerateService( HydrusData.GenerateKey(), HC.TAG_REPOSITORY, 'tag repo', HC.DEFAULT_SERVICE_PORT) cls._serverside_admin_service = HydrusNetwork.GenerateService( HydrusData.GenerateKey(), HC.SERVER_ADMIN, 'server admin', HC.DEFAULT_SERVER_ADMIN_PORT) cls._clientside_file_service = ClientServices.GenerateService( HydrusData.GenerateKey(), HC.FILE_REPOSITORY, 'file repo') cls._clientside_tag_service = ClientServices.GenerateService( HydrusData.GenerateKey(), HC.TAG_REPOSITORY, 'tag repo') cls._clientside_admin_service = ClientServices.GenerateService( HydrusData.GenerateKey(), HC.SERVER_ADMIN, 'server admin') cls._clientside_file_service.SetCredentials( HydrusNetwork.Credentials('127.0.0.1', HC.DEFAULT_SERVICE_PORT + 1, cls._access_key)) cls._clientside_tag_service.SetCredentials( HydrusNetwork.Credentials('127.0.0.1', HC.DEFAULT_SERVICE_PORT, cls._access_key)) cls._clientside_admin_service.SetCredentials( HydrusNetwork.Credentials('127.0.0.1', HC.DEFAULT_SERVER_ADMIN_PORT, cls._access_key)) cls._local_booru = ClientServices.GenerateService( CC.LOCAL_BOORU_SERVICE_KEY, HC.LOCAL_BOORU, 'local booru') services_manager = HG.test_controller.services_manager services_manager._keys_to_services[ cls._clientside_file_service.GetServiceKey( )] = cls._clientside_file_service services_manager._keys_to_services[ cls._clientside_tag_service.GetServiceKey( )] = cls._clientside_tag_service services_manager._keys_to_services[ cls._clientside_admin_service.GetServiceKey( )] = cls._clientside_admin_service account_key = HydrusData.GenerateKey() account_type = HydrusNetwork.AccountType.GenerateAdminAccountType( HC.SERVER_ADMIN) created = HydrusData.GetNow() - 100000 expires = None cls._account = HydrusNetwork.Account(account_key, account_type, created, expires) cls._service_keys_to_empty_account_types = {} cls._service_keys_to_empty_accounts = {} cls._file_hash = HydrusData.GenerateKey() def TWISTEDSetup(): cls._ssl_cert_path = os.path.join(TestController.DB_DIR, 'server.crt') cls._ssl_key_path = os.path.join(TestController.DB_DIR, 'server.key') # if db test ran, this is still hanging around and read-only, so don't bother to fail overwriting if not os.path.exists(cls._ssl_cert_path): HydrusEncryption.GenerateOpenSSLCertAndKeyFile( cls._ssl_cert_path, cls._ssl_key_path) context_factory = twisted.internet.ssl.DefaultOpenSSLContextFactory( cls._ssl_key_path, cls._ssl_cert_path) reactor.listenSSL( HC.DEFAULT_SERVER_ADMIN_PORT, ServerServer.HydrusServiceAdmin(cls._serverside_admin_service), context_factory) reactor.listenSSL( HC.DEFAULT_SERVICE_PORT + 1, ServerServer.HydrusServiceRepositoryFile( cls._serverside_file_service), context_factory) reactor.listenSSL( HC.DEFAULT_SERVICE_PORT, ServerServer.HydrusServiceRepositoryTag( cls._serverside_tag_service), context_factory) reactor.listenTCP( 45866, ClientLocalServer.HydrusServiceBooru( cls._local_booru, allow_non_local_connections=False)) reactor.callFromThread(TWISTEDSetup) time.sleep(3)
def _test_repo(self, service): service_key = service.GetServiceKey() # num_petitions num_petitions = [[ HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_STATUS_PETITIONED, 23 ], [HC.CONTENT_TYPE_TAG_PARENTS, HC.CONTENT_STATUS_PENDING, 0]] HG.test_controller.SetRead('num_petitions', num_petitions) response = service.Request(HC.GET, 'num_petitions') self.assertEqual(response['num_petitions'], num_petitions) # petition action = HC.CONTENT_UPDATE_PETITION petitioner_account = HydrusNetwork.Account.GenerateUnknownAccount() reason = 'it sucks' contents = [ HydrusNetwork.Content( HC.CONTENT_TYPE_FILES, [HydrusData.GenerateKey() for i in range(10)]) ] petition = HydrusNetwork.Petition(action, petitioner_account, reason, contents) HG.test_controller.SetRead('petition', petition) response = service.Request( HC.GET, 'petition', { 'content_type': HC.CONTENT_TYPE_FILES, 'status': HC.CONTENT_UPDATE_PETITION }) self.assertEqual(response['petition'].GetSerialisableTuple(), petition.GetSerialisableTuple()) # definitions definitions_update = HydrusNetwork.DefinitionsUpdate() for i in range(100, 200): definitions_update.AddRow( (HC.DEFINITIONS_TYPE_TAGS, i, 'series:test ' + str(i))) definitions_update.AddRow((HC.DEFINITIONS_TYPE_HASHES, i + 500, HydrusData.GenerateKey())) definitions_update_network_bytes = definitions_update.DumpToNetworkBytes( ) definitions_update_hash = hashlib.sha256( definitions_update_network_bytes).digest() path = ServerFiles.GetExpectedFilePath(definitions_update_hash) HydrusPaths.MakeSureDirectoryExists(path) with open(path, 'wb') as f: f.write(definitions_update_network_bytes) response = service.Request(HC.GET, 'update', {'update_hash': definitions_update_hash}) try: os.remove(path) except: pass self.assertEqual(response, definitions_update_network_bytes) # content rows = [(random.randint(100, 1000), [random.randint(100, 1000) for i in range(50)]) for j in range(20)] content_update = HydrusNetwork.ContentUpdate() for row in rows: content_update.AddRow( (HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_ADD, row)) content_update_network_bytes = content_update.DumpToNetworkBytes() content_update_hash = hashlib.sha256( content_update_network_bytes).digest() path = ServerFiles.GetExpectedFilePath(content_update_hash) with open(path, 'wb') as f: f.write(content_update_network_bytes) response = service.Request(HC.GET, 'update', {'update_hash': content_update_hash}) try: os.remove(path) except: pass self.assertEqual(response, content_update_network_bytes) # metadata metadata = HydrusNetwork.Metadata() metadata.AppendUpdate([definitions_update_hash, content_update_hash], HydrusData.GetNow() - 101000, HydrusData.GetNow() - 1000, HydrusData.GetNow() + 100000) service._metadata = metadata response = service.Request(HC.GET, 'metadata_slice', {'since': 0}) self.assertEqual(response['metadata_slice'].GetSerialisableTuple(), metadata.GetSerialisableTuple()) # post content raise NotImplementedError() '''
def _test_file_repo(self, service): # file path = ServerFiles.GetExpectedFilePath(self._file_hash) HydrusPaths.MakeSureDirectoryExists(os.path.dirname(path)) with open(path, 'wb') as f: f.write(EXAMPLE_FILE) response = service.Request(HC.GET, 'file', {'hash': self._file_hash}) self.assertEqual(response, EXAMPLE_FILE) # try: os.remove(path) except: pass path = os.path.join(HC.STATIC_DIR, 'hydrus.png') with open(path, 'rb') as f: file_bytes = f.read() HG.test_controller.ClearWrites('file') service.Request(HC.POST, 'file', {'file': file_bytes}) written = HG.test_controller.GetWrite('file') [(args, kwargs)] = written (written_service_key, written_account, written_file_dict) = args hash = b'\xadm5\x99\xa6\xc4\x89\xa5u\xeb\x19\xc0&\xfa\xce\x97\xa9\xcdey\xe7G(\xb0\xce\x94\xa6\x01\xd22\xf3\xc3' self.assertEqual(written_file_dict['hash'], hash) self.assertEqual(written_file_dict['ip'], '127.0.0.1') self.assertEqual(written_file_dict['height'], 200) self.assertEqual(written_file_dict['width'], 200) self.assertEqual(written_file_dict['mime'], 2) self.assertEqual(written_file_dict['size'], 5270) # ip (ip, timestamp) = ('94.45.87.123', HydrusData.GetNow() - 100000) HG.test_controller.SetRead('ip', (ip, timestamp)) response = service.Request(HC.GET, 'ip', {'hash': self._file_hash}) self.assertEqual(response['ip'], ip) self.assertEqual(response['timestamp'], timestamp) # account from hash subject_content = HydrusNetwork.Content( content_type=HC.CONTENT_TYPE_FILES, content_data=hash) subject_account_identifier = HydrusNetwork.AccountIdentifier( content=subject_content) HG.test_controller.SetRead('account', self._account) response = service.Request( HC.GET, 'other_account', {'subject_identifier': subject_account_identifier}) self.assertEqual(repr(response['account']), repr(self._account)) # thumbnail path = ServerFiles.GetExpectedThumbnailPath(self._file_hash) HydrusPaths.MakeSureDirectoryExists(os.path.dirname(path)) with open(path, 'wb') as f: f.write(EXAMPLE_THUMBNAIL) response = service.Request(HC.GET, 'thumbnail', {'hash': self._file_hash}) self.assertEqual(response, EXAMPLE_THUMBNAIL) try: os.remove(path) except: pass
def ParseNetworkBytesToParsedHydrusArgs(network_bytes): if len(network_bytes) == 0: return HydrusSerialisable.SerialisableDictionary() args = HydrusSerialisable.CreateFromNetworkBytes(network_bytes) if not isinstance(args, dict): raise HydrusExceptions.BadRequestException( 'The given parameter did not seem to be a JSON Object!') args = ParsedRequestArguments(args) for param_name in BYTE_PARAMS: if param_name in args: args[param_name] = bytes.fromhex(args[param_name]) for param_name in JSON_BYTE_LIST_PARAMS: if param_name in args: args[param_name] = [ bytes.fromhex(encoded_item) for encoded_item in args[param_name] ] # account_types should be a serialisable list, so it just works if 'account' in args: args[ 'account'] = HydrusNetwork.Account.GenerateAccountFromSerialisableTuple( args['account']) if 'accounts' in args: account_tuples = args['accounts'] args['accounts'] = [ HydrusNetwork.Account.GenerateAccountFromSerialisableTuple( account_tuple) for account_tuple in account_tuples ] if 'service_keys_to_access_keys' in args: args['service_keys_to_access_keys'] = { bytes.fromhex(encoded_service_key): bytes.fromhex(encoded_access_key) for (encoded_service_key, encoded_access_key) in args['service_keys_to_access_keys'] } if 'services' in args: service_tuples = args['services'] args['services'] = [ HydrusNetwork.GenerateServiceFromSerialisableTuple(service_tuple) for service_tuple in service_tuples ] return args
def test_server(self): discard = HG.test_controller.GetWrite( 'session') # just to discard gumph from testserver session_key_1 = HydrusData.GenerateKey() service_key = HydrusData.GenerateKey() permissions = [ HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS, HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES ] account_type = HydrusNetwork.AccountType.GenerateAdminAccountType( HC.SERVER_ADMIN) created = HydrusData.GetNow() - 100000 expires = HydrusData.GetNow() + 300 account_key_1 = HydrusData.GenerateKey() account_key_2 = HydrusData.GenerateKey() access_key_1 = HydrusData.GenerateKey() hashed_access_key_1 = hashlib.sha256(access_key_1).digest() access_key_2 = HydrusData.GenerateKey() hashed_access_key_2 = hashlib.sha256(access_key_2).digest() account = HydrusNetwork.Account(account_key_1, account_type, created, expires) account_2 = HydrusNetwork.Account(account_key_2, account_type, created, expires) # test timeout expires = HydrusData.GetNow() - 10 HG.test_controller.SetRead('sessions', [ (session_key_1, service_key, account, hashed_access_key_1, expires) ]) session_manager = HydrusSessions.HydrusSessionManagerServer() with self.assertRaises(HydrusExceptions.SessionException): session_manager.GetAccount(service_key, session_key_1) # test missing with self.assertRaises(HydrusExceptions.SessionException): session_manager.GetAccount(service_key, HydrusData.GenerateKey()) # test fetching a session already in db, after bootup expires = HydrusData.GetNow() + 300 HG.test_controller.SetRead('sessions', [ (session_key_1, service_key, account, hashed_access_key_1, expires) ]) session_manager = HydrusSessions.HydrusSessionManagerServer() read_account = session_manager.GetAccount(service_key, session_key_1) self.assertIs(read_account, account) read_account = session_manager.GetAccountFromAccessKey( service_key, access_key_1) self.assertIs(read_account, account) # test too busy to add a new session for a new account it doesn't know about HG.server_busy.acquire() with self.assertRaises(HydrusExceptions.ServerBusyException): session_manager.AddSession(service_key, HydrusData.GenerateKey()) session_manager.GetAccountFromAccessKey(service_key, HydrusData.GenerateKey()) # but ok to get for a session that already exists while busy session_manager.GetAccount(service_key, session_key_1) session_manager.GetAccountFromAccessKey(service_key, access_key_1) HG.server_busy.release() # test adding a session HG.test_controller.ClearWrites('session') expires = HydrusData.GetNow() + 300 HG.test_controller.SetRead('account_key_from_access_key', account_key_2) HG.test_controller.SetRead('account', account_2) (session_key_2, expires_2) = session_manager.AddSession(service_key, access_key_2) [(args, kwargs)] = HG.test_controller.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) read_account = session_manager.GetAccountFromAccessKey( service_key, access_key_2) self.assertIs(read_account, account_2) # test adding a new session for an account already in the manager HG.test_controller.SetRead('account_key_from_access_key', account_key_1) HG.test_controller.SetRead('account', account) (session_key_3, expires_3) = session_manager.AddSession(service_key, access_key_1) [(args, kwargs)] = HG.test_controller.GetWrite('session') (written_session_key, written_service_key, written_account_key, written_expires) = args self.assertEqual( (session_key_3, service_key, account_key_1, expires_3), (written_session_key, written_service_key, written_account_key, written_expires)) read_account = session_manager.GetAccount(service_key, session_key_1) self.assertIs(read_account, account) read_account = session_manager.GetAccount(service_key, session_key_3) self.assertIs(read_account, account) read_account = session_manager.GetAccountFromAccessKey( service_key, access_key_1) self.assertIs(read_account, account) # test individual account refresh expires = HydrusData.GetNow() + 300 new_obj_account_1 = HydrusNetwork.Account(account_key_1, account_type, created, expires) HG.test_controller.SetRead('account', new_obj_account_1) session_manager.RefreshAccounts(service_key, [account_key_1]) read_account = session_manager.GetAccount(service_key, session_key_1) self.assertIs(read_account, new_obj_account_1) read_account = session_manager.GetAccount(service_key, session_key_3) self.assertIs(read_account, new_obj_account_1) read_account = session_manager.GetAccountFromAccessKey( service_key, access_key_1) self.assertIs(read_account, new_obj_account_1) # test all account refresh expires = HydrusData.GetNow() + 300 new_obj_account_2 = HydrusNetwork.Account(account_key_2, account_type, created, expires) HG.test_controller.SetRead( 'sessions', [(session_key_1, service_key, new_obj_account_2, hashed_access_key_2, expires), (session_key_2, service_key, new_obj_account_1, hashed_access_key_1, expires), (session_key_3, service_key, new_obj_account_2, hashed_access_key_2, expires)]) session_manager.RefreshAllAccounts() read_account = session_manager.GetAccount(service_key, session_key_1) self.assertIs(read_account, new_obj_account_2) read_account = session_manager.GetAccount(service_key, session_key_2) self.assertIs(read_account, new_obj_account_1) read_account = session_manager.GetAccount(service_key, session_key_3) self.assertIs(read_account, new_obj_account_2) read_account = session_manager.GetAccountFromAccessKey( service_key, access_key_1) self.assertIs(read_account, new_obj_account_1) read_account = session_manager.GetAccountFromAccessKey( service_key, access_key_2) self.assertIs(read_account, new_obj_account_2)
def publish_callable( result ): ( self._account_keys_to_accounts, self._account_keys_to_account_info, account_errors ) = result if len( account_errors ) > 0: account_errors = sorted( account_errors ) QW.QMessageBox.information( self, 'Information', 'Errors were encountered during account fetch:{}{}'.format( os.linesep * 2, os.linesep.join( account_errors ) ) ) if not self._done_first_fetch: # if we launched with CPU-expensive mapping identifiers, let's move to nice account keys for future refreshes self._account_identifiers = [ HydrusNetwork.AccountIdentifier( account_key = account_key ) for account_key in self._account_keys_to_accounts.keys() ] # account_keys_sorted = sorted( list( self._account_keys_to_accounts.keys() ), key = lambda sak: ( self._account_keys_to_accounts[ sak ].GetAccountType().GetTitle(), sak.hex() ) ) my_admin_account = self._service.GetAccount() my_admin_account_key = my_admin_account.GetAccountKey() for account_key in account_keys_sorted: item = QW.QListWidgetItem() item.setFlags( item.flags() | QC.Qt.ItemIsUserCheckable ) account = self._account_keys_to_accounts[ account_key ] text = account.GetSingleLineTitle() if account_key == my_admin_account_key: text = 'THIS IS YOU: {}'.format( text ) item.setText( text ) if not self._done_first_fetch or account_key in selected_account_keys: item.setCheckState( QC.Qt.Checked ) else: item.setCheckState( QC.Qt.Unchecked ) item.setData( QC.Qt.UserRole, account_key ) self._account_list.addItem( item ) # self._status_st.setVisible( False ) self._status_st.setText( '' ) if self._account_list.count() > 0: self._account_list.item( 0 ).setSelected( True ) self._AccountClicked() self._accounts_loaded = True self._done_first_fetch = True self.accountsFetchFinished.emit()