diff --git a/app/celery/tasks.py b/app/celery/tasks.py index 57868a070..5b67e3b3c 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -70,7 +70,7 @@ def delete_verify_codes(): def delete_successful_notifications(): try: start = datetime.utcnow() - deleted = delete_notifications_created_more_than_a_week_ago('sent') + deleted = delete_notifications_created_more_than_a_week_ago('sending') current_app.logger.info( "Delete job started {} finished {} deleted {} successful notifications".format( start, @@ -129,12 +129,13 @@ def process_job(job_id): if stats: total_sent = stats.emails_requested + stats.sms_requested - if total_sent + job.notification_count > service.limit: + if total_sent + job.notification_count > service.message_limit: job.status = 'sending limits exceeded' job.processing_finished = datetime.utcnow() dao_update_job(job) current_app.logger.info( - "Job {} size {} error. Sending limits {} exceeded".format(job_id, job.notification_count, service.limit) + "Job {} size {} error. Sending limits {} exceeded".format( + job_id, job.notification_count, service.message_limit) ) return @@ -152,7 +153,7 @@ def process_job(job_id): ).recipients_and_personalisation: encrypted = encryption.encrypt({ - 'template': template.id, + 'template': str(template.id), 'job': str(job.id), 'to': recipient, 'personalisation': personalisation @@ -217,7 +218,7 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at): to=notification['to'], service_id=service_id, job_id=notification.get('job', None), - status='failed' if restricted else 'sent', + status='failed' if restricted else 'sending', created_at=datetime.strptime(created_at, DATETIME_FORMAT), sent_at=sent_at, sent_by=client.get_name() @@ -277,7 +278,7 @@ def send_email(service_id, notification_id, subject, from_address, encrypted_not to=notification['to'], service_id=service_id, job_id=notification.get('job', None), - status='failed' if restricted else 'sent', + status='failed' if restricted else 'sending', created_at=datetime.strptime(created_at, DATETIME_FORMAT), sent_at=sent_at, sent_by=client.get_name() diff --git a/app/clients/email/aws_ses.py b/app/clients/email/aws_ses.py index ae50a42c1..ce139ceff 100644 --- a/app/clients/email/aws_ses.py +++ b/app/clients/email/aws_ses.py @@ -8,7 +8,7 @@ ses_response_map = { 'Bounce': { "message": 'Bounced', "success": False, - "notification_status": 'bounce', + "notification_status": 'failed', "notification_statistics_status": STATISTICS_FAILURE }, 'Delivery': { @@ -19,9 +19,9 @@ ses_response_map = { }, 'Complaint': { "message": 'Complaint', - "success": False, - "notification_status": 'complaint', - "notification_statistics_status": STATISTICS_FAILURE + "success": True, + "notification_status": 'delivered', + "notification_statistics_status": STATISTICS_DELIVERED } } diff --git a/app/clients/sms/firetext.py b/app/clients/sms/firetext.py index f0cb10c6f..7c49848b4 100644 --- a/app/clients/sms/firetext.py +++ b/app/clients/sms/firetext.py @@ -25,9 +25,9 @@ firetext_responses = { }, '2': { "message": 'Undelivered (Pending with Network)', - "success": False, - "notification_statistics_status": None, - "notification_status": 'sent' + "success": True, + "notification_statistics_status": STATISTICS_DELIVERED, + "notification_status": 'delivered' } } diff --git a/app/clients/sms/twilio.py b/app/clients/sms/twilio.py index 085fe96cd..d030cbdf5 100644 --- a/app/clients/sms/twilio.py +++ b/app/clients/sms/twilio.py @@ -44,8 +44,10 @@ class TwilioClient(SmsClient): def status(self, message_id): try: response = self.client.messages.get(message_id) - if response.status in ('delivered', 'undelivered', 'failed'): + if response.status in ('delivered', 'failed'): return response.status + elif response.status == 'undelivered': + return 'sending' return None except TwilioRestException as e: current_app.logger.exception(e) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index b1c7fface..9fbb8bf3e 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -112,12 +112,12 @@ def update_query(notification_type, status): TEMPLATE_TYPE_SMS: { STATISTICS_REQUESTED: NotificationStatistics.sms_requested, STATISTICS_DELIVERED: NotificationStatistics.sms_delivered, - STATISTICS_FAILURE: NotificationStatistics.sms_error + STATISTICS_FAILURE: NotificationStatistics.sms_failed }, TEMPLATE_TYPE_EMAIL: { STATISTICS_REQUESTED: NotificationStatistics.emails_requested, STATISTICS_DELIVERED: NotificationStatistics.emails_delivered, - STATISTICS_FAILURE: NotificationStatistics.emails_error + STATISTICS_FAILURE: NotificationStatistics.emails_failed } } return { diff --git a/app/models.py b/app/models.py index 1729aad7d..583e4c6dc 100644 --- a/app/models.py +++ b/app/models.py @@ -88,7 +88,7 @@ class Service(db.Model): nullable=True, onupdate=datetime.datetime.now) active = db.Column(db.Boolean, index=False, unique=False, nullable=False) - limit = db.Column(db.BigInteger, index=False, unique=False, nullable=False) + message_limit = db.Column(db.BigInteger, index=False, unique=False, nullable=False) users = db.relationship( 'User', secondary=user_to_service, @@ -121,10 +121,10 @@ class NotificationStatistics(db.Model): service = db.relationship('Service', backref=db.backref('service_notification_stats', lazy='dynamic')) emails_requested = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0) emails_delivered = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0) - emails_error = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0) + emails_failed = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0) sms_requested = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0) sms_delivered = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0) - sms_error = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0) + sms_failed = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=0) __table_args__ = ( UniqueConstraint('service_id', 'day', name='uix_service_to_day'), @@ -234,7 +234,7 @@ class VerifyCode(db.Model): return check_hash(cde, self._code) -NOTIFICATION_STATUS_TYPES = ['sent', 'delivered', 'failed', 'complaint', 'bounce'] +NOTIFICATION_STATUS_TYPES = ['sending', 'delivered', 'failed'] class Notification(db.Model): @@ -267,7 +267,7 @@ class Notification(db.Model): nullable=True, onupdate=datetime.datetime.utcnow) status = db.Column( - db.Enum(*NOTIFICATION_STATUS_TYPES, name='notification_status_types'), nullable=False, default='sent') + db.Enum(*NOTIFICATION_STATUS_TYPES, name='notification_status_types'), nullable=False, default='sending') reference = db.Column(db.String, nullable=True, index=True) diff --git a/app/notifications/rest.py b/app/notifications/rest.py index ede349d5d..25fca0d78 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -287,8 +287,9 @@ def send_notification(notification_type): total_sms_count = service_stats.sms_requested total_email_count = service_stats.emails_requested - if (total_email_count + total_sms_count >= service.limit) and service.restricted: - return jsonify(result="error", message='Exceeded send limits ({}) for today'.format(service.limit)), 429 + if (total_email_count + total_sms_count >= service.message_limit) and service.restricted: + return jsonify(result="error", message='Exceeded send limits ({}) for today'.format( + service.message_limit)), 429 notification, errors = ( sms_template_notification_schema if notification_type == 'sms' else email_notification_schema diff --git a/migrations/versions/0001_restart_migrations.py b/migrations/versions/0001_restart_migrations.py index fe4499c6f..02da0c524 100644 --- a/migrations/versions/0001_restart_migrations.py +++ b/migrations/versions/0001_restart_migrations.py @@ -22,7 +22,7 @@ def upgrade(): sa.Column('created_at', sa.DateTime(), nullable=False), sa.Column('updated_at', sa.DateTime(), nullable=True), sa.Column('active', sa.Boolean(), nullable=False), - sa.Column('limit', sa.BigInteger(), nullable=False), + sa.Column('message_limit', sa.BigInteger(), nullable=False), sa.Column('restricted', sa.Boolean(), nullable=False), sa.Column('email_from', sa.Text(), nullable=False), sa.PrimaryKeyConstraint('id'), @@ -78,10 +78,10 @@ def upgrade(): sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('emails_requested', sa.BigInteger(), nullable=False), sa.Column('emails_delivered', sa.BigInteger(), nullable=False), - sa.Column('emails_error', sa.BigInteger(), nullable=False), + sa.Column('emails_failed', sa.BigInteger(), nullable=False), sa.Column('sms_requested', sa.BigInteger(), nullable=False), sa.Column('sms_delivered', sa.BigInteger(), nullable=False), - sa.Column('sms_error', sa.BigInteger(), nullable=False), + sa.Column('sms_failed', sa.BigInteger(), nullable=False), sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('service_id', 'day', name='uix_service_to_day') @@ -175,7 +175,7 @@ def upgrade(): sa.Column('sent_at', sa.DateTime(), nullable=True), sa.Column('sent_by', sa.String(), nullable=True), sa.Column('updated_at', sa.DateTime(), nullable=True), - sa.Column('status', sa.Enum('sent', 'delivered', 'failed', 'complaint', 'bounce', name='notification_status_types'), nullable=False), + sa.Column('status', sa.Enum('sending', 'delivered', 'failed', name='notification_status_types'), nullable=False), sa.Column('reference', sa.String(), nullable=True), sa.ForeignKeyConstraint(['job_id'], ['jobs.id'], ), sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), diff --git a/tests/app/authentication/test_authentication.py b/tests/app/authentication/test_authentication.py index 147d8ffb1..bbf706db4 100644 --- a/tests/app/authentication/test_authentication.py +++ b/tests/app/authentication/test_authentication.py @@ -133,7 +133,7 @@ def test_should_allow_valid_token_with_post_body(notify_api, sample_api_key): 'email_from': 'new name', 'name': 'new name', 'users': [service.users[0].id], - 'limit': 1000, + 'message_limit': 1000, 'restricted': False, 'active': False} diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index 2f8fe5da0..04d32a5f8 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -307,7 +307,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat assert persisted_notification.id == notification_id assert persisted_notification.to == '+447234123123' assert persisted_notification.template_id == sample_template_with_placeholders.id - assert persisted_notification.status == 'sent' + assert persisted_notification.status == 'sending' assert persisted_notification.created_at == now assert persisted_notification.sent_at > now assert persisted_notification.sent_by == 'MMG' @@ -481,7 +481,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa assert persisted_notification.to == '+447234123123' assert persisted_notification.job_id == sample_job.id assert persisted_notification.template_id == sample_job.template.id - assert persisted_notification.status == 'sent' + assert persisted_notification.status == 'sending' assert persisted_notification.sent_at > now assert persisted_notification.created_at == now assert persisted_notification.sent_by == 'MMG' @@ -522,7 +522,7 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh assert persisted_notification.template_id == sample_email_template_with_placeholders.id assert persisted_notification.created_at == now assert persisted_notification.sent_at > now - assert persisted_notification.status == 'sent' + assert persisted_notification.status == 'sending' assert persisted_notification.sent_by == 'ses' diff --git a/tests/app/clients/test_aws_ses.py b/tests/app/clients/test_aws_ses.py index 4ed02f9ac..4d6560b87 100644 --- a/tests/app/clients/test_aws_ses.py +++ b/tests/app/clients/test_aws_ses.py @@ -14,7 +14,7 @@ def test_should_return_correct_details_for_delivery(): def test_should_return_correct_details_for_bounced(): response_dict = get_aws_responses('Bounce') assert response_dict['message'] == 'Bounced' - assert response_dict['notification_status'] == 'bounce' + assert response_dict['notification_status'] == 'failed' assert response_dict['notification_statistics_status'] == 'failure' assert not response_dict['success'] @@ -22,9 +22,9 @@ def test_should_return_correct_details_for_bounced(): def test_should_return_correct_details_for_complaint(): response_dict = get_aws_responses('Complaint') assert response_dict['message'] == 'Complaint' - assert response_dict['notification_status'] == 'complaint' - assert response_dict['notification_statistics_status'] == 'failure' - assert not response_dict['success'] + assert response_dict['notification_status'] == 'delivered' + assert response_dict['notification_statistics_status'] == 'delivered' + assert response_dict['success'] def test_should_be_none_if_unrecognised_status_code(): diff --git a/tests/app/clients/test_firetext.py b/tests/app/clients/test_firetext.py index ba35762fc..75586dbed 100644 --- a/tests/app/clients/test_firetext.py +++ b/tests/app/clients/test_firetext.py @@ -22,9 +22,9 @@ def test_should_return_correct_details_for_bounced(): def test_should_return_correct_details_for_complaint(): response_dict = get_firetext_responses('2') assert response_dict['message'] == 'Undelivered (Pending with Network)' - assert response_dict['notification_status'] == 'sent' - assert not response_dict['notification_statistics_status'] - assert not response_dict['success'] + assert response_dict['notification_status'] == 'delivered' + assert response_dict['notification_statistics_status'] == 'delivered' + assert response_dict['success'] def test_should_be_none_if_unrecognised_status_code(): diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 43551d6ee..1fde778e1 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -112,7 +112,7 @@ def sample_service(notify_db, user = sample_user(notify_db, notify_db_session) data = { 'name': service_name, - 'limit': limit, + 'message_limit': limit, 'active': False, 'restricted': restricted, 'email_from': email_from @@ -285,7 +285,7 @@ def sample_notification(notify_db, template=None, job=None, to_field=None, - status='sent', + status='sending', reference=None, created_at=datetime.utcnow()): if service is None: diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index acbc633ae..b9ffc73d4 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -56,7 +56,7 @@ def test_should_by_able_to_update_status_by_reference(sample_email_template): notification = Notification(**data) dao_create_notification(notification, sample_email_template.template_type) - assert Notification.query.get(notification.id).status == "sent" + assert Notification.query.get(notification.id).status == "sending" update_notification_reference_by_id(notification.id, 'reference') update_notification_status_by_reference('reference', 'delivered', 'delivered') assert Notification.query.get(notification.id).status == 'delivered' @@ -68,11 +68,11 @@ def test_should_by_able_to_update_status_by_reference(sample_email_template): ).one().emails_requested == 1 assert NotificationStatistics.query.filter_by( service_id=sample_email_template.service.id - ).one().emails_error == 0 + ).one().emails_failed == 0 def test_should_by_able_to_update_status_by_id(sample_notification): - assert Notification.query.get(sample_notification.id).status == 'sent' + assert Notification.query.get(sample_notification.id).status == 'sending' count = update_notification_status_by_id(sample_notification.id, 'delivered', 'delivered') assert count == 1 assert Notification.query.get(sample_notification.id).status == 'delivered' @@ -84,11 +84,11 @@ def test_should_by_able_to_update_status_by_id(sample_notification): ).one().sms_requested == 1 assert NotificationStatistics.query.filter_by( service_id=sample_notification.service.id - ).one().sms_error == 0 + ).one().sms_failed == 0 def test_should_be_able_to_record_statistics_failure_for_sms(sample_notification): - assert Notification.query.get(sample_notification.id).status == 'sent' + assert Notification.query.get(sample_notification.id).status == 'sending' count = update_notification_status_by_id(sample_notification.id, 'delivered', 'failure') assert count == 1 assert Notification.query.get(sample_notification.id).status == 'delivered' @@ -100,7 +100,7 @@ def test_should_be_able_to_record_statistics_failure_for_sms(sample_notification ).one().sms_requested == 1 assert NotificationStatistics.query.filter_by( service_id=sample_notification.service.id - ).one().sms_error == 1 + ).one().sms_failed == 1 def test_should_be_able_to_record_statistics_failure_for_email(sample_email_template): @@ -117,9 +117,9 @@ def test_should_be_able_to_record_statistics_failure_for_email(sample_email_temp dao_create_notification(notification, sample_email_template.template_type) update_notification_reference_by_id(notification.id, 'reference') - count = update_notification_status_by_reference('reference', 'bounce', 'failure') + count = update_notification_status_by_reference('reference', 'failed', 'failure') assert count == 1 - assert Notification.query.get(notification.id).status == 'bounce' + assert Notification.query.get(notification.id).status == 'failed' assert NotificationStatistics.query.filter_by( service_id=notification.service.id ).one().emails_delivered == 0 @@ -128,7 +128,7 @@ def test_should_be_able_to_record_statistics_failure_for_email(sample_email_temp ).one().emails_requested == 1 assert NotificationStatistics.query.filter_by( service_id=notification.service.id - ).one().emails_error == 1 + ).one().emails_failed == 1 def test_should_return_zero_count_if_no_notification_with_id(): @@ -157,12 +157,12 @@ def test_should_be_able_to_get_statistics_for_a_service(sample_template): assert stats[0].emails_requested == 0 assert stats[0].sms_requested == 1 assert stats[0].sms_delivered == 0 - assert stats[0].sms_error == 0 + assert stats[0].sms_failed == 0 assert stats[0].day == notification.created_at.strftime(DATE_FORMAT) assert stats[0].service_id == notification.service_id assert stats[0].emails_requested == 0 assert stats[0].emails_delivered == 0 - assert stats[0].emails_error == 0 + assert stats[0].emails_failed == 0 def test_should_be_able_to_get_statistics_for_a_service_for_a_day(sample_template): @@ -182,10 +182,10 @@ def test_should_be_able_to_get_statistics_for_a_service_for_a_day(sample_templat sample_template.service.id, now.strftime(DATE_FORMAT) ) assert stat.emails_requested == 0 - assert stat.emails_error == 0 + assert stat.emails_failed == 0 assert stat.emails_delivered == 0 assert stat.sms_requested == 1 - assert stat.sms_error == 0 + assert stat.sms_failed == 0 assert stat.sms_delivered == 0 assert stat.day == notification.created_at.strftime(DATE_FORMAT) assert stat.service_id == notification.service_id @@ -303,7 +303,7 @@ def test_save_notification_creates_sms_and_template_stats(sample_template, sampl assert data['service'] == notification_from_db.service assert data['template'] == notification_from_db.template assert data['created_at'] == notification_from_db.created_at - assert 'sent' == notification_from_db.status + assert 'sending' == notification_from_db.status assert Job.query.get(sample_job.id).notifications_sent == 1 stats = NotificationStatistics.query.filter( @@ -348,7 +348,7 @@ def test_save_notification_and_create_email_and_template_stats(sample_email_temp assert data['service'] == notification_from_db.service assert data['template'] == notification_from_db.template assert data['created_at'] == notification_from_db.created_at - assert 'sent' == notification_from_db.status + assert 'sending' == notification_from_db.status assert Job.query.get(sample_job.id).notifications_sent == 1 stats = NotificationStatistics.query.filter( @@ -537,7 +537,7 @@ def test_save_notification_and_increment_job(sample_template, sample_job): assert data['service'] == notification_from_db.service assert data['template'] == notification_from_db.template assert data['created_at'] == notification_from_db.created_at - assert 'sent' == notification_from_db.status + assert 'sending' == notification_from_db.status assert Job.query.get(sample_job.id).notifications_sent == 1 notification_2 = Notification(**data) @@ -603,7 +603,7 @@ def test_save_notification_and_increment_correct_job(notify_db, notify_db_sessio assert data['service'] == notification_from_db.service assert data['template'] == notification_from_db.template assert data['created_at'] == notification_from_db.created_at - assert 'sent' == notification_from_db.status + assert 'sending' == notification_from_db.status assert Job.query.get(job_1.id).notifications_sent == 1 assert Job.query.get(job_2.id).notifications_sent == 0 @@ -629,7 +629,7 @@ def test_save_notification_with_no_job(sample_template): assert data['service'] == notification_from_db.service assert data['template'] == notification_from_db.template assert data['created_at'] == notification_from_db.created_at - assert 'sent' == notification_from_db.status + assert 'sending' == notification_from_db.status def test_get_notification(sample_notification): @@ -660,7 +660,7 @@ def test_save_notification_no_job_id(sample_template): assert data['to'] == notification_from_db.to assert data['service'] == notification_from_db.service assert data['template'] == notification_from_db.template - assert 'sent' == notification_from_db.status + assert 'sending' == notification_from_db.status def test_get_notification_for_job(sample_notification): @@ -694,7 +694,7 @@ def test_get_all_notifications_for_job(notify_db, notify_db_session, sample_job) def test_update_notification(sample_notification, sample_template): - assert sample_notification.status == 'sent' + assert sample_notification.status == 'sending' sample_notification.status = 'failed' dao_update_notification(sample_notification) notification_from_db = Notification.query.get(sample_notification.id) @@ -706,7 +706,7 @@ def test_should_delete_notifications_after_one_day(notify_db, notify_db_session) sample_notification(notify_db, notify_db_session, created_at=created_at) sample_notification(notify_db, notify_db_session, created_at=created_at) assert len(Notification.query.all()) == 2 - delete_notifications_created_more_than_a_day_ago('sent') + delete_notifications_created_more_than_a_day_ago('sending') assert len(Notification.query.all()) == 0 @@ -726,7 +726,7 @@ def test_should_not_delete_sent_notifications_before_one_day(notify_db, notify_d sample_notification(notify_db, notify_db_session, created_at=valid, to_field="valid") assert len(Notification.query.all()) == 2 - delete_notifications_created_more_than_a_day_ago('sent') + delete_notifications_created_more_than_a_day_ago('sending') assert len(Notification.query.all()) == 1 assert Notification.query.first().to == 'valid' diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index 140256686..65ab67830 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -17,7 +17,7 @@ from sqlalchemy.exc import IntegrityError def test_create_service(sample_user): assert Service.query.count() == 0 - service = Service(name="service_name", email_from="email_from", limit=1000, active=True, restricted=False) + service = Service(name="service_name", email_from="email_from", message_limit=1000, active=True, restricted=False) dao_create_service(service, sample_user) assert Service.query.count() == 1 assert Service.query.first().name == "service_name" @@ -27,8 +27,8 @@ def test_create_service(sample_user): def test_cannot_create_two_services_with_same_name(sample_user): assert Service.query.count() == 0 - service1 = Service(name="service_name", email_from="email_from1", limit=1000, active=True, restricted=False) - service2 = Service(name="service_name", email_from="email_from2", limit=1000, active=True, restricted=False) + service1 = Service(name="service_name", email_from="email_from1", message_limit=1000, active=True, restricted=False) + service2 = Service(name="service_name", email_from="email_from2", message_limit=1000, active=True, restricted=False) with pytest.raises(IntegrityError) as excinfo: dao_create_service(service1, sample_user) dao_create_service(service2, sample_user) @@ -37,8 +37,8 @@ def test_cannot_create_two_services_with_same_name(sample_user): def test_cannot_create_two_services_with_same_email_from(sample_user): assert Service.query.count() == 0 - service1 = Service(name="service_name1", email_from="email_from", limit=1000, active=True, restricted=False) - service2 = Service(name="service_name2", email_from="email_from", limit=1000, active=True, restricted=False) + service1 = Service(name="service_name1", email_from="email_from", message_limit=1000, active=True, restricted=False) + service2 = Service(name="service_name2", email_from="email_from", message_limit=1000, active=True, restricted=False) with pytest.raises(IntegrityError) as excinfo: dao_create_service(service1, sample_user) dao_create_service(service2, sample_user) @@ -47,14 +47,14 @@ def test_cannot_create_two_services_with_same_email_from(sample_user): def test_cannot_create_service_with_no_user(notify_db_session): assert Service.query.count() == 0 - service = Service(name="service_name", email_from="email_from", limit=1000, active=True, restricted=False) + service = Service(name="service_name", email_from="email_from", message_limit=1000, active=True, restricted=False) with pytest.raises(FlushError) as excinfo: dao_create_service(service, None) assert "Can't flush None value found in collection Service.users" in str(excinfo.value) def test_should_add_user_to_service(sample_user): - service = Service(name="service_name", email_from="email_from", limit=1000, active=True, restricted=False) + service = Service(name="service_name", email_from="email_from", message_limit=1000, active=True, restricted=False) dao_create_service(service, sample_user) assert sample_user in Service.query.first().users new_user = User( @@ -69,7 +69,7 @@ def test_should_add_user_to_service(sample_user): def test_should_remove_user_from_service(sample_user): - service = Service(name="service_name", email_from="email_from", limit=1000, active=True, restricted=False) + service = Service(name="service_name", email_from="email_from", message_limit=1000, active=True, restricted=False) dao_create_service(service, sample_user) new_user = User( name='Test User', diff --git a/tests/app/notifications/test_process_client_response.py b/tests/app/notifications/test_process_client_response.py index 169161e6e..8e4e1580a 100644 --- a/tests/app/notifications/test_process_client_response.py +++ b/tests/app/notifications/test_process_client_response.py @@ -58,7 +58,7 @@ def test_process_sms_response_updates_notification_stats_for_valid_request(notif assert len(stats) == 1 assert stats[0].sms_requested == 1 assert stats[0].sms_delivered == 0 - assert stats[0].sms_error == 0 + assert stats[0].sms_failed == 0 success, error = process_sms_client_response(status='0', reference=str(sample_notification.id), client_name='Firetext') assert error is None @@ -67,7 +67,7 @@ def test_process_sms_response_updates_notification_stats_for_valid_request(notif assert len(stats) == 1 assert stats[0].sms_requested == 1 assert stats[0].sms_delivered == 1 - assert stats[0].sms_error == 0 + assert stats[0].sms_failed == 0 def test_process_sms_response_updates_notification_stats_for_valid_request_with_failed_status(notify_api, @@ -79,7 +79,7 @@ def test_process_sms_response_updates_notification_stats_for_valid_request_with_ assert len(stats) == 1 assert stats[0].sms_requested == 1 assert stats[0].sms_delivered == 0 - assert stats[0].sms_error == 0 + assert stats[0].sms_failed == 0 success, error = process_sms_client_response(status='1', reference=str(sample_notification.id), client_name='Firetext') assert success == "{} callback succeeded. reference {} updated".format('Firetext', sample_notification.id) @@ -88,4 +88,4 @@ def test_process_sms_response_updates_notification_stats_for_valid_request_with_ assert len(stats) == 1 assert stats[0].sms_requested == 1 assert stats[0].sms_delivered == 0 - assert stats[0].sms_error == 1 + assert stats[0].sms_failed == 1 diff --git a/tests/app/notifications/test_rest.py b/tests/app/notifications/test_rest.py index 4b5804b79..9bc5813a4 100644 --- a/tests/app/notifications/test_rest.py +++ b/tests/app/notifications/test_rest.py @@ -28,7 +28,7 @@ def test_get_notification_by_id(notify_api, sample_notification): headers=[auth_header]) notification = json.loads(response.get_data(as_text=True))['data']['notification'] - assert notification['status'] == 'sent' + assert notification['status'] == 'sending' assert notification['template'] == { 'id': str(sample_notification.template.id), 'name': sample_notification.template.name, @@ -74,7 +74,7 @@ def test_get_all_notifications(notify_api, sample_notification): headers=[auth_header]) notifications = json.loads(response.get_data(as_text=True)) - assert notifications['notifications'][0]['status'] == 'sent' + assert notifications['notifications'][0]['status'] == 'sending' assert notifications['notifications'][0]['template'] == { 'id': str(sample_notification.template.id), 'name': sample_notification.template.name, @@ -400,13 +400,13 @@ def test_filter_by_multiple_statuss(notify_api, method='GET') response = client.get( - '/notifications?status=delivered&status=sent', + '/notifications?status=delivered&status=sending', headers=[auth_header]) assert response.status_code == 200 notifications = json.loads(response.get_data(as_text=True)) assert len(notifications['notifications']) == 2 - set(['delivered', 'sent']) == set( + set(['delivered', 'sending']) == set( [x['status'] for x in notifications['notifications']]) @@ -1001,7 +1001,7 @@ def test_no_limit_for_live_service(notify_api, with notify_api.test_request_context(): with notify_api.test_client() as client: - sample_service.limit = 1 + sample_service.message_limit = 1 notify_db.session.add(sample_service) notify_db.session.commit() @@ -1211,7 +1211,7 @@ def test_firetext_callback_should_update_notification_status(notify_api, sample_ with notify_api.test_request_context(): with notify_api.test_client() as client: original = get_notification_by_id(sample_notification.id) - assert original.status == 'sent' + assert original.status == 'sending' response = client.post( path='/notifications/sms/firetext', @@ -1231,14 +1231,14 @@ def test_firetext_callback_should_update_notification_status(notify_api, sample_ assert get_notification_by_id(sample_notification.id).status == 'delivered' assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_delivered == 1 assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_requested == 1 - assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_error == 0 + assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_failed == 0 def test_firetext_callback_should_update_notification_status_failed(notify_api, sample_notification): with notify_api.test_request_context(): with notify_api.test_client() as client: original = get_notification_by_id(sample_notification.id) - assert original.status == 'sent' + assert original.status == 'sending' response = client.post( path='/notifications/sms/firetext', @@ -1257,7 +1257,7 @@ def test_firetext_callback_should_update_notification_status_failed(notify_api, assert updated.status == 'failed' assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_delivered == 0 assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_requested == 1 - assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_error == 1 + assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_failed == 1 def test_firetext_callback_should_update_notification_status_sent(notify_api, notify_db, notify_db_session): @@ -1275,16 +1275,17 @@ def test_firetext_callback_should_update_notification_status_sent(notify_api, no headers=[('Content-Type', 'application/x-www-form-urlencoded')]) json_resp = json.loads(response.get_data(as_text=True)) + print(json_resp) assert response.status_code == 200 assert json_resp['result'] == 'success' assert json_resp['message'] == 'Firetext callback succeeded. reference {} updated'.format( notification.id ) updated = get_notification_by_id(notification.id) - assert updated.status == 'sent' - assert dao_get_notification_statistics_for_service(notification.service_id)[0].sms_delivered == 0 + assert updated.status == 'delivered' + assert dao_get_notification_statistics_for_service(notification.service_id)[0].sms_delivered == 1 assert dao_get_notification_statistics_for_service(notification.service_id)[0].sms_requested == 1 - assert dao_get_notification_statistics_for_service(notification.service_id)[0].sms_error == 0 + assert dao_get_notification_statistics_for_service(notification.service_id)[0].sms_failed == 0 def test_firetext_callback_should_update_multiple_notification_status_sent(notify_api, notify_db, notify_db_session): @@ -1317,7 +1318,7 @@ def test_firetext_callback_should_update_multiple_notification_status_sent(notif assert dao_get_notification_statistics_for_service(notification1.service_id)[0].sms_delivered == 3 assert dao_get_notification_statistics_for_service(notification1.service_id)[0].sms_requested == 3 - assert dao_get_notification_statistics_for_service(notification1.service_id)[0].sms_error == 0 + assert dao_get_notification_statistics_for_service(notification1.service_id)[0].sms_failed == 0 def test_process_mmg_response_return_200_when_cid_is_send_sms_code(notify_api): @@ -1458,7 +1459,7 @@ def test_ses_callback_should_update_notification_status( reference='ref' ) - assert get_notification_by_id(notification.id).status == 'sent' + assert get_notification_by_id(notification.id).status == 'sending' response = client.post( path='/notifications/email/ses', @@ -1472,7 +1473,7 @@ def test_ses_callback_should_update_notification_status( assert get_notification_by_id(notification.id).status == 'delivered' assert dao_get_notification_statistics_for_service(notification.service_id)[0].emails_delivered == 1 assert dao_get_notification_statistics_for_service(notification.service_id)[0].emails_requested == 1 - assert dao_get_notification_statistics_for_service(notification.service_id)[0].emails_error == 0 + assert dao_get_notification_statistics_for_service(notification.service_id)[0].emails_failed == 0 def test_ses_callback_should_update_multiple_notification_status_sent( @@ -1527,7 +1528,7 @@ def test_ses_callback_should_update_multiple_notification_status_sent( assert dao_get_notification_statistics_for_service(notification1.service_id)[0].emails_delivered == 3 assert dao_get_notification_statistics_for_service(notification1.service_id)[0].emails_requested == 3 - assert dao_get_notification_statistics_for_service(notification1.service_id)[0].emails_error == 0 + assert dao_get_notification_statistics_for_service(notification1.service_id)[0].emails_failed == 0 def test_should_handle_invite_email_callbacks(notify_api, notify_db, notify_db_session): diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 1d73f9fcb..31641097a 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -178,7 +178,7 @@ def test_create_service(notify_api, sample_user): data = { 'name': 'created service', 'user_id': str(sample_user.id), - 'limit': 1000, + 'message_limit': 1000, 'restricted': False, 'active': False, 'email_from': 'created.service'} @@ -218,7 +218,7 @@ def test_should_not_create_service_with_missing_user_id_field(notify_api): data = { 'email_from': 'service', 'name': 'created service', - 'limit': 1000, + 'message_limit': 1000, 'restricted': False, 'active': False } @@ -248,7 +248,7 @@ def test_should_not_create_service_with_missing_if_user_id_is_not_in_database(no 'email_from': 'service', 'user_id': fake_uuid, 'name': 'created service', - 'limit': 1000, + 'message_limit': 1000, 'restricted': False, 'active': False } @@ -289,7 +289,7 @@ def test_should_not_create_service_if_missing_data(notify_api, sample_user): assert json_resp['result'] == 'error' assert 'Missing data for required field.' in json_resp['message']['name'] assert 'Missing data for required field.' in json_resp['message']['active'] - assert 'Missing data for required field.' in json_resp['message']['limit'] + assert 'Missing data for required field.' in json_resp['message']['message_limit'] assert 'Missing data for required field.' in json_resp['message']['restricted'] @@ -303,7 +303,7 @@ def test_should_not_create_service_with_duplicate_name(notify_api, data = { 'name': sample_service.name, 'user_id': str(sample_service.users[0].id), - 'limit': 1000, + 'message_limit': 1000, 'restricted': False, 'active': False, 'email_from': 'sample.service2'} @@ -331,7 +331,7 @@ def test_create_service_should_throw_duplicate_key_constraint_for_existing_email data = { 'name': 'First SERVICE', 'user_id': str(first_service.users[0].id), - 'limit': 1000, + 'message_limit': 1000, 'restricted': False, 'active': False, 'email_from': 'first.service'} @@ -547,7 +547,7 @@ def test_default_permissions_are_added_for_user_service(notify_api, data = { 'name': 'created service', 'user_id': str(sample_user.id), - 'limit': 1000, + 'message_limit': 1000, 'restricted': False, 'active': False, 'email_from': 'created.service'} diff --git a/tests/conftest.py b/tests/conftest.py index bc25b6cc4..1b45b6369 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -27,28 +27,23 @@ def notify_api(request): @pytest.fixture(scope='session') def notify_db(notify_api, request): - try: - Migrate(notify_api, db) - Manager(db, MigrateCommand) - BASE_DIR = os.path.dirname(os.path.dirname(__file__)) - ALEMBIC_CONFIG = os.path.join(BASE_DIR, 'migrations') - config = Config(ALEMBIC_CONFIG + '/alembic.ini') - config.set_main_option("script_location", ALEMBIC_CONFIG) + Migrate(notify_api, db) + Manager(db, MigrateCommand) + BASE_DIR = os.path.dirname(os.path.dirname(__file__)) + ALEMBIC_CONFIG = os.path.join(BASE_DIR, 'migrations') + config = Config(ALEMBIC_CONFIG + '/alembic.ini') + config.set_main_option("script_location", ALEMBIC_CONFIG) - with notify_api.app_context(): - upgrade(config, 'head') + with notify_api.app_context(): + upgrade(config, 'head') - def teardown(): - db.session.remove() - db.engine.execute("drop sequence services_id_seq cascade") - db.drop_all() - db.engine.execute("drop table alembic_version") - db.get_engine(notify_api).dispose() + def teardown(): + db.session.remove() + db.drop_all() + db.engine.execute("drop table alembic_version") + db.get_engine(notify_api).dispose() - request.addfinalizer(teardown) - except: - import traceback - traceback.print_exc() + request.addfinalizer(teardown) return db