コード例 #1
0
 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'] = ''
     schema = MessageSchema()
     sut = schema.load(self.json_message)
     self.assertEquals(len(sut.data.msg_id), 36)
コード例 #2
0
 def test_missing_thread_field_does_not_cause_error(self):
     """marshalling message with no thread_id field"""
     message = {'msg_to': 'torrance', 'msg_from': 'someone'}
     schema = MessageSchema()
     errors = schema.load(message)[1]
     self.assertTrue(
         errors != {'thread_id': ['Missing data for required field.']})
コード例 #3
0
 def test_body_too_big_fails_validation(self):
     """marshalling message with body field too long """
     self.json_message['body'] = "x" * (app.constants.MAX_BODY_LEN + 1)
     expected_error = 'Body field length must not be greater than {0}.'.format(
         app.constants.MAX_BODY_LEN)
     schema = MessageSchema()
     sut = schema.load(self.json_message)
     self.assertTrue(expected_error in sut.errors['body'])
コード例 #4
0
 def test_thread_field_too_long_causes_error(self):
     """marshalling message with thread_id field too long"""
     self.json_message['thread_id'] = "x" * (app.constants.MAX_THREAD_LEN +
                                             1)
     expected_error = 'Thread field length must not be greater than {0}.'.format(
         app.constants.MAX_THREAD_LEN)
     schema = MessageSchema()
     sut = schema.load(self.json_message)
     self.assertTrue(expected_error in sut.errors['thread_id'])
コード例 #5
0
 def test_subject_field_too_long_causes_error(self):
     """marshalling message with subject field too long"""
     self.json_message['subject'] = "x" * (app.constants.MAX_SUBJECT_LEN +
                                           1)
     expected_error = 'Subject field length must not be greater than {0}.'.format(
         app.constants.MAX_SUBJECT_LEN)
     schema = MessageSchema()
     sut = schema.load(self.json_message)
     self.assertTrue(expected_error in sut.errors['subject'])
コード例 #6
0
 def test_missing_body_fails_validation(self):
     """marshalling message with no body field """
     message = {
         'urn_to': 'richard',
         'urn_from': 'torrance',
         'body': '',
         'survey': 'RSI'
     }
     schema = MessageSchema()
     errors = schema.load(message)[1]
     self.assertTrue(errors == {'body': ['Body field not populated.']})
コード例 #7
0
 def test_setting_sent_date_field_causes_error(self):
     """marshalling message with no thread_id field"""
     message = {
         'urn_to': 'torrance',
         'urn_from': 'someone',
         'body': 'hello',
         'subject': 'subject',
         'sent_date': self.now
     }
     schema = MessageSchema()
     errors = schema.load(message)[1]
     self.assertTrue(errors == {'_schema': ['sent_date can not be set.']})
コード例 #8
0
 def test_logging_message_endpoint(self):
     """logging message endpoint"""
     out = StringIO()
     sys.stdout = out
     message = {
         'msg_to': 'richard',
         'msg_from': 'torrance',
         'subject': 'hello',
         'body': 'hello world',
         'thread_id': ''
     }
     schema = MessageSchema()
     schema.load(message)
     output = out.getvalue().strip()
     self.assertIsNotNone(output)
コード例 #9
0
 def test_valid_domain_message_passes_deserialization(self):
     """checking marshaling message object to json does not raise errors"""
     schema = MessageSchema()
     message_object = Message(
         **{
             'urn_to': 'Tej',
             'urn_from': 'Gemma',
             'subject': 'MyMessage',
             'body': 'hello',
             'thread_id': "",
             'sent_date': datetime.now(timezone.utc),
             'read_date': datetime.now(timezone.utc)
         })
     message_json = schema.dumps(message_object)
     self.assertTrue(message_json.errors == {})
     self.assertTrue('sent_date' in message_json.data)
     self.assertTrue('read_date' in message_json.data)
コード例 #10
0
    def post(self):
        """used to handle POST requests to send a message"""
        logger.info("Message send POST request.")
        post_data = request.get_json()
        is_draft = False
        draft_id = None
        if 'msg_id' in post_data:
            is_draft = MessageSend().check_if_draft(post_data['msg_id'])
            if is_draft is True:
                draft_id = post_data['msg_id']
                post_data['msg_id'] = ''
            else:
                raise (BadRequest(
                    description="Message can not include msg_id"))

        message = MessageSchema().load(post_data)

        if message.errors == {}:
            return self.message_save(message, is_draft, draft_id)
        else:
            res = jsonify(message.errors)
            res.status_code = 400
            return res
コード例 #11
0
 def test_valid_message_passes_validation(self):
     """marshaling a valid message"""
     schema = MessageSchema()
     result = schema.load(self.json_message)
     self.assertTrue(result.errors == {})
コード例 #12
0
 def test_missing_survey_causes_error(self):
     """marshalling message with no survey field"""
     self.json_message['survey'] = ''
     schema = MessageSchema()
     errors = schema.load(self.json_message)[1]
     self.assertTrue(errors == {'survey': ['Survey field not populated.']})