Ejemplo n.º 1
0
class TestCommandSubmitter:
    def setUp(self):
        self.sender = CommandSubmitter('test', 'sender')
        interfaces.get('test').reset()
        self.interface = interfaces.get('test')

    def test_code(self):
        ok_(self.sender.code == 'sender')

    def test_send_message(self):
        test_msg = 'test message'
        self.sender.send_message(test_msg)
        ok_(len(self.interface.msg_queue[self.sender.code]) == 1)
        ok_(self.interface.msg_queue[self.sender.code][0] == test_msg,
            self.interface.msg_queue)

    def test_get_display_name(self):
        disp_name = self.sender.get_display_name()
        ok_(disp_name == self.interface.get_display_name(self.sender))

    def test_unicode(self):
        u_name = unicode(self.sender)
        ok_(u_name == unicode(self.sender.get_display_name()))

    def test_dict_hashable(self):
        sender_2 = CommandSubmitter('test', 'sender2')
        sender_3 = CommandSubmitter('some', 'sender')
        sender_same = CommandSubmitter('test', 'sender')
        some_dict = defaultdict(lambda: False)
        some_dict[self.sender] = True
        ok_(some_dict[self.sender] is True)
        ok_(some_dict[sender_2] is False)
        ok_(some_dict[sender_3] is False)
        ok_(some_dict[sender_same] is True)
Ejemplo n.º 2
0
    def test_live_notification(self):
        fake_sender = CommandSubmitter('test', 'fake_sender')
        fake_sender2 = CommandSubmitter('test', 'fake_sender2')
        self.sublist = {
            fake_sender: ['testch1'],
            fake_sender2: ['testch2'],
        }

        self.checker.start()
        threading.Event().wait(1)
        self.checker.refresh()
        threading.Event().wait(1)
        ok_(len(interfaces.get('test').msg_queue[fake_sender]) == 0)
        ok_(len(interfaces.get('test').msg_queue[fake_sender2]) == 0)

        self.twitch.live_ch_list = {
            'TESTCH1': {
                'status': u'test_status',
                'game': u'test_game',
                'url': u'test_url'
            }
        }
        self.checker.refresh()
        threading.Event().wait(1)
        self.checker.stop()
        ok_(
            'TESTCH1'
            in ' '.join(interfaces.get('test').msg_queue[fake_sender]),
            interfaces.get('test').msg_queue[fake_sender])
        ok_(
            'TESTCH1'
            not in ' '.join(interfaces.get('test').msg_queue[fake_sender2]),
            interfaces.get('test').msg_queue[fake_sender2])
Ejemplo n.º 3
0
    def test_subscribe_one(self):
        try:
            os.remove(self.service.SUB_FILE)
        except OSError:
            pass

        self.service.start()
        threading.Event().wait(.1)
        self.service.stop()

        self.service._twitch.set_exists_channel_list(['testch1', 'testch2'])
        fake_sender = CommandSubmitter('test', 'fake_sender')
        self.service._subscribe(['testch1'], fake_sender)

        ok_(
            'testch1'
            in [x.lower() for x in self.service._sublist[fake_sender]],
            'sublist = ' + str(self.service._sublist[fake_sender]))
        ok_('not found' not in ' '.join(
            interfaces.get('test').msg_queue[fake_sender]))
        ok_(self.service._channel_sub_count['testch1'] == 1)

        fake_sender2 = CommandSubmitter('test', 'fake_sender2')
        self.service._subscribe(['testch1'], fake_sender2)

        ok_(self.service._channel_sub_count['testch1'] == 2)
Ejemplo n.º 4
0
 def test_dict_hashable(self):
     sender_2 = CommandSubmitter('test', 'sender2')
     sender_3 = CommandSubmitter('some', 'sender')
     sender_same = CommandSubmitter('test', 'sender')
     some_dict = defaultdict(lambda: False)
     some_dict[self.sender] = True
     ok_(some_dict[self.sender] is True)
     ok_(some_dict[sender_2] is False)
     ok_(some_dict[sender_3] is False)
     ok_(some_dict[sender_same] is True)
