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)
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.']})
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'])
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'])
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'])
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.']})
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.']})
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)
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)
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
def test_valid_message_passes_validation(self): """marshaling a valid message""" schema = MessageSchema() result = schema.load(self.json_message) self.assertTrue(result.errors == {})
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.']})