Example #1
0
    def test_publish_message_for_value_error(self):
        with self.assertRaises(ValueError) as err_wrong_param:
            chat.publish_message(channel_name=None, message=None, user_id=self.user_id)
        with self.assertRaises(ValueError) as err_incomplete_param:
            chat.publish_message(channel_name=None, message='some message', user_id=self.user_id)
        self.chat.deleted_on = '2016-04-04'
        session.flush()
        with self.assertRaises(ValueError) as err_deleted_param:
            chat.publish_message(channel_name=self.channel_name, message='some message', user_id=None)
        with self.assertRaises(ValueError) as err_empty_string_param:
            chat.publish_message(channel_name=None, message='some message', user_id=self.user_id)

        self.assertEqual('PUB-REQ-MESSAGE-PAYLOAD', err_wrong_param.exception.message)
        self.assertEqual('PUB-REQ-CHANNEL', err_incomplete_param.exception.message)
        self.assertEqual('REQ-USER-ID', err_deleted_param.exception.message)
        self.assertEqual('PUB-REQ-CHANNEL', err_empty_string_param.exception.message)
Example #2
0
    def post(self):
        publish_message_request_format = reqparse.RequestParser()
        publish_message_request_format.add_argument('message', type=non_empty_str, required=True, help="PUB-REQ-MESSAGE")
        publish_message_request_format.add_argument('channel_name', type=non_empty_str, required=True, help="PUB-REQ-CHANNEL")

        params = publish_message_request_format.parse_args()
        params.update(dict(user_id=current_identity.id))
        log.info('Publish params: {}'.format(params))
        try:
            session.rollback()
            response = publish_message(**params)
            session.commit()
            return response
        except ValueError as val_err:
            log.error(repr(val_err))
            session.rollback()
            abort(400, message=val_err.message)
        except KeyError as key_err:
            log.error(repr(key_err))
            session.rollback()
            abort(400, message="PUB-INVALID-PARAM")
        except SQLAlchemyError as sa_err:
            log.exception(sa_err)
            session.rollback()
            abort(500, message="API-ERR-DB")
        except ConnectionClosed as pika_exception:
            log.error(repr(pika_exception))
            session.rollback()
            abort(400, message='PIKA-CONNECTION-ERROR')
        except AMQPConnectionError as pika_conn_err:
            log.error(repr(pika_conn_err))
            session.rollback()
            abort(400, message='PIKA-CONNECTION-ERROR')
        except Exception as excp:
            log.error(repr(excp))
            session.rollback()
        finally:
            session.close()