Ejemplo n.º 5
0
    def test_list_users(self):
        self.test_subscribe_one()
        self.service._subscribe(['testch2'],
                                CommandSubmitter('test', 'fake_sender2'))
        config['interface']['test'] = {'admin_id': 'test_admin'}

        # check admin only
        fake_sender = CommandSubmitter('test', 'fake_sender')
        self.service._list_users([], fake_sender)
        ok_('Channels' not in ''.join(
            interfaces.get('test').msg_queue[fake_sender]))

        fake_sender = CommandSubmitter('test', 'test_admin')
        self.service._list_users([], fake_sender)

        # check msg response
        ok_('fake_sender' in ''.join(
            interfaces.get('test').msg_queue[fake_sender]))
        ok_('fake_sender2' in ''.join(
            interfaces.get('test').msg_queue[fake_sender]))
        ok_('testch1' not in ''.join(
            interfaces.get('test').msg_queue[fake_sender]))
        ok_('testch2' not in ''.join(
            interfaces.get('test').msg_queue[fake_sender]))

        # test list single user, channels
        interfaces.get('test').reset()
        for index in range(2):
            self.service._list_users([index], fake_sender)
            msg = ' '.join(interfaces.get('test').msg_queue[fake_sender])

            if 'fake_sender' in msg:
                ok_('testch1' in msg)
            elif 'fake_sender2' in msg:
                ok_('testch2' in msg)
                ok_('testch1' in msg)

        # test list multiple user channels
        interfaces.get('test').reset()
        self.service._list_users(range(2), fake_sender)
        ok_('fake_sender' in ''.join(
            interfaces.get('test').msg_queue[fake_sender]))
        ok_('fake_sender2' in ''.join(
            interfaces.get('test').msg_queue[fake_sender]))
        ok_('testch1' in ''.join(
            interfaces.get('test').msg_queue[fake_sender]))
        ok_('testch2' in ''.join(
            interfaces.get('test').msg_queue[fake_sender]))
Ejemplo n.º 6
0
    def test_channel_offline(self):
        self.twitch.live_ch_list = {
            'testch1': {
                'status': u'test_status',
                'game': u'test_game',
                'url': u'test_url'
            }
        }
        fake_sender = CommandSubmitter('test', 'fake_sender')
        self.sublist = {fake_sender: ['testch1']}
        self.checker.start()
        threading.Event().wait(1)
        self.twitch.live_ch_list = {}
        self.checker.refresh()
        threading.Event().wait(1)
        # no message should be sent
        ok_(len(interfaces.get('test').msg_queue[fake_sender]) == 0)

        # simulate channel goes live
        self.twitch.live_ch_list = {
            'testch1': {
                'status': u'test_status',
                'game': u'test_game',
                'url': u'test_url'
            }
        }
        self.checker.refresh()
        threading.Event().wait(1)
        self.checker.stop()
        ok_(
            'testch1'
            in ' '.join(interfaces.get('test').msg_queue[fake_sender]),
            interfaces.get('test').msg_queue[fake_sender])
Ejemplo n.º 7
0
    def test_sub_by_url(self):
        sender = CommandSubmitter('test', 'sender')
        self.service._twitch.exists_ch_list = ['KayDaDa', 'LinotServant']
        self.service.setup(self.parser)
        self.service.start()
        threading.Event().wait(1)
        self.service.stop()
        self.service._sub_by_url(['KayDaDa', 'LinotServant'], 'dummy', sender)
        ok_('KayDaDa'.lower() in self.service._sublist[sender],
            'sublist = ' + str(self.service._sublist[sender]))
        ok_('LinotServant'.lower() in self.service._sublist[sender],
            'sublist = ' + str(self.service._sublist[sender]))

        self.service._unsubscribe(['KayDaDa', 'LinotServant'], sender)
        ok_('KayDaDa'.lower() not in self.service._sublist[sender],
            'sublist = ' + str(self.service._sublist[sender]))
        ok_('LinotServant'.lower() not in self.service._sublist[sender],
            'sublist = ' + str(self.service._sublist[sender]))

        # Integration test
        self.parser.process_direct_commands(
            'www.twitch.tv/KayDaDa twitch.tv/LinotServant', sender)
        ok_('KayDaDa'.lower() in self.service._sublist[sender],
            'sublist = ' + str(self.service._sublist[sender]))
        ok_('LinotServant'.lower() in self.service._sublist[sender],
            'sublist = ' + str(self.service._sublist[sender]))
Ejemplo n.º 8
0
 def add_test(self, test_list):
     for sender_code, cmd, cmd_chk, msg_chk in test_list:
         sender = CommandSubmitter(sender_code, self.if_name)
         interfaces.get(self.in_name).add_command(sender, cmd)
         self.senders[self.total_test_count] = sender
         self.cmd_checkers[self.total_test_count] = cmd_chk
         self.msg_checkers[self.total_test_count] = msg_chk
         self.total_test_count += 1
