Пример #1
0
def makeService(settings):
    service_collection = service.MultiService()
    cardstories_service = CardstoriesService(settings)
    plugins = CardstoriesPlugins(settings)
    plugins.load(cardstories_service)
    cardstories_service.setServiceParent(service_collection)

    site = CardstoriesSite(CardstoriesTree(cardstories_service), settings,
                           plugins.plugins)

    if settings.get('manhole', None):
        internet.TCPServer(
            2222,
            getManholeFactory(locals(), user="******"),
            interface='127.0.0.1').setServiceParent(service_collection)

    internet.TCPServer(settings['port'],
                       site,
                       interface=settings.get(
                           'interface',
                           '127.0.0.1')).setServiceParent(service_collection)

    # if settings.has_key('ssl-port') and settings['ssl-port']:
    #     internet.SSLServer(settings['ssl-port'], site, SSLContextFactory(settings)
    #                        ).setServiceParent(service_collection)

    return service_collection
Пример #2
0
    def test04_handle_internal(self):
        def mock_handle(result, args, internal_request=False):
            return internal_request
        self.service.handle = mock_handle

        self.service.settings['internal-secret'] = 'secret thing'

        resource = CardstoriesInternalResource(self.service)
        self.site = CardstoriesSite(resource, {}, [])

        # Works with good secret param.
        request = server.Request(self.Channel(self.site), True)
        request.method = 'GET'
        request.args = {'secret': ['secret thing']}
        self.assertEquals(True, resource.handle(True, request))

        request = server.Request(self.Channel(self.site), True)
        request.method = 'POST'
        request.args = {'secret': ['secret thing']}
        self.assertEquals(True, resource.handle(True, request))

        # Fails when secret param is bad or doesn't exists.
        request = server.Request(self.Channel(self.site), True)
        request.method = 'GET'
        request.args = {'secret': ['hahahahaha']}
        result = yield resource.handle(True, request)
        self.assertEquals({'error': {'code': 'UNAUTHORIZED'}}, result)

        request = server.Request(self.Channel(self.site), True)
        request.method = 'POST'
        request.args = None
        result = yield resource.handle(True, request)
        self.assertEquals({'error': {'code': 'UNAUTHORIZED'}}, result)
Пример #3
0
    def test04_handle(self):
        resource = CardstoriesResource(self.service)
        self.site = CardstoriesSite(resource, {}, [])
        request = server.Request(self.Channel(self.site), True)

        request.method = 'GET'
        self.assertEquals('handle', resource.handle(True, request))

        request.method = 'POST'
        self.assertEquals('handle', resource.handle(True, request))
Пример #4
0
 def test01_wrap_http(self):
     resource = CardstoriesResource(self.service)
     self.site = CardstoriesSite(resource, {}, [])
     request = server.Request(self.Channel(self.site), True)
     request.site = self.site
     request.method = 'GET'
     d = resource.wrap_http(request)
     def finish(result):
         self.assertSubstring('\r\n\r\n"handle"', request.transport.getvalue())
     d.addCallback(finish)
     return d
Пример #5
0
 def test00_init(self):
     plugins = CardstoriesPlugins({ 'plugins-dir': '..',
                                    'plugins': 'plugin_one plugin_two'})
     class Service:
         def __init__(self):
             self.pollable_plugins = []
     service = Service()
     plugins.load(service)
     site = CardstoriesSite(resource.Resource(), { 'plugins-pre-process': 'plugin_one plugin_two',
                                                   'plugins-post-process': 'plugin_two plugin_one' }, plugins.plugins)
     self.assertEqual(site.preprocess[0], site.postprocess[1])
     self.assertEqual(site.preprocess[1], site.postprocess[0])
Пример #6
0
 def test01_wrap_http_disconnected(self):
     resource = CardstoriesResource(self.service)
     self.site = CardstoriesSite(resource, {}, [])
     request = server.Request(self.Channel(self.site), True)
     request.site = self.site
     request.method = 'GET'
     request._disconnected = True
     d = resource.wrap_http(request)
     def finish(result):
         self.assertEquals('', request.transport.getvalue())
     d.addCallback(finish)
     return d
Пример #7
0
 def test00_render(self):
     self.site = CardstoriesSite(CardstoriesTree(self.service), {}, [])
     r = server.Request(self.Channel(self.site), True)
     r.site = r.channel.site
     input = ''
     r.gotLength(len(input))
     r.handleContentChunk(input)
     r.queued = 0
     d = r.notifyFinish()
     def finish(result):
         self.assertSubstring('\r\n\r\n"handle"', r.transport.getvalue())
     d.addCallback(finish)
     r.requestReceived('GET', '/resource', '')
     return d
