Merge pull request #778 from alphagov/refactor-notify-user-endpoints

Refactor notify user endpoints
This commit is contained in:
Rebecca Law
2016-12-22 14:50:12 +00:00
committed by GitHub
5 changed files with 249 additions and 334 deletions

View File

@@ -1,20 +1,18 @@
import uuid
from datetime import datetime
from flask import ( from flask import (
Blueprint, Blueprint,
request, request,
jsonify, jsonify,
current_app) current_app)
from app import encryption, DATETIME_FORMAT
from app.dao.invited_user_dao import ( from app.dao.invited_user_dao import (
save_invited_user, save_invited_user,
get_invited_user, get_invited_user,
get_invited_users_for_service get_invited_users_for_service
) )
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 EMAIL_TYPE, KEY_TYPE_NORMAL
from app.notifications.process_notifications import persist_notification, send_notification_to_queue
from app.schemas import invited_user_schema from app.schemas import invited_user_schema
from app.celery.tasks import (send_email)
invite = Blueprint('invite', __name__, url_prefix='/service/<service_id>/invite') invite = Blueprint('invite', __name__, url_prefix='/service/<service_id>/invite')
@@ -29,22 +27,23 @@ def create_invited_user(service_id):
save_invited_user(invited_user) save_invited_user(invited_user)
template = dao_get_template_by_id(current_app.config['INVITATION_EMAIL_TEMPLATE_ID']) template = dao_get_template_by_id(current_app.config['INVITATION_EMAIL_TEMPLATE_ID'])
message = {
'template': str(template.id), saved_notification = persist_notification(
'template_version': template.version, template_id=template.id,
'to': invited_user.email_address, template_version=template.version,
'personalisation': { recipient=invited_user.email_address,
service_id=current_app.config['NOTIFY_SERVICE_ID'],
personalisation={
'user_name': invited_user.from_user.name, 'user_name': invited_user.from_user.name,
'service_name': invited_user.service.name, 'service_name': invited_user.service.name,
'url': invited_user_url(invited_user.id) 'url': invited_user_url(invited_user.id)
} },
} notification_type=EMAIL_TYPE,
send_email.apply_async(( api_key_id=None,
current_app.config['NOTIFY_SERVICE_ID'], key_type=KEY_TYPE_NORMAL
str(uuid.uuid4()), )
encryption.encrypt(message),
datetime.utcnow().strftime(DATETIME_FORMAT) send_notification_to_queue(saved_notification, False, queue="notify")
), queue="notify")
return jsonify(data=invited_user_schema.dump(invited_user).data), 201 return jsonify(data=invited_user_schema.dump(invited_user).data), 201

View File

