Check service has permission to send notification type.

At the moment the check_permission boolean is always false.
Will set to true for usage pages
This commit is contained in:
Rebecca Law
2020-02-19 16:57:04 +00:00
parent 0f6f2f1b91
commit ca010ac4cb
2 changed files with 42 additions and 13 deletions

View File

@@ -4,7 +4,7 @@ from flask import current_app
from notifications_utils.statsd_decorators import statsd from notifications_utils.statsd_decorators import statsd
from notifications_utils.timezones import convert_bst_to_utc, convert_utc_to_bst from notifications_utils.timezones import convert_bst_to_utc, convert_utc_to_bst
from sqlalchemy.dialects.postgresql import insert from sqlalchemy.dialects.postgresql import insert
from sqlalchemy import func, case, desc, Date, Integer, and_ from sqlalchemy import func, desc, Date, Integer, and_
from sqlalchemy.sql.expression import literal from sqlalchemy.sql.expression import literal
from app import db from app import db
@@ -310,7 +310,7 @@ def delete_billing_data_for_service_for_day(process_day, service_id):
@statsd(namespace="dao") @statsd(namespace="dao")
def fetch_billing_data_for_day(process_day, service_id=None): def fetch_billing_data_for_day(process_day, service_id=None, check_permissions=False):
start_date = convert_bst_to_utc(datetime.combine(process_day, time.min)) start_date = convert_bst_to_utc(datetime.combine(process_day, time.min))
end_date = convert_bst_to_utc(datetime.combine(process_day + timedelta(days=1), time.min)) end_date = convert_bst_to_utc(datetime.combine(process_day + timedelta(days=1), time.min))
current_app.logger.info("Populate ft_billing for {} to {}".format(start_date, end_date)) current_app.logger.info("Populate ft_billing for {} to {}".format(start_date, end_date))
@@ -322,17 +322,17 @@ def fetch_billing_data_for_day(process_day, service_id=None):
for service in services: for service in services:
for notification_type in (SMS_TYPE, EMAIL_TYPE, LETTER_TYPE): for notification_type in (SMS_TYPE, EMAIL_TYPE, LETTER_TYPE):
table = get_notification_table_to_use(service, notification_type, process_day, has_delete_task_run=False) if (not check_permissions) or service.has_permission(notification_type):
table = get_notification_table_to_use(service, notification_type, process_day,
results = _query_for_billing_data( has_delete_task_run=False)
table=table, results = _query_for_billing_data(
notification_type=notification_type, table=table,
start_date=start_date, notification_type=notification_type,
end_date=end_date, start_date=start_date,
service=service end_date=end_date,
) service=service
)
transit_data += results transit_data += results
return transit_data return transit_data

View File

@@ -70,6 +70,34 @@ def test_fetch_billing_data_for_today_includes_data_with_the_right_key_type(noti
assert results[0].notifications_sent == 2 assert results[0].notifications_sent == 2
@pytest.mark.parametrize("notification_type", ["email", "sms", "letter"])
def test_fetch_billing_data_for_day_only_calls_query_for_permission_type(notify_db_session, notification_type):
service = create_service(service_permissions=[notification_type])
email_template = create_template(service=service, template_type="email")
sms_template = create_template(service=service, template_type="sms")
letter_template = create_template(service=service, template_type="letter")
create_notification(template=email_template, status='delivered')
create_notification(template=sms_template, status='delivered')
create_notification(template=letter_template, status='delivered')
today = convert_utc_to_bst(datetime.utcnow())
results = fetch_billing_data_for_day(process_day=today.date(), check_permissions=True)
assert len(results) == 1
@pytest.mark.parametrize("notification_type", ["email", "sms", "letter"])
def test_fetch_billing_data_for_day_only_calls_query_for_all_channels(notify_db_session, notification_type):
service = create_service(service_permissions=[notification_type])
email_template = create_template(service=service, template_type="email")
sms_template = create_template(service=service, template_type="sms")
letter_template = create_template(service=service, template_type="letter")
create_notification(template=email_template, status='delivered')
create_notification(template=sms_template, status='delivered')
create_notification(template=letter_template, status='delivered')
today = convert_utc_to_bst(datetime.utcnow())
results = fetch_billing_data_for_day(process_day=today.date(), check_permissions=False)
assert len(results) == 3
@freeze_time('2018-04-02 01:20:00') @freeze_time('2018-04-02 01:20:00')
def test_fetch_billing_data_for_today_includes_data_with_the_right_date(notify_db_session): def test_fetch_billing_data_for_today_includes_data_with_the_right_date(notify_db_session):
process_day = datetime(2018, 4, 1, 13, 30, 0) process_day = datetime(2018, 4, 1, 13, 30, 0)
@@ -288,6 +316,7 @@ def test_fetch_billing_data_for_day_bills_correctly_for_status(notify_db_session
assert 4 == email_results[0].notifications_sent assert 4 == email_results[0].notifications_sent
assert 3 == letter_results[0].notifications_sent assert 3 == letter_results[0].notifications_sent
def test_get_rates_for_billing(notify_db_session): def test_get_rates_for_billing(notify_db_session):
create_rate(start_date=datetime.utcnow(), value=12, notification_type='email') create_rate(start_date=datetime.utcnow(), value=12, notification_type='email')
create_rate(start_date=datetime.utcnow(), value=22, notification_type='sms') create_rate(start_date=datetime.utcnow(), value=22, notification_type='sms')