mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Endpoint for reading notification stats for a given day.
This commit is contained in:
@@ -1,13 +1,22 @@
|
|||||||
from datetime import (date, timedelta)
|
from datetime import (
|
||||||
|
date,
|
||||||
|
timedelta,
|
||||||
|
datetime
|
||||||
|
)
|
||||||
|
|
||||||
from flask import (
|
from flask import (
|
||||||
Blueprint,
|
Blueprint,
|
||||||
jsonify,
|
jsonify,
|
||||||
request
|
request,
|
||||||
|
current_app
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from app import DATE_FORMAT
|
||||||
|
|
||||||
from app.dao.notifications_dao import (
|
from app.dao.notifications_dao import (
|
||||||
dao_get_notification_statistics_for_service,
|
dao_get_notification_statistics_for_service,
|
||||||
dao_get_7_day_agg_notification_statistics_for_service
|
dao_get_7_day_agg_notification_statistics_for_service,
|
||||||
|
dao_get_notification_statistics_for_service_and_day
|
||||||
)
|
)
|
||||||
from app.schemas import (
|
from app.schemas import (
|
||||||
notifications_statistics_schema,
|
notifications_statistics_schema,
|
||||||
@@ -47,6 +56,28 @@ def get_all_notification_statistics_for_service(service_id):
|
|||||||
return jsonify(data=data)
|
return jsonify(data=data)
|
||||||
|
|
||||||
|
|
||||||
|
@notifications_statistics.route('/day/<day>', methods=['GET'])
|
||||||
|
def get_notification_statistics_for_service_for_day(service_id, day):
|
||||||
|
|
||||||
|
try:
|
||||||
|
datetime.strptime(day, DATE_FORMAT)
|
||||||
|
except ValueError:
|
||||||
|
raise InvalidRequest('Invalid date {}'.format(day), status_code=400)
|
||||||
|
|
||||||
|
service_stats = dao_get_notification_statistics_for_service_and_day(
|
||||||
|
service_id,
|
||||||
|
day
|
||||||
|
)
|
||||||
|
|
||||||
|
if not service_stats:
|
||||||
|
message = 'No statistics found for service id: {} on day: {} '.format(service_id, day)
|
||||||
|
errors = {'not found': [message]}
|
||||||
|
raise InvalidRequest(errors, status_code=404)
|
||||||
|
|
||||||
|
data = notifications_statistics_schema.dump(service_stats).data
|
||||||
|
return jsonify(data=data)
|
||||||
|
|
||||||
|
|
||||||
@notifications_statistics.route('/seven_day_aggregate')
|
@notifications_statistics.route('/seven_day_aggregate')
|
||||||
def get_notification_statistics_for_service_seven_day_aggregate(service_id):
|
def get_notification_statistics_for_service_seven_day_aggregate(service_id):
|
||||||
data = week_aggregate_notification_statistics_schema.load(request.args).data
|
data = week_aggregate_notification_statistics_schema.load(request.args).data
|
||||||
|
|||||||
@@ -1,31 +1,34 @@
|
|||||||
import json
|
import json
|
||||||
from datetime import (date, timedelta)
|
from datetime import (
|
||||||
|
date,
|
||||||
|
timedelta
|
||||||
|
)
|
||||||
|
|
||||||
from flask import url_for
|
from flask import url_for
|
||||||
from tests import create_authorization_header
|
from tests import create_authorization_header
|
||||||
from tests.app.conftest import sample_notification_statistics as create_sample_notification_statistics
|
from tests.app.conftest import sample_notification_statistics as create_sample_notification_statistics
|
||||||
|
|
||||||
|
from freezegun import freeze_time
|
||||||
|
|
||||||
def test_get_notification_statistics(
|
|
||||||
notify_api,
|
def test_get_notification_statistics_returns_empty_list_if_no_stats(notify_api,
|
||||||
notify_db,
|
notify_db,
|
||||||
notify_db_session,
|
notify_db_session,
|
||||||
sample_template,
|
sample_template,
|
||||||
sample_email_template
|
sample_email_template):
|
||||||
):
|
|
||||||
with notify_api.test_request_context():
|
with notify_api.test_request_context():
|
||||||
with notify_api.test_client() as client:
|
with notify_api.test_client() as client:
|
||||||
|
|
||||||
path = '/service/{}/notifications-statistics'.format(sample_email_template.service)
|
path = '/service/{}/notifications-statistics'.format(sample_email_template.service.id)
|
||||||
|
|
||||||
auth_header = create_authorization_header(
|
auth_header = create_authorization_header(
|
||||||
service_id=sample_email_template.service_id)
|
service_id=sample_email_template.service_id)
|
||||||
|
|
||||||
response = client.get(path, headers=[auth_header])
|
response = client.get(path, headers=[auth_header])
|
||||||
assert response.status_code == 404
|
assert response.status_code == 200
|
||||||
|
|
||||||
stats = json.loads(response.get_data(as_text=True))
|
stats = json.loads(response.get_data(as_text=True))
|
||||||
assert stats['result'] == 'error'
|
assert stats['data'] == []
|
||||||
assert stats['message'] == 'No result found'
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_week_aggregate_statistics(notify_api,
|
def test_get_week_aggregate_statistics(notify_api,
|
||||||
@@ -120,3 +123,79 @@ def test_get_week_aggregate_statistics_invalid_week_count(notify_api,
|
|||||||
json_resp = json.loads(resp.get_data(as_text=True))
|
json_resp = json.loads(resp.get_data(as_text=True))
|
||||||
assert json_resp['result'] == 'error'
|
assert json_resp['result'] == 'error'
|
||||||
assert json_resp['message']['week_count'][0] == 'Not a positive integer'
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user