Merge pull request #773 from alphagov/refactor-confirm-new-email

Refactor endpoint to confirm new email
This commit is contained in:
Rebecca Law
2016-12-19 16:28:44 +00:00
committed by GitHub
2 changed files with 39 additions and 60 deletions

View File

@@ -18,7 +18,7 @@ from app.dao.users_dao import (
from app.dao.permissions_dao import permission_dao from app.dao.permissions_dao import permission_dao
from app.dao.services_dao import dao_fetch_service_by_id from app.dao.services_dao import dao_fetch_service_by_id
from app.dao.templates_dao import dao_get_template_by_id from app.dao.templates_dao import dao_get_template_by_id
from app.models import SMS_TYPE, KEY_TYPE_NORMAL from app.models import SMS_TYPE, KEY_TYPE_NORMAL, EMAIL_TYPE
from app.notifications.process_notifications import ( from app.notifications.process_notifications import (
persist_notification, persist_notification,
send_notification_to_queue send_notification_to_queue
@@ -33,7 +33,6 @@ from app.schemas import (
) )
from app.celery.tasks import ( from app.celery.tasks import (
send_sms,
send_email send_email
) )
@@ -175,24 +174,24 @@ def send_user_confirm_new_email(user_id):
raise InvalidRequest(message=errors, status_code=400) raise InvalidRequest(message=errors, status_code=400)
template = dao_get_template_by_id(current_app.config['CHANGE_EMAIL_CONFIRMATION_TEMPLATE_ID']) template = dao_get_template_by_id(current_app.config['CHANGE_EMAIL_CONFIRMATION_TEMPLATE_ID'])
message = { notify_service_id = current_app.config['NOTIFY_SERVICE_ID']
'template': str(template.id),
'template_version': template.version, saved_notification = persist_notification(
'to': email['email'], template_id=template.id,
'personalisation': { template_version=template.version,
recipient=email['email'],
service_id=notify_service_id,
personalisation={
'name': user_to_send_to.name, 'name': user_to_send_to.name,
'url': _create_confirmation_url(user=user_to_send_to, email_address=email['email']), 'url': _create_confirmation_url(user=user_to_send_to, email_address=email['email']),
'feedback_url': current_app.config['ADMIN_BASE_URL'] + '/feedback' 'feedback_url': current_app.config['ADMIN_BASE_URL'] + '/feedback'
} },
} notification_type=EMAIL_TYPE,
api_key_id=None,
send_email.apply_async(( key_type=KEY_TYPE_NORMAL
current_app.config['NOTIFY_SERVICE_ID'], )
str(uuid.uuid4()),
encryption.encrypt(message),
datetime.utcnow().strftime(DATETIME_FORMAT)
), queue='notify')
send_notification_to_queue(saved_notification, False, queue='notify')
return jsonify({}), 204 return jsonify({}), 204

View File

@@ -5,9 +5,8 @@ from flask import url_for, current_app
from freezegun import freeze_time from freezegun import freeze_time
import app import app
from app.models import (User, Permission, MANAGE_SETTINGS, MANAGE_TEMPLATES) from app.models import (User, Permission, MANAGE_SETTINGS, MANAGE_TEMPLATES, Notification)
from app.dao.permissions_dao import default_service_permissions from app.dao.permissions_dao import default_service_permissions
from app.utils import url_with_token
from tests import create_authorization_header from tests import create_authorization_header
@@ -543,48 +542,29 @@ def test_send_already_registered_email_returns_400_when_data_is_missing(notify_a
assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Missing data for required field.']} assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Missing data for required field.']}
@freeze_time("2016-01-01T11:09:00.061258") def test_send_user_confirm_new_email_returns_204(client, sample_user, change_email_confirmation_template, mocker):
def test_send_user_confirm_new_email_returns_204(notify_api, sample_user, change_email_confirmation_template, mocker): mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
with notify_api.test_request_context(): new_email = 'new_address@dig.gov.uk'
with notify_api.test_client() as client: data = json.dumps({'email': new_email})
mocked = mocker.patch('app.celery.tasks.send_email.apply_async') auth_header = create_authorization_header()
mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id
new_email = 'new_address@dig.gov.uk'
data = json.dumps({'email': new_email})
auth_header = create_authorization_header()
resp = client.post(url_for('user.send_user_confirm_new_email', user_id=str(sample_user.id)), resp = client.post(url_for('user.send_user_confirm_new_email', 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
token_data = json.dumps({'user_id': str(sample_user.id), 'email': new_email}) notification = Notification.query.first()
url = url_with_token(data=token_data, url='/user-profile/email/confirm/', config=current_app.config) mocked.assert_called_once_with(
message = { ([str(notification.id)]),
'template': current_app.config['CHANGE_EMAIL_CONFIRMATION_TEMPLATE_ID'], queue="notify")
'template_version': 1,
'to': 'new_address@dig.gov.uk',
'personalisation': {
'name': sample_user.name,
'url': url,
'feedback_url': current_app.config['ADMIN_BASE_URL'] + '/feedback'
}
}
mocked.assert_called_once_with((
str(current_app.config['NOTIFY_SERVICE_ID']),
"some_uuid",
app.encryption.encrypt(message),
"2016-01-01T11:09:00.061258Z"), queue="notify")
def test_send_user_confirm_new_email_returns_400_when_email_missing(notify_api, sample_user, mocker): def test_send_user_confirm_new_email_returns_400_when_email_missing(client, sample_user, mocker):
with notify_api.test_request_context(): mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
with notify_api.test_client() as client: data = json.dumps({})
mocked = mocker.patch('app.celery.tasks.send_email.apply_async') auth_header = create_authorization_header()
data = json.dumps({}) resp = client.post(url_for('user.send_user_confirm_new_email', user_id=str(sample_user.id)),
auth_header = create_authorization_header() data=data,
resp = client.post(url_for('user.send_user_confirm_new_email', user_id=str(sample_user.id)), headers=[('Content-Type', 'application/json'), auth_header])
data=data, assert resp.status_code == 400
headers=[('Content-Type', 'application/json'), auth_header]) assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Missing data for required field.']}
assert resp.status_code == 400 mocked.assert_not_called()
assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Missing data for required field.']}
mocked.assert_not_called()