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_account_creation(self): result = self._read('account_types', self._tag_service_key) (service_admin_at, ) = result self.assertEqual(service_admin_at.GetTitle(), 'service admin') self.assertEqual(service_admin_at.GetPermissions(), [ HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS, HC.MANAGE_USERS, HC.GENERAL_ADMIN ]) self.assertEqual(service_admin_at.GetMaxBytes(), None) self.assertEqual(service_admin_at.GetMaxRequests(), None) # user_at = HC.AccountType('user', [HC.GET_DATA, HC.POST_DATA], (50000, 500)) edit_log = [(HC.ADD, user_at)] self._write('account_types', self._tag_service_key, edit_log) result = self._read('account_types', self._tag_service_key) (at_1, at_2) = result d = {at_1.GetTitle(): at_1, at_2.GetTitle(): at_2} at = d['user'] self.assertEqual(at.GetPermissions(), [HC.GET_DATA, HC.POST_DATA]) self.assertEqual(at.GetMaxBytes(), 50000) self.assertEqual(at.GetMaxRequests(), 500) # user_at_diff = HC.AccountType('user different', [HC.GET_DATA], (40000, None)) edit_log = [(HC.EDIT, ('user', user_at_diff))] self._write('account_types', self._tag_service_key, edit_log) result = self._read('account_types', self._tag_service_key) (at_1, at_2) = result d = {at_1.GetTitle(): at_1, at_2.GetTitle(): at_2} at = d['user different'] self.assertEqual(at.GetPermissions(), [HC.GET_DATA]) self.assertEqual(at.GetMaxBytes(), 40000) self.assertEqual(at.GetMaxRequests(), None) # r_keys = self._read('registration_keys', self._tag_service_key, 5, 'user different', 86400 * 365) self.assertEqual(len(r_keys), 5) for r_key in r_keys: self.assertEqual(len(r_key), 32) r_key = r_keys[0] access_key = self._read('access_key', r_key) access_key_2 = self._read('access_key', r_key) self.assertNotEqual(access_key, access_key_2) self.assertRaises(HydrusExceptions.ForbiddenException, self._read, 'account_key_from_access_key', self._tag_service_key, access_key) account_key = self._read('account_key_from_access_key', self._tag_service_key, access_key_2) self.assertRaises(HydrusExceptions.ForbiddenException, self._read, 'access_key', r_key)
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)