gajim-plural/test/unit/test_sessions.py

175 lines
5.6 KiB
Python
Raw Normal View History

import unittest
import time
import lib
lib.setup_env()
2008-06-12 05:56:30 +02:00
import notify
from gajim.common import app
2017-08-12 02:39:55 +02:00
from gajim.common import nec
from gajim.common import ged
from gajim.common.nec import NetworkEvent
2018-07-19 21:26:45 +02:00
from gajim.common.modules.message import MessageReceivedEvent
2018-08-18 19:11:26 +02:00
from gajim.common.modules.message import DecryptedMessageReceivedEvent
2013-04-07 23:41:15 +02:00
import nbxmpp
2017-08-12 02:39:55 +02:00
from gajim.session import ChatControlSession
from gajim.roster_window import RosterWindow
from gajim_mocks import *
2017-09-15 15:11:55 +02:00
from data import account1
app.interface = MockInterface()
# name to use for the test account
2017-09-15 15:11:55 +02:00
account_name = account1
class TestChatControlSession(unittest.TestCase):
''' Testclass for session.py '''
2013-04-07 23:41:15 +02:00
@classmethod
def setUpClass(cls):
app.nec = nec.NetworkEventsController()
2013-04-07 23:41:15 +02:00
cls.conn = MockConnection(account_name, {'send_stanza': None})
app.logger = MockLogger()
app.default_session_type = ChatControlSession
2013-04-07 23:41:15 +02:00
def setUp(self):
app.notification = notify.Notification()
# no notifications have been sent
self.assertEqual(0, len(notify.notifications))
def tearDown(self):
app.notification.clean()
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()
msg.setBody(msgtxt)
msg.setType('chat')
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)
# ----- 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)
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
2017-09-10 12:21:10 +02:00
self.assertTrue(first,
2013-04-07 23:41:15 +02:00
'message should have been treated as a first message')
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
2017-09-10 12:21:10 +02:00
self.assertTrue(not first,
2013-04-07 23:41:15 +02:00
'message was unexpectedly treated as a first message')
# ----- tests -----
2013-04-07 23:41:15 +02:00
def test_receive_1nocontrol(self):
'''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)
2013-04-07 23:41:15 +02:00
# session is created
2017-09-10 12:21:10 +02:00
self.assertTrue((jid in self.conn.sessions) and (
2013-04-07 23:41:15 +02:00
'123' in self.conn.sessions[jid]), 'session is not created')
sess = self.conn.sessions[jid]['123']
# message was logged
calls = app.logger.mockGetNamedCalls('insert_into_logs')
self.assertEqual(1, len(calls))
# no ChatControl was open and autopopup was off
# so the message goes into the event queue
self.assertEqual(1, len(app.events.get_events(account_name)))
self.assert_first_message_notification()
# no control is attached to the session
2013-04-07 23:41:15 +02:00
self.assertEqual(None, sess.control)
2013-04-07 23:41:15 +02:00
def test_receive_2already_has_control(self):
'''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'
2017-09-15 15:11:55 +02:00
app.interface.roster = RosterWindow(app.app)
2013-04-07 23:41:15 +02:00
sess = self.conn.sessions[jid]['123']
sess.control = MockChatControl(fjid, account_name)
2013-04-07 23:41:15 +02:00
self.receive_chat_msg(fjid, msgtxt)
# message was logged
calls = app.logger.mockGetNamedCalls('insert_into_logs')
2013-04-07 23:41:15 +02:00
self.assertEqual(2, len(calls))
# the message does not go into the event queue
self.assertEqual(1, len(app.events.get_events(account_name)))
self.assert_not_first_message_notification()
# message was printed to the control
2013-04-07 23:41:15 +02:00
calls = sess.control.mockGetNamedCalls('print_conversation')
self.assertEqual(1, len(calls))
2017-09-15 15:11:55 +02:00
app.interface.roster.window.destroy()
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'''
2013-04-07 23:41:15 +02:00
#jid = 'bct@necronomicorp.com'
#fjid = jid + '/Gajim'
#msgtxt = 'testing three'
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))
2013-04-07 23:41:15 +02:00
#self.receive_chat_msg(fjid, msgtxt)
2013-04-07 23:41:15 +02:00
## message was logged
2017-08-09 15:34:58 +02:00
#calls = gajim.logger.mockGetNamedCalls('insert_into_logs')
2013-04-07 23:41:15 +02:00
#self.assertEqual(1, len(calls))
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)))
2013-04-07 23:41:15 +02:00
#self.assert_not_first_message_notification()
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')
2013-04-07 23:41:15 +02:00
## message was printed to the control
#calls = ctrl.mockGetNamedCalls('print_conversation')
#self.assertEqual(1, len(calls))
if __name__ == '__main__':
unittest.main()