Example #1
0
    def test06_touch_state(self):
        player_id = 200
        sentence = "This is my sentence!"
        # new instance of chat plugin to run the test against
        chat_instance = Plugin(self.service, [])
        # put the chat instance into the service's pollable_plugins
        self.service.pollable_plugins.append(chat_instance)
        # flag to signify whether the callback has run
        self.called = False
        # service to poll instance waiting for chat
        d = self.service.poll({
            'action': ['poll'],
            'player_id': [player_id],
            'type': ['chat'],
            'modified': [chat_instance.get_modified()]
        })

        # callback which runs once the chat plugin calls touch()
        def check(event):
            self.called = True

        d.addCallback(check)
        # make sure our flag is false before we run
        self.assertFalse(self.called)
        # run the test request
        request = Request(action=['message'],
                          player_id=[player_id],
                          sentence=[sentence])
        result = yield chat_instance.preprocess(True, request)
        yield d
        # make sure the flag is now set after we've run the test
        self.assertTrue(self.called)
Example #2
0
 def test06_touch_state(self):
     player_id = 200
     sentence = "This is my sentence!"
     # new instance of chat plugin to run the test against
     chat_instance = Plugin(self.service, [])
     # put the chat instance into the service's pollable_plugins
     self.service.pollable_plugins.append(chat_instance)
     # flag to signify whether the callback has run
     self.called = False
     # service to poll instance waiting for chat
     d = self.service.poll({'action': ['poll'],
                            'player_id': [player_id],
                            'type': ['chat'],
                            'modified': [chat_instance.get_modified()]})
     # callback which runs once the chat plugin calls touch()
     def check(event):
         self.called = True
     d.addCallback(check)
     # make sure our flag is false before we run
     self.assertFalse(self.called)
     # run the test request
     request = Request(action=['message'], player_id=[player_id], sentence=[sentence])
     result = yield chat_instance.preprocess(True, request)
     yield d
     # make sure the flag is now set after we've run the test
     self.assertTrue(self.called)
Example #3
0
 def test05_check_half_of_multiple_messages(self):
     # new instance of the chat plugin to test
     chat_instance = Plugin(self.service, [])
     # create a message event request
     player_ids = [200, 220, 999]
     sentences = [
         "This is my sentence!", "Yeah another test hello.",
         "Ping ping poing pong."
     ]
     when = []
     for i in range(3):
         sleep(0.1)
         when.append(int(runtime.seconds() * 1000))
         request = Request(action=['message'],
                           player_id=[player_ids[i]],
                           sentence=[sentences[i]])
         # run the request
         result = yield chat_instance.preprocess(True, request)
     # check to make sure no message is returned if we ask for now or later
     # we check right back to one second ago to make sure all recently added messages are caught
     state, players_id_list = yield chat_instance.state(
         {"modified": [when[-1] - 150]})
     # this time because of the 100ms delay between messages, and only checking to 150ms ago
     # we should only get the last two messages
     self.assertEquals(len(state['messages']), 2)
     for i in range(2):
         self.assertEquals(state['messages'][i]['player_id'],
                           player_ids[i + 1])
         self.assertEquals(state['messages'][i]['sentence'],
                           sentences[i + 1])
     self.assertEquals(players_id_list, player_ids[-2:])
Example #4
0
 def test01_add_message(self):
     # new instance of the chat plugin to test
     chat_instance = Plugin(self.service, [])
     # create a message event request
     player_id = 200
     sentence = "This is my sentence!"
     now = int(runtime.seconds() * 1000)
     request = Request(action=['message'],
                       player_id=[player_id],
                       sentence=[sentence])
     # verify we have no messages yet
     self.assertEquals(len(chat_instance.messages), 0)
     # run the request
     result = yield chat_instance.preprocess(True, request)
     # verify we now have one message
     self.assertEquals(len(chat_instance.messages), 1)
     # verify the event has been removed from the pipeline
     self.assertFalse(request.args.has_key('action'))
     # verify the message we added is in the list
     self.assertEquals(chat_instance.messages[0]["player_id"], player_id)
     self.assertEquals(chat_instance.messages[0]["sentence"], sentence)
     # check that the message has been recorded in log file
     with open(
             os.path.join(self.test_logdir, 'chat',
                          '%s.log' % strftime('%Y-%m-%d'))) as f:
         lines = f.readlines()
         self.assertEquals(len(lines), 1)
         self.assertIn(sentence, lines[0])
         self.assertIn('player_%d' % player_id, lines[0])
