def setUpClass( self ): services = [] self._file_service = ClientData.GenerateService( HydrusData.GenerateKey(), HC.FILE_REPOSITORY, 'file repo', {} ) self._tag_service = ClientData.GenerateService( HydrusData.GenerateKey(), HC.TAG_REPOSITORY, 'tag repo', {} ) self._admin_service = ClientData.GenerateService( HydrusData.GenerateKey(), HC.SERVER_ADMIN, 'server admin', {} ) services_manager = HydrusGlobals.test_controller.GetServicesManager() 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 HydrusPaths.MakeSureDirectoryExists( ServerFiles.GetExpectedUpdateDir( self._file_service.GetServiceKey() ) ) HydrusPaths.MakeSureDirectoryExists( ServerFiles.GetExpectedUpdateDir( self._tag_service.GetServiceKey() ) ) permissions = [ HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS, HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES ] account_key = HydrusData.GenerateKey() account_type = HydrusData.AccountType( 'account', permissions, ( None, None ) ) created = HydrusData.GetNow() - 100000 expires = None used_bytes = 0 used_requests = 0 self._account = HydrusData.Account( account_key, account_type, created, expires, used_bytes, used_requests ) self._access_key = HydrusData.GenerateKey() self._file_hash = HydrusData.GenerateKey() def TWISTEDSetup(): reactor.listenTCP( HC.DEFAULT_SERVER_ADMIN_PORT, ServerServer.HydrusServiceAdmin( self._admin_service.GetServiceKey(), HC.SERVER_ADMIN, 'hello' ) ) reactor.listenTCP( HC.DEFAULT_LOCAL_FILE_PORT, ClientLocalServer.HydrusServiceLocal( CC.LOCAL_FILE_SERVICE_KEY, HC.LOCAL_FILE, 'hello' ) ) reactor.listenTCP( HC.DEFAULT_LOCAL_BOORU_PORT, ClientLocalServer.HydrusServiceBooru( CC.LOCAL_BOORU_SERVICE_KEY, HC.LOCAL_BOORU, 'hello' ) ) reactor.listenTCP( HC.DEFAULT_SERVICE_PORT, ServerServer.HydrusServiceRepositoryFile( self._file_service.GetServiceKey(), HC.FILE_REPOSITORY, 'hello' ) ) reactor.listenTCP( HC.DEFAULT_SERVICE_PORT + 1, ServerServer.HydrusServiceRepositoryTag( self._tag_service.GetServiceKey(), HC.TAG_REPOSITORY, 'hello' ) ) reactor.callFromThread( TWISTEDSetup ) time.sleep( 1 )
def setUpClass( cls ): services = [] 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._clientside_tag_service.SetCredentials( HydrusNetwork.Credentials( '127.0.0.1', HC.DEFAULT_SERVICE_PORT ) ) cls._clientside_admin_service.SetCredentials( HydrusNetwork.Credentials( '127.0.0.1', HC.DEFAULT_SERVER_ADMIN_PORT ) ) cls._local_booru = ClientServices.GenerateService( HydrusData.GenerateKey(), 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 permissions = [ HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS, HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES ] account_key = HydrusData.GenerateKey() account_type = HydrusData.AccountType( 'account', permissions, ( None, None ) ) created = HydrusData.GetNow() - 100000 expires = None used_bytes = 0 used_requests = 0 cls._account = HydrusData.Account( account_key, account_type, created, expires, used_bytes, used_requests ) cls._access_key = HydrusData.GenerateKey() cls._file_hash = HydrusData.GenerateKey() def TWISTEDSetup(): cls._ssl_cert_path = os.path.join( TestConstants.DB_DIR, 'server.crt' ) cls._ssl_key_path = os.path.join( TestConstants.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( HC.DEFAULT_LOCAL_BOORU_PORT, ClientLocalServer.HydrusServiceBooru( cls._local_booru ) ) reactor.callFromThread( TWISTEDSetup ) time.sleep( 1 )
def test_server(self): discard = HydrusGlobals.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 ] access_key = HydrusData.GenerateKey() account_key = HydrusData.GenerateKey() account_type = HydrusData.AccountType('account', permissions, (None, None)) created = HydrusData.GetNow() - 100000 expires = HydrusData.GetNow() + 300 used_bytes = 0 used_requests = 0 account = HydrusData.Account(account_key, account_type, created, expires, used_bytes, used_requests) expires = HydrusData.GetNow() - 10 HydrusGlobals.test_controller.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 = HydrusData.GetNow() + 300 HydrusGlobals.test_controller.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 = HydrusData.GetNow() + 300 account_key_2 = HydrusData.GenerateKey() account_2 = HydrusData.Account(account_key_2, account_type, created, expires, used_bytes, used_requests) HydrusGlobals.test_controller.SetRead('account_key_from_access_key', account_key_2) HydrusGlobals.test_controller.SetRead('account', account_2) (session_key_2, expires_2) = session_manager.AddSession(service_key, access_key) [(args, kwargs)] = HydrusGlobals.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) # test adding a new session for an account already in the manager HydrusGlobals.test_controller.SetRead('account_key_from_access_key', account_key) HydrusGlobals.test_controller.SetRead('account', account) (session_key_3, expires_3) = session_manager.AddSession(service_key, access_key) [(args, kwargs)] = HydrusGlobals.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, 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 = HydrusData.GetNow() + 300 updated_account = HydrusData.Account(account_key, account_type, created, expires, 1, 1) HydrusGlobals.test_controller.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 = HydrusData.GetNow() + 300 updated_account_2 = HydrusData.Account(account_key, account_type, created, expires, 2, 2) HydrusGlobals.test_controller.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)