Пример #8
0
    def test04_handle_non_internal(self):
        def mock_handle(result, args, internal_request=False):
            return internal_request
        self.service.handle = mock_handle

        resource = CardstoriesResource(self.service)
        self.site = CardstoriesSite(resource, {}, [])
        request = server.Request(self.Channel(self.site), True)

        request.method = 'GET'
        self.assertEquals(False, resource.handle(True, request))

        request.method = 'POST'
        self.assertEquals(False, resource.handle(True, request))
Пример #9
0
 def test03_wrap_http_fail(self):
     resource = CardstoriesResource(self.service)
     fail_string = 'FAIL STRING'
     def fail(result, request):
         raise Exception(fail_string)
     resource.handle = fail
     self.site = CardstoriesSite(resource, {}, [])
     request = server.Request(self.Channel(self.site), True)
     request.site = self.site
     d = resource.wrap_http(request)
     def finish(result):
         self.assertSubstring(fail_string, request.transport.getvalue())
     d.addCallback(finish)
     return d
Пример #10
0
 def test02_wrap_http_plugin_postprocess_fail(self):
     plugins = CardstoriesPlugins({ 'plugins-dir': '..',
                                    'plugins': 'plugin_fail'})
     plugins.load(True)
     resource = CardstoriesResource(self.service)
     self.site = CardstoriesSite(resource, { 'plugins-post-process': 'plugin_fail' }, plugins.plugins)
     request = server.Request(self.Channel(self.site), True)
     request.site = self.site
     request.method = 'GET'
     d = resource.wrap_http(request)
     def finish(result):
         self.assertSubstring('Internal Server Error', request.transport.getvalue())
         self.assertSubstring('POSTPROCESS', request.transport.getvalue())
     d.addCallback(finish)
     return d
Пример #11
0
 def test02_transparent_transform(self):
     self.site = CardstoriesSite(CardstoriesTree(self.service),
                                 { 'plugins-pre-process': 'example',
                                   'plugins-post-process': 'example' },
                                 [ example.Plugin(self.service, [ AnotherPlugin() ]) ])
     r = server.Request(Channel(self.site), True)
     r.site = r.channel.site
     input = ''
     r.gotLength(len(input))
     r.handleContentChunk(input)
     r.queued = 0
     d = r.notifyFinish()
     def finish(result):
         self.assertSubstring('\r\n\r\n{"arg1": ["val10X", "val11X"], "arg2": ["val20X"], "MORE": "YES", "echo": ["yesX"]}', r.transport.getvalue())
     d.addCallback(finish)
     r.requestReceived('GET', '/resource?action=echo&echo=yes&arg1=val10&arg1=val11&arg2=val20', '')
     return d
Пример #12
0
    def test02_wrap_http_plugin(self):
        class MyService:
            def handle(self, result, args):
                return result

        plugins = CardstoriesPlugins({ 'plugins-dir': '..',
                                       'plugins': 'plugin_site'})
        plugins.load(True)
        resource = CardstoriesResource(MyService())
        self.site = CardstoriesSite(resource, { 'plugins-pre-process': 'plugin_site',
                                                'plugins-post-process': 'plugin_site' }, plugins.plugins)
        request = server.Request(self.Channel(self.site), True)
        request.site = self.site
        request.args = {}
        request.method = 'GET'
        d = resource.wrap_http(request)
        def finish(result):
            self.assertSubstring('"preprocess": ["PREPROCESS"]', request.transport.getvalue())
            self.assertSubstring('"postprocess": "POSTPROCESS"', request.transport.getvalue())
        d.addCallback(finish)
        return d