Example #5
0
 def test01_add_message(self):
     # new instance of the chat plugin to test
     chat_instance = Plugin(self.service, [])
     # create a message event request
     player_id = 200
     sentence = "This is my sentence!"
     now = int(runtime.seconds() * 1000)
     request = Request(action=['message'], player_id=[player_id], sentence=[sentence])
     # verify we have no messages yet
     self.assertEquals(len(chat_instance.messages), 0)
     # run the request
     result = yield chat_instance.preprocess(True, request)
     # verify we now have one message
     self.assertEquals(len(chat_instance.messages), 1)
     # verify the event has been removed from the pipeline
     self.assertFalse(request.args.has_key('action'))
     # verify the message we added is in the list
     self.assertEquals(chat_instance.messages[0]["player_id"], player_id)
     self.assertEquals(chat_instance.messages[0]["sentence"], sentence)
     # check that the message has been recorded in log file
     with open(os.path.join(self.test_logdir, 'chat', '%s.log' % strftime('%Y-%m-%d'))) as f:
         lines = f.readlines()
         self.assertEquals(len(lines), 1)
         self.assertIn(sentence, lines[0])
         self.assertIn('player_%d' % player_id, lines[0])
Example #6
0
 def test00_preprocess_noop(self):
     # create a new instance of the plugin and make sure it's the right type
     chat_instance = Plugin(self.service, [])
     self.assertEquals(chat_instance.name(), 'chat')
     # run a game to get into a realistic situation
     yield self.complete_game()
     # run the preprocess method and make sure it does not affect anything during a normal 'game' event
     result_in = 'RESULT'
     result_out = yield chat_instance.preprocess(result_in, Request(action=['game']))
     self.assertEquals(result_in, result_out)
Example #7
0
 def test00_preprocess_noop(self):
     # create a new instance of the plugin and make sure it's the right type
     chat_instance = Plugin(self.service, [])
     self.assertEquals(chat_instance.name(), 'chat')
     # run a game to get into a realistic situation
     yield self.complete_game()
     # run the preprocess method and make sure it does not affect anything during a normal 'game' event
     result_in = 'RESULT'
     result_out = yield chat_instance.preprocess(result_in,
                                                 Request(action=['game']))
     self.assertEquals(result_in, result_out)
Example #8
0
 def test11_link_url(self):
     # new instance of the chat plugin to test
     chat_instance = Plugin(self.service, [])
     # create a message event request
     player_id = 201
     url_sentence = 'For searching the web I use google.com, it\'s great!'
     now = int(runtime.seconds() * 1000)
     request = Request(action=['message'], player_id=[player_id], sentence=[url_sentence])
     # run the request
     result = yield chat_instance.preprocess(True, request)
     # check to make sure our message is returned with a link for the url
     state, players_id_list = yield chat_instance.state({"modified": [now - 1]})
     self.assertEquals(state['messages'][0]['player_id'], player_id)
     self.assertEquals(state['messages'][0]['sentence'], 'For searching the web I use <a target="_blank" href="http://google.com">google.com</a>, it\'s great!')
Example #9
0
 def test10_escape_html(self):
     # new instance of the chat plugin to test
     chat_instance = Plugin(self.service, [])
     # create a message event request
     player_id = 201
     naughty_sentence = '<script>alert("haha!")</script>'
     now = int(runtime.seconds() * 1000)
     request = Request(action=['message'], player_id=[player_id], sentence=[naughty_sentence])
     # run the request
     result = yield chat_instance.preprocess(True, request)
     # check to make sure our naughty message is returned properly escaped
     state, players_id_list = yield chat_instance.state({"modified": [now - 1]})
     self.assertEquals(state['messages'][0]['player_id'], player_id)
     self.assertEquals(state['messages'][0]['sentence'], '&lt;script&gt;alert("haha!")&lt;/script&gt;')
