Merge pull request #327 from alphagov/add_job_row_number

Added job row number to the notification for csv jobs. All tests pass…
This commit is contained in:
NIcholas Staples
2016-05-19 14:33:21 +01:00
8 changed files with 81 additions and 15 deletions

View File

@@ -119,6 +119,7 @@ def test_should_process_sms_job(sample_job, mocker, mock_celery_remove_job):
assert encryption.encrypt.call_args[0][0]['template'] == str(sample_job.template.id)
assert encryption.encrypt.call_args[0][0]['template_version'] == sample_job.template.version
assert encryption.encrypt.call_args[0][0]['personalisation'] == {}
assert encryption.encrypt.call_args[0][0]['row_number'] == 0
tasks.send_sms.apply_async.assert_called_once_with(
(str(sample_job.service_id),
"uuid",
@@ -559,7 +560,11 @@ def test_should_not_send_email_if_restricted_service_and_invalid_email_address(n
def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sample_job, mocker):
notification = _notification_json(sample_job.template, to="+447234123123", job_id=sample_job.id)
notification = _notification_json(
sample_job.template,
to="+447234123123",
job_id=sample_job.id,
row_number=2)
mocker.patch('app.encryption.decrypt', return_value=notification)
mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.mmg_client.get_name', return_value="mmg")
@@ -586,10 +591,15 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa
assert persisted_notification.sent_at > now
assert persisted_notification.created_at == now
assert persisted_notification.sent_by == 'mmg'
assert persisted_notification.job_row_number == 2
def test_should_use_email_template_and_persist(sample_email_template_with_placeholders, mocker):
notification = _notification_json(sample_email_template_with_placeholders, "my_email@my_email.com", {"name": "Jo"})
notification = _notification_json(
sample_email_template_with_placeholders,
"my_email@my_email.com",
{"name": "Jo"},
row_number=1)
mocker.patch('app.encryption.decrypt', return_value=notification)
mocker.patch('app.statsd_client.incr')
mocker.patch('app.statsd_client.timing_with_dates')
@@ -644,6 +654,7 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
assert persisted_notification.sent_at > now
assert persisted_notification.status == 'sending'
assert persisted_notification.sent_by == 'ses'
assert persisted_notification.job_row_number == 1
def test_send_email_should_use_template_version_from_job_not_latest(sample_email_template, mocker):
@@ -952,7 +963,6 @@ def test_email_reset_password_should_send_email(notify_db, notify_db_session, no
aws_ses_client.send_email(current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'],
reset_password_message['to'],
"Reset password for GOV.UK Notify",
message)
@@ -983,7 +993,7 @@ def test_process_email_job_should_use_reply_to_email_if_present(sample_email_job
)
def _notification_json(template, to, personalisation=None, job_id=None):
def _notification_json(template, to, personalisation=None, job_id=None, row_number=None):
notification = {
"template": template.id,
"template_version": template.version,
@@ -993,4 +1003,6 @@ def _notification_json(template, to, personalisation=None, job_id=None):
notification.update({"personalisation": personalisation})
if job_id:
notification.update({"job": job_id})
if row_number:
notification['row_number'] = row_number
return notification

View File

@@ -310,6 +310,7 @@ def sample_notification(notify_db,
service=None,
template=None,
job=None,
job_row_number=None,
to_field=None,
status='sending',
reference=None,
@@ -347,6 +348,8 @@ def sample_notification(notify_db,
'created_at': created_at,
'content_char_count': content_char_count
}
if job_row_number:
data['job_row_number'] = job_row_number
notification = Notification(**data)
if create:
dao_create_notification(notification, template.template_type, provider.identifier)

View File

@@ -130,20 +130,38 @@ def test_get_all_notifications_for_service_in_order(notify_api, notify_db, notif
assert response.status_code == 200
def test_get_all_notifications_for_job_in_order(notify_api, notify_db, notify_db_session, sample_service):
def test_get_all_notifications_for_job_in_order_of_job_number(notify_api,
notify_db,
notify_db_session,
sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
main_job = create_sample_job(notify_db, notify_db_session, service=sample_service)
another_job = create_sample_job(notify_db, notify_db_session, service=sample_service)
notification_1 = create_sample_notification(
notify_db, notify_db_session, job=main_job, to_field="1", created_at=datetime.utcnow()
notify_db,
notify_db_session,
job=main_job,
to_field="1",
created_at=datetime.utcnow(),
job_row_number=1
)
notification_2 = create_sample_notification(
notify_db, notify_db_session, job=main_job, to_field="2", created_at=datetime.utcnow()
notify_db,
notify_db_session,
job=main_job,
to_field="2",
created_at=datetime.utcnow(),
job_row_number=2
)
notification_3 = create_sample_notification(
notify_db, notify_db_session, job=main_job, to_field="3", created_at=datetime.utcnow()
notify_db,
notify_db_session,
job=main_job,
to_field="3",
created_at=datetime.utcnow(),
job_row_number=3
)
create_sample_notification(notify_db, notify_db_session, job=another_job)
@@ -155,9 +173,12 @@ def test_get_all_notifications_for_job_in_order(notify_api, notify_db, notify_db
resp = json.loads(response.get_data(as_text=True))
assert len(resp['notifications']) == 3
assert resp['notifications'][0]['to'] == notification_3.to
assert resp['notifications'][0]['to'] == notification_1.to
assert resp['notifications'][0]['job_row_number'] == notification_1.job_row_number
assert resp['notifications'][1]['to'] == notification_2.to
assert resp['notifications'][2]['to'] == notification_1.to
assert resp['notifications'][1]['job_row_number'] == notification_2.job_row_number
assert resp['notifications'][2]['to'] == notification_3.to
assert resp['notifications'][2]['job_row_number'] == notification_3.job_row_number
assert response.status_code == 200