mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 18:01:08 -05:00
Update the send_user_confirm_new_email to persist the notification then put on the "notify" queue for delivery.
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.
This commit is contained in:
@@ -5,9 +5,8 @@ from flask import url_for, current_app
|
||||
from freezegun import freeze_time
|
||||
|
||||
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.utils import url_with_token
|
||||
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.']}
|
||||
|
||||
|
||||
@freeze_time("2016-01-01T11:09:00.061258")
|
||||
def test_send_user_confirm_new_email_returns_204(notify_api, sample_user, change_email_confirmation_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocked = mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
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()
|
||||
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')
|
||||
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)),
|
||||
data=data,
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
assert resp.status_code == 204
|
||||
token_data = json.dumps({'user_id': str(sample_user.id), 'email': new_email})
|
||||
url = url_with_token(data=token_data, url='/user-profile/email/confirm/', config=current_app.config)
|
||||
message = {
|
||||
'template': current_app.config['CHANGE_EMAIL_CONFIRMATION_TEMPLATE_ID'],
|
||||
'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")
|
||||
resp = client.post(url_for('user.send_user_confirm_new_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_user_confirm_new_email_returns_400_when_email_missing(notify_api, sample_user, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocked = mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
data = json.dumps({})
|
||||
auth_header = create_authorization_header()
|
||||
resp = client.post(url_for('user.send_user_confirm_new_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.']}
|
||||
mocked.assert_not_called()
|
||||
def test_send_user_confirm_new_email_returns_400_when_email_missing(client, sample_user, mocker):
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
data = json.dumps({})
|
||||
auth_header = create_authorization_header()
|
||||
resp = client.post(url_for('user.send_user_confirm_new_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.']}
|
||||
mocked.assert_not_called()
|
||||
|
||||
Reference in New Issue
Block a user