Example #10
0
 def test08_nonascii_characters_message(self):
     # new instance of the chat plugin to test
     chat_instance = Plugin(self.service, [])
     # create a message event request
     player_id = 200
     # The sentence is a 'str' object. Create it by encoding a unicode string.
     unicode_sentence = u"你好 Matjaž Gregorič"
     sentence_bytes = unicode_sentence.encode('utf-8')
     request = Request(action=['message'], player_id=[player_id], sentence=[sentence_bytes])
     # run the request
     result = yield chat_instance.preprocess(True, request)
     # check that the message has been recorded in log file
     with open(os.path.join(self.test_logdir, 'chat', '%s.log' % strftime('%Y-%m-%d'))) as f:
         lines = f.readlines()
         self.assertIn(unicode_sentence, lines[0].decode('utf-8'))
Example #11
0
 def test02_check_added_message_after_now(self):
     # new instance of the chat plugin to test
     chat_instance = Plugin(self.service, [])
     # create a message event request
     player_id = 200
     sentence = "This is my sentence!"
     now = int(runtime.seconds() * 1000)
     request = Request(action=['message'], player_id=[player_id], sentence=[sentence])
     # run the request
     result = yield chat_instance.preprocess(True, request)
     # check to make sure no message is returned if we ask for now or later
     state, players_id_list = yield chat_instance.state({"modified": [now + 1]})
     self.assertTrue(state.has_key('messages'))
     self.assertEquals(len(state['messages']), 0)
     self.assertEquals(players_id_list, [])
Example #12
0
 def test09_nonascii_characters_notification(self):
     # new instance of the chat plugin to test
     chat_instance = Plugin(self.service, [])
     # create a message event request
     class FakeGame:
         id = 102
         owner_id = 303
     unicode_sentence = u"我不明白 šal čez želodec"
     changes = {'type': 'change',
                'details': {'type': 'load',
                            'sentence': unicode_sentence},
                'game': FakeGame()}
     result = yield chat_instance.self_notify(changes)
     with open(os.path.join(self.test_logdir, 'chat', '%s.log' % strftime('%Y-%m-%d'))) as f:
         lines = f.readlines()
         self.assertIn(unicode_sentence, lines[0].decode('utf-8'))
Example #13
0
 def test02_check_added_message_after_now(self):
     # new instance of the chat plugin to test
     chat_instance = Plugin(self.service, [])
     # create a message event request
     player_id = 200
     sentence = "This is my sentence!"
     now = int(runtime.seconds() * 1000)
     request = Request(action=['message'],
                       player_id=[player_id],
                       sentence=[sentence])
     # run the request
     result = yield chat_instance.preprocess(True, request)
     # check to make sure no message is returned if we ask for now or later
     state, players_id_list = yield chat_instance.state(
         {"modified": [now + 1]})
     self.assertTrue(state.has_key('messages'))
     self.assertEquals(len(state['messages']), 0)
     self.assertEquals(players_id_list, [])
Example #14
0
 def test10_escape_html(self):
     # new instance of the chat plugin to test
     chat_instance = Plugin(self.service, [])
     # create a message event request
     player_id = 201
     naughty_sentence = '<script>alert("haha!")</script>'
     now = int(runtime.seconds() * 1000)
     request = Request(action=['message'],
                       player_id=[player_id],
                       sentence=[naughty_sentence])
     # run the request
     result = yield chat_instance.preprocess(True, request)
     # check to make sure our naughty message is returned properly escaped
     state, players_id_list = yield chat_instance.state(
         {"modified": [now - 1]})
     self.assertEquals(state['messages'][0]['player_id'], player_id)
     self.assertEquals(state['messages'][0]['sentence'],
                       '&lt;script&gt;alert("haha!")&lt;/script&gt;')
Example #15
0
 def test08_nonascii_characters_message(self):
     # new instance of the chat plugin to test
     chat_instance = Plugin(self.service, [])
     # create a message event request
     player_id = 200
     # The sentence is a 'str' object. Create it by encoding a unicode string.
     unicode_sentence = u"你好 Matjaž Gregorič"
     sentence_bytes = unicode_sentence.encode('utf-8')
     request = Request(action=['message'],
                       player_id=[player_id],
                       sentence=[sentence_bytes])
     # run the request
     result = yield chat_instance.preprocess(True, request)
     # check that the message has been recorded in log file
     with open(
             os.path.join(self.test_logdir, 'chat',
                          '%s.log' % strftime('%Y-%m-%d'))) as f:
         lines = f.readlines()
         self.assertIn(unicode_sentence, lines[0].decode('utf-8'))
