def test_valid_message_passes_validation(self):
     """marshaling a valid message"""
     self.json_message['msg_to'] = ["01b51fcc-ed43-4cdb-ad1c-450f9986859b"]
     schema = MessageSchema()
     with self.app.app_context():
         g.user = User(self.json_message['msg_from'], 'respondent')
         result = schema.load(self.json_message)
     self.assertTrue(result.errors == {})
    def test_msg_to_string(self):
        """marshalling message where msg_to field is string"""
        self.json_message['msg_to'] = ["01b51fcc-ed43-4cdb-ad1c-450f9986859b"]
        with self.app.app_context():
            g.user = User(self.json_message['msg_from'], 'respondent')
            schema = MessageSchema()
            errors = schema.load(self.json_message)[1]

        self.assertTrue(errors == {})
 def test_missing_survey_causes_error(self):
     """marshalling message with no survey field"""
     self.json_message['msg_to'] = ["01b51fcc-ed43-4cdb-ad1c-450f9986859b"]
     self.json_message['survey'] = ''
     with self.app.app_context():
         g.user = User(self.json_message['msg_from'], 'respondent')
         schema = MessageSchema()
         errors = schema.load(self.json_message)[1]
     self.assertTrue(errors == {'survey': ['Please enter a survey']})
 def test_thread_field_too_long_causes_error(self):
     """marshalling message with thread_id field too long"""
     self.json_message['thread_id'] = "x" * (MAX_THREAD_LEN + 1)
     expected_error = f"Thread field length must not be greater than {MAX_THREAD_LEN}"
     with self.app.app_context():
         g.user = User(self.json_message['msg_from'], 'respondent')
         schema = MessageSchema()
         sut = schema.load(self.json_message)
     self.assertTrue(expected_error in sut.errors['thread_id'])
 def test_missing_thread_field_does_not_cause_error(self):
     """marshalling message with no thread_id field"""
     self.json_message['msg_to'] = ["01b51fcc-ed43-4cdb-ad1c-450f9986859b"]
     self.json_message.pop('thread_id', "")
     with self.app.app_context():
         g.user = User(self.json_message['msg_from'], 'respondent')
         schema = MessageSchema()
         errors = schema.load(self.json_message)[1]
     self.assertTrue(errors == {})
 def test_subject_with_only_spaces_fails_validation(self):
     """marshalling message with no subject field """
     self.json_message['subject'] = '  '
     self.json_message['msg_to'] = ["01b51fcc-ed43-4cdb-ad1c-450f9986859b"]
     with self.app.app_context():
         g.user = User(self.json_message['msg_from'], 'respondent')
         schema = MessageSchema()
         errors = schema.load(self.json_message)[1]
     self.assertTrue(errors == {'subject': ['Please enter a subject']})
 def test_missing_msg_id_causes_a_string_the_same_length_as_uuid_to_be_used(
         self):
     """Missing msg_id causes a uuid is used"""
     self.json_message['msg_id'] = ''
     self.json_message['msg_to'] = ['01b51fcc-ed43-4cdb-ad1c-450f9986859b']
     with self.app.app_context():
         g.user = User(self.json_message['msg_from'], 'respondent')
         schema = MessageSchema()
         sut = schema.load(self.json_message)
     self.assertEqual(len(sut.data.msg_id), 36)
 def test_missing_subject_fails_validation(self):
     """marshalling message with no subject field """
     self.json_message.pop('subject', 'MyMessage')
     self.json_message['msg_to'] = ["01b51fcc-ed43-4cdb-ad1c-450f9986859b"]
     with self.app.app_context():
         g.user = User(self.json_message['msg_from'], 'respondent')
         schema = MessageSchema()
         errors = schema.load(self.json_message)[1]
     self.assertTrue(
         errors == {'subject': ['Missing data for required field.']})
    def test_msg_to_validation_invalid_respondent(self):
        """marshalling message where msg_to field is a invalid user"""
        self.json_message['msg_to'] = ["NotAValidUser"]
        self.json_message['msg_from'] = "01b51fcc-ed43-4cdb-ad1c-450f9986859b"
        with self.app.app_context():
            g.user = User("01b51fcc-ed43-4cdb-ad1c-450f9986859b", 'internal')
            schema = MessageSchema()
            errors = schema.load(self.json_message)[1]

        self.assertTrue(
            errors == {'msg_to': ['NotAValidUser is not a valid respondent.']})
    def test_body_too_big_fails_validation(self):
        """marshalling message with body field too long """
        self.json_message['body'] = "x" * (MAX_BODY_LEN + 1)
        expected_error = f"Body field length must not be greater than {MAX_BODY_LEN}"

        with self.app.app_context():
            g.user = User(self.json_message['msg_from'], 'respondent')
            schema = MessageSchema()
            sut = schema.load(self.json_message)

        self.assertTrue(expected_error in sut.errors['body'])
    def test_same_to_from_causes_error(self):
        """marshalling message with same to and from field"""
        self.json_message['msg_from'] = "01b51fcc-ed43-4cdb-ad1c-450f9986859b"
        self.json_message['msg_to'] = ["01b51fcc-ed43-4cdb-ad1c-450f9986859b"]
        with self.app.app_context():
            g.user = User(self.json_message['msg_from'], 'respondent')
            schema = MessageSchema()
            errors = schema.load(self.json_message)[1]

        self.assertTrue(
            errors ==
            {'_schema': ['msg_to and msg_from fields can not be the same.']})
 def test_valid_domain_message_passes_deserialization(self):
     """checking marshaling message object to json does not raise errors"""
     schema = MessageSchema()
     message_object = Message(
         **{
             'msg_to': ['Tej'],
             'msg_from': 'Gemma',
             'subject': 'MyMessage',
             'body': 'hello',
             'thread_id': "",
             'business_id': "7fc0e8ab-189c-4794-b8f4-9f05a1db185b"
         })
     message_json = schema.dumps(message_object)
     self.assertTrue(message_json.errors == {})
    def test_setting_read_date_field_causes_error(self):
        """marshalling message with no thread_id field"""
        message = {
            'msg_to': ['torrance'],
            'msg_from': 'someone',
            'body': 'hello',
            'subject': 'subject',
            'read_date': self.now
        }

        with self.app.app_context():
            g.user = User(message['msg_from'], 'respondent')
            schema = MessageSchema()
            errors = schema.load(message)[1]

        self.assertTrue(errors == {'_schema': ['read_date can not be set']})
    def test_missing_body_fails_validation(self):
        """marshalling message with no body field """
        message = {
            'msg_to': ['01b51fcc-ed43-4cdb-ad1c-450f9986859b'],
            'msg_from': 'torrance',
            'body': '',
            'survey': 'RSI',
            'subject': 'MyMessage',
            'business_id': '7fc0e8ab-189c-4794-b8f4-9f05a1db185b'
        }

        with self.app.app_context():
            g.user = User(message['msg_from'], 'respondent')
            schema = MessageSchema()
            errors = schema.load(message)[1]
        self.assertTrue(errors == {'body': ['Please enter a message']})
