mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 08:21:13 -05:00
Refactor create_invited_user to persist and send message to the notify queue.
The reason for doing this is to ensure the tasks performed for the Notify users are not queued behind a large job, a way to ensure priority for messages. 6th task for story: https://www.pivotaltracker.com/story/show/135839709
This commit is contained in:
@@ -13,8 +13,9 @@ from app.dao.invited_user_dao import (
|
|||||||
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 +30,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
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,12 @@
|
|||||||
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():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
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'
|
email_address = 'invited_user@service.gov.uk'
|
||||||
invite_from = sample_service.users[0]
|
invite_from = sample_service.users[0]
|
||||||
|
|
||||||
@@ -41,28 +32,12 @@ def test_create_invited_user(notify_api, sample_service, mocker, invitation_emai
|
|||||||
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:
|
|
||||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
|
||||||
email_address = 'notanemail'
|
email_address = 'notanemail'
|
||||||
invite_from = sample_service.users[0]
|
invite_from = sample_service.users[0]
|
||||||
|
|
||||||
@@ -86,10 +61,10 @@ def test_create_invited_user_invalid_email(notify_api, sample_service, mocker):
|
|||||||
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,9 +77,6 @@ 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():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
|
|
||||||
url = '/service/{}/invite'.format(sample_service.id)
|
url = '/service/{}/invite'.format(sample_service.id)
|
||||||
|
|
||||||
auth_header = create_authorization_header()
|
auth_header = create_authorization_header()
|
||||||
@@ -124,11 +96,7 @@ def test_get_all_invited_users_by_service(notify_api, notify_db, notify_db_sessi
|
|||||||
assert invite['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):
|
||||||
|
|
||||||
with notify_api.test_request_context():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
|
|
||||||
url = '/service/{}/invite'.format(sample_service.id)
|
url = '/service/{}/invite'.format(sample_service.id)
|
||||||
|
|
||||||
auth_header = create_authorization_header()
|
auth_header = create_authorization_header()
|
||||||
@@ -142,10 +110,7 @@ def test_get_invited_users_by_service_with_no_invites(notify_api, notify_db, not
|
|||||||
assert len(json_resp['data']) == 0
|
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():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
|
|
||||||
url = '/service/{}/invite/{}'.format(sample_service.id, sample_invited_user.id)
|
url = '/service/{}/invite/{}'.format(sample_service.id, sample_invited_user.id)
|
||||||
|
|
||||||
auth_header = create_authorization_header()
|
auth_header = create_authorization_header()
|
||||||
@@ -166,9 +131,7 @@ def test_get_invited_user_by_service_and_id(notify_api, sample_service, sample_i
|
|||||||
assert json_resp['data']['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():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
unknown_id = uuid.uuid4()
|
unknown_id = uuid.uuid4()
|
||||||
url = '/service/{}/invite/{}'.format(sample_service.id, unknown_id)
|
url = '/service/{}/invite/{}'.format(sample_service.id, unknown_id)
|
||||||
|
|
||||||
@@ -181,10 +144,7 @@ def test_get_invited_user_by_service_but_unknown_invite_id_returns_404(notify_ap
|
|||||||
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():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
|
|
||||||
data = {'status': 'cancelled'}
|
data = {'status': 'cancelled'}
|
||||||
url = '/service/{0}/invite/{1}'.format(sample_invited_user.service_id, sample_invited_user.id)
|
url = '/service/{0}/invite/{1}'.format(sample_invited_user.service_id, sample_invited_user.id)
|
||||||
auth_header = create_authorization_header()
|
auth_header = create_authorization_header()
|
||||||
@@ -197,9 +157,7 @@ def test_update_invited_user_set_status_to_cancelled(notify_api, sample_invited_
|
|||||||
assert json_resp['status'] == 'cancelled'
|
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():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
data = {'status': 'cancelled'}
|
data = {'status': 'cancelled'}
|
||||||
url = '/service/{0}/invite/{1}'.format(fake_uuid, sample_invited_user.id)
|
url = '/service/{0}/invite/{1}'.format(fake_uuid, sample_invited_user.id)
|
||||||
auth_header = create_authorization_header()
|
auth_header = create_authorization_header()
|
||||||
@@ -210,9 +168,7 @@ def test_update_invited_user_for_wrong_service_returns_404(notify_api, sample_in
|
|||||||
assert json_response == 'No result found'
|
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():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
data = {'status': 'garbage'}
|
data = {'status': 'garbage'}
|
||||||
url = '/service/{0}/invite/{1}'.format(sample_invited_user.service_id, sample_invited_user.id)
|
url = '/service/{0}/invite/{1}'.format(sample_invited_user.service_id, sample_invited_user.id)
|
||||||
auth_header = create_authorization_header()
|
auth_header = create_authorization_header()
|
||||||
|
|||||||
Reference in New Issue
Block a user