mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Merge pull request #679 from alphagov/remove-test-keys-from-admin-app
Remove test keys from admin app
This commit is contained in:
@@ -21,7 +21,7 @@ from app.models import (
|
|||||||
NOTIFICATION_CREATED,
|
NOTIFICATION_CREATED,
|
||||||
NOTIFICATION_SENDING,
|
NOTIFICATION_SENDING,
|
||||||
NOTIFICATION_PENDING,
|
NOTIFICATION_PENDING,
|
||||||
NOTIFICATION_TEMPORARY_FAILURE, KEY_TYPE_NORMAL)
|
NOTIFICATION_TEMPORARY_FAILURE, KEY_TYPE_NORMAL, KEY_TYPE_TEST)
|
||||||
|
|
||||||
from app.dao.dao_utils import transactional
|
from app.dao.dao_utils import transactional
|
||||||
from app.statsd_decorators import statsd
|
from app.statsd_decorators import statsd
|
||||||
@@ -97,7 +97,7 @@ def dao_get_template_usage(service_id, limit_days=None):
|
|||||||
Template.template_type
|
Template.template_type
|
||||||
)
|
)
|
||||||
|
|
||||||
query_filter = [table.service_id == service_id]
|
query_filter = [table.service_id == service_id, table.key_type != KEY_TYPE_TEST]
|
||||||
if limit_days is not None:
|
if limit_days is not None:
|
||||||
query_filter.append(table.created_at >= days_ago(limit_days))
|
query_filter.append(table.created_at >= days_ago(limit_days))
|
||||||
|
|
||||||
@@ -110,7 +110,9 @@ def dao_get_template_usage(service_id, limit_days=None):
|
|||||||
|
|
||||||
@statsd(namespace="dao")
|
@statsd(namespace="dao")
|
||||||
def dao_get_last_template_usage(template_id):
|
def dao_get_last_template_usage(template_id):
|
||||||
return NotificationHistory.query.filter(NotificationHistory.template_id == template_id) \
|
return NotificationHistory.query.filter(
|
||||||
|
NotificationHistory.template_id == template_id,
|
||||||
|
NotificationHistory.key_type != KEY_TYPE_TEST) \
|
||||||
.join(Template) \
|
.join(Template) \
|
||||||
.order_by(desc(NotificationHistory.created_at)) \
|
.order_by(desc(NotificationHistory.created_at)) \
|
||||||
.first()
|
.first()
|
||||||
@@ -248,6 +250,9 @@ def get_notifications_for_service(service_id,
|
|||||||
|
|
||||||
if key_type is not None:
|
if key_type is not None:
|
||||||
filters.append(Notification.key_type == key_type)
|
filters.append(Notification.key_type == key_type)
|
||||||
|
else:
|
||||||
|
filters.append(Notification.key_type != KEY_TYPE_TEST)
|
||||||
|
|
||||||
query = Notification.query.filter(*filters)
|
query = Notification.query.filter(*filters)
|
||||||
query = _filter_query(query, filter_dict)
|
query = _filter_query(query, filter_dict)
|
||||||
if personalisation:
|
if personalisation:
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ from app.models import (
|
|||||||
Permission,
|
Permission,
|
||||||
User,
|
User,
|
||||||
InvitedUser,
|
InvitedUser,
|
||||||
Service
|
Service,
|
||||||
)
|
KEY_TYPE_TEST)
|
||||||
from app.statsd_decorators import statsd
|
from app.statsd_decorators import statsd
|
||||||
|
|
||||||
|
|
||||||
@@ -156,7 +156,8 @@ def _stats_for_service_query(service_id):
|
|||||||
Notification.status,
|
Notification.status,
|
||||||
func.count(Notification.id).label('count')
|
func.count(Notification.id).label('count')
|
||||||
).filter(
|
).filter(
|
||||||
Notification.service_id == service_id
|
Notification.service_id == service_id,
|
||||||
|
Notification.key_type != KEY_TYPE_TEST
|
||||||
).group_by(
|
).group_by(
|
||||||
Notification.notification_type,
|
Notification.notification_type,
|
||||||
Notification.status,
|
Notification.status,
|
||||||
|
|||||||
@@ -89,6 +89,36 @@ def test_should_be_able_to_get_all_template_usage_history_order_by_notification_
|
|||||||
assert results.id == most_recent.id
|
assert results.id == most_recent.id
|
||||||
|
|
||||||
|
|
||||||
|
def test_template_usage_should_ignore_test_keys(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
sample_team_api_key,
|
||||||
|
sample_test_api_key
|
||||||
|
):
|
||||||
|
sms = sample_template(notify_db, notify_db_session)
|
||||||
|
|
||||||
|
one_minute_ago = datetime.utcnow() - timedelta(minutes=1)
|
||||||
|
two_minutes_ago = datetime.utcnow() - timedelta(minutes=2)
|
||||||
|
|
||||||
|
team_key = sample_notification(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
created_at=two_minutes_ago,
|
||||||
|
template=sms,
|
||||||
|
api_key_id=sample_team_api_key.id,
|
||||||
|
key_type=KEY_TYPE_TEAM)
|
||||||
|
sample_notification(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
created_at=one_minute_ago,
|
||||||
|
template=sms,
|
||||||
|
api_key_id=sample_test_api_key.id,
|
||||||
|
key_type=KEY_TYPE_TEST)
|
||||||
|
|
||||||
|
results = dao_get_last_template_usage(sms.id)
|
||||||
|
assert results.id == team_key.id
|
||||||
|
|
||||||
|
|
||||||
def test_should_be_able_to_get_no_template_usage_history_if_no_notifications_using_template(
|
def test_should_be_able_to_get_no_template_usage_history_if_no_notifications_using_template(
|
||||||
notify_db,
|
notify_db,
|
||||||
notify_db_session):
|
notify_db_session):
|
||||||
@@ -117,6 +147,30 @@ def test_should_by_able_to_get_template_count_from_notifications_history(notify_
|
|||||||
assert results[1].count == 3
|
assert results[1].count == 3
|
||||||
|
|
||||||
|
|
||||||
|
def test_template_history_should_ignore_test_keys(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
sample_team_api_key,
|
||||||
|
sample_test_api_key,
|
||||||
|
sample_api_key
|
||||||
|
):
|
||||||
|
sms = sample_template(notify_db, notify_db_session)
|
||||||
|
|
||||||
|
sample_notification(
|
||||||
|
notify_db, notify_db_session, template=sms, api_key_id=sample_api_key.id, key_type=KEY_TYPE_NORMAL)
|
||||||
|
sample_notification(
|
||||||
|
notify_db, notify_db_session, template=sms, api_key_id=sample_team_api_key.id, key_type=KEY_TYPE_TEAM)
|
||||||
|
sample_notification(
|
||||||
|
notify_db, notify_db_session, template=sms, api_key_id=sample_test_api_key.id, key_type=KEY_TYPE_TEST)
|
||||||
|
sample_notification(
|
||||||
|
notify_db, notify_db_session, template=sms)
|
||||||
|
|
||||||
|
results = dao_get_template_usage(sms.service_id)
|
||||||
|
assert results[0].name == 'Template Name'
|
||||||
|
assert results[0].template_type == 'sms'
|
||||||
|
assert results[0].count == 3
|
||||||
|
|
||||||
|
|
||||||
def test_should_by_able_to_get_template_count_from_notifications_history_for_service(
|
def test_should_by_able_to_get_template_count_from_notifications_history_for_service(
|
||||||
notify_db,
|
notify_db,
|
||||||
notify_db_session):
|
notify_db_session):
|
||||||
@@ -917,7 +971,7 @@ def test_should_return_notifications_including_jobs(notify_db, notify_db_session
|
|||||||
assert all_notifications[0].id == with_job.id
|
assert all_notifications[0].id == with_job.id
|
||||||
|
|
||||||
|
|
||||||
def test_get_notifications_created_by_api_or_csv_are_returned_correctly(
|
def test_get_notifications_created_by_api_or_csv_are_returned_correctly_excluding_test_key_notifications(
|
||||||
notify_db,
|
notify_db,
|
||||||
notify_db_session,
|
notify_db_session,
|
||||||
sample_service,
|
sample_service,
|
||||||
@@ -948,11 +1002,11 @@ def test_get_notifications_created_by_api_or_csv_are_returned_correctly(
|
|||||||
|
|
||||||
# returns all API derived notifications
|
# returns all API derived notifications
|
||||||
all_notifications = get_notifications_for_service(sample_service.id).items
|
all_notifications = get_notifications_for_service(sample_service.id).items
|
||||||
assert len(all_notifications) == 3
|
assert len(all_notifications) == 2
|
||||||
|
|
||||||
# all notifications including jobs
|
# all notifications including jobs
|
||||||
all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, include_jobs=True).items
|
all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, include_jobs=True).items
|
||||||
assert len(all_notifications) == 4
|
assert len(all_notifications) == 3
|
||||||
|
|
||||||
|
|
||||||
def test_get_notifications_with_a_live_api_key_type(
|
def test_get_notifications_with_a_live_api_key_type(
|
||||||
@@ -1061,3 +1115,42 @@ def test_get_notifications_with_a_team_api_key_type(
|
|||||||
all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, include_jobs=True,
|
all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, include_jobs=True,
|
||||||
key_type=KEY_TYPE_TEAM).items
|
key_type=KEY_TYPE_TEAM).items
|
||||||
assert len(all_notifications) == 1
|
assert len(all_notifications) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_exclude_test_key_notifications_by_default(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
sample_service,
|
||||||
|
sample_job,
|
||||||
|
sample_api_key,
|
||||||
|
sample_team_api_key,
|
||||||
|
sample_test_api_key
|
||||||
|
):
|
||||||
|
sample_notification(
|
||||||
|
notify_db, notify_db_session, created_at=datetime.utcnow(), job=sample_job
|
||||||
|
)
|
||||||
|
|
||||||
|
sample_notification(
|
||||||
|
notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_api_key.id,
|
||||||
|
key_type=sample_api_key.key_type
|
||||||
|
)
|
||||||
|
sample_notification(
|
||||||
|
notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_team_api_key.id,
|
||||||
|
key_type=sample_team_api_key.key_type
|
||||||
|
)
|
||||||
|
sample_notification(
|
||||||
|
notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_test_api_key.id,
|
||||||
|
key_type=sample_test_api_key.key_type
|
||||||
|
)
|
||||||
|
|
||||||
|
all_notifications = Notification.query.all()
|
||||||
|
assert len(all_notifications) == 4
|
||||||
|
|
||||||
|
all_notifications = get_notifications_for_service(sample_service.id, limit_days=1).items
|
||||||
|
assert len(all_notifications) == 2
|
||||||
|
|
||||||
|
all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, include_jobs=True).items
|
||||||
|
assert len(all_notifications) == 3
|
||||||
|
|
||||||
|
all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, key_type=KEY_TYPE_TEST).items
|
||||||
|
assert len(all_notifications) == 1
|
||||||
|
|||||||
@@ -441,6 +441,30 @@ def test_fetch_stats_counts_correctly(notify_db, notify_db_session, sample_templ
|
|||||||
assert stats[2].count == 1
|
assert stats[2].count == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_fetch_stats_counts_should_ignore_team_key(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
sample_template,
|
||||||
|
sample_api_key,
|
||||||
|
sample_test_api_key,
|
||||||
|
sample_team_api_key
|
||||||
|
):
|
||||||
|
# two created email, one failed email, and one created sms
|
||||||
|
create_notification(notify_db, notify_db_session, api_key_id=sample_api_key.id, key_type=sample_api_key.key_type)
|
||||||
|
create_notification(
|
||||||
|
notify_db, notify_db_session, api_key_id=sample_test_api_key.id, key_type=sample_test_api_key.key_type)
|
||||||
|
create_notification(
|
||||||
|
notify_db, notify_db_session, api_key_id=sample_team_api_key.id, key_type=sample_team_api_key.key_type)
|
||||||
|
create_notification(
|
||||||
|
notify_db, notify_db_session)
|
||||||
|
|
||||||
|
stats = dao_fetch_stats_for_service(sample_template.service_id)
|
||||||
|
assert len(stats) == 1
|
||||||
|
assert stats[0].notification_type == 'sms'
|
||||||
|
assert stats[0].status == 'created'
|
||||||
|
assert stats[0].count == 3
|
||||||
|
|
||||||
|
|
||||||
def test_fetch_stats_for_today_only_includes_today(notify_db, notify_db_session, sample_template):
|
def test_fetch_stats_for_today_only_includes_today(notify_db, notify_db_session, sample_template):
|
||||||
# two created email, one failed email, and one created sms
|
# two created email, one failed email, and one created sms
|
||||||
with freeze_time('2001-01-01T23:59:00'):
|
with freeze_time('2001-01-01T23:59:00'):
|
||||||
|
|||||||
Reference in New Issue
Block a user