@@ -1,8 +1,6 @@
import json import json
import uuid
from datetime import datetime from datetime import datetime
from flask import (jsonify, request, Blueprint, current_app) from flask import (jsonify, request, Blueprint, current_app)
from app import encryption, DATETIME_FORMAT
from app.dao.users_dao import ( from app.dao.users_dao import (
get_user_by_id, get_user_by_id,
save_model_user, save_model_user,
@@ -32,10 +30,6 @@ from app.schemas import (
user_update_schema_load_json user_update_schema_load_json
) )
from app.celery.tasks import (
send_email
)
from app.errors import ( from app.errors import (
register_errors, register_errors,
InvalidRequest InvalidRequest
@@ -87,7 +81,6 @@ def update_user_attribute(user_id):
def verify_user_password(user_id): def verify_user_password(user_id):
user_to_verify = get_user_by_id(user_id=user_id) user_to_verify = get_user_by_id(user_id=user_id)
txt_pwd = None
try: try:
txt_pwd = request.get_json()['password'] txt_pwd = request.get_json()['password']
except KeyError: except KeyError:
@@ -227,22 +220,22 @@ def send_already_registered_email(user_id):
to, errors = email_data_request_schema.load(request.get_json()) to, errors = email_data_request_schema.load(request.get_json())
template = dao_get_template_by_id(current_app.config['ALREADY_REGISTERED_EMAIL_TEMPLATE_ID']) template = dao_get_template_by_id(current_app.config['ALREADY_REGISTERED_EMAIL_TEMPLATE_ID'])
message = { saved_notification = persist_notification(
'template': str(template.id), template_id=template.id,
'template_version': template.version, template_version=template.version,
'to': to['email'], recipient=to['email'],
'personalisation': { service_id=current_app.config['NOTIFY_SERVICE_ID'],
personalisation={
'signin_url': current_app.config['ADMIN_BASE_URL'] + '/sign-in', 'signin_url': current_app.config['ADMIN_BASE_URL'] + '/sign-in',
'forgot_password_url': current_app.config['ADMIN_BASE_URL'] + '/forgot-password', 'forgot_password_url': current_app.config['ADMIN_BASE_URL'] + '/forgot-password',
'feedback_url': current_app.config['ADMIN_BASE_URL'] + '/feedback' 'feedback_url': current_app.config['ADMIN_BASE_URL'] + '/feedback'
} },
} notification_type=EMAIL_TYPE,
send_email.apply_async(( api_key_id=None,
current_app.config['NOTIFY_SERVICE_ID'], key_type=KEY_TYPE_NORMAL
str(uuid.uuid4()), )
encryption.encrypt(message),
datetime.utcnow().strftime(DATETIME_FORMAT) send_notification_to_queue(saved_notification, False, queue="notify")
), queue='notify')
return jsonify({}), 204 return jsonify({}), 204
@@ -289,19 +282,22 @@ def send_user_reset_password():
user_to_send_to = get_user_by_email(email['email']) user_to_send_to = get_user_by_email(email['email'])
template = dao_get_template_by_id(current_app.config['PASSWORD_RESET_TEMPLATE_ID']) template = dao_get_template_by_id(current_app.config['PASSWORD_RESET_TEMPLATE_ID'])
message = {
'template': str(template.id), saved_notification = persist_notification(
'template_version': template.version, template_id=template.id,
'to': user_to_send_to.email_address, template_version=template.version,
'personalisation': { recipient=email['email'],
service_id=current_app.config['NOTIFY_SERVICE_ID'],
personalisation={
'user_name': user_to_send_to.name, 'user_name': user_to_send_to.name,
'url': _create_reset_password_url(user_to_send_to.email_address) 'url': _create_reset_password_url(user_to_send_to.email_address)
} },
} notification_type=EMAIL_TYPE,
send_email.apply_async([current_app.config['NOTIFY_SERVICE_ID'], api_key_id=None,
str(uuid.uuid4()), key_type=KEY_TYPE_NORMAL
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

@@ -1,95 +1,70 @@
import json import json
import uuid import uuid
from flask import current_app from app.models import Notification
from freezegun import freeze_time
from app import encryption
from tests import create_authorization_header from tests import create_authorization_header
import app.celery.tasks
@freeze_time("2016-01-01T11:09:00.061258") def test_create_invited_user(client, sample_service, mocker, invitation_email_template):
def test_create_invited_user(notify_api, sample_service, mocker, invitation_email_template): mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
with notify_api.test_request_context(): email_address = 'invited_user@service.gov.uk'
with notify_api.test_client() as client: invite_from = sample_service.users[0]
mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id
mocker.patch('app.celery.tasks.send_email.apply_async')
mocker.patch('notifications_utils.url_safe_token.generate_token', return_value='the-token')
email_address = 'invited_user@service.gov.uk'
invite_from = sample_service.users[0]
data = { data = {
'service': str(sample_service.id), 'service': str(sample_service.id),
'email_address': email_address, 'email_address': email_address,
'from_user': str(invite_from.id), 'from_user': str(invite_from.id),
'permissions': 'send_messages,manage_service,manage_api_keys' 'permissions': 'send_messages,manage_service,manage_api_keys'
} }
auth_header = create_authorization_header() auth_header = create_authorization_header()
response = client.post( response = client.post(
'/service/{}/invite'.format(sample_service.id), '/service/{}/invite'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header], headers=[('Content-Type', 'application/json'), auth_header],
data=json.dumps(data) data=json.dumps(data)
) )
assert response.status_code == 201 assert response.status_code == 201
json_resp = json.loads(response.get_data(as_text=True)) json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['data']['service'] == str(sample_service.id) assert json_resp['data']['service'] == str(sample_service.id)
assert json_resp['data']['email_address'] == email_address assert json_resp['data']['email_address'] == email_address
assert json_resp['data']['from_user'] == str(invite_from.id) assert json_resp['data']['from_user'] == str(invite_from.id)
assert json_resp['data']['permissions'] == 'send_messages,manage_service,manage_api_keys' assert json_resp['data']['permissions'] == 'send_messages,manage_service,manage_api_keys'
assert json_resp['data']['id'] assert json_resp['data']['id']
message = { notification = Notification.query.first()
'template': str(invitation_email_template.id), mocked.assert_called_once_with([(str(notification.id))], queue="notify")
'template_version': invitation_email_template.version,
'to': email_address,
'personalisation': {
'user_name': invite_from.name,
'service_name': sample_service.name,
'url': '{0}/invitation/{1}'.format(current_app.config['ADMIN_BASE_URL'], 'the-token')
}
}
app.celery.tasks.send_email.apply_async.assert_called_once_with(
(str(current_app.config['NOTIFY_SERVICE_ID']),
'some_uuid',
encryption.encrypt(message),
"2016-01-01T11:09:00.061258Z"),
queue="notify")
def test_create_invited_user_invalid_email(notify_api, sample_service, mocker): def test_create_invited_user_invalid_email(client, sample_service, 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: email_address = 'notanemail'
mocker.patch('app.celery.tasks.send_email.apply_async') invite_from = sample_service.users[0]
email_address = 'notanemail'
invite_from = sample_service.users[0]
data = { data = {
'service': str(sample_service.id), 'service': str(sample_service.id),
'email_address': email_address, 'email_address': email_address,
'from_user': str(invite_from.id), 'from_user': str(invite_from.id),
'permissions': 'send_messages,manage_service,manage_api_keys' 'permissions': 'send_messages,manage_service,manage_api_keys'
} }
data = json.dumps(data) data = json.dumps(data)
auth_header = create_authorization_header() auth_header = create_authorization_header()
response = client.post( response = client.post(
'/service/{}/invite'.format(sample_service.id), '/service/{}/invite'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header], headers=[('Content-Type', 'application/json'), auth_header],
data=data data=data
) )
assert response.status_code == 400 assert response.status_code == 400
json_resp = json.loads(response.get_data(as_text=True)) json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['result'] == 'error' assert json_resp['result'] == 'error'
assert json_resp['message'] == {'email_address': ['Not a valid email address.']} assert json_resp['message'] == {'email_address': ['Not a valid email address.']}
app.celery.tasks.send_email.apply_async.assert_not_called() assert mocked.call_count == 0
def test_get_all_invited_users_by_service(notify_api, notify_db, notify_db_session, sample_service): def test_get_all_invited_users_by_service(client, notify_db, notify_db_session, sample_service):
from tests.app.conftest import sample_invited_user from tests.app.conftest import sample_invited_user
invites = [] invites = []
@@ -102,120 +77,101 @@ def test_get_all_invited_users_by_service(notify_api, notify_db, notify_db_sessi
email) email)
invites.append(invited_user) invites.append(invited_user)
with notify_api.test_request_context(): url = '/service/{}/invite'.format(sample_service.id)
with notify_api.test_client() as client:
url = '/service/{}/invite'.format(sample_service.id) auth_header = create_authorization_header()
auth_header = create_authorization_header() response = client.get(
url,
headers=[('Content-Type', 'application/json'), auth_header]
)
assert response.status_code == 200
json_resp = json.loads(response.get_data(as_text=True))
response = client.get( invite_from = sample_service.users[0]
url,
headers=[('Content-Type', 'application/json'), auth_header]
)
assert response.status_code == 200
json_resp = json.loads(response.get_data(as_text=True))
invite_from = sample_service.users[0] for invite in json_resp['data']:
assert invite['service'] == str(sample_service.id)
for invite in json_resp['data']: assert invite['from_user'] == str(invite_from.id)
assert invite['service'] == str(sample_service.id) assert invite['id']
assert invite['from_user'] == str(invite_from.id)
assert invite['id']
def test_get_invited_users_by_service_with_no_invites(notify_api, notify_db, notify_db_session, sample_service): def test_get_invited_users_by_service_with_no_invites(client, notify_db, notify_db_session, sample_service):
url = '/service/{}/invite'.format(sample_service.id)
with notify_api.test_request_context(): auth_header = create_authorization_header()
with notify_api.test_client() as client:
url = '/service/{}/invite'.format(sample_service.id) response = client.get(
url,
auth_header = create_authorization_header() headers=[('Content-Type', 'application/json'), auth_header]
)
response = client.get( assert response.status_code == 200
url, json_resp = json.loads(response.get_data(as_text=True))
headers=[('Content-Type', 'application/json'), auth_header] assert len(json_resp['data']) == 0
)
assert response.status_code == 200
json_resp = json.loads(response.get_data(as_text=True))
assert len(json_resp['data']) == 0
def test_get_invited_user_by_service_and_id(notify_api, sample_service, sample_invited_user): def test_get_invited_user_by_service_and_id(client, sample_service, sample_invited_user):
with notify_api.test_request_context(): url = '/service/{}/invite/{}'.format(sample_service.id, sample_invited_user.id)
with notify_api.test_client() as client:
url = '/service/{}/invite/{}'.format(sample_service.id, sample_invited_user.id) auth_header = create_authorization_header()
auth_header = create_authorization_header() response = client.get(
url,
headers=[('Content-Type', 'application/json'), auth_header]
)
assert response.status_code == 200
json_resp = json.loads(response.get_data(as_text=True))
response = client.get( invite_email_address = sample_invited_user.email_address
url, invite_from = sample_service.users[0]
headers=[('Content-Type', 'application/json'), auth_header]
)
assert response.status_code == 200
json_resp = json.loads(response.get_data(as_text=True))
invite_email_address = sample_invited_user.email_address assert json_resp['data']['service'] == str(sample_service.id)
invite_from = sample_service.users[0] assert json_resp['data']['email_address'] == invite_email_address
assert json_resp['data']['from_user'] == str(invite_from.id)
assert json_resp['data']['service'] == str(sample_service.id) assert json_resp['data']['id']
assert json_resp['data']['email_address'] == invite_email_address
assert json_resp['data']['from_user'] == str(invite_from.id)
assert json_resp['data']['id']
def test_get_invited_user_by_service_but_unknown_invite_id_returns_404(notify_api, sample_service): def test_get_invited_user_by_service_but_unknown_invite_id_returns_404(client, sample_service):
with notify_api.test_request_context(): unknown_id = uuid.uuid4()
with notify_api.test_client() as client: url = '/service/{}/invite/{}'.format(sample_service.id, unknown_id)
unknown_id = uuid.uuid4()
url = '/service/{}/invite/{}'.format(sample_service.id, unknown_id)
auth_header = create_authorization_header() auth_header = create_authorization_header()
response = client.get( response = client.get(
url, url,
headers=[('Content-Type', 'application/json'), auth_header] headers=[('Content-Type', 'application/json'), auth_header]
) )
assert response.status_code == 404 assert response.status_code == 404
def test_update_invited_user_set_status_to_cancelled(notify_api, sample_invited_user): def test_update_invited_user_set_status_to_cancelled(client, sample_invited_user):
with notify_api.test_request_context(): data = {'status': 'cancelled'}
with notify_api.test_client() as client: url = '/service/{0}/invite/{1}'.format(sample_invited_user.service_id, sample_invited_user.id)
auth_header = create_authorization_header()
response = client.post(url,
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
data = {'status': 'cancelled'} assert response.status_code == 200
url = '/service/{0}/invite/{1}'.format(sample_invited_user.service_id, sample_invited_user.id) json_resp = json.loads(response.get_data(as_text=True))['data']
auth_header = create_authorization_header() assert json_resp['status'] == 'cancelled'
response = client.post(url,
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 200
json_resp = json.loads(response.get_data(as_text=True))['data']
assert json_resp['status'] == 'cancelled'
def test_update_invited_user_for_wrong_service_returns_404(notify_api, sample_invited_user, fake_uuid): def test_update_invited_user_for_wrong_service_returns_404(client, sample_invited_user, fake_uuid):
with notify_api.test_request_context(): data = {'status': 'cancelled'}
with notify_api.test_client() as client: url = '/service/{0}/invite/{1}'.format(fake_uuid, sample_invited_user.id)
data = {'status': 'cancelled'} auth_header = create_authorization_header()
url = '/service/{0}/invite/{1}'.format(fake_uuid, sample_invited_user.id) response = client.post(url, data=json.dumps(data),
auth_header = create_authorization_header() headers=[('Content-Type', 'application/json'), auth_header])
response = client.post(url, data=json.dumps(data), assert response.status_code == 404
headers=[('Content-Type', 'application/json'), auth_header]) json_response = json.loads(response.get_data(as_text=True))['message']
assert response.status_code == 404 assert json_response == 'No result found'
json_response = json.loads(response.get_data(as_text=True))['message']
assert json_response == 'No result found'
def test_update_invited_user_for_invalid_data_returns_400(notify_api, sample_invited_user): def test_update_invited_user_for_invalid_data_returns_400(client, sample_invited_user):
with notify_api.test_request_context(): data = {'status': 'garbage'}
with notify_api.test_client() as client: url = '/service/{0}/invite/{1}'.format(sample_invited_user.service_id, sample_invited_user.id)
data = {'status': 'garbage'} auth_header = create_authorization_header()
url = '/service/{0}/invite/{1}'.format(sample_invited_user.service_id, sample_invited_user.id) response = client.post(url, data=json.dumps(data),
auth_header = create_authorization_header() headers=[('Content-Type', 'application/json'), auth_header])
response = client.post(url, data=json.dumps(data), assert response.status_code == 400
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 400

View File

@@ -414,134 +414,98 @@ def test_set_user_permissions_remove_old(notify_api,
@freeze_time("2016-01-01 11:09:00.061258") @freeze_time("2016-01-01 11:09:00.061258")
def test_send_user_reset_password_should_send_reset_password_link(notify_api, def test_send_user_reset_password_should_send_reset_password_link(client,
sample_user, sample_user,
mocker, mocker,
password_reset_email_template): password_reset_email_template):
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({'email': sample_user.email_address})
mocker.patch('notifications_utils.url_safe_token.generate_token', return_value='the-token') auth_header = create_authorization_header()
mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id resp = client.post(
mocker.patch('app.celery.tasks.send_email.apply_async') url_for('user.send_user_reset_password'),
data = json.dumps({'email': sample_user.email_address}) data=data,
auth_header = create_authorization_header() headers=[('Content-Type', 'application/json'), auth_header])
resp = client.post(
url_for('user.send_user_reset_password'),
data=data,
headers=[('Content-Type', 'application/json'), auth_header])
message = { assert resp.status_code == 204
'template': str(password_reset_email_template.id), notification = Notification.query.first()
'template_version': password_reset_email_template.version, mocked.assert_called_once_with([str(notification.id)], queue="notify")
'to': sample_user.email_address,
'personalisation': {
'user_name': sample_user.name,
'url': current_app.config['ADMIN_BASE_URL'] + '/new-password/' + 'the-token'
}
}
assert resp.status_code == 204
app.celery.tasks.send_email.apply_async.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_reset_password_should_return_400_when_email_is_missing(notify_api): def test_send_user_reset_password_should_return_400_when_email_is_missing(client, 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({})
data = json.dumps({}) auth_header = create_authorization_header()
auth_header = create_authorization_header()
resp = client.post( resp = client.post(
url_for('user.send_user_reset_password'), url_for('user.send_user_reset_password'),
data=data, data=data,
headers=[('Content-Type', 'application/json'), auth_header]) headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 400
assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Missing data for required field.']}
assert mocked.call_count == 0
def test_send_user_reset_password_should_return_400_when_user_doesnot_exist(client,
mocker):
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
bad_email_address = 'bad@email.gov.uk'
data = json.dumps({'email': bad_email_address})
auth_header = create_authorization_header()
resp = client.post(
url_for('user.send_user_reset_password'),
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'
assert mocked.call_count == 0
def test_send_user_reset_password_should_return_400_when_data_is_not_email_address(client, mocker):
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
bad_email_address = 'bad.email.gov.uk'
data = json.dumps({'email': bad_email_address})
auth_header = create_authorization_header()
resp = client.post(
url_for('user.send_user_reset_password'),
data=data,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 400
assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Not a valid email address.']}
assert mocked.call_count == 0
def test_send_already_registered_email(client, sample_user, already_registered_template, mocker):
data = json.dumps({'email': sample_user.email_address})
auth_header = create_authorization_header()
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
resp = client.post(
url_for('user.send_already_registered_email', user_id=str(sample_user.id)),
data=data,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 204
notification = Notification.query.first()
mocked.assert_called_once_with(([str(notification.id)]), queue="notify")
def test_send_already_registered_email_returns_400_when_data_is_missing(client, sample_user):
data = json.dumps({})
auth_header = create_authorization_header()
resp = client.post(
url_for('user.send_already_registered_email', user_id=str(sample_user.id)),
data=data,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 400 assert resp.status_code == 400
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.']}
def test_send_user_reset_password_should_return_400_when_user_doesnot_exist(notify_api,
mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
bad_email_address = 'bad@email.gov.uk'
data = json.dumps({'email': bad_email_address})
auth_header = create_authorization_header()
resp = client.post(
url_for('user.send_user_reset_password'),
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'
def test_send_user_reset_password_should_return_400_when_data_is_not_email_address(notify_api, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
bad_email_address = 'bad.email.gov.uk'
data = json.dumps({'email': bad_email_address})
auth_header = create_authorization_header()
resp = client.post(
url_for('user.send_user_reset_password'),
data=data,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 400
assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Not a valid email address.']}
@freeze_time("2016-01-01 11:09:00.061258")
def test_send_already_registered_email(notify_api, sample_user, already_registered_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = json.dumps({'email': sample_user.email_address})
auth_header = create_authorization_header()
mocker.patch('app.celery.tasks.send_email.apply_async')
mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id
resp = client.post(
url_for('user.send_already_registered_email', user_id=str(sample_user.id)),
data=data,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 204
message = {
'template': str(already_registered_template.id),
'template_version': already_registered_template.version,
'to': sample_user.email_address,
'personalisation': {
'signin_url': current_app.config['ADMIN_BASE_URL'] + '/sign-in',
'forgot_password_url': current_app.config['ADMIN_BASE_URL'] + '/forgot-password',
'feedback_url': current_app.config['ADMIN_BASE_URL'] + '/feedback'
}
}
app.celery.tasks.send_email.apply_async.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_already_registered_email_returns_400_when_data_is_missing(notify_api, sample_user):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = json.dumps({})
auth_header = create_authorization_header()
resp = client.post(
url_for('user.send_already_registered_email', user_id=str(sample_user.id)),
data=data,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 400
assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Missing data for required field.']}
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(client, sample_user, change_email_confirmation_template, mocker):
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
new_email = 'new_address@dig.gov.uk' new_email = 'new_address@dig.gov.uk'

View File

@@ -324,15 +324,14 @@ def test_send_user_email_verification(client,
headers=[('Content-Type', 'application/json'), auth_header]) headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 204 assert resp.status_code == 204
notification = Notification.query.first() notification = Notification.query.first()
mocked.assert_called_once_with( mocked.assert_called_once_with(([str(notification.id)]), queue="notify")
([str(notification.id)]),
queue="notify")
def test_send_email_verification_returns_404_for_bad_input_data(client, notify_db, notify_db_session): def test_send_email_verification_returns_404_for_bad_input_data(client, notify_db, notify_db_session, mocker):
""" """
Tests POST endpoint /user/<user_id>/sms-code return 404 for bad input data Tests POST endpoint /user/<user_id>/sms-code return 404 for bad input data
""" """
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
data = json.dumps({}) data = json.dumps({})
import uuid import uuid
uuid_ = uuid.uuid4() uuid_ = uuid.uuid4()
@@ -343,3 +342,4 @@ def test_send_email_verification_returns_404_for_bad_input_data(client, notify_d
headers=[('Content-Type', 'application/json'), auth_header]) headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 404 assert resp.status_code == 404
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'
assert mocked.call_count == 0