hydrus/include/TestHydrusServer.py

830 lines
28 KiB
Python
Raw Normal View History

2013-11-20 21:12:21 +00:00
import ClientConstants as CC
2015-03-18 21:46:29 +00:00
import ClientData
import ClientFiles
2015-08-05 18:42:35 +00:00
import ClientLocalServer
2015-03-18 21:46:29 +00:00
import ClientMedia
2013-12-04 22:44:16 +00:00
import hashlib
2013-11-20 21:12:21 +00:00
import httplib
import HydrusConstants as HC
import HydrusServer
import HydrusServerResources
2015-06-03 21:05:13 +00:00
import HydrusSerialisable
2013-11-20 21:12:21 +00:00
import itertools
import os
2015-03-18 21:46:29 +00:00
import ServerFiles
2013-11-20 21:12:21 +00:00
import shutil
import stat
import TestConstants
import time
import threading
import unittest
from twisted.internet import reactor
2013-11-27 18:27:11 +00:00
from twisted.internet.endpoints import TCP4ClientEndpoint, connectProtocol
from twisted.internet.defer import deferredGenerator, waitForDeferred
2015-03-25 22:04:19 +00:00
import HydrusData
import HydrusGlobals
2013-11-20 21:12:21 +00:00
class TestServer( unittest.TestCase ):
@classmethod
def setUpClass( self ):
services = []
2015-07-01 22:02:07 +00:00
self._file_service = ClientData.Service( HydrusData.GenerateKey(), HC.FILE_REPOSITORY, 'file repo', {} )
self._tag_service = ClientData.Service( HydrusData.GenerateKey(), HC.TAG_REPOSITORY, 'tag repo', {} )
self._admin_service = ClientData.Service( HydrusData.GenerateKey(), HC.SERVER_ADMIN, 'server admin', {} )
2014-08-27 22:15:22 +00:00
2015-09-16 18:11:00 +00:00
services_manager = HydrusGlobals.test_controller.GetServicesManager()
2014-08-27 22:15:22 +00:00
2014-09-17 21:28:26 +00:00
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
2013-11-20 21:12:21 +00:00
2015-11-04 22:30:28 +00:00
os.mkdir( ServerFiles.GetExpectedUpdateDir( self._file_service.GetServiceKey() ) )
os.mkdir( ServerFiles.GetExpectedUpdateDir( self._tag_service.GetServiceKey() ) )
2013-11-20 21:12:21 +00:00
permissions = [ HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS, HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES ]
2015-07-01 22:02:07 +00:00
account_key = HydrusData.GenerateKey()
2015-03-25 22:04:19 +00:00
account_type = HydrusData.AccountType( 'account', permissions, ( None, None ) )
created = HydrusData.GetNow() - 100000
2014-10-01 22:58:32 +00:00
expires = None
used_bytes = 0
used_requests = 0
2013-11-20 21:12:21 +00:00
2015-03-25 22:04:19 +00:00
self._account = HydrusData.Account( account_key, account_type, created, expires, used_bytes, used_requests )
2013-11-20 21:12:21 +00:00
2015-07-01 22:02:07 +00:00
self._access_key = HydrusData.GenerateKey()
self._file_hash = HydrusData.GenerateKey()
2013-11-20 21:12:21 +00:00
def TWISTEDSetup():
2014-09-17 21:28:26 +00:00
reactor.listenTCP( HC.DEFAULT_SERVER_ADMIN_PORT, HydrusServer.HydrusServiceAdmin( self._admin_service.GetServiceKey(), HC.SERVER_ADMIN, 'hello' ) )
2015-08-05 18:42:35 +00:00
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' ) )
2014-09-17 21:28:26 +00:00
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' ) )
2013-11-20 21:12:21 +00:00
reactor.callFromThread( TWISTEDSetup )
time.sleep( 1 )
2015-11-04 22:30:28 +00:00
@classmethod
def tearDownClass( self ):
shutil.rmtree( ServerFiles.GetExpectedUpdateDir( self._file_service.GetServiceKey() ) )
shutil.rmtree( ServerFiles.GetExpectedUpdateDir( self._tag_service.GetServiceKey() ) )
2013-11-20 21:12:21 +00:00
def _test_basics( self, host, port ):
connection = httplib.HTTPConnection( host, port, timeout = 10 )
#
connection.request( 'GET', '/' )
response = connection.getresponse()
data = response.read()
p1 = data == HydrusServerResources.CLIENT_ROOT_MESSAGE
p2 = data == HydrusServerResources.ROOT_MESSAGE_BEGIN + 'hello' + HydrusServerResources.ROOT_MESSAGE_END
self.assertTrue( p1 or p2 )
#
2015-11-04 22:30:28 +00:00
with open( os.path.join( HC.STATIC_DIR, 'hydrus.ico' ), 'rb' ) as f: favicon = f.read()
2013-11-20 21:12:21 +00:00
connection.request( 'GET', '/favicon.ico' )
response = connection.getresponse()
data = response.read()
self.assertEqual( data, favicon )
def _test_local_file( self, host, port ):
connection = httplib.HTTPConnection( host, port, timeout = 10 )
#
2015-03-18 21:46:29 +00:00
path = ClientFiles.GetExpectedFilePath( self._file_hash, HC.IMAGE_JPEG )
2013-11-20 21:12:21 +00:00
with open( path, 'wb' ) as f: f.write( 'file' )
connection.request( 'GET', '/file?hash=' + self._file_hash.encode( 'hex' ) )
response = connection.getresponse()
data = response.read()
self.assertEqual( data, 'file' )
try: os.remove( path )
except: pass
#
2015-03-18 21:46:29 +00:00
path = ClientFiles.GetExpectedThumbnailPath( self._file_hash )
2013-11-20 21:12:21 +00:00
with open( path, 'wb' ) as f: f.write( 'thumb' )
connection.request( 'GET', '/thumbnail?hash=' + self._file_hash.encode( 'hex' ) )
response = connection.getresponse()
data = response.read()
self.assertEqual( data, 'thumb' )
try: os.remove( path )
except: pass
2014-08-27 22:15:22 +00:00
def _test_file_repo( self, service, host, port ):
2013-11-20 21:12:21 +00:00
2014-08-27 22:15:22 +00:00
info = service.GetInfo()
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
info[ 'access_key' ] = self._access_key
2013-11-20 21:12:21 +00:00
# file
2015-03-18 21:46:29 +00:00
path = ServerFiles.GetExpectedPath( 'file', self._file_hash )
2013-11-20 21:12:21 +00:00
with open( path, 'wb' ) as f: f.write( 'file' )
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'file', { 'hash' : self._file_hash.encode( 'hex' ) } )
2013-11-20 21:12:21 +00:00
self.assertEqual( response, 'file' )
try: os.remove( path )
except: pass
2015-11-04 22:30:28 +00:00
path = os.path.join( HC.STATIC_DIR, 'hydrus.png' )
2013-11-20 21:12:21 +00:00
with open( path, 'rb' ) as f: file = f.read()
2014-02-12 23:09:38 +00:00
service.Request( HC.POST, 'file', { 'file' : file } )
2013-11-20 21:12:21 +00:00
2015-09-16 18:11:00 +00:00
written = HydrusGlobals.test_controller.GetWrite( 'file' )
2013-11-20 21:12:21 +00:00
[ ( args, kwargs ) ] = written
2014-08-27 22:15:22 +00:00
( written_service_key, written_account, written_file_dict ) = args
2013-11-20 21:12:21 +00:00
self.assertEqual( written_file_dict[ 'hash' ], '\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[ '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
2015-03-25 22:04:19 +00:00
( ip, timestamp ) = ( '94.45.87.123', HydrusData.GetNow() - 100000 )
2013-11-20 21:12:21 +00:00
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'ip', ( ip, timestamp ) )
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'ip', { 'hash' : self._file_hash.encode( 'hex' ) } )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'ip' ], ip )
self.assertEqual( response[ 'timestamp' ], timestamp )
# thumbnail
2015-03-18 21:46:29 +00:00
path = ServerFiles.GetExpectedPath( 'thumbnail', self._file_hash )
2013-11-20 21:12:21 +00:00
with open( path, 'wb' ) as f: f.write( 'thumb' )
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'thumbnail', { 'hash' : self._file_hash.encode( 'hex' ) } )
2013-11-20 21:12:21 +00:00
self.assertEqual( response, 'thumb' )
try: os.remove( path )
except: pass
2014-07-16 20:50:18 +00:00
def _test_local_booru( self, host, port ):
2014-09-10 22:37:38 +00:00
#
2014-07-16 20:50:18 +00:00
connection = httplib.HTTPConnection( host, port, timeout = 10 )
#
2015-11-04 22:30:28 +00:00
with open( os.path.join( HC.STATIC_DIR, 'local_booru_style.css' ), 'rb' ) as f:
css = f.read()
2014-07-16 20:50:18 +00:00
connection.request( 'GET', '/style.css' )
response = connection.getresponse()
data = response.read()
self.assertEqual( data, css )
#
2015-07-01 22:02:07 +00:00
share_key = HydrusData.GenerateKey()
hashes = [ HydrusData.GenerateKey() for i in range( 5 ) ]
2014-07-16 20:50:18 +00:00
2015-03-18 21:46:29 +00:00
with open( ClientFiles.GetExpectedFilePath( hashes[0], HC.IMAGE_JPEG ), 'wb' ) as f: f.write( 'file' )
with open( ClientFiles.GetExpectedThumbnailPath( hashes[0], False ), 'wb' ) as f: f.write( 'thumbnail' )
2014-07-16 20:50:18 +00:00
2015-09-16 18:11:00 +00:00
local_booru_manager = HydrusGlobals.test_controller.GetManager( 'local_booru' )
2014-07-16 20:50:18 +00:00
#
self._test_local_booru_requests( connection, share_key, hashes[0], 404 )
#
info = {}
info[ 'name' ] = 'name'
info[ 'text' ] = 'text'
info[ 'timeout' ] = 0
info[ 'hashes' ] = hashes
2014-08-27 22:15:22 +00:00
# hash, inbox, size, mime, timestamp, width, height, duration, num_frames, num_words, tags_manager, locations_manager, local_ratings, remote_ratings
2014-07-16 20:50:18 +00:00
2015-03-18 21:46:29 +00:00
media_results = [ ClientMedia.MediaResult( ( hash, True, 500, HC.IMAGE_JPEG, 0, 640, 480, None, None, None, None, None, None, None ) ) for hash in hashes ]
2014-07-16 20:50:18 +00:00
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'local_booru_share_keys', [ share_key ] )
HydrusGlobals.test_controller.SetRead( 'local_booru_share', info )
HydrusGlobals.test_controller.SetRead( 'media_results', media_results )
2014-07-16 20:50:18 +00:00
local_booru_manager.RefreshShares()
#
self._test_local_booru_requests( connection, share_key, hashes[0], 403 )
#
info[ 'timeout' ] = None
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'local_booru_share', info )
2014-07-16 20:50:18 +00:00
local_booru_manager.RefreshShares()
#
self._test_local_booru_requests( connection, share_key, hashes[0], 200 )
#
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'local_booru_share_keys', [] )
2014-07-16 20:50:18 +00:00
local_booru_manager.RefreshShares()
#
self._test_local_booru_requests( connection, share_key, hashes[0], 404 )
def _test_local_booru_requests( self, connection, share_key, hash, expected_result ):
requests = []
requests.append( '/gallery?share_key=' + share_key.encode( 'hex' ) )
requests.append( '/page?share_key=' + share_key.encode( 'hex' ) + '&hash=' + hash.encode( 'hex' ) )
requests.append( '/file?share_key=' + share_key.encode( 'hex' ) + '&hash=' + hash.encode( 'hex' ) )
requests.append( '/thumbnail?share_key=' + share_key.encode( 'hex' ) + '&hash=' + hash.encode( 'hex' ) )
for request in requests:
connection.request( 'GET', request )
response = connection.getresponse()
data = response.read()
self.assertEqual( response.status, expected_result )
2014-09-10 22:37:38 +00:00
def _test_repo( self, service, host, port ):
2014-02-12 23:09:38 +00:00
2014-09-17 21:28:26 +00:00
service_key = service.GetServiceKey()
2013-11-20 21:12:21 +00:00
# news
news = 'this is the news'
2014-02-12 23:09:38 +00:00
service.Request( HC.POST, 'news', { 'news' : news } )
2013-11-20 21:12:21 +00:00
2015-09-16 18:11:00 +00:00
written = HydrusGlobals.test_controller.GetWrite( 'news' )
2013-11-20 21:12:21 +00:00
[ ( args, kwargs ) ] = written
2014-08-27 22:15:22 +00:00
( written_service_key, written_news ) = args
2013-11-20 21:12:21 +00:00
self.assertEqual( news, written_news )
# num_petitions
num_petitions = 23
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'num_petitions', num_petitions )
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'num_petitions' )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'num_petitions' ], num_petitions )
# petition
2015-10-14 21:02:25 +00:00
action = HC.CONTENT_UPDATE_PETITION
account_identifier = HydrusData.AccountIdentifier( account_key = HydrusData.GenerateKey() )
reason = 'it sucks'
contents = [ HydrusData.Content( HC.CONTENT_TYPE_FILES, [ HydrusData.GenerateKey() for i in range( 10 ) ] ) ]
petition = HydrusData.ServerToClientPetition( action = action, petitioner_account_identifier = account_identifier, reason = reason, contents = contents )
2013-11-20 21:12:21 +00:00
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'petition', petition )
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'petition' )
2013-11-20 21:12:21 +00:00
2015-10-14 21:02:25 +00:00
self.assertEqual( type( response ), HydrusData.ServerToClientPetition )
2013-11-20 21:12:21 +00:00
# update
2014-03-26 21:23:10 +00:00
begin = 100
2015-06-03 21:05:13 +00:00
subindex_count = 5
2013-11-20 21:12:21 +00:00
2015-06-03 21:05:13 +00:00
update = HydrusData.ServerToClientServiceUpdatePackage()
update.SetBeginEnd( begin, begin + HC.UPDATE_DURATION - 1 )
update.SetSubindexCount( subindex_count )
2013-11-20 21:12:21 +00:00
2015-06-03 21:05:13 +00:00
path = ServerFiles.GetExpectedServiceUpdatePackagePath( service_key, begin )
2013-11-20 21:12:21 +00:00
2015-10-14 21:02:25 +00:00
with open( path, 'wb' ) as f: f.write( update.DumpToNetworkString() )
2013-11-20 21:12:21 +00:00
2015-06-03 21:05:13 +00:00
response = service.Request( HC.GET, 'service_update_package', { 'begin' : begin } )
self.assertEqual( response.GetBegin(), update.GetBegin() )
2013-11-20 21:12:21 +00:00
try: os.remove( path )
except: pass
2015-06-03 21:05:13 +00:00
subindex = 2
num_hashes = 12
tag = 'series:blah'
2015-07-01 22:02:07 +00:00
hash_ids_to_hashes = { i : HydrusData.GenerateKey() for i in range( 12 ) }
2015-06-03 21:05:13 +00:00
rows = [ ( tag, [ i for i in range( num_hashes ) ] ) ]
update = HydrusData.ServerToClientContentUpdatePackage()
2015-10-14 21:02:25 +00:00
update.AddContentData( HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_ADD, rows, hash_ids_to_hashes )
2015-06-03 21:05:13 +00:00
path = ServerFiles.GetExpectedContentUpdatePackagePath( service_key, begin, subindex )
2015-10-14 21:02:25 +00:00
with open( path, 'wb' ) as f: f.write( update.DumpToNetworkString() )
2015-06-03 21:05:13 +00:00
response = service.Request( HC.GET, 'content_update_package', { 'begin' : begin, 'subindex' : subindex } )
self.assertEqual( response.GetNumContentUpdates(), update.GetNumContentUpdates() )
try: os.remove( path )
except: pass
update = HydrusData.ClientToServerContentUpdatePackage( {}, hash_ids_to_hashes )
service.Request( HC.POST, 'content_update_package', { 'update' : update } )
2013-11-20 21:12:21 +00:00
2015-09-16 18:11:00 +00:00
written = HydrusGlobals.test_controller.GetWrite( 'update' )
2013-11-20 21:12:21 +00:00
[ ( args, kwargs ) ] = written
2014-08-27 22:15:22 +00:00
( written_service_key, written_account, written_update ) = args
2013-11-20 21:12:21 +00:00
2015-06-03 21:05:13 +00:00
self.assertEqual( update.GetHashes(), written_update.GetHashes() )
2013-11-20 21:12:21 +00:00
2014-08-27 22:15:22 +00:00
def _test_restricted( self, service, host, port ):
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
# access_key
2013-11-20 21:12:21 +00:00
2015-07-01 22:02:07 +00:00
registration_key = HydrusData.GenerateKey()
2013-11-20 21:12:21 +00:00
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'access_key', self._access_key )
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
request_headers = {}
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
request_headers[ 'Hydrus-Key' ] = registration_key.encode( 'hex' )
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'access_key', request_headers = request_headers )
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
self.assertEqual( response[ 'access_key' ], self._access_key )
2013-12-11 22:09:25 +00:00
2014-08-27 22:15:22 +00:00
info = service.GetInfo()
2013-12-11 22:09:25 +00:00
2014-08-27 22:15:22 +00:00
info[ 'access_key' ] = self._access_key
2013-11-20 21:12:21 +00:00
# set up session
last_error = 0
account = self._account
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'service', service )
2013-11-20 21:12:21 +00:00
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'account_key_from_access_key', HydrusData.GenerateKey() )
HydrusGlobals.test_controller.SetRead( 'account', self._account )
2013-11-20 21:12:21 +00:00
# account
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'account' )
2013-11-20 21:12:21 +00:00
self.assertEqual( repr( response[ 'account' ] ), repr( self._account ) )
# account_info
account_info = { 'message' : 'hello' }
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'account_info', account_info )
HydrusGlobals.test_controller.SetRead( 'account_key_from_identifier', HydrusData.GenerateKey() )
2013-11-20 21:12:21 +00:00
2015-07-01 22:02:07 +00:00
response = service.Request( HC.GET, 'account_info', { 'subject_account_key' : HydrusData.GenerateKey().encode( 'hex' ) } )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'account_info' ], account_info )
2015-07-01 22:02:07 +00:00
response = service.Request( HC.GET, 'account_info', { 'subject_hash' : HydrusData.GenerateKey().encode( 'hex' ) } )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'account_info' ], account_info )
2015-07-01 22:02:07 +00:00
response = service.Request( HC.GET, 'account_info', { 'subject_hash' : HydrusData.GenerateKey().encode( 'hex' ), 'subject_tag' : 'hello'.encode( 'hex' ) } )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'account_info' ], account_info )
# account_types
account_types = { 'message' : 'hello' }
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'account_types', account_types )
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'account_types' )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'account_types' ], account_types )
edit_log = 'blah'
2014-02-12 23:09:38 +00:00
service.Request( HC.POST, 'account_types', { 'edit_log' : edit_log } )
2013-11-20 21:12:21 +00:00
2015-09-16 18:11:00 +00:00
written = HydrusGlobals.test_controller.GetWrite( 'account_types' )
2013-11-20 21:12:21 +00:00
[ ( args, kwargs ) ] = written
2014-08-27 22:15:22 +00:00
( written_service_key, written_edit_log ) = args
2013-11-20 21:12:21 +00:00
self.assertEqual( edit_log, written_edit_log )
# registration_keys
2015-07-01 22:02:07 +00:00
registration_key = HydrusData.GenerateKey()
2013-11-20 21:12:21 +00:00
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'registration_keys', [ registration_key ] )
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'registration_keys', { 'num' : 1, 'title' : 'blah' } )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'registration_keys' ], [ registration_key ] )
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'registration_keys', { 'num' : 1, 'title' : 'blah', 'lifetime' : 100 } )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'registration_keys' ], [ registration_key ] )
# stats
stats = { 'message' : 'hello' }
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'stats', stats )
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'stats' )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'stats' ], stats )
2014-08-27 22:15:22 +00:00
def _test_server_admin( self, service, host, port ):
2013-11-20 21:12:21 +00:00
2014-08-27 22:15:22 +00:00
info = service.GetInfo()
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
info[ 'host' ] = host
info[ 'port' ] = port
2013-11-20 21:12:21 +00:00
# init
2015-07-01 22:02:07 +00:00
access_key = HydrusData.GenerateKey()
2013-11-20 21:12:21 +00:00
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'init', access_key )
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'init' )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'access_key' ], access_key )
2014-02-12 23:09:38 +00:00
#
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
info[ 'access_key' ] = self._access_key
2013-11-20 21:12:21 +00:00
# backup
2014-02-12 23:09:38 +00:00
response = service.Request( HC.POST, 'backup' )
2013-11-20 21:12:21 +00:00
# services
2014-09-17 21:28:26 +00:00
services_info = { 'message' : 'hello' }
2013-11-20 21:12:21 +00:00
2015-09-16 18:11:00 +00:00
HydrusGlobals.test_controller.SetRead( 'services_info', services_info )
2013-11-20 21:12:21 +00:00
2014-09-17 21:28:26 +00:00
response = service.Request( HC.GET, 'services_info' )
2013-11-20 21:12:21 +00:00
2014-09-17 21:28:26 +00:00
self.assertEqual( response[ 'services_info' ], services_info )
2013-11-20 21:12:21 +00:00
edit_log = 'blah'
2014-02-12 23:09:38 +00:00
registration_keys = service.Request( HC.POST, 'services', { 'edit_log' : edit_log } )
2013-11-20 21:12:21 +00:00
2015-09-16 18:11:00 +00:00
written = HydrusGlobals.test_controller.GetWrite( 'services' )
2013-11-20 21:12:21 +00:00
[ ( args, kwargs ) ] = written
2014-08-27 22:15:22 +00:00
( written_service_key, written_edit_log ) = args
2013-11-20 21:12:21 +00:00
self.assertEqual( edit_log, written_edit_log )
2014-09-10 22:37:38 +00:00
def _test_tag_repo( self, service, host, port ):
2013-11-20 21:12:21 +00:00
pass
def test_local_service( self ):
host = '127.0.0.1'
port = HC.DEFAULT_LOCAL_FILE_PORT
self._test_basics( host, port )
self._test_local_file( host, port )
def test_repository_file( self ):
host = '127.0.0.1'
port = HC.DEFAULT_SERVICE_PORT
2014-08-27 22:15:22 +00:00
info = self._file_service.GetInfo()
info[ 'host' ] = host
info[ 'port' ] = port
2013-11-20 21:12:21 +00:00
self._test_basics( host, port )
2014-08-27 22:15:22 +00:00
self._test_restricted( self._file_service, host, port )
self._test_repo( self._file_service, host, port )
self._test_file_repo( self._file_service, host, port )
2013-11-20 21:12:21 +00:00
def test_repository_tag( self ):
host = '127.0.0.1'
port = HC.DEFAULT_SERVICE_PORT + 1
2014-08-27 22:15:22 +00:00
info = self._tag_service.GetInfo()
info[ 'host' ] = host
info[ 'port' ] = port
2013-11-20 21:12:21 +00:00
self._test_basics( host, port )
2014-08-27 22:15:22 +00:00
self._test_restricted( self._tag_service, host, port )
self._test_repo( self._tag_service, host, port )
self._test_tag_repo( self._tag_service, host, port )
2013-11-20 21:12:21 +00:00
def test_server_admin( self ):
host = '127.0.0.1'
port = HC.DEFAULT_SERVER_ADMIN_PORT
2014-08-27 22:15:22 +00:00
info = self._admin_service.GetInfo()
info[ 'host' ] = host
info[ 'port' ] = port
2013-11-20 21:12:21 +00:00
self._test_basics( host, port )
2014-08-27 22:15:22 +00:00
self._test_restricted( self._admin_service, host, port )
self._test_server_admin( self._admin_service, host, port )
2013-11-20 21:12:21 +00:00
2014-07-16 20:50:18 +00:00
def test_local_booru( self ):
host = '127.0.0.1'
port = HC.DEFAULT_LOCAL_BOORU_PORT
self._test_basics( host, port )
self._test_local_booru( host, port )
2014-12-25 00:07:20 +00:00
'''
2013-11-20 21:12:21 +00:00
class TestAMP( unittest.TestCase ):
@classmethod
def setUpClass( self ):
2015-07-01 22:02:07 +00:00
self._alice = HydrusData.GenerateKey()
self._bob = HydrusData.GenerateKey()
2013-11-20 21:12:21 +00:00
2013-11-27 18:27:11 +00:00
self._server_port = HC.DEFAULT_SERVICE_PORT + 10
2013-11-20 21:12:21 +00:00
2015-07-01 22:02:07 +00:00
self._service_key = HydrusData.GenerateKey()
2013-11-27 18:27:11 +00:00
def TWISTEDSetup():
2014-08-27 22:15:22 +00:00
self._factory = HydrusServer.MessagingServiceFactory( self._service_key )
2013-11-27 18:27:11 +00:00
reactor.listenTCP( self._server_port, self._factory )
reactor.callFromThread( TWISTEDSetup )
time.sleep( 1 )
2013-11-20 21:12:21 +00:00
2013-11-27 18:27:11 +00:00
def _get_deferred_result( self, deferred ):
def err( failure ):
failure.trap( Exception )
return failure.type( failure.value )
2013-11-20 21:12:21 +00:00
2013-11-27 18:27:11 +00:00
deferred.addErrback( err )
2013-11-20 21:12:21 +00:00
2014-11-12 23:33:13 +00:00
before = time.time()
while not deferred.called:
time.sleep( 0.1 )
if time.time() - before > 10: raise Exception( 'Trying to get deferred timed out!' )
2013-11-27 18:27:11 +00:00
result = deferred.result
if issubclass( type( result ), Exception ): raise result
return result
2013-11-20 21:12:21 +00:00
2013-11-27 18:27:11 +00:00
def _get_client_protocol( self ):
2013-11-20 21:12:21 +00:00
2013-11-27 18:27:11 +00:00
point = TCP4ClientEndpoint( reactor, '127.0.0.1', self._server_port )
2013-11-20 21:12:21 +00:00
2013-12-04 22:44:16 +00:00
deferred = connectProtocol( point, HydrusServerAMP.MessagingClientProtocol() )
2013-11-27 18:27:11 +00:00
protocol = self._get_deferred_result( deferred )
return protocol
2013-11-20 21:12:21 +00:00
2013-12-04 22:44:16 +00:00
def _make_persistent_connection( self, protocol, access_key, name ):
2013-11-20 21:12:21 +00:00
2013-12-04 22:44:16 +00:00
identifier = hashlib.sha256( access_key ).digest()
2013-11-20 21:12:21 +00:00
2013-11-27 18:27:11 +00:00
HC.app.SetRead( 'im_identifier', identifier )
2013-12-04 22:44:16 +00:00
permissions = [ HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS, HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES ]
2015-07-01 22:02:07 +00:00
account_key = HydrusData.GenerateKey()
2013-12-04 22:44:16 +00:00
account_type = HC.AccountType( 'account', permissions, ( None, None ) )
created = HC.GetNow() - 100000
2014-10-01 22:58:32 +00:00
expires = None
used_bytes = 0
used_requests = 0
2013-12-04 22:44:16 +00:00
2014-10-01 22:58:32 +00:00
account = HC.Account( account_key, account_type, created, expires, used_bytes, used_requests )
2013-12-04 22:44:16 +00:00
2015-07-01 22:02:07 +00:00
HC.app.SetRead( 'account_key_from_access_key', HydrusData.GenerateKey() )
2013-12-04 22:44:16 +00:00
HC.app.SetRead( 'account', account )
2013-11-27 18:27:11 +00:00
deferred = protocol.callRemote( HydrusServerAMP.IMSessionKey, access_key = access_key, name = name )
result = self._get_deferred_result( deferred )
session_key = result[ 'session_key' ]
2013-12-04 22:44:16 +00:00
deferred = protocol.callRemote( HydrusServerAMP.IMLoginPersistent, network_version = HC.NETWORK_VERSION, session_key = session_key )
2013-11-27 18:27:11 +00:00
result = self._get_deferred_result( deferred )
self.assertEqual( result, {} )
def _make_temporary_connection( self, protocol, identifier, name ):
2013-12-04 22:44:16 +00:00
deferred = protocol.callRemote( HydrusServerAMP.IMLoginTemporary, network_version = HC.NETWORK_VERSION, identifier = identifier, name = name )
2013-11-27 18:27:11 +00:00
result = self._get_deferred_result( deferred )
self.assertEqual( result, {} )
2013-11-20 21:12:21 +00:00
def test_connections( self ):
2013-11-27 18:27:11 +00:00
persistent_protocol = self._get_client_protocol()
2015-07-01 22:02:07 +00:00
persistent_access_key = HydrusData.GenerateKey()
2013-12-11 22:09:25 +00:00
persistent_identifier = hashlib.sha256( persistent_access_key ).digest()
2013-11-27 18:27:11 +00:00
persistent_name = 'persistent'
2013-12-04 22:44:16 +00:00
self._make_persistent_connection( persistent_protocol, persistent_access_key, persistent_name )
2013-11-27 18:27:11 +00:00
2013-12-11 22:09:25 +00:00
self.assertIn( persistent_identifier, self._factory._persistent_connections )
self.assertIn( persistent_name, self._factory._persistent_connections[ persistent_identifier ] )
temp_protocol_1 = self._get_client_protocol()
temp_protocol_2 = self._get_client_protocol()
temp_name_1 = 'temp_1'
2015-07-01 22:02:07 +00:00
temp_identifier = HydrusData.GenerateKey()
2013-12-11 22:09:25 +00:00
temp_name_2 = 'temp_2'
2013-11-27 18:27:11 +00:00
2013-12-11 22:09:25 +00:00
self._make_temporary_connection( temp_protocol_1, temp_identifier, temp_name_1 )
self._make_temporary_connection( temp_protocol_2, temp_identifier, temp_name_2 )
2013-11-20 21:12:21 +00:00
2013-12-11 22:09:25 +00:00
self.assertIn( temp_identifier, self._factory._temporary_connections )
self.assertIn( temp_name_1, self._factory._temporary_connections[ temp_identifier ] )
self.assertIn( temp_name_2, self._factory._temporary_connections[ temp_identifier ] )
2013-11-20 21:12:21 +00:00
def test_status( self ):
# some of this is UDP, so get that working!
# add two bobs
# ask for status of the bobs
# test that we get both, online
# now disconnect a bob
# ask for bob status
# test that we only have one bob
# now disconnect other bob
# repeat for nothing
pass
def test_message( self ):
2013-12-11 22:09:25 +00:00
persistent_protocol = self._get_client_protocol()
2015-07-01 22:02:07 +00:00
persistent_access_key = HydrusData.GenerateKey()
2013-12-11 22:09:25 +00:00
persistent_identifier = hashlib.sha256( persistent_access_key ).digest()
persistent_name = 'persistent'
self._make_persistent_connection( persistent_protocol, persistent_access_key, persistent_name )
2013-11-27 18:27:11 +00:00
2013-12-11 22:09:25 +00:00
temp_protocol = self._get_client_protocol()
2015-07-01 22:02:07 +00:00
temp_identifier = HydrusData.GenerateKey()
2013-12-11 22:09:25 +00:00
temp_name = 'temp'
2013-11-27 18:27:11 +00:00
2013-12-11 22:09:25 +00:00
self._make_temporary_connection( temp_protocol, temp_identifier, temp_name )
#
HC.pubsub.ClearPubSubs()
message = 'hello temp'
deferred = persistent_protocol.callRemote( HydrusServerAMP.IMMessageServer, identifier_to = temp_identifier, name_to = temp_name, message = message )
result = self._get_deferred_result( deferred )
self.assertEqual( result, {} )
result = HC.pubsub.GetPubSubs( 'im_message_received' )
[ ( args, kwargs ) ] = result
self.assertEqual( args, ( persistent_identifier, persistent_name, temp_identifier, temp_name, message ) )
#
HC.pubsub.ClearPubSubs()
message = 'hello persistent'
deferred = temp_protocol.callRemote( HydrusServerAMP.IMMessageServer, identifier_to = persistent_identifier, name_to = persistent_name, message = message )
result = self._get_deferred_result( deferred )
self.assertEqual( result, {} )
result = HC.pubsub.GetPubSubs( 'im_message_received' )
[ ( args, kwargs ) ] = result
self.assertEqual( args, ( temp_identifier, temp_name, persistent_identifier, persistent_name, message ) )
2014-12-25 00:07:20 +00:00
'''