mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-21 16:01:15 -05:00
Use notify to send email verification
This commit is contained in:
@@ -28,8 +28,8 @@ from app.schemas import (
|
|||||||
from app.celery.tasks import (
|
from app.celery.tasks import (
|
||||||
send_sms,
|
send_sms,
|
||||||
email_reset_password,
|
email_reset_password,
|
||||||
email_registration_verification
|
email_registration_verification,
|
||||||
)
|
send_email)
|
||||||
|
|
||||||
from app.errors import register_errors
|
from app.errors import register_errors
|
||||||
|
|
||||||
@@ -157,13 +157,26 @@ def send_user_email_verification(user_id):
|
|||||||
secret_code = create_secret_code()
|
secret_code = create_secret_code()
|
||||||
create_user_code(user_to_send_to, secret_code, 'email')
|
create_user_code(user_to_send_to, secret_code, 'email')
|
||||||
|
|
||||||
email = user_to_send_to.email_address
|
template = dao_get_template_by_id(current_app.config['EMAIL_VERIFY_CODE_TEMPLATE_ID'])
|
||||||
verification_message = {'to': email,
|
message = {
|
||||||
'name': user_to_send_to.name,
|
'template': template.id,
|
||||||
'url': _create_verification_url(user_to_send_to, secret_code)}
|
'template_version': template.version,
|
||||||
|
'to': user_to_send_to.email_address,
|
||||||
email_registration_verification.apply_async([encryption.encrypt(verification_message)],
|
'personalisation': {
|
||||||
queue='email-registration-verification')
|
'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
|
return jsonify({}), 204
|
||||||
|
|
||||||
|
|||||||
@@ -589,3 +589,37 @@ def sms_code_template(notify_db,
|
|||||||
template = Template(**data)
|
template = Template(**data)
|
||||||
db.session.add(template)
|
db.session.add(template)
|
||||||
return 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
|
||||||
|
|||||||
@@ -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'
|
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,
|
def test_send_user_email_verification(notify_api,
|
||||||
sample_email_code,
|
sample_user,
|
||||||
mock_celery_email_registration_verification,
|
mocker,
|
||||||
mock_encryption):
|
email_verification_template):
|
||||||
|
|
||||||
with notify_api.test_request_context():
|
with notify_api.test_request_context():
|
||||||
with notify_api.test_client() as client:
|
with notify_api.test_client() as client:
|
||||||
data = json.dumps({})
|
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()
|
auth_header = create_authorization_header()
|
||||||
resp = client.post(
|
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,
|
data=data,
|
||||||
headers=[('Content-Type', 'application/json'), auth_header])
|
headers=[('Content-Type', 'application/json'), auth_header])
|
||||||
assert resp.status_code == 204
|
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/<user_id>/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'
|
||||||
|
|||||||
Reference in New Issue
Block a user