mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 02:11:11 -05:00
/notifications/statistics now takes 'day' parameter
avoids potential issues with date.today being inconsistent with user timezone
This commit is contained in:
@@ -388,9 +388,16 @@ def send_notification(notification_type):
|
|||||||
|
|
||||||
|
|
||||||
@notifications.route('/notifications/statistics')
|
@notifications.route('/notifications/statistics')
|
||||||
def get_notification_statistics_for_today():
|
def get_notification_statistics_for_day():
|
||||||
today = date.today()
|
data, errors = notifications_statistics_schema.load(request.args)
|
||||||
statistics = notifications_dao.dao_get_notification_statistics_for_day(day=today)
|
if errors:
|
||||||
|
return jsonify(result='error', message=errors), 400
|
||||||
|
if not data.day:
|
||||||
|
return jsonify(result='error', message='Please provide day as query parameter.'), 400
|
||||||
|
|
||||||
|
statistics = notifications_dao.dao_get_notification_statistics_for_day(
|
||||||
|
day=data.day
|
||||||
|
)
|
||||||
|
|
||||||
data, errors = notifications_statistics_schema.dump(statistics, many=True)
|
data, errors = notifications_statistics_schema.dump(statistics, many=True)
|
||||||
return jsonify(data=data, date=today.isoformat()), 200
|
return jsonify(data=data), 200
|
||||||
|
|||||||
@@ -15,11 +15,13 @@ def test_get_notification_statistics(notify_api, sample_notification_statistics)
|
|||||||
)
|
)
|
||||||
|
|
||||||
response = client.get(
|
response = client.get(
|
||||||
'/notifications/statistics',
|
'/notifications/statistics?day={}'.format(date.today().isoformat()),
|
||||||
headers=[auth_header]
|
headers=[auth_header]
|
||||||
)
|
)
|
||||||
|
|
||||||
notifications = json.loads(response.get_data(as_text=True))
|
notifications = json.loads(response.get_data(as_text=True))
|
||||||
|
|
||||||
|
assert len(notifications['data']) == 1
|
||||||
stats = notifications['data'][0]
|
stats = notifications['data'][0]
|
||||||
assert stats['emails_requested'] == 2
|
assert stats['emails_requested'] == 2
|
||||||
assert stats['emails_delivered'] == 1
|
assert stats['emails_delivered'] == 1
|
||||||
@@ -58,11 +60,46 @@ def test_get_notification_statistics_only_returns_today(notify_api, notify_db, n
|
|||||||
)
|
)
|
||||||
|
|
||||||
response = client.get(
|
response = client.get(
|
||||||
'/notifications/statistics',
|
'/notifications/statistics?day={}'.format(date.today().isoformat()),
|
||||||
headers=[auth_header]
|
headers=[auth_header]
|
||||||
)
|
)
|
||||||
|
|
||||||
notifications = json.loads(response.get_data(as_text=True))
|
notifications = json.loads(response.get_data(as_text=True))
|
||||||
|
|
||||||
assert len(notifications['data']) == 1
|
assert len(notifications['data']) == 1
|
||||||
assert notifications['data'][0]['day'] == date.today().isoformat()
|
assert notifications['data'][0]['day'] == date.today().isoformat()
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_notification_statistics_fails_if_no_date(notify_api, sample_notification_statistics):
|
||||||
|
with notify_api.test_request_context():
|
||||||
|
with notify_api.test_client() as client:
|
||||||
|
auth_header = create_authorization_header(
|
||||||
|
service_id=sample_notification_statistics.service_id
|
||||||
|
)
|
||||||
|
|
||||||
|
response = client.get(
|
||||||
|
'/notifications/statistics',
|
||||||
|
headers=[auth_header]
|
||||||
|
)
|
||||||
|
|
||||||
|
resp = json.loads(response.get_data(as_text=True))
|
||||||
|
assert resp['result'] == 'error'
|
||||||
|
assert response.status_code == 400
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_notification_statistics_fails_if_invalid_date(notify_api, sample_notification_statistics):
|
||||||
|
with notify_api.test_request_context():
|
||||||
|
with notify_api.test_client() as client:
|
||||||
|
auth_header = create_authorization_header(
|
||||||
|
service_id=sample_notification_statistics.service_id
|
||||||
|
)
|
||||||
|
|
||||||
|
response = client.get(
|
||||||
|
'/notifications/statistics?day=2016-99-99',
|
||||||
|
headers=[auth_header]
|
||||||
|
)
|
||||||
|
|
||||||
|
resp = json.loads(response.get_data(as_text=True))
|
||||||
|
assert resp['result'] == 'error'
|
||||||
|
assert response.status_code == 400
|
||||||
|
|||||||
Reference in New Issue
Block a user