Exemplo n.º 1
0
 def test_msg_returned_with_msg_id_returns_404(self):
     """retrieves message using id that doesn't exist"""
     message_id = "1"
     with self.app.app_context():
         with current_app.test_request_context():
             with self.assertRaises(NotFound):
                 Retriever.retrieve_message(message_id, self.user_internal)
    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)
Exemplo n.º 3
0
 def test_msg_returned_with_msg_id_true(self):
     """retrieves message using id"""
     self.create_thread(no_of_messages=2)
     with self.engine.connect() as con:
         query = con.execute(
             'SELECT msg_id FROM securemessage.secure_message LIMIT 1')
         msg_id = query.first()[0]
         with self.app.app_context():
             with current_app.test_request_context():
                 response = Retriever.retrieve_message(
                     msg_id, self.user_internal)
                 self.assertEqual(response['msg_id'], msg_id)
 def test_read_date_is_not_reset(self):
     """testing message read_date is not reset when unread label is removed again"""
     self.populate_database(1)
     with self.engine.connect() as con:
         query = con.execute(
             'SELECT msg_id FROM securemessage.secure_message LIMIT 1')
         msg_id = query.first()[0]
     with self.app.app_context():
         with current_app.test_request_context():
             message = Retriever.retrieve_message(msg_id,
                                                  self.user_internal)
             Modifier.mark_message_as_read(message, self.user_internal)
             message = Retriever.retrieve_message(msg_id,
                                                  self.user_internal)
             read_date_set = message['read_date']
             Modifier.add_unread(message, self.user_internal)
             message = Retriever.retrieve_message(msg_id,
                                                  self.user_internal)
             Modifier.mark_message_as_read(message, self.user_internal)
             message = Retriever.retrieve_message(msg_id,
                                                  self.user_internal)
             self.assertEqual(message['read_date'], read_date_set)
Exemplo n.º 5
0
 def test_sent_date_returned_for_message(self):
     """retrieves message using id and checks the sent date returned"""
     self.create_thread()
     with self.engine.connect() as con:
         query = con.execute(
             'SELECT msg_id FROM securemessage.secure_message LIMIT 1')
         msg_id = query.first()[0]
         with self.app.app_context():
             with current_app.test_request_context():
                 response = Retriever.retrieve_message(
                     msg_id, self.user_internal)
                 self.assertTrue('modified_date' not in response)
                 self.assertTrue(response['sent_date'] != "N/A")
Exemplo n.º 6
0
 def test_correct_labels_returned_external(self):
     """retrieves message using id and checks the labels are correct"""
     self.create_thread()
     with self.engine.connect() as con:
         query = con.execute(
             'SELECT msg_id FROM securemessage.secure_message LIMIT 1')
         msg_id = query.first()[0]
         with self.app.app_context():
             with current_app.test_request_context():
                 response = Retriever.retrieve_message(
                     msg_id, self.user_respondent)
                 labels = ['SENT']
                 self.assertCountEqual(response['labels'], labels)
Exemplo n.º 7
0
 def test_read_date_returned_for_message(self):
     """retrieves message using id and checks the read date returned"""
     self.create_thread(no_of_messages=2)
     with self.engine.connect() as con:
         query_sql = "SELECT securemessage.secure_message.msg_id FROM securemessage.secure_message " \
                     "JOIN securemessage.events ON securemessage.secure_message.msg_id = securemessage.events.msg_id " \
                     "WHERE securemessage.events.event = '" + EventsApi.READ.value + "' LIMIT 1"
         query = con.execute(query_sql)
         msg_id = query.first()[0]
         with self.app.app_context():
             with current_app.test_request_context():
                 response = Retriever.retrieve_message(
                     msg_id, self.user_internal)
                 self.assertTrue('modified_date' not in response)
                 self.assertTrue(response['read_date'] != "N/A")
Exemplo n.º 8
0
 def test_correct_to_and_from_returned_internal_user(self):
     """retrieves message using id and checks the to and from urns are correct"""
     self.create_thread(internal_actor=self.user_internal.user_uuid)
     with self.engine.connect() as con:
         query = con.execute(
             'SELECT msg_id FROM securemessage.secure_message LIMIT 1')
         msg_id = query.first()[0]
         with self.app.app_context():
             with current_app.test_request_context():
                 response = Retriever.retrieve_message(
                     msg_id, self.user_respondent)
                 self.assertEqual(response['msg_to'][0],
                                  self.user_internal.user_uuid)
                 self.assertEqual(response['msg_from'],
                                  self.user_respondent.user_uuid)
 def test_two_unread_labels_are_added_to_message(self):
     """testing duplicate message labels are not added to the database"""
     self.populate_database(1)
     with self.engine.connect() as con:
         query = con.execute(
             'SELECT msg_id FROM securemessage.secure_message LIMIT 1')
         msg_id = query.first()[0]
     with self.app.app_context():
         with current_app.test_request_context():
             message = Retriever.retrieve_message(msg_id,
                                                  self.user_internal)
             Modifier.add_unread(message, self.user_internal)
             Modifier.add_unread(message, self.user_internal)
     with self.engine.connect() as con:
         query = f"SELECT count(label) FROM securemessage.status WHERE msg_id = '{msg_id}' AND label = 'UNREAD'"
         query_x = con.execute(query)
         unread_label_total = []
         for row in query_x:
             unread_label_total.append(row[0])
         self.assertTrue(unread_label_total[0] == 1)
    def put(message_id):
        """Update message by status"""

        request_data = request.get_json()
        action, label = MessageModifyById._validate_request(request_data)
        message = Retriever.retrieve_message(message_id, g.user)

        if label == Labels.UNREAD.value:
            resp = MessageModifyById._try_modify_unread(action, message, g.user)
        else:
            resp = MessageModifyById._modify_label(action, message, g.user, label)

        if resp:
            res = jsonify({'status': 'ok'})
            res.status_code = 200

        else:
            res = jsonify({'status': 'error'})
            res.status_code = 400
            logger.error('Error updating message', msg_id=message_id, status_code=res.status_code)
        return res