Example #16
0
 def test04_check_multiple_messages(self):
     # new instance of the chat plugin to test
     chat_instance = Plugin(self.service, [])
     # create a message event request
     player_ids = [200, 220, 999]
     sentences = ["This is my sentence!", "Yeah another test hello.", "Ping ping poing pong."]
     when = []
     for i in range(3):
         when.append(int(runtime.seconds() * 1000))
         request = Request(action=['message'], player_id=[player_ids[i]], sentence=[sentences[i]])
         # run the request
         result = yield chat_instance.preprocess(True, request)
     # check to make sure no message is returned if we ask for now or later
     # we check right back to one second ago to make sure all recently added messages are caught
     state, players_id_list = yield chat_instance.state({"modified": [when[-1] - 1000]})
     self.assertEquals(len(state['messages']), 3)
     for i in range(3):
         self.assertEquals(state['messages'][i]['player_id'], player_ids[i])
         self.assertEquals(state['messages'][i]['sentence'], sentences[i])
     self.assertEquals(players_id_list, player_ids)
Example #17
0
 def test11_link_url(self):
     # new instance of the chat plugin to test
     chat_instance = Plugin(self.service, [])
     # create a message event request
     player_id = 201
     url_sentence = 'For searching the web I use google.com, it\'s great!'
     now = int(runtime.seconds() * 1000)
     request = Request(action=['message'],
                       player_id=[player_id],
                       sentence=[url_sentence])
     # run the request
     result = yield chat_instance.preprocess(True, request)
     # check to make sure our message is returned with a link for the url
     state, players_id_list = yield chat_instance.state(
         {"modified": [now - 1]})
     self.assertEquals(state['messages'][0]['player_id'], player_id)
     self.assertEquals(
         state['messages'][0]['sentence'],
         'For searching the web I use <a target="_blank" href="http://google.com">google.com</a>, it\'s great!'
     )
Example #18
0
    def test07_notification_messages(self):
        # new instance of chat plugin to run the test against
        chat_instance = Plugin(self.service, [])

        self.count = 0
        def build_message(self, message):
            """
            message == {'type': 'notification',
                        'game_id': GAME_ID,
                        'player_id': 'OWNER_ID',
                        'sentence': 'SENTENCE'}

            """
            self.count += 1
            self.assertEquals(self.count, 1)
            self.assertEquals(message['type'], 'notification')
            self.assertEquals(message['player_id'], '15')
            self.assertEquals(message['sentence'], 'SENTENCE')

        # build_message should only be called once, upon game creation.
        chat_instance.build_message = build_message
        # run a game to get into a realistic situation
        yield self.complete_game()
Example #19
0
    def test09_nonascii_characters_notification(self):
        # new instance of the chat plugin to test
        chat_instance = Plugin(self.service, [])

        # create a message event request
        class FakeGame:
            id = 102
            owner_id = 303

        unicode_sentence = u"我不明白 šal čez želodec"
        changes = {
            'type': 'change',
            'details': {
                'type': 'load',
                'sentence': unicode_sentence
            },
            'game': FakeGame()
        }
        result = yield chat_instance.self_notify(changes)
        with open(
                os.path.join(self.test_logdir, 'chat',
                             '%s.log' % strftime('%Y-%m-%d'))) as f:
            lines = f.readlines()
            self.assertIn(unicode_sentence, lines[0].decode('utf-8'))
Example #20
0
    def test07_notification_messages(self):
        # new instance of chat plugin to run the test against
        chat_instance = Plugin(self.service, [])

        self.count = 0

        def build_message(self, message):
            """
            message == {'type': 'notification',
                        'game_id': GAME_ID,
                        'player_id': 'OWNER_ID',
                        'sentence': 'SENTENCE'}

            """
            self.count += 1
            self.assertEquals(self.count, 1)
            self.assertEquals(message['type'], 'notification')
            self.assertEquals(message['player_id'], '15')
            self.assertEquals(message['sentence'], 'SENTENCE')

        # build_message should only be called once, upon game creation.
        chat_instance.build_message = build_message
        # run a game to get into a realistic situation
        yield self.complete_game()
Example #21
0
 def test00_create_logdir(self):
     chat_instance = Plugin(self.service, [])
     logdir = os.path.join(self.test_logdir, 'chat')
     self.assertTrue(os.path.exists(logdir))