| 1 | import unittest |
|---|
| 2 | |
|---|
| 3 | import time |
|---|
| 4 | |
|---|
| 5 | import lib |
|---|
| 6 | lib.setup_env() |
|---|
| 7 | |
|---|
| 8 | from common import gajim |
|---|
| 9 | from common import xmpp |
|---|
| 10 | |
|---|
| 11 | from mock import Mock, expectParams |
|---|
| 12 | from mocks import * |
|---|
| 13 | |
|---|
| 14 | from common.stanza_session import StanzaSession |
|---|
| 15 | |
|---|
| 16 | # name to use for the test account |
|---|
| 17 | account_name = 'test' |
|---|
| 18 | |
|---|
| 19 | class TestStanzaSession(unittest.TestCase): |
|---|
| 20 | def setUp(self): |
|---|
| 21 | self.jid = 'test@example.org/Gajim' |
|---|
| 22 | self.conn = MockConnection(account_name, {'send_stanza': None}) |
|---|
| 23 | self.sess = StanzaSession(self.conn, self.jid, None, 'chat') |
|---|
| 24 | |
|---|
| 25 | def test_generate_thread_id(self): |
|---|
| 26 | # thread_id is a string |
|---|
| 27 | self.assert_(isinstance(self.sess.thread_id, str)) |
|---|
| 28 | |
|---|
| 29 | # it should be somewhat long, to avoid clashes |
|---|
| 30 | self.assert_(len(self.sess.thread_id) >= 32) |
|---|
| 31 | |
|---|
| 32 | def test_is_loggable(self): |
|---|
| 33 | # by default a session should be loggable |
|---|
| 34 | # (unless the no_log_for setting says otherwise) |
|---|
| 35 | self.assert_(self.sess.is_loggable()) |
|---|
| 36 | |
|---|
| 37 | def test_terminate(self): |
|---|
| 38 | # termination is sent by default |
|---|
| 39 | self.sess.last_send = time.time() |
|---|
| 40 | self.sess.terminate() |
|---|
| 41 | |
|---|
| 42 | self.assertEqual(None, self.sess.status) |
|---|
| 43 | |
|---|
| 44 | calls = self.conn.mockGetNamedCalls('send_stanza') |
|---|
| 45 | msg = calls[0].getParam(0) |
|---|
| 46 | |
|---|
| 47 | self.assertEqual(msg.getThread(), self.sess.thread_id) |
|---|
| 48 | |
|---|
| 49 | def test_terminate_without_sending(self): |
|---|
| 50 | # no termination is sent if no messages have been sent in the session |
|---|
| 51 | self.sess.terminate() |
|---|
| 52 | |
|---|
| 53 | self.assertEqual(None, self.sess.status) |
|---|
| 54 | |
|---|
| 55 | calls = self.conn.mockGetNamedCalls('send_stanza') |
|---|
| 56 | self.assertEqual(0, len(calls)) |
|---|
| 57 | |
|---|
| 58 | def test_terminate_no_remote_xep_201(self): |
|---|
| 59 | # no termination is sent if only messages without thread ids have been |
|---|
| 60 | # received |
|---|
| 61 | self.sess.last_send = time.time() |
|---|
| 62 | self.sess.last_receive = time.time() |
|---|
| 63 | self.sess.terminate() |
|---|
| 64 | |
|---|
| 65 | self.assertEqual(None, self.sess.status) |
|---|
| 66 | |
|---|
| 67 | calls = self.conn.mockGetNamedCalls('send_stanza') |
|---|
| 68 | self.assertEqual(0, len(calls)) |
|---|
| 69 | |
|---|
| 70 | from session import ChatControlSession |
|---|
| 71 | |
|---|
| 72 | gajim.interface = MockInterface() |
|---|
| 73 | |
|---|
| 74 | import notify |
|---|
| 75 | |
|---|
| 76 | class TestChatControlSession(unittest.TestCase): |
|---|
| 77 | def setUp(self): |
|---|
| 78 | self.jid = 'test@example.org/Gajim' |
|---|
| 79 | self.conn = MockConnection(account_name, {'send_stanza': None}) |
|---|
| 80 | self.sess = ChatControlSession(self.conn, self.jid, None) |
|---|
| 81 | gajim.logger = MockLogger() |
|---|
| 82 | |
|---|
| 83 | # initially there are no events |
|---|
| 84 | self.assertEqual(0, len(gajim.events.get_events(account_name))) |
|---|
| 85 | |
|---|
| 86 | # no notifications have been sent |
|---|
| 87 | self.assertEqual(0, len(notify.notifications)) |
|---|
| 88 | |
|---|
| 89 | def tearDown(self): |
|---|
| 90 | # remove all events and notifications that were added |
|---|
| 91 | gajim.events._events = {} |
|---|
| 92 | notify.notifications = [] |
|---|
| 93 | |
|---|
| 94 | def receive_chat_msg(self, jid, msgtxt): |
|---|
| 95 | '''simulate receiving a chat message from jid''' |
|---|
| 96 | msg = xmpp.Message() |
|---|
| 97 | msg.setBody(msgtxt) |
|---|
| 98 | msg.setType('chat') |
|---|
| 99 | |
|---|
| 100 | tim = time.localtime() |
|---|
| 101 | encrypted = False |
|---|
| 102 | self.sess.received(jid, msgtxt, tim, encrypted, msg) |
|---|
| 103 | |
|---|
| 104 | # ----- custom assertions ----- |
|---|
| 105 | def assert_new_message_notification(self): |
|---|
| 106 | '''a new_message notification has been sent''' |
|---|
| 107 | self.assertEqual(1, len(notify.notifications)) |
|---|
| 108 | notif = notify.notifications[0] |
|---|
| 109 | self.assertEqual('new_message', notif[0]) |
|---|
| 110 | |
|---|
| 111 | def assert_first_message_notification(self): |
|---|
| 112 | '''this message was treated as a first message''' |
|---|
| 113 | self.assert_new_message_notification() |
|---|
| 114 | notif = notify.notifications[0] |
|---|
| 115 | params = notif[3] |
|---|
| 116 | first = params[1] |
|---|
| 117 | self.assert_(first, 'message should have been treated as a first message') |
|---|
| 118 | |
|---|
| 119 | def assert_not_first_message_notification(self): |
|---|
| 120 | '''this message was not treated as a first message''' |
|---|
| 121 | self.assert_new_message_notification() |
|---|
| 122 | notif = notify.notifications[0] |
|---|
| 123 | params = notif[3] |
|---|
| 124 | first = params[1] |
|---|
| 125 | self.assert_(not first, |
|---|
| 126 | 'message was unexpectedly treated as a first message') |
|---|
| 127 | |
|---|
| 128 | # ----- tests ----- |
|---|
| 129 | def test_receive_nocontrol(self): |
|---|
| 130 | '''test receiving a message in a blank state''' |
|---|
| 131 | jid = 'bct@necronomicorp.com/Gajim' |
|---|
| 132 | msgtxt = 'testing one two three' |
|---|
| 133 | |
|---|
| 134 | self.receive_chat_msg(jid, msgtxt) |
|---|
| 135 | |
|---|
| 136 | # message was logged |
|---|
| 137 | calls = gajim.logger.mockGetNamedCalls('write') |
|---|
| 138 | self.assertEqual(1, len(calls)) |
|---|
| 139 | |
|---|
| 140 | # no ChatControl was open and autopopup was off |
|---|
| 141 | # so the message goes into the event queue |
|---|
| 142 | self.assertEqual(1, len(gajim.events.get_events(account_name))) |
|---|
| 143 | |
|---|
| 144 | self.assert_first_message_notification() |
|---|
| 145 | |
|---|
| 146 | # no control is attached to the session |
|---|
| 147 | self.assertEqual(None, self.sess.control) |
|---|
| 148 | |
|---|
| 149 | def test_receive_already_has_control(self): |
|---|
| 150 | '''test receiving a message with a session already attached to a |
|---|
| 151 | control''' |
|---|
| 152 | |
|---|
| 153 | jid = 'bct@necronomicorp.com/Gajim' |
|---|
| 154 | msgtxt = 'testing one two three' |
|---|
| 155 | |
|---|
| 156 | self.sess.control = MockChatControl(jid, account_name) |
|---|
| 157 | |
|---|
| 158 | self.receive_chat_msg(jid, msgtxt) |
|---|
| 159 | |
|---|
| 160 | # message was logged |
|---|
| 161 | calls = gajim.logger.mockGetNamedCalls('write') |
|---|
| 162 | self.assertEqual(1, len(calls)) |
|---|
| 163 | |
|---|
| 164 | # the message does not go into the event queue |
|---|
| 165 | self.assertEqual(0, len(gajim.events.get_events(account_name))) |
|---|
| 166 | |
|---|
| 167 | self.assert_not_first_message_notification() |
|---|
| 168 | |
|---|
| 169 | # message was printed to the control |
|---|
| 170 | calls = self.sess.control.mockGetNamedCalls('print_conversation') |
|---|
| 171 | self.assertEqual(1, len(calls)) |
|---|
| 172 | |
|---|
| 173 | def test_received_orphaned_control(self): |
|---|
| 174 | '''test receiving a message when a control that doesn't have a session |
|---|
| 175 | attached exists''' |
|---|
| 176 | |
|---|
| 177 | jid = 'bct@necronomicorp.com' |
|---|
| 178 | fjid = jid + '/Gajim' |
|---|
| 179 | msgtxt = 'testing one two three' |
|---|
| 180 | |
|---|
| 181 | ctrl = MockChatControl(jid, account_name) |
|---|
| 182 | gajim.interface.msg_win_mgr = Mock({'get_control': ctrl}) |
|---|
| 183 | gajim.interface.msg_win_mgr.mockSetExpectation('get_control', |
|---|
| 184 | expectParams(jid, account_name)) |
|---|
| 185 | |
|---|
| 186 | self.receive_chat_msg(fjid, msgtxt) |
|---|
| 187 | |
|---|
| 188 | # message was logged |
|---|
| 189 | calls = gajim.logger.mockGetNamedCalls('write') |
|---|
| 190 | self.assertEqual(1, len(calls)) |
|---|
| 191 | |
|---|
| 192 | # the message does not go into the event queue |
|---|
| 193 | self.assertEqual(0, len(gajim.events.get_events(account_name))) |
|---|
| 194 | |
|---|
| 195 | self.assert_not_first_message_notification() |
|---|
| 196 | |
|---|
| 197 | # this session is now attached to that control |
|---|
| 198 | self.assertEqual(self.sess, ctrl.session) |
|---|
| 199 | self.assertEqual(ctrl, self.sess.control, 'foo') |
|---|
| 200 | |
|---|
| 201 | # message was printed to the control |
|---|
| 202 | calls = ctrl.mockGetNamedCalls('print_conversation') |
|---|
| 203 | self.assertEqual(1, len(calls)) |
|---|
| 204 | |
|---|
| 205 | if __name__ == '__main__': |
|---|
| 206 | unittest.main() |
|---|
| 207 | |
|---|
| 208 | # vim: se ts=3: |
|---|