Пример #1
0
 def test_thread_returned_with_thread_id_returns_404(self):
     """retrieves thread using id that doesn't exist"""
     with self.app.app_context():
         with current_app.test_request_context():
             with self.assertRaises(NotFound):
                 Retriever.retrieve_thread('anotherThreadId',
                                           self.user_respondent)
 def test_all_messages_in_conversation_marked_unread(self):
     # create a thread with two messages
     conversation_id = self.create_conversation_with_respondent_as_unread(
         user=self.user_respondent, message_count=2)
     with self.app.app_context():
         conversation = Retriever.retrieve_thread(conversation_id,
                                                  self.user_respondent)
         for msg in conversation.all():
             # as there's two ways that a message is unread, first check the `read at` time isn't set
             self.assertIsNone(msg.read_at)
             # now collect all the message labels
             labels = []
             for status in msg.statuses:
                 labels.append(status.label)
             # and check the unread is present
             self.assertTrue("UNREAD" in labels)
         # now mark the first message as read and check the whole conversation is now read
         Modifier.mark_message_as_read(
             conversation[0].serialize(self.user_respondent),
             self.user_respondent)
         con = Retriever.retrieve_thread(conversation_id,
                                         self.user_respondent)
         for msg in con.all():
             # message `read at` should now be set
             self.assertIsNotNone(msg.read_at)
             # collect the labels again
             labels = []
             for status in msg.statuses:
                 labels.append(status.label)
             # and there should be no unread
             self.assertFalse("UNREAD" in labels)
Пример #3
0
    def test_latest_message_from_each_thread_chosen_desc(self):
        """checks the message chosen for each thread is the latest message within that thread"""
        for _ in range(5):
            self.create_thread(no_of_messages=3)

        with self.app.app_context():
            with current_app.test_request_context():
                args = get_args(limit=MESSAGE_QUERY_LIMIT)
                response = Retriever.retrieve_thread_list(
                    self.user_internal, args)

                date = []
                thread_ids = []
                msg_ids = []
                for message in response.items:
                    serialized_msg = message.serialize(self.user_internal)
                    if 'sent_date' in serialized_msg:
                        date.append(serialized_msg['sent_date'])
                    elif 'modified_date' in serialized_msg:
                        date.append(serialized_msg['modified_date'])
                    thread_ids.append(serialized_msg['thread_id'])
                    msg_ids.append(serialized_msg['msg_id'])

                self.assertEqual(len(msg_ids), 5)

                args = get_args(page=1, limit=MESSAGE_QUERY_LIMIT)

                for x in range(0, len(thread_ids)):
                    thread = Retriever.retrieve_thread(thread_ids[x],
                                                       self.user_internal)
                    self.assertEqual(date[x],
                                     str(thread.all()[0].events[0].date_time))
                    self.assertEqual(msg_ids[x],
                                     thread.all()[0].events[0].msg_id)
Пример #4
0
    def test_all_msg_returned_for_thread_id(self):
        """retrieves messages for thread_id from database"""
        thread_id = self.create_thread(no_of_messages=6)

        with self.app.app_context():
            with current_app.test_request_context():
                response = Retriever.retrieve_thread(thread_id,
                                                     self.user_respondent)
                self.assertEqual(len(response.all()), 6)
Пример #5
0
    def test_thread_returned_in_desc_order(self):
        """check thread returned in correct order"""
        thread_id = self.create_thread(no_of_messages=6)

        with self.app.app_context():
            with current_app.test_request_context():
                response = Retriever.retrieve_thread(thread_id,
                                                     self.user_respondent)
                self.assertEqual(len(response.all()), 6)

                sent = [
                    str(message.events[0].date_time)
                    for message in response.all()
                ]

                desc_date = sorted(sent, reverse=True)
                self.assertEqual(len(sent), 6)
                self.assertListEqual(desc_date, sent)
    def test_read_date_is_set(self):
        """testing message read_date is set when unread label is removed"""
        thread_id = self.populate_database(1, mark_as_read=False)
        with self.app.app_context():
            thread = Retriever.retrieve_thread(thread_id,
                                               self.user_respondent).all()
            serialised_message = Retriever.retrieve_message(
                thread[0].msg_id, self.user_internal)
            Modifier.mark_message_as_read(serialised_message,
                                          self.user_internal)
            serialised_message = Retriever.retrieve_message(
                thread[0].msg_id, self.user_internal)
            db_message = SecureMessage.query.filter(
                SecureMessage.msg_id == serialised_message['msg_id']).one()

            self.assertIsNotNone(serialised_message['read_date'])
            self.assertTrue(isinstance(db_message.read_at, datetime.datetime))
            # Test that timestamp on read message is less than 3 seconds old to prove it
            # was only just created
            delta = datetime.datetime.utcnow() - db_message.read_at
            self.assertTrue(delta.total_seconds() < 3)
Пример #7
0
    def test_respondent_can_only_see_their_messages(self):
        """tests that a respondent can only see their messages i.e. they should not
        see any messages sent to another respondent"""
        first_respondent_thread_id = self.create_thread(
            no_of_messages=1,
            external_actor=self.default_external_actor,
            internal_actor=self.default_internal_actor)
        second_respondent_thread_id = self.create_thread(
            no_of_messages=1,
            external_actor=self.second_external_actor,
            internal_actor=self.default_internal_actor)

        with self.app.app_context():
            with current_app.test_request_context():
                args = get_args()
                first_respondent_thread_list = Retriever.retrieve_thread_list(
                    self.user_respondent, args)
                self.assertEqual(first_respondent_thread_list.total, 1)
                second_respondent_thread_list = Retriever.retrieve_thread_list(
                    self.second_user_respondent, args)
                self.assertEqual(second_respondent_thread_list.total, 1)
                internal_thread_list = Retriever.retrieve_thread_list(
                    self.user_internal, args)
                self.assertEqual(internal_thread_list.total, 2)

                # first respondent can retrieve the message they sent
                first_respondent_thread = Retriever.retrieve_thread(
                    first_respondent_thread_id, self.user_respondent)
                self.assertIsNotNone(first_respondent_thread)

                # second respondent can retrieve the message they sent
                second_respondent_thread = Retriever.retrieve_thread(
                    second_respondent_thread_id, self.second_user_respondent)
                self.assertIsNotNone(second_respondent_thread)

                # first respondent shouldn't be able to retrieve second respondent's message
                with self.assertRaises(Forbidden):
                    Retriever.retrieve_thread(first_respondent_thread_id,
                                              self.second_user_respondent)

                # second respondent shouldn't be able to retrieve first respondent's message
                with self.assertRaises(Forbidden):
                    Retriever.retrieve_thread(second_respondent_thread_id,
                                              self.user_respondent)
Пример #8
0
    def get(thread_id):
        """Get messages by thread id"""
        logger.info("Getting messages from thread",
                    thread_id=thread_id,
                    user_uuid=g.user.user_uuid)

        conversation = Retriever.retrieve_thread(thread_id, g.user)
        conversation_metadata = Retriever.retrieve_conversation_metadata(
            thread_id)

        logger.info("Successfully retrieved messages from thread",
                    thread_id=thread_id,
                    user_uuid=g.user.user_uuid)
        messages = []
        for message in conversation.all():
            msg = message.serialize(g.user, body_summary=False)
            messages.append(msg)

        if conversation_metadata and conversation_metadata.is_closed:
            return jsonify({
                "messages":
                add_users_and_business_details(messages),
                "is_closed":
                conversation_metadata.is_closed,
                "closed_by":
                conversation_metadata.closed_by,
                "closed_by_uuid":
                conversation_metadata.closed_by_uuid,
                "closed_at":
                conversation_metadata.closed_at.isoformat()
            })

        return jsonify({
            "messages": add_users_and_business_details(messages),
            "is_closed": False
        })