mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-16 02:02:13 -05:00
Ensure GET /notifications returns JOB or API data correctly, using new boolean query param "include_jobs"
This commit is contained in:
@@ -168,6 +168,7 @@ def get_notification_by_id(notification_id):
|
||||
@notifications.route('/notifications', methods=['GET'])
|
||||
def get_all_notifications():
|
||||
data = notifications_filter_schema.load(request.args).data
|
||||
include_jobs = data.get('include_jobs', False)
|
||||
page = data['page'] if 'page' in data else 1
|
||||
page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE')
|
||||
limit_days = data.get('limit_days')
|
||||
@@ -179,7 +180,8 @@ def get_all_notifications():
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
limit_days=limit_days,
|
||||
key_type=api_user.key_type)
|
||||
key_type=api_user.key_type,
|
||||
include_jobs=include_jobs)
|
||||
return jsonify(
|
||||
notifications=notification_with_personalisation_schema.dump(pagination.items, many=True).data,
|
||||
page_size=page_size,
|
||||
|
||||
@@ -11,7 +11,6 @@ from app.dao.templates_dao import dao_update_template
|
||||
from app.models import ApiKey, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST
|
||||
from tests import create_authorization_header
|
||||
from tests.app.conftest import sample_notification as create_sample_notification
|
||||
from notifications_utils.template import NeededByTemplateError
|
||||
|
||||
|
||||
def test_get_sms_notification_by_id(notify_api, sample_notification):
|
||||
@@ -32,10 +31,6 @@ def test_get_sms_notification_by_id(notify_api, sample_notification):
|
||||
'template_type': sample_notification.template.template_type,
|
||||
'version': 1
|
||||
}
|
||||
assert notification['job'] == {
|
||||
'id': str(sample_notification.job.id),
|
||||
'original_file_name': sample_notification.job.original_file_name
|
||||
}
|
||||
assert notification['to'] == '+447700900855'
|
||||
assert notification['service'] == str(sample_notification.service_id)
|
||||
assert notification['body'] == "This is a template:\nwith a newline"
|
||||
@@ -67,11 +62,6 @@ def test_get_email_notification_by_id(notify_api, notify_db, notify_db_session,
|
||||
'template_type': email_notification.template.template_type,
|
||||
'version': 1
|
||||
}
|
||||
assert notification['job'] == {
|
||||
'id': str(email_notification.job.id),
|
||||
'original_file_name': email_notification.job.original_file_name
|
||||
}
|
||||
|
||||
assert notification['to'] == '+447700900855'
|
||||
assert notification['service'] == str(email_notification.service_id)
|
||||
assert response.status_code == 200
|
||||
@@ -173,10 +163,6 @@ def test_get_all_notifications(notify_api, sample_notification):
|
||||
'template_type': sample_notification.template.template_type,
|
||||
'version': 1
|
||||
}
|
||||
assert notifications['notifications'][0]['job'] == {
|
||||
'id': str(sample_notification.job.id),
|
||||
'original_file_name': sample_notification.job.original_file_name
|
||||
}
|
||||
|
||||
assert notifications['notifications'][0]['to'] == '+447700900855'
|
||||
assert notifications['notifications'][0]['service'] == str(sample_notification.service_id)
|
||||
@@ -272,6 +258,149 @@ def test_get_all_notifications_only_returns_notifications_of_matching_type(
|
||||
assert notifications[0]['id'] == str(notification_objs[key_type].id)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type', [KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST])
|
||||
def test_no_api_keys_return_job_notifications_by_default(
|
||||
notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service,
|
||||
sample_job,
|
||||
key_type
|
||||
):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
team_api_key = ApiKey(service=sample_service,
|
||||
name='team_api_key',
|
||||
created_by=sample_service.created_by,
|
||||
key_type=KEY_TYPE_TEAM)
|
||||
save_model_api_key(team_api_key)
|
||||
|
||||
normal_api_key = ApiKey(service=sample_service,
|
||||
name='normal_api_key',
|
||||
created_by=sample_service.created_by,
|
||||
key_type=KEY_TYPE_NORMAL)
|
||||
save_model_api_key(normal_api_key)
|
||||
|
||||
test_api_key = ApiKey(service=sample_service,
|
||||
name='test_api_key',
|
||||
created_by=sample_service.created_by,
|
||||
key_type=KEY_TYPE_TEST)
|
||||
save_model_api_key(test_api_key)
|
||||
|
||||
job_notification = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
api_key_id=normal_api_key.id,
|
||||
job=sample_job
|
||||
)
|
||||
normal_notification = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
api_key_id=normal_api_key.id,
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
)
|
||||
team_notification = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
api_key_id=team_api_key.id,
|
||||
key_type=KEY_TYPE_TEAM
|
||||
)
|
||||
test_notification = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
api_key_id=test_api_key.id,
|
||||
key_type=KEY_TYPE_TEST
|
||||
)
|
||||
|
||||
notification_objs = {
|
||||
KEY_TYPE_NORMAL: normal_notification,
|
||||
KEY_TYPE_TEAM: team_notification,
|
||||
KEY_TYPE_TEST: test_notification
|
||||
}
|
||||
|
||||
response = client.get(
|
||||
path='/notifications',
|
||||
headers=_create_auth_header_from_key(notification_objs[key_type].api_key))
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
notifications = json.loads(response.get_data(as_text=True))['notifications']
|
||||
assert len(notifications) == 1
|
||||
assert notifications[0]['id'] == str(notification_objs[key_type].id)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type', [
|
||||
(KEY_TYPE_NORMAL, 2),
|
||||
(KEY_TYPE_TEAM, 1),
|
||||
(KEY_TYPE_TEST, 1)
|
||||
])
|
||||
def test_only_normal_api_keys_can_return_job_notifications(
|
||||
notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service,
|
||||
sample_job,
|
||||
key_type
|
||||
):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
team_api_key = ApiKey(service=sample_service,
|
||||
name='team_api_key',
|
||||
created_by=sample_service.created_by,
|
||||
key_type=KEY_TYPE_TEAM)
|
||||
save_model_api_key(team_api_key)
|
||||
|
||||
normal_api_key = ApiKey(service=sample_service,
|
||||
name='normal_api_key',
|
||||
created_by=sample_service.created_by,
|
||||
key_type=KEY_TYPE_NORMAL)
|
||||
save_model_api_key(normal_api_key)
|
||||
|
||||
test_api_key = ApiKey(service=sample_service,
|
||||
name='test_api_key',
|
||||
created_by=sample_service.created_by,
|
||||
key_type=KEY_TYPE_TEST)
|
||||
save_model_api_key(test_api_key)
|
||||
|
||||
job_notification = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
api_key_id=normal_api_key.id,
|
||||
job=sample_job
|
||||
)
|
||||
normal_notification = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
api_key_id=normal_api_key.id,
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
)
|
||||
team_notification = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
api_key_id=team_api_key.id,
|
||||
key_type=KEY_TYPE_TEAM
|
||||
)
|
||||
test_notification = create_sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
api_key_id=test_api_key.id,
|
||||
key_type=KEY_TYPE_TEST
|
||||
)
|
||||
|
||||
notification_objs = {
|
||||
KEY_TYPE_NORMAL: normal_notification,
|
||||
KEY_TYPE_TEAM: team_notification,
|
||||
KEY_TYPE_TEST: test_notification
|
||||
}
|
||||
|
||||
response = client.get(
|
||||
path='/notifications?include_jobs=true',
|
||||
headers=_create_auth_header_from_key(notification_objs[key_type[0]].api_key))
|
||||
|
||||
assert response.status_code == 200
|
||||
notifications = json.loads(response.get_data(as_text=True))['notifications']
|
||||
assert len(notifications) == key_type[1]
|
||||
assert notifications[0]['id'] == str(notification_objs[key_type[0]].id)
|
||||
|
||||
|
||||
def test_get_all_notifications_newest_first(notify_api, notify_db, notify_db_session, sample_email_template):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
Reference in New Issue
Block a user