From 655beddba6e8f9ddb7073b7aa751f2e03f5f3626 Mon Sep 17 00:00:00 2001 From: Martyn Inglis Date: Tue, 16 Feb 2016 11:22:44 +0000 Subject: [PATCH] Fixed up the get_notitication endpoint - returns a notification --- app/__init__.py | 2 +- app/clients/sms/twilio.py | 2 -- app/dao/notifications_dao.py | 8 ++++++-- app/job/rest.py | 16 ++++++--------- app/notifications/rest.py | 23 ++++++++++++++-------- app/schemas.py | 2 +- tests/app/dao/test_notification_dao.py | 19 +++++++++++++----- tests/app/notifications/test_rest.py | 27 ++++++++++++++------------ 8 files changed, 58 insertions(+), 41 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 619824c16..f035df8ed 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -30,9 +30,9 @@ def create_app(config_name, config_overrides=None): application.config['NOTIFY_API_ENVIRONMENT'] = config_name application.config.from_object(configs[config_name]) + init_app(application, config_overrides) db.init_app(application) ma.init_app(application) - init_app(application, config_overrides) logging.init_app(application) twilio_client.init_app(application) celery.init_app(application) diff --git a/app/clients/sms/twilio.py b/app/clients/sms/twilio.py index 0a3eaee69..52b0375ee 100644 --- a/app/clients/sms/twilio.py +++ b/app/clients/sms/twilio.py @@ -22,8 +22,6 @@ class TwilioClient(SmsClient): config.config.get('TWILIO_ACCOUNT_SID'), config.config.get('TWILIO_AUTH_TOKEN')) self.from_number = config.config.get('TWILIO_NUMBER') - print(config.config) - def send_sms(self, notification, content): try: diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index ed3cd0f6d..f917f4429 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -14,9 +14,13 @@ def save_notification(notification, update_dict={}): db.session.commit() -def get_notification(service_id, job_id, notification_id): +def get_notification_for_job(service_id, job_id, notification_id): return Notification.query.filter_by(service_id=service_id, job_id=job_id, id=notification_id).one() -def get_notifications(service_id, job_id): +def get_notifications_for_job(service_id, job_id): return Notification.query.filter_by(service_id=service_id, job_id=job_id).all() + + +def get_notification(service_id, notification_id): + return Notification.query.filter_by(service_id=service_id, id=notification_id).one() diff --git a/app/job/rest.py b/app/job/rest.py index 17f1e341c..1928e5d66 100644 --- a/app/job/rest.py +++ b/app/job/rest.py @@ -17,11 +17,7 @@ from app.dao.jobs_dao import ( get_jobs_by_service ) -from app.dao.notifications_dao import ( - save_notification, - get_notification, - get_notifications -) +from app.dao import notifications_dao from app.schemas import ( job_schema, @@ -89,7 +85,7 @@ def create_notification_for_job(service_id, job_id): if errors: return jsonify(result="error", message=errors), 400 try: - save_notification(notification) + notifications_dao.save_notification(notification) except Exception as e: return jsonify(result="error", message=str(e)), 500 return jsonify(data=notification_status_schema.dump(notification).data), 201 @@ -100,7 +96,7 @@ def create_notification_for_job(service_id, job_id): def get_notification_for_job(service_id, job_id, notification_id=None): if notification_id: try: - notification = get_notification(service_id, job_id, notification_id) + notification = notifications_dao.get_notification_for_job(service_id, job_id, notification_id) data, errors = notification_status_schema.dump(notification) return jsonify(data=data) except DataError: @@ -108,7 +104,7 @@ def get_notification_for_job(service_id, job_id, notification_id=None): except NoResultFound: return jsonify(result="error", message="Notification not found"), 404 else: - notifications = get_notifications(service_id, job_id) + notifications = notifications_dao.get_notifications_for_job(service_id, job_id) data, errors = notifications_status_schema.dump(notifications) return jsonify(data=data) @@ -116,13 +112,13 @@ def get_notification_for_job(service_id, job_id, notification_id=None): @job.route('//notification/', methods=['PUT']) def update_notification_for_job(service_id, job_id, notification_id): - notification = get_notification(service_id, job_id, notification_id) + notification = notifications_dao.get_notification_for_job(service_id, job_id, notification_id) update_dict, errors = notification_status_schema_load_json.load(request.get_json()) if errors: return jsonify(result="error", message=errors), 400 try: - save_notification(notification, update_dict=update_dict) + notifications_dao.save_notification(notification, update_dict=update_dict) except Exception as e: return jsonify(result="error", message=str(e)), 400 diff --git a/app/notifications/rest.py b/app/notifications/rest.py index 8bf50002e..6a9a888c2 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -4,16 +4,20 @@ from flask import ( Blueprint, jsonify, request, - current_app) -from itsdangerous import URLSafeSerializer + current_app +) +from itsdangerous import URLSafeSerializer from app import api_user from app.aws_sqs import add_notification_to_queue -from app.dao import (templates_dao) +from app.dao import (templates_dao, notifications_dao) from app.schemas import ( - email_notification_schema, sms_template_notification_schema) + email_notification_schema, + sms_template_notification_schema, + notification_status_schema +) from app.celery.tasks import send_sms - +from sqlalchemy.orm.exc import NoResultFound notifications = Blueprint('notifications', __name__) @@ -22,10 +26,13 @@ def create_notification_id(): return str(uuid.uuid4()) -@notifications.route('/', methods=['GET']) +@notifications.route('/', methods=['GET']) def get_notifications(notification_id): - # TODO return notification id details - return jsonify({'id': notification_id}), 200 + try: + notification = notifications_dao.get_notification(api_user['client'], notification_id) + return jsonify({'notification': notification_status_schema.dump(notification).data}), 200 + except NoResultFound: + return jsonify(result="error", message="not found"), 404 @notifications.route('/sms', methods=['POST']) diff --git a/app/schemas.py b/app/schemas.py index 050e35d48..b8fbbd757 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -141,7 +141,7 @@ class EmailNotificationSchema(NotificationSchema): class NotificationStatusSchema(BaseSchema): class Meta: - model = models.Notification + model = models.Noti∫~fication user_schema = UserSchema() diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index a28851520..384be5bff 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -3,7 +3,8 @@ from app.models import Notification from app.dao.notifications_dao import ( save_notification, get_notification, - get_notifications + get_notification_for_job, + get_notifications_for_job ) @@ -31,6 +32,13 @@ def test_save_notification(notify_db, notify_db_session, sample_template, sample assert 'sent' == notification_from_db.status +def test_get_notification(notify_db, notify_db_session, sample_notification): + notifcation_from_db = get_notification( + sample_notification.service.id, + sample_notification.id) + assert sample_notification == notifcation_from_db + + def test_save_notification_no_job_id(notify_db, notify_db_session, sample_template): assert Notification.query.count() == 0 @@ -54,9 +62,10 @@ def test_save_notification_no_job_id(notify_db, notify_db_session, sample_templa def test_get_notification_for_job(notify_db, notify_db_session, sample_notification): - notifcation_from_db = get_notification(sample_notification.service.id, - sample_notification.job_id, - sample_notification.id) + notifcation_from_db = get_notification_for_job( + sample_notification.service.id, + sample_notification.job_id, + sample_notification.id) assert sample_notification == notifcation_from_db @@ -70,7 +79,7 @@ def test_get_all_notifications_for_job(notify_db, notify_db_session, sample_job) template=sample_job.template, job=sample_job) - notifcations_from_db = get_notifications(sample_job.service.id, sample_job.id) + notifcations_from_db = get_notifications_for_job(sample_job.service.id, sample_job.id) assert len(notifcations_from_db) == 5 diff --git a/tests/app/notifications/test_rest.py b/tests/app/notifications/test_rest.py index 33cef53b0..dbe8be2b6 100644 --- a/tests/app/notifications/test_rest.py +++ b/tests/app/notifications/test_rest.py @@ -4,46 +4,49 @@ import uuid from tests import create_authorization_header from flask import url_for, json from app.models import Service -from tests.app.conftest import sample_service as create_sample_service -from tests.app.conftest import sample_template as create_sample_template def test_get_notifications( - notify_api, notify_db, notify_db_session, sample_api_key, mocker): + notify_api, notify_db, notify_db_session, sample_api_key, sample_notification): """ Tests GET endpoint '/' to retrieve entire service list. """ with notify_api.test_request_context(): with notify_api.test_client() as client: auth_header = create_authorization_header( - service_id=sample_api_key.service_id, - path=url_for('notifications.get_notifications', notification_id=123), + service_id=sample_notification.service_id, + path='/notifications/{}'.format(sample_notification.id), method='GET') response = client.get( - url_for('notifications.get_notifications', notification_id=123), + '/notifications/{}'.format(sample_notification.id), headers=[auth_header]) + notification = json.loads(response.get_data(as_text=True))['notification'] + assert notification['status'] == 'sent' + assert notification['template'] == sample_notification.template.id + assert notification['to'] == '+44709123456' + assert notification['service'] == str(sample_notification.service_id) assert response.status_code == 200 def test_get_notifications_empty_result( notify_api, notify_db, notify_db_session, sample_api_key, mocker): - """ - Tests GET endpoint '/' to retrieve entire service list. - """ with notify_api.test_request_context(): with notify_api.test_client() as client: + + id = uuid.uuid4() + auth_header = create_authorization_header( service_id=sample_api_key.service_id, - path=url_for('notifications.get_notifications', notification_id=123), + path=url_for('notifications.get_notifications', notification_id=id), method='GET') response = client.get( - url_for('notifications.get_notifications', notification_id=123), + url_for('notifications.get_notifications', notification_id=id), headers=[auth_header]) - assert response.status_code == 200 + assert response.status_code == 404 def test_create_sms_should_reject_if_no_phone_numbers(