mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-12 16:22:17 -05:00
Add schema and validate platform stats requests
Added a schema to check that the start_date and end_date arguments are both in the date format.
This commit is contained in:
10
app/platform_stats/platform_stats_schema.py
Normal file
10
app/platform_stats/platform_stats_schema.py
Normal file
@@ -0,0 +1,10 @@
|
||||
platform_stats_request = {
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"description": "platform stats request schema",
|
||||
"type": "object",
|
||||
"title": "Platform stats request",
|
||||
"properties": {
|
||||
"start_date": {"type": ["string", "null"], "format": "date"},
|
||||
"end_date": {"type": ["string", "null"], "format": "date"},
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,9 @@ from flask import Blueprint, jsonify, request
|
||||
|
||||
from app.dao.notifications_dao import fetch_aggregate_stats_by_date_range_for_all_services
|
||||
from app.errors import register_errors
|
||||
from app.platform_stats.platform_stats_schema import platform_stats_request
|
||||
from app.service.statistics import format_admin_stats
|
||||
from app.schema_validation import validate
|
||||
|
||||
platform_stats_blueprint = Blueprint('platform_stats', __name__)
|
||||
|
||||
@@ -12,7 +14,10 @@ register_errors(platform_stats_blueprint)
|
||||
|
||||
|
||||
@platform_stats_blueprint.route('')
|
||||
def get_new_platform_stats():
|
||||
def get_platform_stats():
|
||||
if request.args:
|
||||
validate(request.args, platform_stats_request)
|
||||
|
||||
# If start and end date are not set, we are expecting today's stats.
|
||||
today = str(datetime.utcnow().date())
|
||||
|
||||
|
||||
@@ -4,22 +4,34 @@ from freezegun import freeze_time
|
||||
|
||||
|
||||
@freeze_time('2018-06-01')
|
||||
def test_get_new_platform_stats_uses_todays_date_if_no_start_or_end_date_is_provided(admin_request, mocker):
|
||||
def test_get_platform_stats_uses_todays_date_if_no_start_or_end_date_is_provided(admin_request, mocker):
|
||||
today = datetime.now().date()
|
||||
dao_mock = mocker.patch('app.platform_stats.rest.fetch_aggregate_stats_by_date_range_for_all_services')
|
||||
mocker.patch('app.service.rest.statistics.format_statistics')
|
||||
|
||||
admin_request.get('platform_stats.get_new_platform_stats')
|
||||
admin_request.get('platform_stats.get_platform_stats')
|
||||
|
||||
dao_mock.assert_called_once_with(start_date=today, end_date=today)
|
||||
|
||||
|
||||
def test_get_new_platform_stats_can_filter_by_date(admin_request, mocker):
|
||||
def test_get_platform_stats_can_filter_by_date(admin_request, mocker):
|
||||
start_date = date(2017, 1, 1)
|
||||
end_date = date(2018, 1, 1)
|
||||
dao_mock = mocker.patch('app.platform_stats.rest.fetch_aggregate_stats_by_date_range_for_all_services')
|
||||
mocker.patch('app.service.rest.statistics.format_statistics')
|
||||
|
||||
admin_request.get('platform_stats.get_new_platform_stats', start_date=start_date, end_date=end_date)
|
||||
admin_request.get('platform_stats.get_platform_stats', start_date=start_date, end_date=end_date)
|
||||
|
||||
dao_mock.assert_called_once_with(start_date=start_date, end_date=end_date)
|
||||
|
||||
|
||||
def test_get_platform_stats_validates_the_date(admin_request):
|
||||
start_date = '1234-56-78'
|
||||
|
||||
response = admin_request.get(
|
||||
'platform_stats.get_platform_stats', start_date=start_date,
|
||||
_expected_status=400
|
||||
)
|
||||
|
||||
assert response['errors'][0]['message'] == 'start_date time data {} does not match format %Y-%m-%d'.format(
|
||||
start_date)
|
||||
|
||||
Reference in New Issue
Block a user