diff --git a/app/user/rest.py b/app/user/rest.py index 12fde343a..f4905ef36 100644 --- a/app/user/rest.py +++ b/app/user/rest.py @@ -28,8 +28,8 @@ from app.schemas import ( from app.celery.tasks import ( send_sms, email_reset_password, - email_registration_verification -) + email_registration_verification, + send_email) from app.errors import register_errors @@ -157,13 +157,26 @@ def send_user_email_verification(user_id): secret_code = create_secret_code() create_user_code(user_to_send_to, secret_code, 'email') - email = user_to_send_to.email_address - verification_message = {'to': email, - 'name': user_to_send_to.name, - 'url': _create_verification_url(user_to_send_to, secret_code)} - - email_registration_verification.apply_async([encryption.encrypt(verification_message)], - queue='email-registration-verification') + template = dao_get_template_by_id(current_app.config['EMAIL_VERIFY_CODE_TEMPLATE_ID']) + message = { + 'template': template.id, + 'template_version': template.version, + 'to': user_to_send_to.email_address, + 'personalisation': { + 'user_name': user_to_send_to.name, + 'url': _create_verification_url(user_to_send_to, secret_code) + } + } + email_from = '"GOV.UK Notify" <{}>'.format( + current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'] + ) + send_email.apply_async(( + current_app.config['NOTIFY_SERVICE_ID'], + str(uuid.uuid4()), + email_from, + encryption.encrypt(message), + datetime.utcnow().strftime(DATETIME_FORMAT) + ), queue='email-registration-verification') return jsonify({}), 204 diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 01c6cb952..70b4ddbe0 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -589,3 +589,37 @@ def sms_code_template(notify_db, template = Template(**data) db.session.add(template) return template + + +@pytest.fixture(scope='function') +def email_verification_template(notify_db, + notify_db_session): + user = sample_user(notify_db, notify_db_session) + service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID']) + if not service: + data = { + 'id': current_app.config['NOTIFY_SERVICE_ID'], + 'name': 'Notify Service', + 'message_limit': 1000, + 'active': True, + 'restricted': False, + 'email_from': 'notify.service', + 'created_by': user + } + service = Service(**data) + db.session.add(service) + + template = Template.query.get(current_app.config['SMS_CODE_TEMPLATE_ID']) + if not template: + data = { + 'id': current_app.config['EMAIL_VERIFY_CODE_TEMPLATE_ID'], + 'name': 'Email verification template', + 'template_type': 'email', + 'content': '((user_name)) use ((url)) to complete registration', + 'service': service, + 'created_by': user, + 'archived': False + } + template = Template(**data) + db.session.add(template) + return template diff --git a/tests/app/user/test_rest_verify.py b/tests/app/user/test_rest_verify.py index 3ca379171..7f2a6130e 100644 --- a/tests/app/user/test_rest_verify.py +++ b/tests/app/user/test_rest_verify.py @@ -309,18 +309,47 @@ def test_send_sms_code_returns_404_for_bad_input_data(notify_api, notify_db, not assert json.loads(resp.get_data(as_text=True))['message'] == 'No result found' +@freeze_time("2016-01-01 11:09:00.061258") def test_send_user_email_verification(notify_api, - sample_email_code, - mock_celery_email_registration_verification, - mock_encryption): + sample_user, + mocker, + email_verification_template): with notify_api.test_request_context(): with notify_api.test_client() as client: data = json.dumps({}) + mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id + mocker.patch('app.encryption.encrypt', return_value="something_encrypted") + mocked = mocker.patch('app.celery.tasks.send_email.apply_async') auth_header = create_authorization_header() resp = client.post( - url_for('user.send_user_email_verification', user_id=str(sample_email_code.user.id)), + url_for('user.send_user_email_verification', user_id=str(sample_user.id)), data=data, headers=[('Content-Type', 'application/json'), auth_header]) assert resp.status_code == 204 - app.celery.tasks.email_registration_verification.apply_async.assert_called_once_with(['something_encrypted'], queue='email-registration-verification') # noqa + assert mocked.call_count == 1 + app.celery.tasks.send_email.apply_async.assert_called_once_with( + (str(current_app.config['NOTIFY_SERVICE_ID']), + 'some_uuid', + '"GOV.UK Notify" <{}>'.format(current_app.config['VERIFY_CODE_FROM_EMAIL_ADDRESS']), + "something_encrypted", + "2016-01-01T11:09:00.061258"), + queue="email-registration-verification") + + +def test_send_email_verification_returns_404_for_bad_input_data(notify_api, notify_db, notify_db_session): + """ + Tests POST endpoint /user//sms-code return 404 for bad input data + """ + with notify_api.test_request_context(): + with notify_api.test_client() as client: + data = json.dumps({}) + import uuid + uuid_ = uuid.uuid4() + auth_header = create_authorization_header() + resp = client.post( + url_for('user.send_user_email_verification', user_id=uuid_), + data=data, + headers=[('Content-Type', 'application/json'), auth_header]) + assert resp.status_code == 404 + assert json.loads(resp.get_data(as_text=True))['message'] == 'No result found'