Example #1
0
 def setUp(self):
     self.output = FakeOutput()
     self.lp = LineParser(client_class=FakeClient, ui=self.output)
     self.output.lp = self.lp
     logging.getLogger().addHandler(LogHandler(self.output))
     logging.getLogger().setLevel(logging.DEBUG)
Example #2
0
class LineParserTest(unittest.TestCase):
    def setUp(self):
        self.output = FakeOutput()
        self.lp = LineParser(client_class=FakeClient, ui=self.output)
        self.output.lp = self.lp
        logging.getLogger().addHandler(LogHandler(self.output))
        logging.getLogger().setLevel(logging.DEBUG)

    def _connect(self, jid='*****@*****.**', password='******'):
        self.lp.parse('/connect %s %s' % (jid, password))

    def c(self, i):
        return self.lp.connections[i].__dict__

    def testConnection(self):
        self.assertEqual(len(self.lp.connections), 0)
        self._connect()
        self.assertEqual(len(self.lp.connections), 1)
        self.assertEqual(self.c(0)['jid'], '*****@*****.**')
        self.assertEqual(self.c(0)['password'], 'password')

        self._connect('[email protected]')
        self.assertEqual(len(self.lp.connections), 2)
        self.assertEqual(self.c(1)['jid'], '[email protected]')
        self.assertEqual(self.c(1)['password'], 'password')

    def testMessage(self):
        self._connect()
        self.lp.parse('/msg [email protected] hi there')
        self.assertEqual(self.c(0)['target'], '*****@*****.**')
        self.assertEqual(self.c(0)['body'], 'hi there')

        self._connect('[email protected]')
        self.lp.connection_context = 0
        self.lp.parse('/msg -account [email protected] [email protected] Message')
        self.assertEqual(self.c(1)['target'], '*****@*****.**')

    def testAccountSettingWithoutResource(self):
        self._connect('[email protected]/CustomResource')
        self.lp.parse('/msg -account [email protected] [email protected] Message')
        self.assertEqual(self.c(0)['target'], '*****@*****.**')

    def testAccountSettingViaNumber(self):
        self._connect('[email protected]')
        self._connect('[email protected]')
        self.lp.parse('/msg -account 0 [email protected] Message')
        self.assertEqual(self.c(0)['target'], '*****@*****.**')

    def testMessageContext(self):
        """Both these messages should go to the same user:
        > /msg [email protected] Message 1
        > Message 2

        """
        self._connect()
        self.lp.parse('/msg [email protected] Message 1')
        self.lp.parse('Message 2')

        self.assertEqual(self.c(0)['target'], '*****@*****.**')

        self.lp.parse('/msg [email protected] Message 1')

        self.assertEqual(self.c(0)['target'], '[email protected]')

        self.lp.message_context = '*****@*****.**'
        self.lp.parse('Message 3')

        self.assertEqual(self.c(0)['target'], '*****@*****.**')

    def testArgumentFidelity(self):
        """ '/msg [email protected] foo\tbar' should be passed with whitespace intact

        """
        self._connect()
        self.lp.parse('/msg [email protected] Message\n1')
        self.assertEqual(self.c(0)['body'], 'Message\n1')
        self.lp.parse('Message\t2')
        self.assertEqual(self.c(0)['body'], 'Message\t2')

    def testConnectionContext(self):
        self._connect()
        self._connect()
        self.lp.parse('/msg [email protected] Message')
        self.assertEqual(self.c(1)['target'], '[email protected]')

        self.lp.connection_context = 0
        self.lp.parse('/msg [email protected] Message')
        self.assertEqual(self.c(0)['target'], '[email protected]')

    def testCommandOutput(self):
        self._connect()
        self.assertEqual(len(self.output.queue), 1)
        self.assertEqual(self.output.queue[-1][0], LineParser.INFO)
        self.assertEqual(self.output.queue[-1][1], 0)
        self.assertEqual(self.output.queue[-1][2], None)
        self.assertEqual(self.output.queue[-1][3], 'Connected!')

        self.lp.parse('/msg [email protected] Message')
        self.assertEqual(len(self.output.queue), 2)
        self.assertEqual(self.output.queue[-1][0], LineParser.MESSAGE_SENT)
        self.assertEqual(self.output.queue[-1][1], 0)
        self.assertEqual(self.output.queue[-1][2], '[email protected]')
        self.assertEqual(self.output.queue[-1][3], 'Message')

    def testAccountsCommand(self):
        self._connect('[email protected]')

        self.lp.parse('/accounts')
        self.assertEqual(len(self.output.queue), 2)
        self.assertEqual(self.output.queue[-1][0], LineParser.INFO)
        self.assertEqual(self.output.queue[-1][1], None)
        self.assertEqual(self.output.queue[-1][2], None)
        self.assertEqual(self.output.queue[-1][3], 'Accounts:\n0: [email protected]')

    def testFailure(self):
        self.lp.parse('/accounts')
        self.assertEqual(len(self.output.queue), 1)
        self.assertEqual(self.output.queue[-1][0], LineParser.INFO)
        self.assertEqual(self.output.queue[-1][1], None)
        self.assertEqual(self.output.queue[-1][2], None)
        self.assertEqual(self.output.queue[-1][3], 'No accounts connected')

        self._connect('[email protected]')

        self.lp.parse('/hoobyfroob')
        self.assertEqual(len(self.output.queue), 3)
        self.assertEqual(self.output.queue[-1][0], LineParser.ERROR)
        self.assertEqual(self.output.queue[-1][1], 0)
        self.assertEqual(self.output.queue[-1][2], None)
        self.assertEqual(type(self.output.queue[-1][3]), logging.LogRecord)
        self.assertEqual(self.output.queue[-1][3].levelname, 'ERROR')
        self.assertEqual(self.output.queue[-1][3].msg,
                         'No such command: "hoobyfroob"')

        self.lp.parse('/msg [email protected]')
        self.assertEqual(len(self.output.queue), 4)
        self.assertEqual(self.output.queue[-1][0], LineParser.ERROR)
        self.assertEqual(self.output.queue[-1][1], 0)
        self.assertEqual(self.output.queue[-1][2], '[email protected]')
        self.assertEqual(type(self.output.queue[-1][3]), logging.LogRecord)
        self.assertEqual(self.output.queue[-1][3].levelname, 'ERROR')
        self.assertEqual(self.output.queue[-1][3].msg,
                'Usage: /msg [jid] [message]')