diff --git a/app/celery/tasks.py b/app/celery/tasks.py index f3ce2b8fb..3c63fa790 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -180,10 +180,9 @@ def process_job(job_id): ) if template.template_type == 'email': - send_email_v2.apply_async(( + send_email.apply_async(( str(job.service_id), create_uuid(), - '', encrypted, datetime.utcnow().strftime(DATETIME_FORMAT)), {'reply_to_addresses': service.reply_to_email_address}, @@ -289,12 +288,13 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at): @notify_celery.task(name="send-email") -def send_email(service_id, notification_id, from_address, encrypted_notification, created_at, reply_to_addresses=None): - send_email_v2(service_id, notification_id, encrypted_notification, created_at, reply_to_addresses=None) +def send_email_v1(service_id, notification_id, from_address, + encrypted_notification, created_at, reply_to_addresses=None): + send_email(service_id, notification_id, encrypted_notification, created_at, reply_to_addresses=None) @notify_celery.task(name="send-email-v2") -def send_email_v2(service_id, notification_id, encrypted_notification, created_at, reply_to_addresses=None): +def send_email(service_id, notification_id, encrypted_notification, created_at, reply_to_addresses=None): task_start = monotonic() notification = encryption.decrypt(encrypted_notification) service = dao_fetch_service_by_id(service_id) diff --git a/app/notifications/rest.py b/app/notifications/rest.py index fa3c9d783..a98cff336 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -31,7 +31,7 @@ from app.schemas import ( day_schema, unarchived_template_schema ) -from app.celery.tasks import send_sms, send_email_v2 +from app.celery.tasks import send_sms, send_email notifications = Blueprint('notifications', __name__) @@ -384,7 +384,7 @@ def send_notification(notification_type): datetime.utcnow().strftime(DATETIME_FORMAT) ), queue='sms') else: - send_email_v2.apply_async(( + send_email.apply_async(( service_id, notification_id, '', diff --git a/app/user/rest.py b/app/user/rest.py index 750898220..46668b98c 100644 --- a/app/user/rest.py +++ b/app/user/rest.py @@ -28,7 +28,7 @@ from app.schemas import ( from app.celery.tasks import ( send_sms, email_reset_password, - send_email_v2) + send_email) from app.errors import register_errors @@ -166,7 +166,7 @@ def send_user_email_verification(user_id): 'url': _create_verification_url(user_to_send_to, secret_code) } } - send_email_v2.apply_async(( + send_email.apply_async(( current_app.config['NOTIFY_SERVICE_ID'], str(uuid.uuid4()), encryption.encrypt(message), diff --git a/migrations/versions/0029_fix_email_from.py b/migrations/versions/0029_fix_email_from.py new file mode 100644 index 000000000..52d048c69 --- /dev/null +++ b/migrations/versions/0029_fix_email_from.py @@ -0,0 +1,26 @@ +"""empty message + +Revision ID: 0029_fix_email_from +Revises: 0028_fix_reg_template_history +Create Date: 2016-06-13 15:15:34.035984 + +""" + +# revision identifiers, used by Alembic. +revision = '0029_fix_email_from' +down_revision = '0028_fix_reg_template_history' + +from alembic import op +import sqlalchemy as sa + +service_id = 'd6aa2c68-a2d9-4437-ab19-3ae8eb202553' +def upgrade(): + op.get_bind() + op.execute("update services set email_from = 'gov.uk.notify' where id = '{}'".format(service_id)) + op.execute("update services_history set email_from = 'gov.uk.notify' where id = '{}'".format(service_id)) + + +def downgrade(): + ### commands auto generated by Alembic - please adjust! ### + pass + ### end Alembic commands ### diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index 201982295..01173ccd5 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -6,7 +6,6 @@ from notifications_utils.recipients import validate_phone_number, format_phone_n from app.celery.tasks import ( send_sms, - send_email, process_job, email_invited_user, email_reset_password, @@ -15,8 +14,8 @@ from app.celery.tasks import ( delete_failed_notifications, delete_successful_notifications, provider_to_use, - timeout_notifications -) + timeout_notifications, + send_email) from app.celery.research_mode_tasks import ( send_email_response, send_sms_response @@ -248,7 +247,6 @@ def test_should_process_email_job_if_exactly_on_send_limits(notify_db, ( str(job.service_id), "uuid", - "", "something_encrypted", "2016-01-01T11:09:00.061258" ), @@ -294,7 +292,6 @@ def test_should_process_email_job(sample_email_job, mocker, mock_celery_remove_j ( str(sample_email_job.service_id), "uuid", - "", "something_encrypted", "2016-01-01T11:09:00.061258" ), @@ -517,13 +514,12 @@ def test_should_send_email_if_restricted_service_and_valid_email(notify_db, noti send_email( service.id, notification_id, - 'email_from', "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) aws_ses_client.send_email.assert_called_once_with( - "email_from", + '"Sample service" ', "test@restricted.com", template.subject, body=template.content, @@ -548,7 +544,6 @@ def test_should_not_send_email_if_restricted_service_and_invalid_email_address(n send_email( service.id, notification_id, - 'email_from', "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) @@ -619,14 +614,13 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh send_email( sample_email_template_with_placeholders.service_id, notification_id, - 'email_from', "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) freezer.stop() aws_ses_client.send_email.assert_called_once_with( - "email_from", + '"Sample service" ', "my_email@my_email.com", notification['personalisation']['name'], body="Hello Jo", @@ -674,12 +668,11 @@ def test_send_email_should_use_template_version_from_job_not_latest(sample_email send_email( sample_email_template.service_id, notification_id, - 'email_from', "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) aws_ses_client.send_email.assert_called_once_with( - "email_from", + '"Sample service" ', "my_email@my_email.com", sample_email_template.subject, body="This is a template", @@ -710,12 +703,11 @@ def test_should_use_email_template_subject_placeholders(sample_email_template_wi send_email( sample_email_template_with_placeholders.service_id, notification_id, - 'email_from', "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) aws_ses_client.send_email.assert_called_once_with( - "email_from", + '"Sample service" ', "my_email@my_email.com", notification['personalisation']['name'], body="Hello Jo", @@ -744,7 +736,6 @@ def test_should_use_email_template_and_persist_ses_reference(sample_email_templa send_email( sample_email_template_with_placeholders.service_id, notification_id, - 'email_from', "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) @@ -765,12 +756,11 @@ def test_should_use_email_template_and_persist_without_personalisation(sample_em send_email( sample_email_template.service_id, notification_id, - 'email_from', "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) aws_ses_client.send_email.assert_called_once_with( - "email_from", + '"Sample service" ', "my_email@my_email.com", sample_email_template.subject, body="This is a template", @@ -823,12 +813,11 @@ def test_should_persist_notification_as_failed_if_email_client_fails(sample_emai send_email( sample_email_template.service_id, notification_id, - 'email_from', "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) aws_ses_client.send_email.assert_called_once_with( - "email_from", + '"Sample service" ', "my_email@my_email.com", sample_email_template.subject, body=sample_email_template.content, @@ -879,7 +868,6 @@ def test_should_not_send_email_if_db_peristance_failed(sample_email_template, mo send_email( sample_email_template.service_id, notification_id, - 'email_from', "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) @@ -952,7 +940,6 @@ def test_process_email_job_should_use_reply_to_email_if_present(sample_email_job ( str(sample_email_job.service_id), "uuid", - "", "something_encrypted", ANY ), @@ -1025,7 +1012,6 @@ def test_should_call_send_email_response_task_if_research_mode( send_email( sample_service.id, notification_id, - "myservice@notify.com", "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) @@ -1077,7 +1063,6 @@ def test_should_call_send_not_update_provider_email_stats_if_research_mode( send_email( sample_service.id, notification_id, - "myservice@notify.com", "encrypted-in-reality", now.strftime(DATETIME_FORMAT) ) diff --git a/tests/app/notifications/test_rest.py b/tests/app/notifications/test_rest.py index 1a0389950..0853e66f5 100644 --- a/tests/app/notifications/test_rest.py +++ b/tests/app/notifications/test_rest.py @@ -775,7 +775,7 @@ def test_create_email_should_reject_if_missing_required_fields(notify_api, sampl headers=[('Content-Type', 'application/json'), auth_header]) json_resp = json.loads(response.get_data(as_text=True)) - app.celery.tasks.send_email_v2.apply_async.assert_not_called() + app.celery.tasks.send_email.apply_async.assert_not_called() assert json_resp['result'] == 'error' assert 'Missing data for required field.' in json_resp['message']['to'][0] assert 'Missing data for required field.' in json_resp['message']['template'][0] @@ -799,7 +799,7 @@ def test_should_reject_email_notification_with_bad_email(notify_api, sample_emai headers=[('Content-Type', 'application/json'), auth_header]) data = json.loads(response.get_data(as_text=True)) - app.celery.tasks.send_email_v2.apply_async.assert_not_called() + app.celery.tasks.send_email.apply_async.assert_not_called() assert response.status_code == 400 assert data['result'] == 'error' assert data['message']['to'][0] == 'Not a valid email address' @@ -822,7 +822,7 @@ def test_should_reject_email_notification_with_template_id_that_cant_be_found( headers=[('Content-Type', 'application/json'), auth_header]) data = json.loads(response.get_data(as_text=True)) - app.celery.tasks.send_email_v2.apply_async.assert_not_called() + app.celery.tasks.send_email.apply_async.assert_not_called() assert response.status_code == 404 assert data['result'] == 'error' test_string = 'No result found' @@ -854,7 +854,7 @@ def test_should_not_allow_email_template_from_another_service(notify_api, servic headers=[('Content-Type', 'application/json'), auth_header]) json_resp = json.loads(response.get_data(as_text=True)) - app.celery.tasks.send_email_v2.apply_async.assert_not_called() + app.celery.tasks.send_email.apply_async.assert_not_called() assert response.status_code == 404 test_string = 'No result found' @@ -882,7 +882,7 @@ def test_should_not_send_email_if_restricted_and_not_a_service_user(notify_api, headers=[('Content-Type', 'application/json'), auth_header]) json_resp = json.loads(response.get_data(as_text=True)) - app.celery.tasks.send_email_v2.apply_async.assert_not_called() + app.celery.tasks.send_email.apply_async.assert_not_called() assert response.status_code == 400 assert 'Invalid email address for restricted service' in json_resp['message']['to'] @@ -914,7 +914,7 @@ def test_should_not_send_email_for_job_if_restricted_and_not_a_service_user( headers=[('Content-Type', 'application/json'), auth_header]) json_resp = json.loads(response.get_data(as_text=True)) - app.celery.tasks.send_email_v2.apply_async.assert_not_called() + app.celery.tasks.send_email.apply_async.assert_not_called() assert response.status_code == 400 assert 'Invalid email address for restricted service' in json_resp['message']['to'] @@ -943,7 +943,7 @@ def test_should_allow_valid_email_notification(notify_api, sample_email_template assert app.encryption.encrypt.call_args[0][0]['to'] == 'ok@ok.com' assert app.encryption.encrypt.call_args[0][0]['template'] == str(sample_email_template.id) assert app.encryption.encrypt.call_args[0][0]['template_version'] == sample_email_template.version - app.celery.tasks.send_email_v2.apply_async.assert_called_once_with( + app.celery.tasks.send_email.apply_async.assert_called_once_with( (str(sample_email_template.service_id), notification_id, "", diff --git a/tests/app/user/test_rest_verify.py b/tests/app/user/test_rest_verify.py index b9f52196f..c012c0bc6 100644 --- a/tests/app/user/test_rest_verify.py +++ b/tests/app/user/test_rest_verify.py @@ -328,10 +328,9 @@ def test_send_user_email_verification(notify_api, headers=[('Content-Type', 'application/json'), auth_header]) assert resp.status_code == 204 assert mocked.call_count == 1 - app.celery.tasks.send_email_v2.apply_async.assert_called_once_with( + app.celery.tasks.send_email.apply_async.assert_called_once_with( (str(current_app.config['NOTIFY_SERVICE_ID']), 'some_uuid', - '', "something_encrypted", "2016-01-01T11:09:00.061258"), queue="email-registration-verification")