2008-05-29 04:49:03 +02:00
|
|
|
import unittest
|
|
|
|
|
2008-06-04 01:54:22 +02:00
|
|
|
import time
|
|
|
|
|
2008-08-09 02:24:08 +02:00
|
|
|
import lib
|
|
|
|
lib.setup_env()
|
2008-06-12 05:56:30 +02:00
|
|
|
|
2009-11-05 09:06:46 +01:00
|
|
|
|
|
|
|
import notify
|
|
|
|
|
2008-05-29 04:49:03 +02:00
|
|
|
from common import gajim
|
2013-04-07 23:41:15 +02:00
|
|
|
from common import nec
|
|
|
|
from common import ged
|
|
|
|
from common.nec import NetworkEvent
|
|
|
|
from common.connection_handlers_events import MessageReceivedEvent
|
|
|
|
from common.connection_handlers_events import DecryptedMessageReceivedEvent
|
|
|
|
import nbxmpp
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2009-11-05 09:06:46 +01:00
|
|
|
from common.stanza_session import StanzaSession
|
|
|
|
from session import ChatControlSession
|
2013-04-07 23:41:15 +02:00
|
|
|
from roster_window import RosterWindow
|
2009-11-05 09:06:46 +01:00
|
|
|
|
2008-06-04 01:54:22 +02:00
|
|
|
from mock import Mock, expectParams
|
2009-01-11 14:49:03 +01:00
|
|
|
from gajim_mocks import *
|
2008-06-04 01:54:22 +02:00
|
|
|
|
2009-11-05 09:06:46 +01:00
|
|
|
gajim.interface = MockInterface()
|
|
|
|
|
2008-05-29 04:49:03 +02:00
|
|
|
|
|
|
|
# name to use for the test account
|
|
|
|
account_name = 'test'
|
|
|
|
|
|
|
|
class TestStanzaSession(unittest.TestCase):
|
2010-02-08 15:08:40 +01:00
|
|
|
''' Testclass for common/stanzasession.py '''
|
2009-11-05 09:06:46 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def setUp(self):
|
2013-04-07 23:41:15 +02:00
|
|
|
self.jid = nbxmpp.JID('test@example.org/Gajim')
|
2010-02-08 15:08:40 +01:00
|
|
|
self.conn = MockConnection(account_name, {'send_stanza': None})
|
|
|
|
self.sess = StanzaSession(self.conn, self.jid, None, 'chat')
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def test_generate_thread_id(self):
|
|
|
|
# thread_id is a string
|
|
|
|
self.assert_(isinstance(self.sess.thread_id, str))
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# it should be somewhat long, to avoid clashes
|
|
|
|
self.assert_(len(self.sess.thread_id) >= 32)
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def test_is_loggable(self):
|
|
|
|
# by default a session should be loggable
|
|
|
|
# (unless the no_log_for setting says otherwise)
|
|
|
|
self.assert_(self.sess.is_loggable())
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def test_terminate(self):
|
|
|
|
# termination is sent by default
|
|
|
|
self.sess.last_send = time.time()
|
|
|
|
self.sess.terminate()
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.assertEqual(None, self.sess.status)
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
calls = self.conn.mockGetNamedCalls('send_stanza')
|
|
|
|
msg = calls[0].getParam(0)
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.assertEqual(msg.getThread(), self.sess.thread_id)
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def test_terminate_without_sending(self):
|
|
|
|
# no termination is sent if no messages have been sent in the session
|
|
|
|
self.sess.terminate()
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.assertEqual(None, self.sess.status)
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
calls = self.conn.mockGetNamedCalls('send_stanza')
|
|
|
|
self.assertEqual(0, len(calls))
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def test_terminate_no_remote_xep_201(self):
|
|
|
|
# no termination is sent if only messages without thread ids have been
|
|
|
|
# received
|
|
|
|
self.sess.last_send = time.time()
|
|
|
|
self.sess.last_receive = time.time()
|
|
|
|
self.sess.terminate()
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.assertEqual(None, self.sess.status)
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
calls = self.conn.mockGetNamedCalls('send_stanza')
|
|
|
|
self.assertEqual(0, len(calls))
|
2008-05-29 04:49:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestChatControlSession(unittest.TestCase):
|
2010-02-08 15:08:40 +01:00
|
|
|
''' Testclass for session.py '''
|
2009-11-05 09:06:46 +01:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
gajim.nec = nec.NetworkEventsController()
|
|
|
|
cls.conn = MockConnection(account_name, {'send_stanza': None})
|
2010-02-08 15:08:40 +01:00
|
|
|
gajim.logger = MockLogger()
|
2013-04-07 23:41:15 +02:00
|
|
|
gajim.default_session_type = ChatControlSession
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
def setUp(self):
|
|
|
|
gajim.notification = notify.Notification()
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# no notifications have been sent
|
|
|
|
self.assertEqual(0, len(notify.notifications))
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def tearDown(self):
|
2013-04-07 23:41:15 +02:00
|
|
|
gajim.notification.clean()
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def receive_chat_msg(self, jid, msgtxt):
|
|
|
|
'''simulate receiving a chat message from jid'''
|
2013-04-07 23:41:15 +02:00
|
|
|
msg = nbxmpp.Message()
|
2010-02-08 15:08:40 +01:00
|
|
|
msg.setBody(msgtxt)
|
|
|
|
msg.setType('chat')
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
tim = time.localtime()
|
|
|
|
encrypted = False
|
2013-04-07 23:41:15 +02:00
|
|
|
xml = """<message from='%s' id='1' type='chat'><body>%s</body>
|
|
|
|
<thread>123</thread></message>""" % (jid, msgtxt)
|
|
|
|
stanza = nbxmpp.protocol.Message(node=nbxmpp.simplexml.XML2Node(xml))
|
|
|
|
self.conn._messageCB(None, stanza)
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# ----- custom assertions -----
|
|
|
|
def assert_new_message_notification(self):
|
|
|
|
'''a new_message notification has been sent'''
|
|
|
|
self.assertEqual(1, len(notify.notifications))
|
2013-04-07 23:41:15 +02:00
|
|
|
notif = notify.notifications[-1]
|
|
|
|
self.assertEqual('New Message', notif.popup_event_type)
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def assert_first_message_notification(self):
|
|
|
|
'''this message was treated as a first message'''
|
|
|
|
self.assert_new_message_notification()
|
2013-04-07 23:41:15 +02:00
|
|
|
notif = notify.notifications[-1]
|
|
|
|
first = notif.first_unread
|
|
|
|
self.assert_(first,
|
|
|
|
'message should have been treated as a first message')
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def assert_not_first_message_notification(self):
|
|
|
|
'''this message was not treated as a first message'''
|
|
|
|
self.assert_new_message_notification()
|
2013-04-07 23:41:15 +02:00
|
|
|
notif = notify.notifications[-1]
|
|
|
|
first = notif.first_unread
|
2010-02-08 15:08:40 +01:00
|
|
|
self.assert_(not first,
|
2013-04-07 23:41:15 +02:00
|
|
|
'message was unexpectedly treated as a first message')
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# ----- tests -----
|
2013-04-07 23:41:15 +02:00
|
|
|
def test_receive_1nocontrol(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
'''test receiving a message in a blank state'''
|
2013-04-07 23:41:15 +02:00
|
|
|
jid = 'bct@necronomicorp.com'
|
|
|
|
fjid = 'bct@necronomicorp.com/Gajim'
|
|
|
|
msgtxt = 'testing one'
|
|
|
|
|
|
|
|
self.receive_chat_msg(fjid, msgtxt)
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
# session is created
|
|
|
|
self.assert_((jid in self.conn.sessions) and (
|
|
|
|
'123' in self.conn.sessions[jid]), 'session is not created')
|
|
|
|
sess = self.conn.sessions[jid]['123']
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# message was logged
|
|
|
|
calls = gajim.logger.mockGetNamedCalls('write')
|
|
|
|
self.assertEqual(1, len(calls))
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# no ChatControl was open and autopopup was off
|
|
|
|
# so the message goes into the event queue
|
|
|
|
self.assertEqual(1, len(gajim.events.get_events(account_name)))
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.assert_first_message_notification()
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# no control is attached to the session
|
2013-04-07 23:41:15 +02:00
|
|
|
self.assertEqual(None, sess.control)
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
def test_receive_2already_has_control(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
'''test receiving a message with a session already attached to a
|
|
|
|
control'''
|
2013-04-07 23:41:15 +02:00
|
|
|
jid = 'bct@necronomicorp.com'
|
|
|
|
fjid = 'bct@necronomicorp.com/Gajim'
|
|
|
|
msgtxt = 'testing two'
|
|
|
|
roster = RosterWindow()
|
2008-06-04 01:54:22 +02:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
sess = self.conn.sessions[jid]['123']
|
|
|
|
sess.control = MockChatControl(fjid, account_name)
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
self.receive_chat_msg(fjid, msgtxt)
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# message was logged
|
|
|
|
calls = gajim.logger.mockGetNamedCalls('write')
|
2013-04-07 23:41:15 +02:00
|
|
|
self.assertEqual(2, len(calls))
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# the message does not go into the event queue
|
2013-04-07 23:41:15 +02:00
|
|
|
self.assertEqual(1, len(gajim.events.get_events(account_name)))
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.assert_not_first_message_notification()
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# message was printed to the control
|
2013-04-07 23:41:15 +02:00
|
|
|
calls = sess.control.mockGetNamedCalls('print_conversation')
|
2010-02-08 15:08:40 +01:00
|
|
|
self.assertEqual(1, len(calls))
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
#def test_received_3orphaned_control(self):
|
|
|
|
#'''test receiving a message when a control that doesn't have a session
|
|
|
|
#attached exists'''
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
#jid = 'bct@necronomicorp.com'
|
|
|
|
#fjid = jid + '/Gajim'
|
|
|
|
#msgtxt = 'testing three'
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
#ctrl = MockChatControl(jid, account_name)
|
|
|
|
#gajim.interface.msg_win_mgr = Mock({'get_control': ctrl})
|
|
|
|
#gajim.interface.msg_win_mgr.mockSetExpectation('get_control',
|
|
|
|
#expectParams(jid, account_name))
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
#self.receive_chat_msg(fjid, msgtxt)
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
## message was logged
|
|
|
|
#calls = gajim.logger.mockGetNamedCalls('write')
|
|
|
|
#self.assertEqual(1, len(calls))
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
## the message does not go into the event queue
|
|
|
|
#self.assertEqual(0, len(gajim.events.get_events(account_name)))
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
#self.assert_not_first_message_notification()
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
## this session is now attached to that control
|
|
|
|
#self.assertEqual(self.sess, ctrl.session)
|
|
|
|
#self.assertEqual(ctrl, self.sess.control, 'foo')
|
2008-05-29 04:49:03 +02:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
## message was printed to the control
|
|
|
|
#calls = ctrl.mockGetNamedCalls('print_conversation')
|
|
|
|
#self.assertEqual(1, len(calls))
|
2008-05-29 04:49:03 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|