Ejemplo n.º 9
0
 def test_cmd_process(self):
     test_sender = CommandSubmitter('test', 'mock')
     self.service._cmd_process(None, test_sender)
     test_if = interfaces.get('test')
     # cmd_process arg=None indicates the msg contains no recognized command
     # we should return something to let user know
     ok_('Unknown' in test_if.msg_queue[test_sender.code][0])
     test_if.reset()
     self.service._cmd_process('test', test_sender)
     ok_('not implemented' in test_if.msg_queue[test_sender.code][0])
Ejemplo n.º 10
0
    def test_unsubscribe_one(self):
        self.test_subscribe_one()

        fake_sender = CommandSubmitter('test', 'fake_sender')
        fake_sender2 = CommandSubmitter('test', 'fake_sender2')
        self.service._subscribe(['testch2'], fake_sender)
        self.service._unsubscribe(['testch1'], fake_sender)

        ok_('testch1' not in self.service._sublist[fake_sender],
            'sublist = ' + str(self.service._sublist[fake_sender]))
        ok_('not found' not in ' '.join(
            interfaces.get('test').msg_queue[fake_sender]))
        ok_(self.service._channel_sub_count['testch1'] == 1)

        self.service._unsubscribe(['testch1'], fake_sender2)

        ok_('testch1' not in self.service._sublist[fake_sender2],
            'sublist = ' + str(self.service._sublist[fake_sender2]))
        ok_(self.service._channel_sub_count['testch1'] == 0)
Ejemplo n.º 11
0
    def test_unknown_commands(self):
        # test command server can properly handle 1 or multiple commands
        def default_process(args, sender):
            ok_(getattr(args, sender.code) is True)
            default_process.called += 1

        lap = LinotArgParser('testcmd', self.parser, default_process)
        lap.add_argument('-a', action='store_true')
        lap.add_argument('-b', action='store_true')
        lap.add_argument('-c', action='store_true')

        # Test 1 unknown command, command server respose to user
        sender_a = CommandSubmitter('test', 'a')
        fake_cmd = [
            (sender_a, 'some_unknown_words'),
        ]
        interfaces.get('test').reset()
        interfaces.get('test').add_command_list(fake_cmd)
        command_server.start(self.parser, ['test'])
        default_process.called = 0
        threading.Event().wait(.5)
        command_server.stop()
        ok_(default_process.called == 0)
        ok_('Unknown' in ' '.join(interfaces.get('test').msg_queue[sender_a.code]))

        # Test multiple unknown commands
        sender_a = CommandSubmitter('test', 'a')
        sender_u = CommandSubmitter('test', 'u')
        fake_cmd = [
            (sender_u, 'some_unknown_cmds'),
            (sender_a, 'testcmd -a'),
            (sender_u, 'some_unknown_cmds'),
        ]
        interfaces.get('test').reset()
        interfaces.get('test').add_command_list(fake_cmd)
        command_server.start(self.parser, ['test'])
        default_process.called = 0
        threading.Event().wait(.5)
        command_server.stop()
        unknown_response = ' '.join(interfaces.get('test').msg_queue[sender_u.code])
        ok_(unknown_response.count('Unknown') == 2)
        ok_(default_process.called == 1)
Ejemplo n.º 12
0
    def test_refresh(self):
        # check admin only
        self.service.start()
        threading.Event().wait(.1)
        self.service.stop()

        def self_ret():
            self_ret.val = True

        self_ret.val = False

        self.service._check_thread.refresh = self_ret

        config['interface']['test'] = {'admin_id': 'test_admin'}
        fake_sender = CommandSubmitter('test', 'fake_sender')
        self.service._refresh(True, fake_sender)

        ok_(self_ret.val is not True)

        self.service._refresh(True, CommandSubmitter('test', 'test_admin'))
        ok_(self_ret.val is True)
Ejemplo n.º 13
0
    def test_subscribe_one_not_found(self):
        self.service.start()
        threading.Event().wait(.1)
        self.service.stop()

        self.service._twitch.set_exists_channel_list(['testch2', 'testch3'])
        fake_sender = CommandSubmitter('test', 'fake_sender')
        self.service._subscribe(['testch1'], fake_sender)

        ok_('testch1' not in self.service._sublist[fake_sender],
            'sublist = ' + str(self.service._sublist[fake_sender]))
        ok_('not found' in ' '.join(
            interfaces.get('test').msg_queue[fake_sender]))
