Ejemplo n.º 1
0
 def test_MessageObject_CanBeSerializedAndDeSerialized(self):
     m = chat.Message(user="******",
                      date=datetime.utcnow(),
                      message="unit test message",
                      room=self._roomName)
     chat.storeMessage(m)
     msg, users = getMessagesAndUsersFromJson(
         chat.getMessages(self._roomName))
     self.assertEqual(1, len(msg))
Ejemplo n.º 2
0
    def test_WhenPreviousDayWasMostRecentlyUse_GetMessagesUpdatesToTodayAndReturnsNewMessages(
            self):
        oneDayAgo = timedelta(days=-1)
        m = chat.Message(user="******",
                         date=datetime.utcnow() + oneDayAgo,
                         message="test",
                         room=self._roomName)
        chat._todaysKey = (datetime.utcnow() + oneDayAgo).strftime("%Y_%m_%d")
        chat.storeMessage(m)
        chat._todaysKey = (datetime.utcnow() + oneDayAgo).strftime("%Y_%m_%d")

        m = chat.Message(user="******",
                         date=datetime.utcnow(),
                         message="test 1",
                         room=self._roomName)
        chat.storeMessage(m)
        msg, _ = getMessagesAndUsersFromJson(chat.getMessages(self._roomName))
        self.assertEqual(1, len(msg))
Ejemplo n.º 3
0
    def test_ReadMessage_ThenPost_ThenReadMessage_ReturnsAMessage(self):
        chat.getMessages(self._roomName)
        m = chat.Message(user="******",
                         date=datetime.utcnow(),
                         message="test message",
                         room=self._roomName)
        chat.storeMessage(m)

        msg, users = getMessagesAndUsersFromJson(
            chat.getMessages(self._roomName))
        self.assertEqual(1, len(msg))
Ejemplo n.º 4
0
    def test_LinkInMessage_IsConvertedToHtmlLink(self):
        m = chat.Message(user="******",
                         message="http://www.google.com",
                         room=self._roomName)
        chat.storeMessage(m)

        msg, users = getMessagesAndUsersFromJson(
            chat.getMessages(self._roomName))
        self.assertEqual(1, len(msg))
        self.assertEqual(
            "<a href=\"http://www.google.com\" target=\"_blank\" tabindex=\"-1\">http://www.google.com</a>",
            msg[0].message)
Ejemplo n.º 5
0
 def test_ReadMessage_DoesNotReturnMessages24HoursOld(self):
     today = datetime.utcnow()
     twentyFourHours = timedelta(days=-1)
     m = chat.Message(user="******",
                      date=today + twentyFourHours,
                      message="test message",
                      room=self._roomName)
     yesterday = today + twentyFourHours
     chat._todaysKey = yesterday.strftime("%Y_%m_%d")
     chat.storeMessage(m)
     chat._todaysKey = yesterday.strftime("%Y_%m_%d")
     msg, users = getMessagesAndUsersFromJson(
         chat.getMessages(self._roomName))
     self.assertEqual(0, len(msg))
     self.assertEqual(today.strftime("%Y_%m_%d"), chat._todaysKey)
Ejemplo n.º 6
0
    def test_ComplexLinkInMessage_IsConvertedToHtmlLink(self):
        m = chat.Message(
            user="******",
            message=
            "http://johnbragg.smugmug.com/Other/ChoreoBusiness-Furniture/37756450_cFGjCq#!i=3127167673&k=cgrFStR",
            room=self._roomName)
        chat.storeMessage(m)

        msg, users = getMessagesAndUsersFromJson(
            chat.getMessages(self._roomName))
        self.assertEqual(1, len(msg))
        self.assertEqual(
            "<a href=\"http://johnbragg.smugmug.com/Other/ChoreoBusiness-Furniture/37756450_cFGjCq#!i=3127167673&k=cgrFStR\" target=\"_blank\" tabindex=\"-1\">"
            +
            "http://johnbragg.smugmug.com/Other/ChoreoBusiness-Furniture/37756450_cFGjCq#!i=3127167673&k=cgrFStR</a>",
            msg[0].message)
Ejemplo n.º 7
0
    def test_UserIsLoggedIn_WhileTheyAreTyping(self):
        today = datetime.utcnow()
        threeMinuteAgoDelta = timedelta(minutes=-3)
        u = chat.UserActivity(name="TestUser",
                              active=False,
                              date=today + threeMinuteAgoDelta,
                              room=self._roomName)
        chat.logUserActivity(u)

        m = chat.Message(user="******",
                         date=datetime.utcnow(),
                         message="test message",
                         room=self._roomName)
        chat.storeMessage(m)

        msg, users = getMessagesAndUsersFromJson(
            chat.getMessages(self._roomName))
        self.assertEqual(1, len(users))
Ejemplo n.º 8
0
    def on_message(self, message):
        try:
            message = json.loads(message)
            type = message["type"]

            if type == 0:
                # chats[self.chat_id].new_message(message)
                content = message["content"]
                name = content["name"]
                new_chat = bool(content["newChat"])
                chat_id = newChatString(
                ) if new_chat else content["chatid"].upper()

                temp_user = chat.User(name, self, "")
                temp_sys = chat.User("SYS", None, "")

                response = {}

                if new_chat and (chat_id in chats):
                    response["message"] = "Chat already exists"
                    chat.Message(0, temp_sys, [temp_user], response).send()
                elif not new_chat and chat_id not in chats:
                    response["message"] = "Chat Does Not Exist"
                    chat.Message(0, temp_sys, [temp_user], response).send()
                elif not new_chat and chats[chat_id].user_exists(temp_user):
                    response['message'] = "Username taken in the chat"
                    chat.Message(0, temp_sys, [temp_user], response).send()
                elif not new_chat and chats[chat_id].locked:
                    response['message'] = "Chat is locked"
                    chat.Message(0, temp_sys, [temp_user], response).send()
                else:
                    self.name = name
                    self.chat_id = chat_id

                    if new_chat:
                        chats[self.chat_id] = chat.ChatRoom()

                    # Else chat id is already valid and the chat exists, the user will be added in the next step

                    response['message'] = "Success"
                    response['name'] = self.name
                    response['chatid'] = self.chat_id
                    chat.Message(0, temp_sys, [temp_user], response).send()

            elif self.chat_id in chats:
                if type == 1:
                    user_dict = message["content"]
                    role = "host" if len(
                        chats[self.chat_id].users) == 0 else "member"
                    user = chat.User(user_dict["name"],
                                     self,
                                     user_dict["public_key"],
                                     role=role)
                    chats[self.chat_id].add_user(user)
                elif type == 2:  # User can't send system messages
                    pass
                elif type == 3:
                    chats[self.chat_id].new_message(message)
                elif type == 4:
                    chats[self.chat_id].run_command(message["content"],
                                                    message["sender"])
                elif type == 5:
                    chats[self.chat_id].del_user(self.name)
                    if len(chats[self.chat_id].users) <= 0:
                        try:
                            chats.pop(self.chat_id)
                        except KeyError as e:
                            print(e)

                    self.name = ""
                    self.chat_id = ""

        except Exception as e:
            print(e)
Ejemplo n.º 9
0
 def test_MessageObject_DoesNotReturnHTMLInMessagePropertyWhenAssigned(
         self):
     msg = chat.Message()
     msg.message = "<html>"
     self.assertEqual("&lt;html&gt;", msg.message)
Ejemplo n.º 10
0
 def test_MessageObject_DoesNotReturnHTMLInMessagePropertyWhenConstructedWithHTML(
         self):
     msg = chat.Message(message="<html>")
     self.assertEqual("&lt;html&gt;", msg.message)