Merge branch 'master' into service-not-found-returns-404

Conflicts:
	app/errors.py
This commit is contained in:
Rebecca Law
2016-02-25 15:33:53 +00:00
36 changed files with 917 additions and 767 deletions

View File

@@ -0,0 +1,7 @@
import os
def load_example_csv(file):
file_path = os.path.join("test_csv_files", "{}.csv".format(file))
with open(file_path) as f:
return f.read()

View File

@@ -1,13 +1,88 @@
import uuid
import pytest
from flask import current_app
from app.celery.tasks import (send_sms, send_sms_code, send_email_code, send_email)
from app.celery.tasks import (send_sms, send_sms_code, send_email_code, send_email, process_job)
from app import (firetext_client, aws_ses_client, encryption)
from app.clients.email.aws_ses import AwsSesClientException
from app.clients.sms.firetext import FiretextClientException
from app.dao import notifications_dao
from app.dao import notifications_dao, jobs_dao
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm.exc import NoResultFound
from app.celery.tasks import s3
from app.celery import tasks
from tests.app import load_example_csv
from datetime import datetime
from freezegun import freeze_time
@freeze_time("2016-01-01 11:09:00.061258")
def test_should_process_sms_job(sample_job, mocker):
mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('sms'))
mocker.patch('app.celery.tasks.send_sms.apply_async')
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
mocker.patch('app.celery.tasks.create_uuid', return_value="uuid")
process_job(sample_job.id)
s3.get_job_from_s3.assert_called_once_with(sample_job.bucket_name, sample_job.id)
tasks.send_sms.apply_async.assert_called_once_with(
(str(sample_job.service_id),
"uuid",
"something_encrypted",
"2016-01-01 11:09:00.061258"),
queue="bulk-sms"
)
job = jobs_dao.dao_get_job_by_id(sample_job.id)
assert job.status == 'finished'
def test_should_not_create_send_task_for_empty_file(sample_job, mocker):
mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('empty'))
mocker.patch('app.celery.tasks.send_sms.apply_async')
process_job(sample_job.id)
s3.get_job_from_s3.assert_called_once_with(sample_job.bucket_name, sample_job.id)
job = jobs_dao.dao_get_job_by_id(sample_job.id)
assert job.status == 'finished'
tasks.send_sms.apply_async.assert_not_called
@freeze_time("2016-01-01 11:09:00.061258")
def test_should_process_email_job(sample_email_job, mocker):
mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('email'))
mocker.patch('app.celery.tasks.send_email.apply_async')
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
mocker.patch('app.celery.tasks.create_uuid', return_value="uuid")
process_job(sample_email_job.id)
s3.get_job_from_s3.assert_called_once_with(sample_email_job.bucket_name, sample_email_job.id)
tasks.send_email.apply_async.assert_called_once_with(
(str(sample_email_job.service_id),
"uuid",
sample_email_job.template.subject,
"{}@{}".format(sample_email_job.service.email_from, "test.notify.com"),
"something_encrypted",
"2016-01-01 11:09:00.061258"),
queue="bulk-email"
)
job = jobs_dao.dao_get_job_by_id(sample_email_job.id)
assert job.status == 'finished'
def test_should_process_all_sms_job(sample_job, mocker):
mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('multiple_sms'))
mocker.patch('app.celery.tasks.send_sms.apply_async')
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
mocker.patch('app.celery.tasks.create_uuid', return_value="uuid")
process_job(sample_job.id)
s3.get_job_from_s3.assert_called_once_with(sample_job.bucket_name, sample_job.id)
tasks.send_sms.apply_async.call_count == 10
job = jobs_dao.dao_get_job_by_id(sample_job.id)
assert job.status == 'finished'
def test_should_send_template_to_correct_sms_provider_and_persist(sample_template, mocker):
@@ -17,13 +92,16 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
}
mocker.patch('app.encryption.decrypt', return_value=notification)
mocker.patch('app.firetext_client.send_sms')
mocker.patch('app.firetext_client.get_name', return_value="firetext")
notification_id = uuid.uuid4()
now = datetime.utcnow()
send_sms(
sample_template.service_id,
notification_id,
"encrypted-in-reality")
"encrypted-in-reality",
now
)
firetext_client.send_sms.assert_called_once_with("+441234123123", sample_template.content)
persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
@@ -31,24 +109,60 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
assert persisted_notification.to == '+441234123123'
assert persisted_notification.template_id == sample_template.id
assert persisted_notification.status == 'sent'
assert persisted_notification.created_at == now
assert persisted_notification.sent_at > now
assert persisted_notification.sent_by == 'firetext'
assert not persisted_notification.job_id
def test_should_send_template_to_email_provider_and_persist(sample_email_template, mocker):
def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sample_job, mocker):
notification = {
"template": sample_job.template.id,
"job": sample_job.id,
"to": "+441234123123"
}
mocker.patch('app.encryption.decrypt', return_value=notification)
mocker.patch('app.firetext_client.send_sms')
mocker.patch('app.firetext_client.get_name', return_value="firetext")
notification_id = uuid.uuid4()
now = datetime.utcnow()
send_sms(
sample_job.service.id,
notification_id,
"encrypted-in-reality",
now)
firetext_client.send_sms.assert_called_once_with("+441234123123", sample_job.template.content)
persisted_notification = notifications_dao.get_notification(sample_job.template.service_id, notification_id)
assert persisted_notification.id == notification_id
assert persisted_notification.to == '+441234123123'
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.sent_at > now
assert persisted_notification.created_at == now
assert persisted_notification.sent_by == 'firetext'
def test_should_use_email_template_and_persist(sample_email_template, mocker):
notification = {
"template": sample_email_template.id,
"to": "my_email@my_email.com"
}
mocker.patch('app.encryption.decrypt', return_value=notification)
mocker.patch('app.aws_ses_client.send_email')
mocker.patch('app.aws_ses_client.get_name', return_value='ses')
notification_id = uuid.uuid4()
now = datetime.utcnow()
send_email(
sample_email_template.service_id,
notification_id,
'subject',
'email_from',
"encrypted-in-reality")
"encrypted-in-reality",
now)
aws_ses_client.send_email.assert_called_once_with(
"email_from",
@@ -60,7 +174,10 @@ def test_should_send_template_to_email_provider_and_persist(sample_email_templat
assert persisted_notification.id == notification_id
assert persisted_notification.to == 'my_email@my_email.com'
assert persisted_notification.template_id == sample_email_template.id
assert persisted_notification.created_at == now
assert persisted_notification.sent_at > now
assert persisted_notification.status == 'sent'
assert persisted_notification.sent_by == 'ses'
def test_should_persist_notification_as_failed_if_sms_client_fails(sample_template, mocker):
@@ -70,13 +187,16 @@ def test_should_persist_notification_as_failed_if_sms_client_fails(sample_templa
}
mocker.patch('app.encryption.decrypt', return_value=notification)
mocker.patch('app.firetext_client.send_sms', side_effect=FiretextClientException())
mocker.patch('app.firetext_client.get_name', return_value="firetext")
now = datetime.utcnow()
notification_id = uuid.uuid4()
send_sms(
sample_template.service_id,
notification_id,
"encrypted-in-reality")
"encrypted-in-reality",
now)
firetext_client.send_sms.assert_called_once_with("+441234123123", sample_template.content)
persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
@@ -84,6 +204,9 @@ def test_should_persist_notification_as_failed_if_sms_client_fails(sample_templa
assert persisted_notification.to == '+441234123123'
assert persisted_notification.template_id == sample_template.id
assert persisted_notification.status == 'failed'
assert persisted_notification.created_at == now
assert persisted_notification.sent_at > now
assert persisted_notification.sent_by == 'firetext'
def test_should_persist_notification_as_failed_if_email_client_fails(sample_email_template, mocker):
@@ -93,6 +216,9 @@ def test_should_persist_notification_as_failed_if_email_client_fails(sample_emai
}
mocker.patch('app.encryption.decrypt', return_value=notification)
mocker.patch('app.aws_ses_client.send_email', side_effect=AwsSesClientException())
mocker.patch('app.aws_ses_client.get_name', return_value="ses")
now = datetime.utcnow()
notification_id = uuid.uuid4()
@@ -101,7 +227,8 @@ def test_should_persist_notification_as_failed_if_email_client_fails(sample_emai
notification_id,
'subject',
'email_from',
"encrypted-in-reality")
"encrypted-in-reality",
now)
aws_ses_client.send_email.assert_called_once_with(
"email_from",
@@ -114,6 +241,9 @@ def test_should_persist_notification_as_failed_if_email_client_fails(sample_emai
assert persisted_notification.to == 'my_email@my_email.com'
assert persisted_notification.template_id == sample_email_template.id
assert persisted_notification.status == 'failed'
assert persisted_notification.created_at == now
assert persisted_notification.sent_by == 'ses'
assert persisted_notification.sent_at > now
def test_should_not_send_sms_if_db_peristance_failed(sample_template, mocker):
@@ -124,13 +254,15 @@ def test_should_not_send_sms_if_db_peristance_failed(sample_template, mocker):
mocker.patch('app.encryption.decrypt', return_value=notification)
mocker.patch('app.firetext_client.send_sms')
mocker.patch('app.db.session.add', side_effect=SQLAlchemyError())
now = datetime.utcnow()
notification_id = uuid.uuid4()
send_sms(
sample_template.service_id,
notification_id,
"encrypted-in-reality")
"encrypted-in-reality",
now)
firetext_client.send_sms.assert_not_called()
with pytest.raises(NoResultFound) as e:
@@ -146,6 +278,7 @@ def test_should_not_send_email_if_db_peristance_failed(sample_email_template, mo
mocker.patch('app.encryption.decrypt', return_value=notification)
mocker.patch('app.aws_ses_client.send_email')
mocker.patch('app.db.session.add', side_effect=SQLAlchemyError())
now = datetime.utcnow()
notification_id = uuid.uuid4()
@@ -154,7 +287,8 @@ def test_should_not_send_email_if_db_peristance_failed(sample_email_template, mo
notification_id,
'subject',
'email_from',
"encrypted-in-reality")
"encrypted-in-reality",
now)
aws_ses_client.send_email.assert_not_called()
with pytest.raises(NoResultFound) as e:
@@ -192,7 +326,9 @@ def test_should_send_email_code(mocker):
send_email_code(encrypted_verification)
aws_ses_client.send_email.assert_called_once_with(current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'],
verification['to'],
"Verification code",
verification['secret_code'])
aws_ses_client.send_email.assert_called_once_with(
current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'],
verification['to'],
"Verification code",
verification['secret_code']
)

View File

@@ -1,13 +1,13 @@
import pytest
from datetime import datetime
from app import email_safe
from app.models import (User, Service, Template, ApiKey, Job, Notification, InvitedUser)
from app.dao.users_dao import (save_model_user, create_user_code, create_secret_code)
from app.dao.services_dao import dao_create_service
from app.dao.templates_dao import save_model_template
from app.dao.templates_dao import dao_create_template
from app.dao.api_key_dao import save_model_api_key
from app.dao.jobs_dao import save_job
from app.dao.notifications_dao import save_notification
from app.dao.jobs_dao import dao_create_job
from app.dao.notifications_dao import dao_create_notification
from app.dao.invited_user_dao import save_invited_user
import uuid
@@ -104,7 +104,6 @@ def sample_service(notify_db,
user = sample_user(notify_db, notify_db_session)
data = {
'name': service_name,
'users': [],
'limit': 1000,
'active': False,
'restricted': False,
@@ -139,7 +138,7 @@ def sample_template(notify_db,
'subject': subject_line
})
template = Template(**data)
save_model_template(template)
dao_create_template(template)
return template
@@ -166,7 +165,7 @@ def sample_email_template(
'subject': subject_line
})
template = Template(**data)
save_model_template(template)
dao_create_template(template)
return template
@@ -205,7 +204,36 @@ def sample_job(notify_db,
'notification_count': 1
}
job = Job(**data)
save_job(job)
dao_create_job(job)
return job
@pytest.fixture(scope='function')
def sample_email_job(notify_db,
notify_db_session,
service=None,
template=None):
if service is None:
service = sample_service(notify_db, notify_db_session)
if template is None:
template = sample_email_template(
notify_db,
notify_db_session,
service=service)
job_id = uuid.uuid4()
bucket_name = 'service-{}-notify'.format(service.id)
file_name = '{}.csv'.format(job_id)
data = {
'id': uuid.uuid4(),
'service_id': service.id,
'template_id': template.id,
'bucket_name': bucket_name,
'file_name': file_name,
'original_file_name': 'some.csv',
'notification_count': 1
}
job = Job(**data)
dao_create_job(job)
return job
@@ -249,10 +277,11 @@ def sample_notification(notify_db,
'to': to,
'job': job,
'service': service,
'template': template
'template': template,
'created_at': datetime.utcnow()
}
notification = Notification(**data)
save_notification(notification)
dao_create_notification(notification)
return notification

View File

@@ -1,18 +1,16 @@
import uuid
import json
from app.dao.jobs_dao import (
save_job,
get_job,
get_jobs_by_service,
_get_jobs
dao_get_job_by_service_id_and_job_id,
dao_create_job,
dao_update_job,
dao_get_jobs_by_service_id
)
from app.models import Job
def test_save_job(notify_db, notify_db_session, sample_template):
def test_create_job(sample_template):
assert Job.query.count() == 0
job_id = uuid.uuid4()
@@ -29,39 +27,33 @@ def test_save_job(notify_db, notify_db_session, sample_template):
}
job = Job(**data)
save_job(job)
dao_create_job(job)
assert Job.query.count() == 1
job_from_db = Job.query.get(job_id)
assert job == job_from_db
def test_get_job_by_id(notify_db, notify_db_session, sample_job):
job_from_db = get_job(sample_job.service.id, sample_job.id)
def test_get_job_by_id(sample_job):
job_from_db = dao_get_job_by_service_id_and_job_id(sample_job.service.id, sample_job.id)
assert sample_job == job_from_db
def test_get_jobs_for_service(notify_db, notify_db_session, sample_template):
from tests.app.conftest import sample_job as create_job
from tests.app.conftest import sample_service as create_service
from tests.app.conftest import sample_template as create_template
from tests.app.conftest import sample_user as create_user
one_job = create_job(notify_db, notify_db_session, sample_template.service,
sample_template)
one_job = create_job(notify_db, notify_db_session, sample_template.service, sample_template)
other_user = create_user(notify_db, notify_db_session,
email="test@digital.cabinet-office.gov.uk")
other_service = create_service(notify_db, notify_db_session,
user=other_user, service_name="other service")
other_template = create_template(notify_db, notify_db_session,
service=other_service)
other_job = create_job(notify_db, notify_db_session, service=other_service,
template=other_template)
other_user = create_user(notify_db, notify_db_session, email="test@digital.cabinet-office.gov.uk")
other_service = create_service(notify_db, notify_db_session, user=other_user, service_name="other service")
other_template = create_template(notify_db, notify_db_session, service=other_service)
other_job = create_job(notify_db, notify_db_session, service=other_service, template=other_template)
one_job_from_db = get_jobs_by_service(one_job.service_id)
other_job_from_db = get_jobs_by_service(other_job.service_id)
one_job_from_db = dao_get_jobs_by_service_id(one_job.service_id)
other_job_from_db = dao_get_jobs_by_service_id(other_job.service_id)
assert len(one_job_from_db) == 1
assert one_job == one_job_from_db[0]
@@ -72,31 +64,12 @@ def test_get_jobs_for_service(notify_db, notify_db_session, sample_template):
assert one_job_from_db != other_job_from_db
def test_get_all_jobs(notify_db, notify_db_session, sample_template):
from tests.app.conftest import sample_job as create_job
for i in range(5):
create_job(notify_db,
notify_db_session,
sample_template.service,
sample_template)
jobs_from_db = _get_jobs()
assert len(jobs_from_db) == 5
def test_update_job(notify_db, notify_db_session, sample_job):
def test_update_job(sample_job):
assert sample_job.status == 'pending'
update_dict = {
'id': sample_job.id,
'service': sample_job.service.id,
'template': sample_job.template.id,
'bucket_name': sample_job.bucket_name,
'file_name': sample_job.file_name,
'original_file_name': sample_job.original_file_name,
'status': 'in progress'
}
sample_job.status = 'in progress'
save_job(sample_job, update_dict=update_dict)
dao_update_job(sample_job)
job_from_db = Job.query.get(sample_job.id)

View File

@@ -1,26 +1,27 @@
from app.models import Notification
from datetime import datetime
from app.dao.notifications_dao import (
save_notification,
dao_create_notification,
dao_update_notification,
get_notification,
get_notification_for_job,
get_notifications_for_job
)
def test_save_notification(notify_db, notify_db_session, sample_template, sample_job):
def test_save_notification(sample_template, sample_job):
assert Notification.query.count() == 0
to = '+44709123456'
data = {
'to': to,
'to': '+44709123456',
'job': sample_job,
'service': sample_template.service,
'template': sample_template
'template': sample_template,
'created_at': datetime.utcnow()
}
notification = Notification(**data)
save_notification(notification)
dao_create_notification(notification)
assert Notification.query.count() == 1
notification_from_db = Notification.query.all()[0]
@@ -29,28 +30,30 @@ def test_save_notification(notify_db, notify_db_session, sample_template, sample
assert data['job'] == notification_from_db.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
def test_get_notification(notify_db, notify_db_session, sample_notification):
def test_get_notification(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):
def test_save_notification_no_job_id(sample_template):
assert Notification.query.count() == 0
to = '+44709123456'
data = {
'to': to,
'service': sample_template.service,
'template': sample_template
'template': sample_template,
'created_at': datetime.utcnow()
}
notification = Notification(**data)
save_notification(notification)
dao_create_notification(notification)
assert Notification.query.count() == 1
notification_from_db = Notification.query.all()[0]
@@ -61,7 +64,7 @@ def test_save_notification_no_job_id(notify_db, notify_db_session, sample_templa
assert 'sent' == notification_from_db.status
def test_get_notification_for_job(notify_db, notify_db_session, sample_notification):
def test_get_notification_for_job(sample_notification):
notifcation_from_db = get_notification_for_job(
sample_notification.service.id,
sample_notification.job_id,
@@ -83,17 +86,9 @@ def test_get_all_notifications_for_job(notify_db, notify_db_session, sample_job)
assert len(notifcations_from_db) == 5
def test_update_notification(notify_db, notify_db_session, sample_notification):
def test_update_notification(sample_notification):
assert sample_notification.status == 'sent'
update_dict = {
'id': str(sample_notification.id),
'service': str(sample_notification.service.id),
'template': sample_notification.template.id,
'job': str(sample_notification.job.id),
'status': 'failed'
}
save_notification(sample_notification, update_dict=update_dict)
sample_notification.status = 'failed'
dao_update_notification(sample_notification)
notification_from_db = Notification.query.get(sample_notification.id)
assert notification_from_db.status == 'failed'

View File

@@ -1,328 +0,0 @@
import boto3
import moto
import json
import uuid
from flask import url_for
from tests import create_authorization_header
from tests.app.conftest import sample_job as create_job
def test_get_jobs(notify_api, notify_db, notify_db_session, sample_template):
_setup_jobs(notify_db, notify_db_session, sample_template)
service_id = sample_template.service.id
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = url_for('job.get_job_for_service', service_id=service_id)
auth_header = create_authorization_header(service_id=service_id,
path=path,
method='GET')
response = client.get(path, headers=[auth_header])
assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True))
assert len(resp_json['data']) == 5
def test_get_job_with_invalid_id_returns400(notify_api, notify_db,
notify_db_session,
sample_template):
service_id = sample_template.service.id
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = url_for('job.get_job_for_service', job_id='invalid_id', service_id=service_id)
auth_header = create_authorization_header(service_id=sample_template.service.id,
path=path,
method='GET')
response = client.get(path, headers=[auth_header])
assert response.status_code == 400
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json == {'message': 'Invalid job id',
'result': 'error'}
def test_get_job_with_unknown_id_returns404(notify_api, notify_db,
notify_db_session,
sample_template):
random_id = str(uuid.uuid4())
service_id = sample_template.service.id
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = url_for('job.get_job_for_service', job_id=random_id, service_id=service_id)
auth_header = create_authorization_header(service_id=sample_template.service.id,
path=path,
method='GET')
response = client.get(path, headers=[auth_header])
assert response.status_code == 404
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json == {'message': 'Job not found', 'result': 'error'}
def test_get_job_by_id(notify_api, notify_db, notify_db_session,
sample_job):
job_id = str(sample_job.id)
service_id = sample_job.service.id
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = url_for('job.get_job_for_service', job_id=job_id, service_id=service_id)
auth_header = create_authorization_header(service_id=sample_job.service.id,
path=path,
method='GET')
response = client.get(path, headers=[auth_header])
assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['data']['id'] == job_id
@moto.mock_sqs
def test_create_job(notify_api, notify_db, notify_db_session, sample_template):
job_id = uuid.uuid4()
template_id = sample_template.id
service_id = sample_template.service.id
original_file_name = 'thisisatest.csv'
bucket_name = 'service-{}-notify'.format(service_id)
file_name = '{}.csv'.format(job_id)
data = {
'id': str(job_id),
'service': str(service_id),
'template': template_id,
'original_file_name': original_file_name,
'bucket_name': bucket_name,
'file_name': file_name,
'notification_count': 1
}
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = url_for('job.create_job', service_id=service_id)
auth_header = create_authorization_header(service_id=sample_template.service.id,
path=path,
method='POST',
request_body=json.dumps(data))
headers = [('Content-Type', 'application/json'), auth_header]
response = client.post(
path,
data=json.dumps(data),
headers=headers)
assert response.status_code == 201
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['data']['id'] == str(job_id)
assert resp_json['data']['service'] == str(service_id)
assert resp_json['data']['template'] == template_id
assert resp_json['data']['original_file_name'] == original_file_name
boto3.setup_default_session(region_name='eu-west-1')
q = boto3.resource('sqs').get_queue_by_name(QueueName=notify_api.config['NOTIFY_JOB_QUEUE'])
messages = q.receive_messages()
assert len(messages) == 1
expected_message = json.loads(messages[0].body)
assert expected_message['id'] == str(job_id)
assert expected_message['service'] == str(service_id)
assert expected_message['template'] == template_id
assert expected_message['bucket_name'] == bucket_name
def test_get_update_job_status(notify_api,
notify_db,
notify_db_session,
sample_job):
assert sample_job.status == 'pending'
job_id = str(sample_job.id)
service_id = str(sample_job.service.id)
update_data = {
'id': job_id,
'service': service_id,
'template': sample_job.template.id,
'bucket_name': sample_job.bucket_name,
'file_name': sample_job.file_name,
'original_file_name': sample_job.original_file_name,
'status': 'in progress',
'notification_count': 1
}
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = url_for('job.update_job', service_id=service_id, job_id=job_id)
auth_header = create_authorization_header(service_id=service_id,
path=path,
method='PUT',
request_body=json.dumps(update_data))
headers = [('Content-Type', 'application/json'), auth_header]
response = client.put(path, headers=headers, data=json.dumps(update_data))
assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['data']['status'] == 'in progress'
def test_get_notification(notify_api, notify_db, notify_db_session, sample_notification):
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = url_for('job.get_notification_for_job',
service_id=sample_notification.service.id,
job_id=sample_notification.job.id,
notification_id=sample_notification.id)
auth_header = create_authorization_header(service_id=sample_notification.service.id,
path=path,
method='GET')
headers = [('Content-Type', 'application/json'), auth_header]
response = client.get(path, headers=headers)
resp_json = json.loads(response.get_data(as_text=True))
assert str(sample_notification.id) == resp_json['data']['id']
assert str(sample_notification.service.id) == resp_json['data']['service']
assert sample_notification.template.id == resp_json['data']['template']
assert str(sample_notification.job.id) == resp_json['data']['job']
assert sample_notification.status == resp_json['data']['status']
def test_get_notifications(notify_api, notify_db, notify_db_session, sample_job):
from tests.app.conftest import sample_notification
for i in range(0, 5):
sample_notification(notify_db,
notify_db_session,
service=sample_job.service,
template=sample_job.template,
job=sample_job)
service_id = str(sample_job.service.id)
job_id = str(sample_job.id)
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = url_for('job.get_notification_for_job',
service_id=service_id,
job_id=job_id)
auth_header = create_authorization_header(service_id=service_id,
path=path,
method='GET')
headers = [('Content-Type', 'application/json'), auth_header]
response = client.get(path, headers=headers)
resp_json = json.loads(response.get_data(as_text=True))
assert len(resp_json['data']) == 5
def test_add_notification(notify_api, notify_db, notify_db_session, sample_job):
to = '+44709123456'
data = {
'to': to,
'job': str(sample_job.id),
'service': str(sample_job.service.id),
'template': sample_job.template.id
}
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = url_for('job.create_notification_for_job',
service_id=sample_job.service.id,
job_id=sample_job.id)
auth_header = create_authorization_header(service_id=sample_job.service.id,
path=path,
method='POST',
request_body=json.dumps(data))
headers = [('Content-Type', 'application/json'), auth_header]
response = client.post(path, headers=headers, data=json.dumps(data))
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['data']['id']
assert data['to'] == resp_json['data']['to']
assert data['service'] == resp_json['data']['service']
assert data['template'] == resp_json['data']['template']
assert data['job'] == resp_json['data']['job']
assert 'sent' == resp_json['data']['status']
def test_add_notification_with_id(notify_api, notify_db, notify_db_session, sample_job):
notification_id = str(uuid.uuid4())
to = '+44709123456'
data = {
'id': notification_id,
'to': to,
'job': str(sample_job.id),
'service': str(sample_job.service.id),
'template': sample_job.template.id
}
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = url_for('job.create_notification_for_job',
service_id=sample_job.service.id,
job_id=sample_job.id)
auth_header = create_authorization_header(service_id=sample_job.service.id,
path=path,
method='POST',
request_body=json.dumps(data))
headers = [('Content-Type', 'application/json'), auth_header]
response = client.post(path, headers=headers, data=json.dumps(data))
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['data']['id'] == notification_id
assert data['to'] == resp_json['data']['to']
assert data['service'] == resp_json['data']['service']
assert data['template'] == resp_json['data']['template']
assert data['job'] == resp_json['data']['job']
assert 'sent' == resp_json['data']['status']
def test_update_notification(notify_api, notify_db, notify_db_session, sample_notification):
assert sample_notification.status == 'sent'
update_data = {
'id': str(sample_notification.id),
'to': sample_notification.to,
'job': str(sample_notification.job.id),
'service': str(sample_notification.service.id),
'template': sample_notification.template.id,
'status': 'failed'
}
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = url_for('job.update_notification_for_job',
service_id=sample_notification.service.id,
job_id=sample_notification.job.id,
notification_id=sample_notification.id)
auth_header = create_authorization_header(service_id=sample_notification.service.id,
path=path,
method='PUT',
request_body=json.dumps(update_data))
headers = [('Content-Type', 'application/json'), auth_header]
response = client.put(path, headers=headers, data=json.dumps(update_data))
resp_json = json.loads(response.get_data(as_text=True))
assert update_data['id'] == resp_json['data']['id']
assert 'failed' == resp_json['data']['status']
def _setup_jobs(notify_db, notify_db_session, template, number_of_jobs=5):
for i in range(number_of_jobs):
create_job(notify_db, notify_db_session, service=template.service,
template=template)

226
tests/app/job/test_rest.py Normal file
View File

@@ -0,0 +1,226 @@
import json
import uuid
import app.celery.tasks
from tests import create_authorization_header
from tests.app.conftest import sample_job as create_job
def test_get_jobs(notify_api, notify_db, notify_db_session, sample_template):
_setup_jobs(notify_db, notify_db_session, sample_template)
service_id = sample_template.service.id
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = '/service/{}/job'.format(service_id)
auth_header = create_authorization_header(
service_id=service_id,
path=path,
method='GET')
response = client.get(path, headers=[auth_header])
assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True))
assert len(resp_json['data']) == 5
def test_get_job_with_invalid_service_id_returns404(notify_api, sample_api_key, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = '/service/{}/job'.format(sample_service.id)
auth_header = create_authorization_header(
service_id=sample_service.id,
path=path,
method='GET')
response = client.get(path, headers=[auth_header])
assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True))
assert len(resp_json['data']) == 0
def test_get_job_with_invalid_job_id_returns404(notify_api, sample_template):
service_id = sample_template.service.id
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = '/service/{}/job/{}'.format(service_id, "bad-id")
auth_header = create_authorization_header(
service_id=sample_template.service.id,
path=path,
method='GET')
response = client.get(path, headers=[auth_header])
assert response.status_code == 404
resp_json = json.loads(response.get_data(as_text=True))
print(resp_json)
assert resp_json['result'] == 'error'
assert resp_json['message'] == 'No result found'
def test_get_job_with_unknown_id_returns404(notify_api, sample_template):
random_id = str(uuid.uuid4())
service_id = sample_template.service.id
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = '/service/{}/job/{}'.format(service_id, random_id)
auth_header = create_authorization_header(
service_id=sample_template.service.id,
path=path,
method='GET')
response = client.get(path, headers=[auth_header])
assert response.status_code == 404
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json == {
'message': 'Job {} not found for service {}'.format(random_id, service_id),
'result': 'error'
}
def test_get_job_by_id(notify_api, sample_job):
job_id = str(sample_job.id)
service_id = sample_job.service.id
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = '/service/{}/job/{}'.format(service_id, job_id)
auth_header = create_authorization_header(
service_id=sample_job.service.id,
path=path,
method='GET')
response = client.get(path, headers=[auth_header])
assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['data']['id'] == job_id
def test_create_job(notify_api, sample_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.tasks.process_job.apply_async')
job_id = uuid.uuid4()
data = {
'id': str(job_id),
'service': str(sample_template.service.id),
'template': sample_template.id,
'original_file_name': 'thisisatest.csv',
'bucket_name': 'service-{}-notify'.format(sample_template.service.id),
'file_name': '{}.csv'.format(job_id),
'notification_count': 1
}
path = '/service/{}/job'.format(sample_template.service.id)
auth_header = create_authorization_header(
service_id=sample_template.service.id,
path=path,
method='POST',
request_body=json.dumps(data))
headers = [('Content-Type', 'application/json'), auth_header]
response = client.post(
path,
data=json.dumps(data),
headers=headers)
assert response.status_code == 201
app.celery.tasks.process_job.apply_async.assert_called_once_with(
([str(job_id)]),
queue="process-job"
)
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['data']['id'] == str(job_id)
assert resp_json['data']['service'] == str(sample_template.service.id)
assert resp_json['data']['template'] == sample_template.id
assert resp_json['data']['original_file_name'] == 'thisisatest.csv'
def test_create_job_returns_400_if_missing_data(notify_api, sample_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.tasks.process_job.apply_async')
data = {
}
path = '/service/{}/job'.format(sample_template.service.id)
auth_header = create_authorization_header(
service_id=sample_template.service.id,
path=path,
method='POST',
request_body=json.dumps(data))
headers = [('Content-Type', 'application/json'), auth_header]
response = client.post(
path,
data=json.dumps(data),
headers=headers)
resp_json = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
app.celery.tasks.process_job.apply_async.assert_not_called()
assert resp_json['result'] == 'error'
assert 'Missing data for required field.' in resp_json['message']['original_file_name']
assert 'Missing data for required field.' in resp_json['message']['file_name']
assert 'Missing data for required field.' in resp_json['message']['notification_count']
assert 'Missing data for required field.' in resp_json['message']['id']
assert 'Missing data for required field.' in resp_json['message']['bucket_name']
def test_create_job_returns_404_if_missing_service(notify_api, sample_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.tasks.process_job.apply_async')
random_id = str(uuid.uuid4())
data = {}
path = '/service/{}/job'.format(random_id)
auth_header = create_authorization_header(
service_id=sample_template.service.id,
path=path,
method='POST',
request_body=json.dumps(data))
headers = [('Content-Type', 'application/json'), auth_header]
response = client.post(
path,
data=json.dumps(data),
headers=headers)
resp_json = json.loads(response.get_data(as_text=True))
assert response.status_code == 404
app.celery.tasks.process_job.apply_async.assert_not_called()
print(resp_json)
assert resp_json['result'] == 'error'
assert resp_json['message'] == 'Service {} not found'.format(random_id)
def test_get_update_job(notify_api, sample_job):
assert sample_job.status == 'pending'
job_id = str(sample_job.id)
service_id = str(sample_job.service.id)
update_data = {
'status': 'in progress'
}
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = '/service/{}/job/{}'.format(service_id, job_id)
auth_header = create_authorization_header(
service_id=service_id,
path=path,
method='POST',
request_body=json.dumps(update_data))
headers = [('Content-Type', 'application/json'), auth_header]
response = client.post(path, headers=headers, data=json.dumps(update_data))
resp_json = json.loads(response.get_data(as_text=True))
assert response.status_code == 200
assert resp_json['data']['status'] == 'in progress'
def _setup_jobs(notify_db, notify_db_session, template, number_of_jobs=5):
for i in range(number_of_jobs):
print(i)
create_job(
notify_db,
notify_db_session,
service=template.service,
template=template)

View File

@@ -1,11 +1,11 @@
import uuid
import app.celery.tasks
import moto
from tests import create_authorization_header
from flask import json
from app.models import Service
from app.dao.templates_dao import get_model_templates
from app.dao.templates_dao import dao_get_all_templates_for_service
from app.dao.services_dao import dao_update_service
from freezegun import freeze_time
def test_get_notification_by_id(notify_api, sample_notification):
@@ -123,9 +123,10 @@ def test_send_notification_invalid_template_id(notify_api, sample_template, mock
json_resp = json.loads(response.get_data(as_text=True))
app.celery.tasks.send_sms.apply_async.assert_not_called()
assert response.status_code == 400
assert response.status_code == 404
assert len(json_resp['message'].keys()) == 1
assert 'Template not found' in json_resp['message']['template']
test_string = 'Template {} not found for service {}'.format(9999, sample_template.service.id)
assert test_string in json_resp['message']['template']
def test_prevents_sending_to_any_mobile_on_restricted_service(notify_api, sample_template, mocker):
@@ -170,7 +171,7 @@ def test_should_not_allow_template_from_another_service(notify_api, service_fact
service_1 = service_factory.get('service 1', user=sample_user)
service_2 = service_factory.get('service 2', user=sample_user)
service_2_templates = get_model_templates(service_id=service_2.id)
service_2_templates = dao_get_all_templates_for_service(service_id=service_2.id)
data = {
'to': sample_user.mobile_number,
'template': service_2_templates[0].id
@@ -190,10 +191,12 @@ def test_should_not_allow_template_from_another_service(notify_api, service_fact
json_resp = json.loads(response.get_data(as_text=True))
app.celery.tasks.send_sms.apply_async.assert_not_called()
assert response.status_code == 400
assert 'Template not found' in json_resp['message']['template']
assert response.status_code == 404
test_string = 'Template {} not found for service {}'.format(service_2_templates[0].id, service_1.id)
assert test_string in json_resp['message']['template']
@freeze_time("2016-01-01 11:09:00.061258")
def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
@@ -221,7 +224,8 @@ def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker
app.celery.tasks.send_sms.apply_async.assert_called_once_with(
(str(sample_template.service_id),
notification_id,
"something_encrypted"),
"something_encrypted",
"2016-01-01 11:09:00.061258"),
queue="sms"
)
assert response.status_code == 201
@@ -302,9 +306,13 @@ def test_should_reject_email_notification_with_template_id_that_cant_be_found(
data = json.loads(response.get_data(as_text=True))
app.celery.tasks.send_email.apply_async.assert_not_called()
assert response.status_code == 400
assert response.status_code == 404
assert data['result'] == 'error'
assert data['message']['template'][0] == 'Template not found'
test_string = 'Template {} not found for service {}'.format(
1234,
sample_email_template.service.id
)
assert test_string in data['message']['template']
def test_should_not_allow_email_template_from_another_service(notify_api, service_factory, sample_user, mocker):
@@ -315,7 +323,7 @@ def test_should_not_allow_email_template_from_another_service(notify_api, servic
service_1 = service_factory.get('service 1', template_type='email', user=sample_user)
service_2 = service_factory.get('service 2', template_type='email', user=sample_user)
service_2_templates = get_model_templates(service_id=service_2.id)
service_2_templates = dao_get_all_templates_for_service(service_id=service_2.id)
data = {
'to': sample_user.email_address,
@@ -336,8 +344,9 @@ def test_should_not_allow_email_template_from_another_service(notify_api, servic
json_resp = json.loads(response.get_data(as_text=True))
app.celery.tasks.send_email.apply_async.assert_not_called()
assert response.status_code == 400
assert 'Template not found' in json_resp['message']['template']
assert response.status_code == 404
test_string = 'Template {} not found for service {}'.format(service_2_templates[0].id, service_1.id)
assert test_string in json_resp['message']['template']
def test_should_not_send_email_if_restricted_and_not_a_service_user(notify_api, sample_email_template, mocker):
@@ -371,6 +380,43 @@ def test_should_not_send_email_if_restricted_and_not_a_service_user(notify_api,
assert 'Email address not permitted for restricted service' in json_resp['message']['to']
def test_should_not_send_email_for_job_if_restricted_and_not_a_service_user(
notify_api,
sample_job,
sample_email_template,
mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
mocker.patch('app.celery.tasks.send_email.apply_async')
sample_email_template.service.restricted = True
dao_update_service(sample_email_template)
data = {
'to': "not-someone-we-trust@email-address.com",
'template': sample_job.template.id,
'job': sample_job.id
}
auth_header = create_authorization_header(
service_id=sample_job.service.id,
request_body=json.dumps(data),
path='/notifications/email',
method='POST')
response = client.post(
path='/notifications/email',
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
app.celery.tasks.send_email.apply_async.assert_not_called()
assert response.status_code == 400
assert 'Email address not permitted for restricted service' in json_resp['message']['to']
@freeze_time("2016-01-01 11:09:00.061258")
def test_should_allow_valid_email_notification(notify_api, sample_email_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
@@ -400,78 +446,9 @@ def test_should_allow_valid_email_notification(notify_api, sample_email_template
notification_id,
"Email Subject",
"sample.service@test.notify.com",
"something_encrypted"),
"something_encrypted",
"2016-01-01 11:09:00.061258"),
queue="email"
)
assert response.status_code == 201
assert notification_id
@moto.mock_sqs
def test_valid_message_with_service_id(notify_api,
notify_db,
notify_db_session,
sqs_client_conn,
sample_user,
sample_template,
mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
job_id = uuid.uuid4()
service_id = sample_template.service.id
url = '/notifications/sms/service/{}'.format(service_id)
data = {
'to': '+441234123123',
'template': sample_template.id,
'job': job_id
}
auth_header = create_authorization_header(
request_body=json.dumps(data),
path=url,
method='POST')
response = client.post(
url,
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 201
assert json.loads(response.data)['notification_id'] is not None
@moto.mock_sqs
def test_message_with_incorrect_service_id_should_fail(notify_api,
notify_db,
notify_db_session,
sqs_client_conn,
sample_user,
sample_template,
mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
job_id = uuid.uuid4()
invalid_service_id = uuid.uuid4()
url = '/notifications/sms/service/{}'.format(invalid_service_id)
data = {
'to': '+441234123123',
'template': sample_template.id,
'job': job_id
}
auth_header = create_authorization_header(
request_body=json.dumps(data),
path=url,
method='POST')
response = client.post(
url,
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
expected_error = 'Invalid template: id {} for service id: {}'.format(sample_template.id,
invalid_service_id)
assert json_resp['message'] == expected_error

View File

@@ -381,8 +381,8 @@ def test_get_users_for_service_returns_empty_list_if_no_users_associated_with_se
'/service/{}/users'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header]
)
assert response.status_code == 200
result = json.loads(response.get_data(as_text=True))
assert response.status_code == 200
assert result['data'] == []

28
tests/app/test_csv.py Normal file
View File

@@ -0,0 +1,28 @@
from app.csv import get_recipient_from_csv
from tests.app import load_example_csv
def test_should_process_single_phone_number_file():
sms_file = load_example_csv('sms')
len(get_recipient_from_csv(sms_file)) == 1
assert get_recipient_from_csv(sms_file)[0] == '+441234123123'
def test_should_process_multple_phone_number_file_in_order():
sms_file = load_example_csv('multiple_sms')
len(get_recipient_from_csv(sms_file)) == 10
assert get_recipient_from_csv(sms_file)[0] == '+441234123121'
assert get_recipient_from_csv(sms_file)[9] == '+441234123120'
def test_should_process_single_email_file():
sms_file = load_example_csv('email')
len(get_recipient_from_csv(sms_file)) == 1
assert get_recipient_from_csv(sms_file)[0] == 'test@test.com'
def test_should_process_multple_email_file_in_order():
sms_file = load_example_csv('multiple_email')
len(get_recipient_from_csv(sms_file)) == 10
assert get_recipient_from_csv(sms_file)[0] == 'test1@test.com'
assert get_recipient_from_csv(sms_file)[9] == 'test0@test.com'