Ejemplo n.º 14
0
    def test_cmd_process(self):
        # test command server can properly handle 1 or multiple commands
        def default_process(args, sender):
            ok_(getattr(args, sender.code) is True)
            default_process.called += 1

        lap = LinotArgParser('testcmd', self.parser, default_process)
        lap.add_argument('-a', action='store_true')
        lap.add_argument('-b', action='store_true')
        lap.add_argument('-c', action='store_true')

        # Test 1 cmd return by polling_command
        # command = [(sender, cmd string), ...]
        sender = CommandSubmitter('test', 'a')
        fake_cmd = [(sender, 'testcmd -a')]
        interfaces.get('test').add_command_list(fake_cmd)
        default_process.called = 0
        command_server.start(self.parser, ['test'])
        threading.Event().wait(.5)
        command_server.stop()
        ok_(default_process.called == 1)

        # Test 3 cmds return by polling_command
        # command = [(sender, cmd string), ...]
        sender_a = CommandSubmitter('test', 'a')
        sender_b = CommandSubmitter('test', 'b')
        sender_c = CommandSubmitter('test', 'c')
        fake_cmd = [
            (sender_a, 'testcmd -a'),
            (sender_b, 'testcmd -b'),
            (sender_c, 'testcmd -c'),
        ]
        interfaces.get('test').add_command_list(fake_cmd)
        command_server.start(self.parser, ['test'])
        default_process.called = 0
        threading.Event().wait(.5)
        command_server.stop()
        ok_(default_process.called == 3)
Ejemplo n.º 15
0
    def test_import(self):
        sender = CommandSubmitter('test', 'tester')
        short_test_channels = ['testch' + str(x) for x in range(3)]
        long_test_channels = ['testch' + str(x) for x in range(10)]

        self.service.setup(self.parser)
        self.service.start()
        threading.Event().wait(1)
        self.service.stop()

        # test small amount of followed channel
        self.service._twitch.followed_ch_list = {
            'some_twitch_user': short_test_channels
        }
        self.service._import(['some_twitch_user'], sender)
        self.service._list_channel(True, sender)
        for ch in short_test_channels:
            ok_(ch.lower() in ' '.join(
                interfaces.get('test').msg_queue[sender]).lower())
        interfaces.get('test').reset()
        self.service._unsub_all(True, sender)

        # test large amount of followed channel
        self.service._twitch.followed_ch_list = {
            'some_twitch_user': long_test_channels
        }
        self.service._import(['some_twitch_user'], sender)
        self.service._list_channel(True, sender)
        for ch in long_test_channels:
            ok_(ch.lower() in ' '.join(
                interfaces.get('test').msg_queue[sender]).lower())
        ok_('a while' in ' '.join(interfaces.get(
            'test').msg_queue[sender]).lower())
        interfaces.get('test').reset()
        self.service._unsub_all(True, sender)

        # test not found
        self.service._twitch.followed_ch_list = {
            'some_twitch_user': long_test_channels
        }
        self.service._import(['some_twitch_user2'], sender)
        self.service._list_channel(True, sender)
        for ch in long_test_channels:
            ok_(ch.lower() not in ' '.join(
                interfaces.get('test').msg_queue[sender]).lower())
        ok_('not found' in ' '.join(interfaces.get(
            'test').msg_queue[sender]).lower())
        interfaces.get('test').reset()
        self.service._unsub_all(True, sender)
Ejemplo n.º 16
0
    def test_skip_already_lived_on_boot(self):
        self.twitch.live_ch_list = {
            'testch1': {
                'status': 'test_status',
                'game': 'test_game',
                'url': 'test_url'
            }
        }
        fake_sender = CommandSubmitter('test', 'fake_sender')
        self.sublist = {fake_sender: ['testch1']}

        self.checker.start()
        threading.Event().wait(1)
        self.checker.stop()
        ok_(len(interfaces.get('test').msg_queue[fake_sender]) == 0)
Ejemplo n.º 17
0
    def test_list_channel(self):
        self.service._twitch.set_live_channel_list(
            {'testch2': {
                'display_name': 'TESTCH2'
            }})
        self.test_subscribe_multi()

        fake_sender = CommandSubmitter('test', 'fake_sender')
        self.service._list_channel(True, fake_sender)
        ok_(' '.join(interfaces.get('test').msg_queue[fake_sender]).count(
            'LIVE') == 1)

        check_msg = ' '.join(interfaces.get('test').msg_queue[fake_sender])
        ok_('testch2' in check_msg.lower())
        ok_('testch1' in check_msg.lower())
Ejemplo n.º 18
0
    def test_list_users_no_user(self):
        # issue #10, list user hit exception while there is no user
        config['interface']['test'] = {'admin_id': 'test_admin'}
        try:
            os.remove(self.service.SUB_FILE)
        except OSError:
            pass

        self.service.start()
        threading.Event().wait(.1)
        self.service.stop()

        fake_sender = CommandSubmitter('test', 'test_admin')
        self.service._list_users([], fake_sender)

        # check there is a response to user
        ok_(len(''.join(interfaces.get('test').msg_queue[fake_sender])) > 0)
