def test_update_notification(session, loop): """Assert the test can update notification.""" notification = NotificationModel(**NOTIFICATION_DATA[0]) session.add(notification) session.commit() notification = session.merge(notification) notification.sent_date = datetime.now() notification.status_code = 'FAILURE' result = loop.run_until_complete( NotificaitonCRUD.update_notification(session, notification)) assert result == notification assert result.id == NOTIFICATION_DATA[0]['id'] assert result.recipients == NOTIFICATION_DATA[0]['recipients'] assert result.status_code == 'FAILURE'
def test_create_notification(session, loop): """Assert the test can create notification.""" result = loop.run_until_complete( NotificaitonCRUD.create_notification( session, NotificationModel(**NOTIFICATION_DATA[0]))) assert result.id == NOTIFICATION_DATA[0]['id'] assert result.recipients == NOTIFICATION_DATA[0]['recipients']
def test_get_by_id(session, app, client): # pylint: disable=unused-argument """Assert the test can retrieve notification details by id.""" notification = NotificationModel(**NOTIFICATION_DATA[0]) session.add(notification) session.commit() notification = session.merge(notification) res = client.get('/api/v1/notify/{}'.format(notification.id)) response_data = res.json() assert res.status_code == 200 assert notification.recipients == response_data['recipients']
async def create_notification(db_session: Session, notification: NotificationRequest): """create notification.""" db_notification = NotificationModel(recipients=notification.recipients, request_date=datetime.utcnow(), type_code=NotificationTypeEnum.EMAIL, status_code=NotificationStatusEnum.PENDING) db_session.add(db_notification) db_session.commit() db_session.refresh(db_notification) return db_notification
def test_find_no_notification_by_status_time_(session, loop): """Assert the test can not retrieve notification by status and time frame.""" notification = NotificationModel(**NOTIFICATION_DATA[3]) session.add(notification) session.commit() notification = session.merge(notification) result = loop.run_until_complete( NotifyService.find_notifications_by_status(session, notification.status_code)) assert result == []
def test_find_notification_by_id(session, loop): """Assert the test can retrieve notification by id.""" notification = NotificationModel(**NOTIFICATION_DATA[0]) session.add(notification) session.commit() notification = session.merge(notification) result = loop.run_until_complete( NotifyService.find_notification(session, notification.id)) assert result == notification assert result.id == NOTIFICATION_DATA[0]['id'] assert result.recipients == NOTIFICATION_DATA[0]['recipients']
def test_find_notification_by_status_time(session, loop): """Assert the test can retrieve notification by status and time frame.""" notification = NotificationModel(**NOTIFICATION_DATA[2]) session.add(notification) session.commit() notification = session.merge(notification) result = loop.run_until_complete( NotifyService.find_notifications_by_status(session, notification.status_code)) assert result[0] == notification assert result[0].id == NOTIFICATION_DATA[2]['id'] assert result[0].recipients == NOTIFICATION_DATA[2]['recipients']
async def test_process_notification_failed(session): """Assert that notification can not be process due to data issue.""" notification = NotificationModel(**NOTIFICATION_DATA[0]) session.add(notification) session.commit() notification = session.merge(notification) await worker.process_notification(notification.id, session) notification_update: NotificationModel = await NotifyService.find_notification(session, notification.id) assert notification_update is not None assert notification_update.id == notification.id assert notification_update.status_code == NotificationStatusEnum.FAILURE
def test_create_contents(session, loop): """Assert the test can create notification contents.""" notification = NotificationModel(**NOTIFICATION_DATA[0]) session.add(notification) session.commit() notification = session.merge(notification) content = NotificationContentsModel(**CONTENT_DATA[0]) request_content: NotificationContentsRequest = NotificationContentsRequest(subject=content.subject, body=content.body) result = loop.run_until_complete( NotificaitonContentsCRUD.create_contents(session, request_content, notification_id=notification.id) ) assert result.id == CONTENT_DATA[0]['id'] assert result.subject == CONTENT_DATA[0]['subject']
async def test_process_notification_delivered(session, sendmail_mock): # pylint: disable=unused-argument """Assert that notification can not be skip due to DELIVERED status.""" notification = NotificationModel(**NOTIFICATION_DATA[4]) session.add(notification) session.commit() notification = session.merge(notification) content = NotificationContentsModel(**CONTENT_DATA[0], notification_id=notification.id) session.add(content) session.commit() await worker.process_notification(notification.id, session) notification_update: NotificationModel = await NotifyService.find_notification(session, notification.id) assert notification_update is not None assert notification_update.id == notification.id assert notification_update.status_code == NotificationStatusEnum.DELIVERED
async def test_process_notification_sendmail_failed(session, sendmail_failed_mock): # pylint: disable=unused-argument """Assert that notification can not be process due to smtp server issue.""" notification = NotificationModel(**NOTIFICATION_DATA[0]) session.add(notification) session.commit() notification = session.merge(notification) content = ContentModel(**CONTENT_DATA[1], notification_id=notification.id) session.add(content) session.commit() await worker.process_notification(notification.id, session) notification_update: NotificationModel = await NotifyService.find_notification(session, notification.id) assert notification_update is not None assert notification_update.id == notification.id assert notification_update.status_code == NotificationStatusEnum.FAILURE
def test_create_contents_with_attachment_url(session, loop): """Assert the test can create notification contents with attachment url.""" notification = NotificationModel(**NOTIFICATION_DATA[0]) session.add(notification) session.commit() notification = session.merge(notification) request_content = NotificationContentsRequest(subject=CONTENT_DATA[1]['subject'], body=CONTENT_DATA[1]['body'], attachmentName=CONTENT_DATA[1]['attachment_name'], attachmentBytes=CONTENT_DATA[1]['attachment'], attachmentUrl=CONTENT_DATA[1]['attachment_url']) result = loop.run_until_complete( NotificaitonContentsCRUD.create_contents(session, request_content, notification_id=notification.id) ) assert result.subject == CONTENT_DATA[1]['subject'] assert result.attachment_name == CONTENT_DATA[1]['attachment_name']
def test_find_content_by_notification_id(session, loop): """Assert the test can retrieve notification contents with notification id.""" notification = NotificationModel(**NOTIFICATION_DATA[0]) session.add(notification) session.commit() notification = session.merge(notification) content = NotificationContentsModel(**CONTENT_DATA[0], notification_id=notification.id) session.add(content) session.commit() content = session.merge(content) result = loop.run_until_complete( NotificaitonContentsCRUD.find_contents_by_notification_id(session, notification.id) ) assert result == content assert result.id == CONTENT_DATA[0]['id'] assert result.subject == CONTENT_DATA[0]['subject']
async def create_notification(db_session: Session, notification: NotificationRequest): """Create notification.""" db_notification = NotificationModel( recipients=notification.recipients, request_date=datetime.utcnow(), request_by=notification.request_by, type_code=NotificationTypeEnum.EMAIL, status_code=NotificationStatusEnum.PENDING) db_session.add(db_notification) db_session.commit() db_session.refresh(db_notification) # save email content await ContentCRUD.create_content(db_session, content=notification.content, notification_id=db_notification.id) return db_notification
async def test_job_handler(session, sendmail_mock): # pylint: disable=unused-argument """Assert that the notification records can be retrieve by job handler.""" for i in NOTIFICATION_DATA: notification = NotificationModel(**i) session.add(notification) session.commit() content = NotificationContentsModel(**CONTENT_DATA[0], notification_id=notification.id) session.add(content) session.commit() notification_status = NotificationStatusEnum.FAILURE await worker.job_handler(notification_status) hours: int = AppConfig.DELIVERY_FAILURE_RETRY_TIME_FRAME for i in NOTIFICATION_DATA: if i['status_code'] == notification_status \ and (datetime.utcnow() - timedelta(hours=hours)) < i['request_date']: notification_update: NotificationModel = await NotifyService.find_notification(session, i['id']) assert notification_update is not None assert notification_update.id == i['id'] assert notification_update.status_code == NotificationStatusEnum.DELIVERED
async def test_cb_subscription_handler(session, # pylint: disable=too-many-arguments, too-many-locals sendmail_mock, stan_server, event_loop, # pylint: disable=unused-argument notification_stan, future): """Assert that notification id can be retrieved and decoded from the Queue.""" from entity_queue_common.service import ServiceWorker # pylint: disable=import-outside-toplevel # vars uuid = str(random.SystemRandom().randint(0, 999999)) notification_id = uuid notification_subject = f'test_subject.{uuid}' notification_queue = f'test_queue.{uuid}' notification_durable_name = f'test_durable.{uuid}' notification = NotificationModel(**NOTIFICATION_DATA[0]) notification.id = notification_id session.add(notification) session.commit() notification = session.merge(notification) content = NotificationContentsModel(**CONTENT_DATA[0], notification_id=notification.id) session.add(content) session.commit() content = session.merge(content) # register the handler to test it notification_subject = await subscribe_to_queue(notification_stan, notification_subject, notification_queue, notification_durable_name, cb_subscription_handler) # file handler callback msgs = [] async def cb_file_handler(msg): nonlocal msgs nonlocal future msgs.append(msg) if len(msgs) == 1: future.set_result(True) file_handler_subject = app_config.FILER_PUBLISH_OPTIONS['subject'] await subscribe_to_queue(notification_stan, file_handler_subject, f'notification_queue.{file_handler_subject}', f'notification_durable_name.{file_handler_subject}', cb_file_handler) service_worker = ServiceWorker() service_worker.sc = notification_stan qsm.service = service_worker # add notification id to queue await helper_add_notification_to_queue(notification_stan, notification_subject, notification_id=notification_id) try: await asyncio.wait_for(future, 2, loop=event_loop) except Exception as err: # pylint: disable=broad-except print(err) notification_update: NotificationModel = await NotifyService.find_notification(session, notification_id) assert notification_update is not None assert notification_update.id == notification.id assert notification_update.status_code == NotificationStatusEnum.DELIVERED