From 99250b22482fdc77a7f3ba96c94ac31b0dded18e Mon Sep 17 00:00:00 2001 From: Martyn Inglis Date: Wed, 7 Sep 2016 13:44:56 +0100 Subject: [PATCH] Adds a builder method to the notification object. This is used to construct a notification from the sorts of data an API call provides. This is used in both the db-email / db-sms tasks and the notifications rest endpoint to construct the notification DB object. --- app/models.py | 36 ++++++++++++++++----- tests/app/test_model.py | 72 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 tests/app/test_model.py diff --git a/app/models.py b/app/models.py index c4facc46c..16ac0279a 100644 --- a/app/models.py +++ b/app/models.py @@ -1,6 +1,5 @@ import uuid import datetime - from sqlalchemy.dialects.postgresql import ( UUID, JSON @@ -15,8 +14,8 @@ from app.encryption import ( from app.authentication.utils import get_secret from app import ( db, - encryption -) + encryption, + DATETIME_FORMAT) from app.history_meta import Versioned @@ -74,7 +73,6 @@ user_to_service = db.Table( UniqueConstraint('user_id', 'service_id', name='uix_user_to_service') ) - BRANDING_GOVUK = 'govuk' BRANDING_ORG = 'org' BRANDING_BOTH = 'both' @@ -395,6 +393,7 @@ class VerifyCode(db.Model): def check_code(self, cde): return check_hash(cde, self._code) + NOTIFICATION_CREATED = 'created' NOTIFICATION_SENDING = 'sending' NOTIFICATION_DELIVERED = 'delivered' @@ -427,7 +426,6 @@ NOTIFICATION_STATUS_TYPES_ENUM = db.Enum(*NOTIFICATION_STATUS_TYPES, name='notif class Notification(db.Model): - __tablename__ = 'notifications' id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) @@ -482,6 +480,31 @@ class Notification(db.Model): if personalisation: self._personalisation = encryption.encrypt(personalisation) + @staticmethod + def from_api_request( + created_at, + notification, + notification_id, + service_id, + notification_type, + api_key_id, + key_type): + return Notification( + id=notification_id, + template_id=notification['template'], + template_version=notification['template_version'], + to=notification['to'], + service_id=service_id, + job_id=notification.get('job', None), + job_row_number=notification.get('row_number', None), + status='created', + created_at=datetime.datetime.strptime(created_at, DATETIME_FORMAT), + personalisation=notification.get('personalisation'), + notification_type=notification_type, + api_key_id=api_key_id, + key_type=key_type + ) + class NotificationHistory(db.Model): __tablename__ = 'notification_history' @@ -522,7 +545,6 @@ INVITED_USER_STATUS_TYPES = ['pending', 'accepted', 'cancelled'] class InvitedUser(db.Model): - __tablename__ = 'invited_users' id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) @@ -598,7 +620,6 @@ class Permission(db.Model): class TemplateStatistics(db.Model): - id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False, nullable=False) service = db.relationship('Service', backref=db.backref('template_statistics', lazy='dynamic')) @@ -615,7 +636,6 @@ class TemplateStatistics(db.Model): class Event(db.Model): - __tablename__ = 'events' id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) diff --git a/tests/app/test_model.py b/tests/app/test_model.py new file mode 100644 index 000000000..04385a6b6 --- /dev/null +++ b/tests/app/test_model.py @@ -0,0 +1,72 @@ +from datetime import datetime + +from app import DATETIME_FORMAT +from app.models import Notification + + +def test_should_build_notification_from_minimal_set_of_api_derived_params(notify_api): + now = datetime.utcnow() + + notification = { + 'template': 'template', + 'template_version': '1', + 'to': 'someone', + 'personalisation': {} + } + notification = Notification.from_api_request( + created_at=now.strftime(DATETIME_FORMAT), + notification=notification, + notification_id="notification_id", + service_id="service_id", + notification_type='SMS', + api_key_id='api_key_id', + key_type='key_type' + ) + assert notification.created_at == now + assert notification.id == "notification_id" + assert notification.template_id == 'template' + assert notification.template_version == '1' + assert not notification.job_row_number + assert not notification.job_id + assert notification.to == 'someone' + assert notification.service_id == 'service_id' + assert notification.status == 'created' + assert not notification.personalisation + assert notification.notification_type == 'SMS' + assert notification.api_key_id == 'api_key_id' + assert notification.key_type == 'key_type' + + +def test_should_build_notification_from_full_set_of_api_derived_params(notify_api): + now = datetime.utcnow() + + notification = { + 'template': 'template', + 'template_version': '1', + 'to': 'someone', + 'personalisation': {'key': 'value'}, + 'job': 'job_id', + 'row_number': 100 + } + notification = Notification.from_api_request( + created_at=now.strftime(DATETIME_FORMAT), + notification=notification, + notification_id="notification_id", + service_id="service_id", + notification_type='SMS', + api_key_id='api_key_id', + key_type='key_type' + ) + assert notification.created_at == now + assert notification.id == "notification_id" + assert notification.template_id == 'template' + assert notification.template_version == '1' + assert notification.job_row_number == 100 + assert notification.job_id == 'job_id' + assert notification.to == 'someone' + assert notification.service_id == 'service_id' + assert notification.status == 'created' + assert notification.personalisation == {'key': 'value'} + assert notification.notification_type == 'SMS' + assert notification.api_key_id == 'api_key_id' + assert notification.key_type == 'key_type'