mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 18:01:08 -05:00
Merge pull request #309 from alphagov/statsd-integration
Statsd integration
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import uuid
|
||||
import pytest
|
||||
from flask import current_app
|
||||
from mock import ANY
|
||||
from notifications_utils.recipients import validate_phone_number, format_phone_number
|
||||
|
||||
from app.celery.tasks import (
|
||||
@@ -16,7 +17,7 @@ from app.celery.tasks import (
|
||||
delete_successful_notifications,
|
||||
provider_to_use
|
||||
)
|
||||
from app import (aws_ses_client, encryption, DATETIME_FORMAT, mmg_client)
|
||||
from app import (aws_ses_client, encryption, DATETIME_FORMAT, mmg_client, statsd_client)
|
||||
from app.clients.email.aws_ses import AwsSesClientException
|
||||
from app.clients.sms.mmg import MMGClientException
|
||||
from app.dao import notifications_dao, jobs_dao, provider_details_dao
|
||||
@@ -103,6 +104,8 @@ def test_should_call_delete_invotations_on_delete_invitations_task(notify_api, m
|
||||
|
||||
@freeze_time("2016-01-01 11:09:00.061258")
|
||||
def test_should_process_sms_job(sample_job, mocker, mock_celery_remove_job):
|
||||
mocker.patch('app.statsd_client.incr')
|
||||
mocker.patch('app.statsd_client.timing')
|
||||
mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('sms'))
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
@@ -126,6 +129,8 @@ def test_should_process_sms_job(sample_job, mocker, mock_celery_remove_job):
|
||||
)
|
||||
job = jobs_dao.dao_get_job_by_id(sample_job.id)
|
||||
assert job.status == 'finished'
|
||||
statsd_client.incr.assert_called_once_with("notifications.tasks.process-job")
|
||||
statsd_client.timing.assert_called_once_with("notifications.tasks.process-job.task-time", ANY)
|
||||
|
||||
|
||||
@freeze_time("2016-01-01 11:09:00.061258")
|
||||
@@ -333,21 +338,41 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
|
||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||
mocker.patch('app.mmg_client.send_sms')
|
||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
||||
mocker.patch('app.statsd_client.incr')
|
||||
mocker.patch('app.statsd_client.timing_with_dates')
|
||||
mocker.patch('app.statsd_client.timing')
|
||||
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
freezer = freeze_time("2016-01-01 11:09:00.00000")
|
||||
freezer.start()
|
||||
now = datetime.utcnow()
|
||||
freezer.stop()
|
||||
|
||||
freezer = freeze_time("2016-01-01 11:10:00.00000")
|
||||
freezer.start()
|
||||
|
||||
send_sms(
|
||||
sample_template_with_placeholders.service_id,
|
||||
notification_id,
|
||||
"encrypted-in-reality",
|
||||
now.strftime(DATETIME_FORMAT)
|
||||
)
|
||||
freezer.stop()
|
||||
|
||||
statsd_client.timing_with_dates.assert_called_once_with(
|
||||
"notifications.tasks.send-sms.queued-for",
|
||||
datetime(2016, 1, 1, 11, 10, 0, 00000),
|
||||
datetime(2016, 1, 1, 11, 9, 0, 00000)
|
||||
)
|
||||
statsd_client.timing.assert_called_once_with("notifications.tasks.send-sms.task-time", ANY)
|
||||
|
||||
mmg_client.send_sms.assert_called_once_with(
|
||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
||||
content="Sample service: Hello Jo",
|
||||
reference=str(notification_id)
|
||||
)
|
||||
statsd_client.incr.assert_called_once_with("notifications.tasks.send-sms")
|
||||
persisted_notification = notifications_dao.get_notification(
|
||||
sample_template_with_placeholders.service_id, notification_id
|
||||
)
|
||||
@@ -564,11 +589,22 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa
|
||||
def test_should_use_email_template_and_persist(sample_email_template_with_placeholders, mocker):
|
||||
notification = _notification_json(sample_email_template_with_placeholders, "my_email@my_email.com", {"name": "Jo"})
|
||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||
mocker.patch('app.aws_ses_client.send_email')
|
||||
mocker.patch('app.statsd_client.incr')
|
||||
mocker.patch('app.statsd_client.timing_with_dates')
|
||||
mocker.patch('app.statsd_client.timing')
|
||||
mocker.patch('app.aws_ses_client.get_name', return_value='ses')
|
||||
mocker.patch('app.aws_ses_client.send_email', return_value='ses')
|
||||
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
freezer = freeze_time("2016-01-01 11:09:00.00000")
|
||||
freezer.start()
|
||||
now = datetime.utcnow()
|
||||
freezer.stop()
|
||||
|
||||
freezer = freeze_time("2016-01-01 11:10:00.00000")
|
||||
freezer.start()
|
||||
|
||||
send_email(
|
||||
sample_email_template_with_placeholders.service_id,
|
||||
notification_id,
|
||||
@@ -576,6 +612,8 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
|
||||
"encrypted-in-reality",
|
||||
now.strftime(DATETIME_FORMAT)
|
||||
)
|
||||
freezer.stop()
|
||||
|
||||
aws_ses_client.send_email.assert_called_once_with(
|
||||
"email_from",
|
||||
"my_email@my_email.com",
|
||||
@@ -583,9 +621,18 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
|
||||
body="Hello Jo",
|
||||
html_body=AnyStringWith("Hello Jo")
|
||||
)
|
||||
|
||||
statsd_client.incr.assert_called_once_with("notifications.tasks.send-email")
|
||||
statsd_client.timing_with_dates.assert_called_once_with(
|
||||
"notifications.tasks.send-email.queued-for",
|
||||
datetime(2016, 1, 1, 11, 10, 0, 00000),
|
||||
datetime(2016, 1, 1, 11, 9, 0, 00000)
|
||||
)
|
||||
statsd_client.timing.assert_called_once_with("notifications.tasks.send-email.task-time", ANY)
|
||||
persisted_notification = notifications_dao.get_notification(
|
||||
sample_email_template_with_placeholders.service_id, notification_id
|
||||
)
|
||||
|
||||
assert persisted_notification.id == notification_id
|
||||
assert persisted_notification.to == 'my_email@my_email.com'
|
||||
assert persisted_notification.template_id == sample_email_template_with_placeholders.id
|
||||
|
||||
@@ -287,7 +287,6 @@ def test_get_all_notifications_returns_empty_list(notify_api, sample_api_key):
|
||||
def test_filter_by_template_type(notify_api, notify_db, notify_db_session, sample_template, sample_email_template):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
notification_1 = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
@@ -318,7 +317,6 @@ def test_filter_by_multiple_template_types(notify_api,
|
||||
sample_email_template):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
notification_1 = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
@@ -346,7 +344,6 @@ def test_filter_by_multiple_template_types(notify_api,
|
||||
def test_filter_by_status(notify_api, notify_db, notify_db_session, sample_email_template):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
notification_1 = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
@@ -378,7 +375,6 @@ def test_filter_by_multiple_statuss(notify_api,
|
||||
sample_email_template):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
notification_1 = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
@@ -412,7 +408,6 @@ def test_filter_by_status_and_template_type(notify_api,
|
||||
sample_email_template):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
notification_1 = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
@@ -955,7 +950,6 @@ def test_no_limit_for_live_service(notify_api,
|
||||
sample_notification):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
sample_service.message_limit = 1
|
||||
notify_db.session.add(sample_service)
|
||||
notify_db.session.commit()
|
||||
@@ -1356,7 +1350,6 @@ def test_ses_callback_should_fail_if_invalid_json(notify_api):
|
||||
def test_ses_callback_should_fail_if_invalid_notification_type(notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=ses_invalid_notification_type_callback(),
|
||||
@@ -1371,7 +1364,6 @@ def test_ses_callback_should_fail_if_invalid_notification_type(notify_api):
|
||||
def test_ses_callback_should_fail_if_missing_message_id(notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=ses_missing_notification_id_callback(),
|
||||
@@ -1386,7 +1378,6 @@ def test_ses_callback_should_fail_if_missing_message_id(notify_api):
|
||||
def test_ses_callback_should_fail_if_notification_cannot_be_found(notify_db, notify_db_session, notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=ses_invalid_notification_id_callback(),
|
||||
@@ -1436,7 +1427,6 @@ def test_ses_callback_should_update_multiple_notification_status_sent(
|
||||
sample_email_template):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
notification1 = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
@@ -1487,7 +1477,6 @@ def test_ses_callback_should_update_multiple_notification_status_sent(
|
||||
def test_should_handle_invite_email_callbacks(notify_api, notify_db, notify_db_session):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
notify_api.config['INVITATION_EMAIL_FROM'] = 'test-invite'
|
||||
notify_api.config['NOTIFY_EMAIL_DOMAIN'] = 'test-domain.com'
|
||||
|
||||
@@ -1505,7 +1494,6 @@ def test_should_handle_invite_email_callbacks(notify_api, notify_db, notify_db_s
|
||||
def test_should_handle_validation_code_callbacks(notify_api, notify_db, notify_db_session):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
notify_api.config['VERIFY_CODE_FROM_EMAIL_ADDRESS'] = 'valid-code@test.com'
|
||||
|
||||
response = client.post(
|
||||
@@ -1519,6 +1507,108 @@ def test_should_handle_validation_code_callbacks(notify_api, notify_db, notify_d
|
||||
assert json_resp['message'] == 'SES callback succeeded'
|
||||
|
||||
|
||||
def test_should_record_email_request_in_statsd(notify_api, notify_db, notify_db_session, sample_email_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.statsd_client.incr')
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
data = {
|
||||
'to': 'ok@ok.com',
|
||||
'template': str(sample_email_template.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(service_id=sample_email_template.service_id)
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
assert response.status_code == 201
|
||||
app.statsd_client.incr.assert_called_once_with("notifications.api.email")
|
||||
|
||||
|
||||
def test_should_record_sms_request_in_statsd(notify_api, notify_db, notify_db_session, sample_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.statsd_client.incr')
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
data = {
|
||||
'to': '07123123123',
|
||||
'template': str(sample_template.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/sms',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
assert response.status_code == 201
|
||||
app.statsd_client.incr.assert_called_once_with("notifications.api.sms")
|
||||
|
||||
|
||||
def test_ses_callback_should_update_record_statsd(
|
||||
notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_email_template,
|
||||
mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.statsd_client.incr')
|
||||
|
||||
notification = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template=sample_email_template,
|
||||
reference='ref'
|
||||
)
|
||||
|
||||
assert get_notification_by_id(notification.id).status == 'sending'
|
||||
|
||||
client.post(
|
||||
path='/notifications/email/ses',
|
||||
data=ses_notification_callback(),
|
||||
headers=[('Content-Type', 'text/plain; charset=UTF-8')]
|
||||
)
|
||||
app.statsd_client.incr.assert_called_once_with("notifications.callback.ses.delivered")
|
||||
|
||||
|
||||
def test_process_mmg_response_records_statsd(notify_api, sample_notification, mocker):
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.statsd_client.incr')
|
||||
data = json.dumps({"reference": "mmg_reference",
|
||||
"CID": str(sample_notification.id),
|
||||
"MSISDN": "447777349060",
|
||||
"status": "3",
|
||||
"deliverytime": "2016-04-05 16:01:07"})
|
||||
|
||||
client.post(path='notifications/sms/mmg',
|
||||
data=data,
|
||||
headers=[('Content-Type', 'application/json')])
|
||||
app.statsd_client.incr.assert_called_once_with("notifications.callback.mmg.delivered")
|
||||
|
||||
|
||||
def test_firetext_callback_should_record_statsd(notify_api, notify_db, notify_db_session, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.statsd_client.incr')
|
||||
notification = create_sample_notification(notify_db, notify_db_session, status='delivered')
|
||||
|
||||
client.post(
|
||||
path='/notifications/sms/firetext',
|
||||
data='mobile=441234123123&status=2&time=2016-03-10 14:17:00&reference={}'.format(
|
||||
notification.id
|
||||
),
|
||||
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
|
||||
|
||||
app.statsd_client.incr.assert_called_once_with("notifications.callback.firetext.delivered")
|
||||
|
||||
|
||||
def ses_validation_code_callback():
|
||||
return b'{\n "Type" : "Notification",\n "MessageId" : "ref",\n "TopicArn" : "arn:aws:sns:eu-west-1:123456789012:testing",\n "Message" : "{\\"notificationType\\":\\"Delivery\\",\\"mail\\":{\\"timestamp\\":\\"2016-03-14T12:35:25.909Z\\",\\"source\\":\\"valid-code@test.com\\",\\"sourceArn\\":\\"arn:aws:ses:eu-west-1:123456789012:identity/testing-notify\\",\\"sendingAccountId\\":\\"123456789012\\",\\"messageId\\":\\"ref\\",\\"destination\\":[\\"testing@digital.cabinet-office.gov.uk\\"]},\\"delivery\\":{\\"timestamp\\":\\"2016-03-14T12:35:26.567Z\\",\\"processingTimeMillis\\":658,\\"recipients\\":[\\"testing@digital.cabinet-office.gov.u\\"],\\"smtpResponse\\":\\"250 2.0.0 OK 1457958926 uo5si26480932wjc.221 - gsmtp\\",\\"reportingMTA\\":\\"a6-238.smtp-out.eu-west-1.amazonses.com\\"}}",\n "Timestamp" : "2016-03-14T12:35:26.665Z",\n "SignatureVersion" : "1",\n "Signature" : "X8d7eTAOZ6wlnrdVVPYanrAlsX0SMPfOzhoTEBnQqYkrNWTqQY91C0f3bxtPdUhUtOowyPAOkTQ4KnZuzphfhVb2p1MyVYMxNKcBFB05/qaCX99+92fjw4x9LeUOwyGwMv5F0Vkfi5qZCcEw69uVrhYLVSTFTrzi/yCtru+yFULMQ6UhbY09GwiP6hjxZMVr8aROQy5lLHglqQzOuSZ4KeD85JjifHdKzlx8jjQ+uj+FLzHXPMAPmPU1JK9kpoHZ1oPshAFgPDpphJe+HwcJ8ezmk+3AEUr3wWli3xF+49y8Z2anASSVp6YI2YP95UT8Rlh3qT3T+V9V8rbSVislxA==",\n "SigningCertURL" : "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-bb750dd426d95ee9390147a5624348ee.pem",\n "UnsubscribeURL" : "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:302763885840:preview-emails:d6aad3ef-83d6-4cf3-a470-54e2e75916da"\n}' # noqa
|
||||
|
||||
|
||||
Reference in New Issue
Block a user