예제 #15
0
 def test_logging_message_endpoint(self):
     """logging message endpoint"""
     out = StringIO()
     sys.stdout = out
     message = {
         'msg_to': [constants.NON_SPECIFIC_INTERNAL_USER],
         'msg_from': self.msg_from,
         'subject': 'hello',
         'body': 'hello world',
         'thread_id': '',
         'business_id': self.business_id,
         'survey': self.survey_id
     }
     with self.app.app_context():
         g.user = User('Gemma', 'respondent')
         schema = MessageSchema()
         schema.load(message)
     output = out.getvalue().strip()
     self.assertIsNotNone(output)
    def _validate_post_data(post_data):
        if 'msg_id' in post_data:
            raise BadRequest(description="Message can not include msg_id")

        message = MessageSchema().load(post_data)

        if post_data.get('thread_id'):
            if post_data['from_internal'] and not party.get_users_details(post_data['msg_to']):
                # If an internal person is sending a message to a respondent, we need to check that they exist.
                # If they don't exist (because they've been deleted) then we raise a NotFound exception as the
                # respondent can't be found in the system.
                raise NotFound(description="Respondent not found")

            conversation_metadata = Retriever.retrieve_conversation_metadata(post_data.get('thread_id'))
            # Ideally, we'd return a 404 if there isn't a record in the conversation table.  But until we
            # ensure there is a record in here for every thread_id in the secure_message table, we just have to
            # assume that it's fine if it's empty.
            if conversation_metadata and conversation_metadata.is_closed:
                raise BadRequest(description="Cannot reply to a closed conversation")
        return message