Merge branch 'master' into scheduled-delivery-of-jobs

Conflicts:
	app/dao/jobs_dao.py
	tests/app/dao/test_jobs_dao.py
	tests/app/job/test_rest.py
This commit is contained in:
Martyn Inglis
2016-08-25 14:53:00 +01:00
21 changed files with 403 additions and 670 deletions

View File

@@ -4,9 +4,9 @@ from flask import json
import app.celery.tasks
from app.dao.notifications_dao import (
get_notification_by_id,
dao_get_notification_statistics_for_service
get_notification_by_id
)
from app.models import NotificationStatistics
from tests.app.conftest import sample_notification as create_sample_notification
@@ -162,7 +162,7 @@ def test_firetext_callback_should_update_notification_status(notify_api, sample_
updated = get_notification_by_id(sample_notification.id)
assert updated.status == 'delivered'
assert get_notification_by_id(sample_notification.id).status == 'delivered'
stats = dao_get_notification_statistics_for_service(sample_notification.service_id)[0]
stats = get_notification_stats(sample_notification.service_id)
assert stats.sms_delivered == 1
assert stats.sms_requested == 1
assert stats.sms_failed == 0
@@ -189,7 +189,7 @@ def test_firetext_callback_should_update_notification_status_failed(notify_api,
sample_notification.id
)
assert get_notification_by_id(sample_notification.id).status == 'permanent-failure'
stats = dao_get_notification_statistics_for_service(sample_notification.service_id)[0]
stats = get_notification_stats(sample_notification.service_id)
assert stats.sms_delivered == 0
assert stats.sms_requested == 1
assert stats.sms_failed == 1
@@ -217,7 +217,7 @@ def test_firetext_callback_should_update_notification_status_pending(notify_api,
notification.id
)
assert get_notification_by_id(notification.id).status == 'pending'
stats = dao_get_notification_statistics_for_service(notification.service_id)[0]
stats = get_notification_stats(notification.service_id)
assert stats.sms_delivered == 0
assert stats.sms_requested == 1
assert stats.sms_failed == 0
@@ -257,7 +257,7 @@ def test_firetext_callback_should_update_multiple_notification_status_sent(
),
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
stats = dao_get_notification_statistics_for_service(notification1.service_id)[0]
stats = get_notification_stats(notification1.service_id)
assert stats.sms_delivered == 3
assert stats.sms_requested == 3
assert stats.sms_failed == 0
@@ -481,7 +481,7 @@ def test_ses_callback_should_update_notification_status(
assert json_resp['result'] == 'success'
assert json_resp['message'] == 'SES callback succeeded'
assert get_notification_by_id(notification.id).status == 'delivered'
stats = dao_get_notification_statistics_for_service(notification.service_id)[0]
stats = get_notification_stats(notification.service_id)
assert stats.emails_delivered == 1
assert stats.emails_requested == 1
assert stats.emails_failed == 0
@@ -536,7 +536,7 @@ def test_ses_callback_should_update_multiple_notification_status_sent(
assert resp2.status_code == 200
assert resp3.status_code == 200
stats = dao_get_notification_statistics_for_service(notification1.service_id)[0]
stats = get_notification_stats(notification1.service_id)
assert stats.emails_delivered == 3
assert stats.emails_requested == 3
assert stats.emails_failed == 0
@@ -568,7 +568,7 @@ def test_ses_callback_should_set_status_to_temporary_failure(notify_api,
assert json_resp['result'] == 'success'
assert json_resp['message'] == 'SES callback succeeded'
assert get_notification_by_id(notification.id).status == 'temporary-failure'
stats = dao_get_notification_statistics_for_service(notification.service_id)[0]
stats = get_notification_stats(notification.service_id)
assert stats.emails_delivered == 0
assert stats.emails_requested == 1
assert stats.emails_failed == 1
@@ -628,7 +628,7 @@ def test_ses_callback_should_set_status_to_permanent_failure(notify_api,
assert json_resp['result'] == 'success'
assert json_resp['message'] == 'SES callback succeeded'
assert get_notification_by_id(notification.id).status == 'permanent-failure'
stats = dao_get_notification_statistics_for_service(notification.service_id)[0]
stats = get_notification_stats(notification.service_id)
assert stats.emails_delivered == 0
assert stats.emails_requested == 1
assert stats.emails_failed == 1
@@ -730,3 +730,7 @@ def ses_hard_bounce_callback():
def ses_soft_bounce_callback():
return b'{\n "Type" : "Notification",\n "MessageId" : "ref",\n "TopicArn" : "arn:aws:sns:eu-west-1:123456789012:testing",\n "Message" : "{\\"notificationType\\":\\"Bounce\\",\\"bounce\\":{\\"bounceType\\":\\"Undetermined\\",\\"bounceSubType\\":\\"General\\"}, \\"mail\\":{\\"messageId\\":\\"ref\\",\\"timestamp\\":\\"2016-03-14T12:35:25.909Z\\",\\"source\\":\\"test@test-domain.com\\",\\"sourceArn\\":\\"arn:aws:ses:eu-west-1:123456789012:identity/testing-notify\\",\\"sendingAccountId\\":\\"123456789012\\",\\"destination\\":[\\"testing@digital.cabinet-office.gov.uk\\"]},\\"delivery\\":{\\"timestamp\\":\\"2016-03-14T12:35:26.567Z\\",\\"processingTimeMillis\\":658,\\"recipients\\":[\\"testing@digital.cabinet-office.gov.uk\\"],\\"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
def get_notification_stats(service_id):
return NotificationStatistics.query.filter_by(service_id=service_id).one()

View File

@@ -1,207 +0,0 @@
import json
from datetime import (
date,
timedelta
)
from flask import url_for
from tests import create_authorization_header
from tests.app.conftest import sample_notification_statistics as create_sample_notification_statistics
from freezegun import freeze_time
def test_get_notification_statistics_returns_empty_list_if_no_stats(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:
path = '/service/{}/notifications-statistics'.format(sample_email_template.service.id)
auth_header = create_authorization_header(
service_id=sample_email_template.service_id)
response = client.get(path, headers=[auth_header])
assert response.status_code == 200
stats = json.loads(response.get_data(as_text=True))
assert stats['data'] == []
def test_get_week_aggregate_statistics(notify_api,
notify_db,
notify_db_session,
sample_service):
with notify_api.test_request_context():
sample_notification_statistics = create_sample_notification_statistics(
notify_db,
notify_db_session,
day=date(date.today().year, 4, 1))
with notify_api.test_client() as client:
endpoint = url_for(
'notifications-statistics.get_notification_statistics_for_service_seven_day_aggregate',
service_id=sample_service.id)
auth_header = create_authorization_header(
service_id=sample_service.id)
resp = client.get(endpoint, headers=[auth_header])
assert resp.status_code == 200
json_resp = json.loads(resp.get_data(as_text=True))
week_len_index = len(json_resp['data']) - 1
assert json_resp['data'][week_len_index]['emails_requested'] == 2
assert json_resp['data'][week_len_index]['sms_requested'] == 2
assert json_resp['data'][week_len_index]['week_start'] == date(date.today().year, 4, 1).strftime('%Y-%m-%d')
assert json_resp['data'][week_len_index]['week_end'] == date(date.today().year, 4, 7).strftime('%Y-%m-%d')
def test_get_week_aggregate_statistics_date_from(notify_api,
notify_db,
notify_db_session,
sample_service):
with notify_api.test_request_context():
sample_notification_statistics = create_sample_notification_statistics(
notify_db,
notify_db_session,
day=date(date.today().year, 4, 1))
date_from_str = date(date.today().year, 4, 1).strftime('%Y-%m-%d')
with notify_api.test_client() as client:
endpoint = url_for(
'notifications-statistics.get_notification_statistics_for_service_seven_day_aggregate',
service_id=sample_service.id,
date_from=date_from_str)
auth_header = create_authorization_header(
service_id=sample_service.id)
resp = client.get(endpoint, headers=[auth_header])
assert resp.status_code == 200
json_resp = json.loads(resp.get_data(as_text=True))
week_len_index = len(json_resp['data']) - 1
assert json_resp['data'][week_len_index]['emails_requested'] == 2
assert json_resp['data'][week_len_index]['sms_requested'] == 2
assert json_resp['data'][week_len_index]['week_start'] == date_from_str
assert json_resp['data'][week_len_index]['week_end'] == date(date.today().year, 4, 7).strftime('%Y-%m-%d')
def test_get_week_aggregate_statistics_date_in_future(notify_api,
notify_db,
notify_db_session,
sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
endpoint = url_for(
'notifications-statistics.get_notification_statistics_for_service_seven_day_aggregate',
service_id=sample_service.id,
date_from=(date.today() + timedelta(days=1)).strftime('%Y-%m-%d'))
auth_header = create_authorization_header(
service_id=sample_service.id)
resp = client.get(endpoint, headers=[auth_header])
assert resp.status_code == 400
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['result'] == 'error'
assert json_resp['message']['date_from'][0] == 'Date cannot be in the future'
def test_get_week_aggregate_statistics_invalid_week_count(notify_api,
notify_db,
notify_db_session,
sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
endpoint = url_for(
'notifications-statistics.get_notification_statistics_for_service_seven_day_aggregate',
service_id=sample_service.id,
week_count=-1)
auth_header = create_authorization_header(
service_id=sample_service.id)
resp = client.get(endpoint, headers=[auth_header])
assert resp.status_code == 400
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['result'] == 'error'
assert json_resp['message']['week_count'][0] == 'Not a positive integer'
@freeze_time('2016-01-01')
def test_get_notification_statistics_for_specific_day(notify_api,
notify_db,
notify_db_session,
sample_template):
the_day = date.today()
sample_notification_statistics = create_sample_notification_statistics(
notify_db,
notify_db_session,
day=the_day)
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = '/service/{}/notifications-statistics/day/{}'.format(sample_template.service_id, the_day)
auth_header = create_authorization_header(service_id=sample_template.service_id)
response = client.get(path, headers=[auth_header])
assert response.status_code == 200
stats = json.loads(response.get_data(as_text=True))
assert stats['data']['id'] == str(sample_notification_statistics.id)
assert stats['data']['day'] == the_day.strftime('%Y-%m-%d')
another_day = the_day - timedelta(days=1)
path = '/service/{}/notifications-statistics/day/{}'.format(sample_template.service_id, another_day)
response = client.get(path, headers=[auth_header])
assert response.status_code == 404
@freeze_time('2016-01-01')
def test_get_notification_statistics_for_specific_day_returns_404_if_no_stats(notify_api,
notify_db,
notify_db_session,
sample_template):
the_day = date.today()
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = '/service/{}/notifications-statistics/day/{}'.format(sample_template.service_id, the_day)
auth_header = create_authorization_header(service_id=sample_template.service_id)
response = client.get(path, headers=[auth_header])
assert response.status_code == 404
@freeze_time('2016-01-01')
def test_get_notification_statistics_for_specific_day_returns_400_for_incorrect_date(notify_api,
notify_db,
notify_db_session,
sample_template):
the_day = date.today()
incorrect_date_format = the_day.strftime('%d-%m-%Y')
create_sample_notification_statistics(
notify_db,
notify_db_session,
day=the_day)
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = '/service/{}/notifications-statistics/day/{}'.format(
sample_template.service_id,
incorrect_date_format)
auth_header = create_authorization_header(service_id=sample_template.service_id)
response = client.get(path, headers=[auth_header])
assert response.status_code == 400
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['result'] == 'error'
assert resp_json['message'] == 'Invalid date 01-01-2016'
another_dodgy_date = 'fish'
path = '/service/{}/notifications-statistics/day/{}'.format(
sample_template.service_id,
another_dodgy_date)
response = client.get(path, headers=[auth_header])
assert response.status_code == 400
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['result'] == 'error'
assert resp_json['message'] == 'Invalid date fish'

View File

@@ -206,7 +206,7 @@ def test_normal_api_key_returns_notifications_created_from_jobs_and_from_api(
notifications = json.loads(response.get_data(as_text=True))['notifications']
assert len(notifications) == 2
assert set(x['id'] for x in notifications) == set([str(sample_notification.id), str(api_notification.id)])
assert set(x['id'] for x in notifications) == {str(sample_notification.id), str(api_notification.id)}
@pytest.mark.parametrize('key_type', [KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST])