def render_chat_box(self, chat_id, box_id, tzoffset): """ """ chat_type, audience = chat_id.split("_", 1) response = utils.get_last_conversation(self.context, audience, chat_type) if response["status"] == config.AUTH_FAIL: return elif response["status"] != config.SUCCESS: if response.get("errmsg"): log.warn(response["errmsg"]) messages = {} return elif chat_type == "chatroom": messages = response["chatroom_messages"] else: messages = response["messages"] messages = self._to_local_timezone(messages, tzoffset) return self.template( messages=messages, audience=audience, last_msg_date=response["last_msg_date"], box_id=box_id, chat_type=chat_type, chat_id=chat_id, )
def _test_messaging(self, username1, username2): """ Base class, to allow different usernames (email vs normal) to be used. """ portal = self.portal traverse = portal.restrictedTraverse # Make sure username1 is registered on the chatserver by calling # initialize self.login(name=username1) resp = traverse('@@babblechat/initialize')() resp = json.loads(resp) self.assertEquals(resp['status'], config.SUCCESS) # Test some methods' handling of anon users self.logout() resp = traverse('@@babblechat/initialize')() resp = json.loads(resp) self.assertEquals(resp['status'], config.AUTH_FAIL) member = self.mtool.getAuthenticatedMember() resp = traverse('@@babblechat/poll')(member.getId(), config.NULL_DATE) resp = json.loads(resp) self.assertEquals(resp['status'], config.AUTH_FAIL) resp = traverse('@@babblechat/send_message')(username1, 'message') resp = json.loads(resp) self.assertEquals(resp['status'], config.AUTH_FAIL) resp = traverse('@@babblechat/clear_messages')(username1, 'chatbox', datetime.datetime.now(utc).isoformat()) resp = json.loads(resp) self.assertEquals(resp['status'], config.AUTH_FAIL) messages = utils.get_last_conversation(portal, username1) self.assertEquals(messages['status'], config.AUTH_FAIL) # Test methods' response to a user ('portal_owner') who wasn't # initialized self.logout() self.loginAsPortalOwner() member = self.mtool.getAuthenticatedMember() resp = traverse('@@babblechat/poll')(member.getId(), config.NULL_DATE) resp = json.loads(resp) self.assertEquals(resp['status'], config.AUTH_FAIL) resp = traverse('@@babblechat/send_message')(username1, 'message') resp = json.loads(resp) self.assertEquals(resp['status'], config.AUTH_FAIL) method = traverse('@@babblechat/clear_messages') pars = [username1, 'chatbox', datetime.datetime.now(utc)] self.assertRaises(AttributeError, method, *pars) messages = utils.get_last_conversation(portal, username1) self.assertEquals(messages['status'], config.ERROR) # Make sure username2 is registered on the chatserver by calling # initialize self.login(name=username2) resp = traverse('@@babblechat/initialize')() resp = json.loads(resp) self.assertEquals(resp['status'], config.SUCCESS) # Send a message from username2 to username1 and note the time so that we # can test for it later on timeminutes = datetime.datetime.now(utc).strftime("%H:%M") resp = traverse('@@babblechat/send_message')(username1, 'hello') resp = json.loads(resp) self.assertEquals(resp['status'], config.SUCCESS) self.assertEqual(bool(RE.search(resp['last_msg_date'])), True) message_timestamp = resp['last_msg_date'] # Poll for username1 and see if we got our message self.login(name=username1) resp = traverse('@@babblechat/poll')(username1, config.NULL_DATE) resp = json.loads(resp) self.assertEquals(resp['status'], config.SUCCESS) self.assertEquals(resp['last_msg_date'], message_timestamp) # Check that a message was received from username2 self.assertEquals(resp['messages'].keys(), [username2]) # Check the message format hello_message = [username2, 'hello', 'dummydate'] member2 = self.mtool.getMemberById(username2) # { # 'chatroom_messages': {}, # 'last_msg_date': '2011-11-19T12:43:34.922511+00:00', # 'messages': { # 'member2': [['member2', 'hello', '2011-11-19T12:43:34.922511+00:00', 'Member2']] # }, # 'status': 0 # } self.assertEquals(resp['messages'][username2][0][-1], member2.getProperty('fullname', username2).decode('utf-8')) self.assertEquals(resp['messages'][username2][0][0], hello_message[0]) self.assertEquals(resp['messages'][username2][0][1], hello_message[1]) # Check that the last item in the message tuple is an iso8601 timestamp self.assertEqual(bool(RE.search(resp['messages'][username2][0][2])), True) self.assertEqual(resp['messages'][username2][0][2], message_timestamp) # Check that the next poll (with new timestamp) returns no new messages resp = traverse('@@babblechat/poll')(username1, message_timestamp) resp = json.loads(resp) self.assertEquals(resp['status'], config.SUCCESS) self.assertEquals(resp['messages'], {}) # Check that calling get_uncleared_messages will return the original message, # since it has not been cleared resp = traverse('@@babblechat/get_uncleared_messages')() resp = json.loads(resp) messages = resp['messages'] self.assertEquals(resp['status'], config.SUCCESS) self.assertEquals(resp['messages'][username2][0][-1], member2.getProperty('fullname', username2).decode('utf-8')) self.assertEquals(resp['messages'][username2][0][0], hello_message[0]) self.assertEquals(resp['messages'][username2][0][1], hello_message[1]) # Check that the last item in the message tuple is an iso8601 timestamp self.assertEqual(bool(RE.search(resp['messages'][username2][0][2])), True) self.assertEqual(resp['messages'][username2][0][2], message_timestamp) # Also test that utils' get_last_conversation returns this message messages = utils.get_last_conversation(portal, username2) self.assertEquals(messages.keys(), ['status', 'messages', 'last_msg_date', 'chatroom_messages']) self.assertEquals(messages['status'], config.SUCCESS) self.assertEquals(resp['messages'][username2][0][0], hello_message[0]) self.assertEquals(resp['messages'][username2][0][1], hello_message[1]) self.assertEqual(bool(RE.search(resp['messages'][username2][0][2])), True) self.assertEqual(resp['messages'][username2][0][2], message_timestamp) # Now we clear the messages resp = traverse('@@babblechat/clear_messages')(username2, 'chatbox', datetime.datetime.now(utc).isoformat()) resp = json.loads(resp) self.assertEquals(resp['status'], config.SUCCESS) messages = utils.get_last_conversation(portal, username2) check = {'status': config.SUCCESS, 'last_msg_date': message_timestamp, 'messages':{}, 'chatroom_messages': {}} self.assertEquals(messages, check) resp = json.loads(traverse('@@babblechat/get_uncleared_messages')()) self.assertEquals(resp['status'], config.SUCCESS) self.assertEquals(resp['messages'], {}) resp = traverse('@@babblechat/initialize')() resp = json.loads(resp) self.assertEquals(resp['status'], config.SUCCESS) # Test method's response to wrong authentication member = self.mtool.getAuthenticatedMember() member.chatpass = '******' resp = traverse('@@babblechat/send_message')(username2, 'message') resp = json.loads(resp) self.assertEquals(resp['status'], config.AUTH_FAIL) resp = traverse('@@babblechat/clear_messages')(username2, 'chatbox', datetime.datetime.now(utc).isoformat()) resp = json.loads(resp) self.assertEquals(resp['status'], config.AUTH_FAIL) resp = json.loads(traverse('@@babblechat/get_uncleared_messages')(username2)) self.assertEquals(resp['status'], config.AUTH_FAIL)