def test_get_user_by_id_user_for_key_error(self): with self.assertRaises(KeyError) as err_empty_param: auth.get_user_by_id() with self.assertRaises(KeyError) as err_empty_string_param: auth.get_user_by_id(user_key_id='') self.assertEqual('REQ-USER-ID', err_empty_param.exception.message) self.assertEqual('REQ-USER-ID', err_empty_string_param.exception.message)
def get(self): try: response = get_user_by_id(user_id=current_identity.id) session.commit() return dict( username=response.username, first_name=response.first_name, last_name=response.last_name, created_on=response.created_on, ) 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="USR-INVALID-PARAM") except IOError as io_err: log.exception(io_err) session.rollback() abort(500, message="API-ERR-IO") except SQLAlchemyError as sa_err: log.exception(sa_err) session.rollback() abort(500, message="API-ERR-DB") finally: session.close()
def publish_message(**kwargs): log.info('Publish Message kwargs: {}'.format(kwargs)) user = get_user_by_id(user_id=kwargs['user_id']) payload = dict( published_by=user.id, message=kwargs['message'], channel=get_channel_by_name(channel_name=kwargs['channel_name']).id, created_on=str(datetime.now()), ) # Comment the following part upto conneciton.close(), to make the process sync # And uncomment the below commented part, so that db call is done here and not in pika worker. connection = pika.BlockingConnection(pika.ConnectionParameters(host=PIKA_RABBITMQ_HOST)) pika_channel = connection.channel() pika_channel.exchange_declare(exchange=PIKA_RABBITMQ_EXCHANGE, type=PIKA_RABBITMQ_TYPE) pika_channel.basic_publish(exchange=PIKA_RABBITMQ_EXCHANGE, routing_key='', body=json.dumps(payload)) connection.close() # payload.update(dict( # id=str(uuid4()), # )) # chat_obj = ChatHistory(**payload) # session.add(chat_obj) # session.flush() return dict( message_published=True )
def fetch_stream_messages(**kwargs): log.info('Fetch Message Stream kwargs: {}'.format(kwargs)) return dict( message=kwargs['message'], channel=get_channel_by_id(channel_id=kwargs['channel']), published_by_name=get_user_by_id(user_id=kwargs['published_by']).first_name, created_on=datetime.now(), )
def get_pending_invitation(**kwargs): log.info('Get Pending Invitation kwargs: {}'.format(kwargs)) user_id = kwargs['user_id'] pending_invitation = session.query(ChannelJoinRequest).\ filter(ChannelJoinRequest.requested_for == get_user_by_id(user_id=user_id).username, ChannelJoinRequest.rejected_on.is_(None), ChannelJoinRequest.deleted_on.is_(None)).\ all() return pending_invitation
def test_get_user_by_id_user_for_value_error(self): with self.assertRaises(ValueError) as err_wrong_param: auth.get_user_by_id(user_id='wrong-id') with self.assertRaises(ValueError) as err_empty_string_param: auth.get_user_by_id(user_id='') self.user.deleted_on = '2016-04-04' session.flush() with self.assertRaises(ValueError) as err_deleted_param: auth.get_user_by_id(user_id=self.user_id) self.assertEqual('BAD-USER-ID', err_wrong_param.exception.message) self.assertEqual('REQ-USER-ID', err_empty_string_param.exception.message) self.assertEqual('BAD-USER-ID', err_deleted_param.exception.message)
def post(self): fetch_message_request_format = reqparse.RequestParser() fetch_message_request_format.add_argument('channel_name', type=non_empty_str, required=True, help="MSG-FETCH-REQ-CHANNEL") fetch_message_request_format.add_argument('page_num', type=int, required=True, help="MSG-FETCH-REQ-PAGE-NUM") params = fetch_message_request_format.parse_args() params.update(dict(user_id=current_identity.id)) params.update(dict(page_size=PER_PAGE_RESPONSE_LIMIT)) try: message_list = [] session.rollback() response = fetch_message(**params) session.commit() for a_message in response: setattr(a_message, 'published_by_name', get_user_by_id(user_id=a_message.published_by).first_name) message_list.append(message_transformation(a_message)) return message_list 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="MSG-INVALID-PARAM") except IOError as io_err: log.exception(io_err) session.rollback() abort(500, message="API-ERR-IO") except SQLAlchemyError as sa_err: log.exception(sa_err) session.rollback() abort(500, message="API-ERR-DB") except Exception as excp: log.error(repr(excp)) session.rollback() abort(400, message=excp.message) finally: session.close()
def test_get_user_by_id_successful(self): self.assertIsNotNone(auth.get_user_by_id(user_id=self.user_id))