Ejemplo n.º 19
0
    def polling_command(self):
        # hide longPoll debug msg
        org_stdout = sys.stdout
        sys.stdout = io.BytesIO()
        self._client.lock.acquire(True)
        ge = self._client.longPoll()
        op_list = []
        for op in ge:
            op_list.append(op)
        self._client.lock.release()
        sys.stdout = org_stdout

        # construct formal command list
        command_list = []
        for op in op_list:
            submitter = CommandSubmitter(self.NAME, op[0].id)
            command_list.append((submitter, op[2].text))
        return command_list
Ejemplo n.º 20
0
    def test_print_help(self):
        def cmd_process(args, sender):
            pass

        test_str = 'testtesttest'
        lap = LinotArgParser('testcmd', self.parser, cmd_process)
        lap.add_argument('-a', action='store_true', help=test_str)
        lap.add_argument('-noshow',
                         action='store_true',
                         help=argparse.SUPPRESS)
        lap.add_argument('-showme', action='store_true', help='')

        # test -h and --help goes to print_help
        args, unknown_args = self.parser.parse_known_args('testcmd -h'.split())
        eq_(len(unknown_args), 0)
        sender = CommandSubmitter('test', 'test_sender')
        args.proc(args, sender)
        msg_queue = interfaces.get('test').msg_queue
        ok_('command list' in ' '.join(msg_queue[sender.code]))
        args, unknown_args = self.parser.parse_known_args(
            'testcmd --help'.split())
        eq_(len(unknown_args), 0)
        interfaces.get('test').reset()
        args.proc(args, sender)
        msg_queue = interfaces.get('test').msg_queue
        ok_('command list' in ' '.join(msg_queue[sender.code]))

        # add help
        args, unknown_args = self.parser.parse_known_args('testcmd -h'.split())
        interfaces.get('test').reset()
        args.proc(args, sender)
        msg_queue = interfaces.get('test').msg_queue
        msg = ' '.join(msg_queue[sender.code])
        ok_(test_str in msg, msg)
        ok_('-nowshow' not in msg)
        ok_('-showme' in msg)

        # Test help suppress if sender not found (for coverage)
        args, unknown_args = self.parser.parse_known_args('testcmd -h'.split())
        interfaces.get('test').reset()
        args.proc(args, None)
        msg_queue = interfaces.get('test').msg_queue
        msg = ' '.join(msg_queue[sender.code])
        ok_(msg is '', msg)
Ejemplo n.º 21
0
    def test_direct_command(self):
        def default_process(args, sender):
            ok_(False)  # should not reach here

        lap = LinotArgParser('testcmd', self.parser, default_process)

        def cmd_checker(match_list, cmd, sender):
            ok_('somechannel' in match_list)
            ok_(len(match_list) == 1)
            ok_(cmd == 'www.twitch.tv/somechannel')
            cmd_checker.runned = True
        lap.add_direct_command(cmd_checker, 'twitch\.tv/(\w+)[\s\t,]*', re.IGNORECASE)
        cmd_checker.runned = False

        sender = CommandSubmitter('test', 'sender')
        fake_cmd = [
            (sender, 'www.twitch.tv/somechannel')
        ]
        interfaces.get('test').add_command_list(fake_cmd)
        command_server.start(self.parser, ['test'])
        threading.Event().wait(.5)
        command_server.stop()
        ok_(cmd_checker.runned)
Ejemplo n.º 22
0
    def test_unsub_all(self):
        sender = CommandSubmitter('test', 'sender')
        test_channels = ['testch1', 'testch2', 'testch3']
        self.service._twitch.set_exists_channel_list(test_channels)

        self.service.setup(self.parser)
        self.service.start()
        threading.Event().wait(1)
        self.service.stop()

        self.service._subscribe(test_channels, sender)
        self.service._list_channel(True, sender)
        # make sure subscribe success
        for ch in test_channels:
            ok_(ch.lower() in ' '.join(
                interfaces.get('test').msg_queue[sender]).lower())

        interfaces.get('test').reset()

        self.service._unsub_all(True, sender)
        self.service._list_channel(True, sender)
        for ch in test_channels:
            ok_(ch.lower() not in ' '.join(
                interfaces.get('test').msg_queue[sender]).lower())
Ejemplo n.º 23
0
 def setUp(self):
     self.sender = CommandSubmitter('test', 'sender')
     interfaces.get('test').reset()
     self.interface = interfaces.get('test')