From bc434f173612ceed508e617372cf5eb22627e69f Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 17 Nov 2016 13:42:34 +0000 Subject: [PATCH] Create new column in notifications and notification_history to store the client_reference because I remembered that reference is used for email providers. --- app/models.py | 2 ++ app/notifications/process_notifications.py | 2 +- app/v2/notifications/notification_schemas.py | 4 ++-- .../versions/0061_add_client_reference.py | 24 +++++++++++++++++++ .../test_process_notification.py | 7 ++++-- .../notifications/test_post_notifications.py | 9 ++++--- 6 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 migrations/versions/0061_add_client_reference.py diff --git a/app/models.py b/app/models.py index 5e2d16f6f..bd805c400 100644 --- a/app/models.py +++ b/app/models.py @@ -519,6 +519,7 @@ class Notification(db.Model): onupdate=datetime.datetime.utcnow) status = db.Column(NOTIFICATION_STATUS_TYPES_ENUM, index=True, nullable=False, default='created') reference = db.Column(db.String, nullable=True, index=True) + client_reference = db.Column(db.String, index=True, nullable=True) _personalisation = db.Column(db.String, nullable=True) template_history = db.relationship('TemplateHistory', primaryjoin=and_( @@ -561,6 +562,7 @@ class NotificationHistory(db.Model): updated_at = db.Column(db.DateTime, index=False, unique=False, nullable=True) status = db.Column(NOTIFICATION_STATUS_TYPES_ENUM, index=True, nullable=False, default='created') reference = db.Column(db.String, nullable=True, index=True) + client_reference = db.Column(db.String, nullable=True) @classmethod def from_notification(cls, notification): diff --git a/app/notifications/process_notifications.py b/app/notifications/process_notifications.py index d76d92521..b5ab8eb97 100644 --- a/app/notifications/process_notifications.py +++ b/app/notifications/process_notifications.py @@ -60,7 +60,7 @@ def persist_notification(template_id, created_at=created_at or datetime.utcnow().strftime(DATETIME_FORMAT), job_id=job_id, job_row_number=job_row_number, - reference=reference + client_reference=reference ) dao_create_notification(notification) return notification diff --git a/app/v2/notifications/notification_schemas.py b/app/v2/notifications/notification_schemas.py index 2832487a9..d77a4d21a 100644 --- a/app/v2/notifications/notification_schemas.py +++ b/app/v2/notifications/notification_schemas.py @@ -101,7 +101,7 @@ post_email_response = { def create_post_sms_response_from_notification(notification, body, from_number, url_root): return {"id": notification.id, - "reference": notification.reference, + "reference": notification.client_reference, "content": {'body': body, 'from_number': from_number}, "uri": "{}/v2/notifications/{}".format(url_root, str(notification.id)), @@ -112,7 +112,7 @@ def create_post_sms_response_from_notification(notification, body, from_number, def create_post_email_response_from_notification(notification, content, subject, email_from, url_root): return { "id": notification.id, - "reference": notification.reference, + "reference": notification.client_reference, "content": { "from_email": email_from, "body": content, diff --git a/migrations/versions/0061_add_client_reference.py b/migrations/versions/0061_add_client_reference.py new file mode 100644 index 000000000..05bf0d7e6 --- /dev/null +++ b/migrations/versions/0061_add_client_reference.py @@ -0,0 +1,24 @@ +"""empty message + +Revision ID: 0061_add_client_reference +Revises: 0060_add_letter_template_type +Create Date: 2016-11-17 13:19:25.820617 + +""" + +# revision identifiers, used by Alembic. +revision = '0061_add_client_reference' +down_revision = '0060_add_letter_template_type' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + op.add_column('notifications', sa.Column('client_reference', sa.String(), index=True, nullable=True)) + op.add_column('notification_history', sa.Column('client_reference', sa.String(), nullable=True)) + + +def downgrade(): + op.drop_column('notifications', 'client_reference') + op.drop_column('notification_history', 'client_reference') diff --git a/tests/app/notifications/test_process_notification.py b/tests/app/notifications/test_process_notification.py index 851865a6b..8789909bc 100644 --- a/tests/app/notifications/test_process_notification.py +++ b/tests/app/notifications/test_process_notification.py @@ -64,7 +64,7 @@ def test_persist_notification_throws_exception_when_missing_template(sample_api_ assert NotificationHistory.query.count() == 0 -def test_persist_notification_with_job_and_created(sample_job, sample_api_key): +def test_persist_notification_with_optionals(sample_job, sample_api_key): assert Notification.query.count() == 0 assert NotificationHistory.query.count() == 0 created_at = datetime.datetime(2016, 11, 11, 16, 8, 18) @@ -77,13 +77,16 @@ def test_persist_notification_with_job_and_created(sample_job, sample_api_key): key_type=sample_api_key.key_type, created_at=created_at, job_id=sample_job.id, - job_row_number=10) + job_row_number=10, + reference="ref from client") assert Notification.query.count() == 1 assert NotificationHistory.query.count() == 1 persisted_notification = Notification.query.all()[0] assert persisted_notification.job_id == sample_job.id assert persisted_notification.job_row_number == 10 assert persisted_notification.created_at == created_at + assert persisted_notification.client_reference == "ref from client" + assert persisted_notification.reference is None @pytest.mark.parametrize('research_mode, queue, notification_type, key_type', diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index eb612ea89..a509d2a2f 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -119,15 +119,14 @@ def test_post_email_notification_returns_201(client, sample_email_template, mock headers=[('Content-Type', 'application/json'), auth_header]) assert response.status_code == 201 resp_json = json.loads(response.get_data(as_text=True)) - notifications = Notification.query.all() - assert len(notifications) == 1 - notification_id = notifications[0].id - assert resp_json['id'] is not None + notification = Notification.query.first() + assert resp_json['id'] == str(notification.id) assert resp_json['reference'] == "reference from caller" + assert notification.reference is None assert resp_json['content']['body'] == sample_email_template.content assert resp_json['content']['subject'] == sample_email_template.subject assert resp_json['content']['from_email'] == sample_email_template.service.email_from - assert 'v2/notifications/{}'.format(notification_id) in resp_json['uri'] + assert 'v2/notifications/{}'.format(notification.id) in resp_json['uri'] assert resp_json['template']['id'] == str(sample_email_template.id) assert resp_json['template']['version'] == sample_email_template.version assert 'v2/templates/{}'.format(sample_email_template.id) in resp_json['template']['uri']