Пример #13
0
 def test03_pipeline(self):
     plugin = example.Plugin(self.service, [ AnotherPlugin() ])
     resource = CardstoriesResource(self.service)
     self.site = CardstoriesSite(CardstoriesTree(self.service),
                                 { 'plugins-pre-process': 'example',
                                   'plugins-post-process': 'example' },
                                 [ plugin ])
     self.collected = []
     def collect(result):
         self.collected.append([ plugin.preprocessed, plugin.postprocessed ])
         return result
     def send(action, args):
         args['action'] = [action];
         request = server.Request(Channel(self.site), True)
         request.site = self.site
         request.args = args
         request.method = 'GET'
         d = resource.wrap_http(request)
         d.addCallback(collect)
         return d
     self.service.startService()
     yield self.complete_game(send)
     yield self.service.stopService()
     count = 0
     self.assertEquals(self.collected[count],
                       [{'action': ['create'],
                         'owner_id': [self.owner_id]},
                        {'game_id': self.game_id}]);
     count += 1
     del self.collected[count][1]['modified']
     self.assertEquals(self.collected[count],
                       [{'action': ['set_card'],
                         'card': [self.winner_card],
                         'player_id': [self.owner_id],
                         'game_id': [self.game_id]},
                        {'type': 'set_card',
                         'card': self.winner_card,
                         'player_id': self.owner_id,
                         'game_id': [self.game_id]}]);
     count += 1
     del self.collected[count][1]['modified']
     self.assertEquals(self.collected[count],
                       [{'action': ['set_sentence'],
                         'sentence': [self.sentence],
                         'player_id': [self.owner_id],
                         'game_id': [self.game_id]},
                        {'type': 'set_sentence',
                         'sentence': self.sentence,
                         'game_id': [self.game_id]}]);
     count += 1
     del self.collected[count][1]['modified']
     self.assertEquals(self.collected[count],
                       [{'action': ['invite'],
                         'game_id': [self.game_id],
                         'owner_id': [self.owner_id],
                         'player_id': [self.player1]},
                        {'game_id': [self.game_id],
                         'invited': [self.player1],
                         'type': 'invite'}]);
     count += 1
     del self.collected[count][1]['modified']
     self.assertEquals(self.collected[count],
                       [{'action': ['participate'],
                         'game_id': [self.game_id],
                         'player_id': [self.player1]},
                        {'game_id': [self.game_id],
                         'player_id': self.player1,
                         'type': 'participate'}]);
     count += 1
     del self.collected[count][1]['modified']
     player1_card = self.collected[count][0]['card'][0]
     self.assertEquals(self.collected[count],
                       [{'action': ['pick'],
                         'card': [player1_card],
                         'game_id': [self.game_id],
                         'player_id': [self.player1]},
                        {'card': player1_card,
                         'game_id': [self.game_id],
                         'player_id': self.player1,
                         'type': 'pick'}]);
     count += 1
     del self.collected[count][1]['modified']
     self.assertEquals(self.collected[count],
                       [{'action': ['participate'],
                         'game_id': [self.game_id],
                         'player_id': [self.player2]},
                        {'game_id': [self.game_id],
                         'player_id': self.player2,
                         'type': 'participate'}]);
     count += 1
     del self.collected[count][1]['modified']
     player2_card = self.collected[count][0]['card'][0]
     self.assertEquals(self.collected[count],
                       [{'action': ['pick'],
                         'card': [player2_card],
                         'game_id': [self.game_id],
                         'player_id': [self.player2]},
                        {'card': player2_card,
                         'game_id': [self.game_id],
                         'player_id': self.player2,
                         'type': 'pick'}]);
     count += 1
     del self.collected[count][1]['modified']
     self.assertEquals(self.collected[count],
                       [{'action': ['voting'],
                         'game_id': [self.game_id],
                         'owner_id': [self.owner_id]},
                        {'game_id': [self.game_id], 'type': 'voting'}]);
     count += 1
     del self.collected[count][1]['modified']
     self.assertEquals(self.collected[count],
                       [{'action': ['vote'],
                         'card': [self.winner_card],
                         'game_id': [self.game_id],
                         'player_id': [self.player1]},
                        {'game_id': [self.game_id],
                         'player_id': self.player1,
                         'type': 'vote',
                         'vote': self.winner_card}]);
     count += 1
     del self.collected[count][1]['modified']
     looser_card = self.collected[count][0]['card'][0]
     self.assertEquals(self.collected[count],
                       [{'action': ['vote'],
                         'card': [looser_card],
                         'game_id': [self.game_id],
                         'player_id': [self.player2]},
                        {'game_id': [self.game_id],
                         'player_id': self.player2,
                         'type': 'vote',
                         'vote': looser_card}]);
     count += 1
     del self.collected[count][1]['modified']
     self.assertEquals(self.collected[count],
                       [{'action': ['complete'],
                         'game_id': [self.game_id],
                         'owner_id': [self.owner_id]},
                        {'game_id': [self.game_id],
                         'type': 'complete'}]);
     count += 1
     self.assertEqual(len(self.collected), count)