mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 18:31:13 -05:00
Merge branch 'main' of https://github.com/GSA/notifications-api into notify-260
This commit is contained in:
@@ -7,6 +7,7 @@ from freezegun import freeze_time
|
||||
from app.celery import nightly_tasks
|
||||
from app.celery.nightly_tasks import (
|
||||
_delete_notifications_older_than_retention_by_type,
|
||||
cleanup_unfinished_jobs,
|
||||
delete_email_notifications_older_than_retention,
|
||||
delete_inbound_sms,
|
||||
delete_sms_notifications_older_than_retention,
|
||||
@@ -15,7 +16,7 @@ from app.celery.nightly_tasks import (
|
||||
save_daily_notification_processing_time,
|
||||
timeout_notifications,
|
||||
)
|
||||
from app.models import EMAIL_TYPE, SMS_TYPE, FactProcessingTime
|
||||
from app.models import EMAIL_TYPE, SMS_TYPE, FactProcessingTime, Job
|
||||
from tests.app.db import (
|
||||
create_job,
|
||||
create_notification,
|
||||
@@ -313,3 +314,17 @@ def test_delete_notifications_task_calls_task_for_services_that_have_sent_notifi
|
||||
'datetime_to_delete_before': date(2021, 3, 26)
|
||||
}),
|
||||
])
|
||||
|
||||
|
||||
def test_cleanup_unfinished_jobs(mocker):
|
||||
mock_s3 = mocker.patch('app.celery.nightly_tasks.remove_csv_object')
|
||||
mock_dao_archive = mocker.patch('app.celery.nightly_tasks.dao_archive_job')
|
||||
mock_dao = mocker.patch('app.celery.nightly_tasks.dao_get_unfinished_jobs')
|
||||
mock_job_unfinished = Job()
|
||||
mock_job_unfinished.processing_started = datetime(2023, 1, 1, 0, 0, 0)
|
||||
mock_job_unfinished.original_file_name = "blah"
|
||||
|
||||
mock_dao.return_value = [mock_job_unfinished]
|
||||
cleanup_unfinished_jobs()
|
||||
mock_s3.assert_called_once_with('blah')
|
||||
mock_dao_archive.assert_called_once_with(mock_job_unfinished)
|
||||
|
||||
@@ -67,11 +67,11 @@ def test_should_retry_and_log_warning_if_SmsClientResponseException_for_deliver_
|
||||
)
|
||||
mocker.patch('app.celery.provider_tasks.deliver_sms.retry')
|
||||
mock_logger_warning = mocker.patch('app.celery.tasks.current_app.logger.warning')
|
||||
assert sample_notification.status == 'created'
|
||||
|
||||
deliver_sms(sample_notification.id)
|
||||
|
||||
assert provider_tasks.deliver_sms.retry.called is True
|
||||
assert sample_notification.status == 'created'
|
||||
assert mock_logger_warning.called
|
||||
|
||||
|
||||
@@ -82,14 +82,13 @@ def test_should_retry_and_log_exception_for_non_SmsClientResponseException_excep
|
||||
mocker.patch('app.celery.provider_tasks.deliver_sms.retry')
|
||||
mock_logger_exception = mocker.patch('app.celery.tasks.current_app.logger.exception')
|
||||
|
||||
assert sample_notification.status == 'created'
|
||||
deliver_sms(sample_notification.id)
|
||||
|
||||
assert provider_tasks.deliver_sms.retry.called is True
|
||||
assert sample_notification.status == 'created'
|
||||
assert mock_logger_exception.called
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Needs updating for TTS: Failing for unknown reason")
|
||||
def test_should_go_into_technical_error_if_exceeds_retries_on_deliver_sms_task(sample_notification, mocker):
|
||||
mocker.patch('app.delivery.send_to_providers.send_sms_to_provider', side_effect=Exception("EXPECTED"))
|
||||
mocker.patch('app.celery.provider_tasks.deliver_sms.retry', side_effect=MaxRetriesExceededError())
|
||||
@@ -128,7 +127,6 @@ def test_should_add_to_retry_queue_if_notification_not_found_in_deliver_email_ta
|
||||
app.celery.provider_tasks.deliver_email.retry.assert_called_with(queue="retry-tasks")
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Needs updating for TTS: Failing for unknown reason")
|
||||
@pytest.mark.parametrize(
|
||||
'exception_class', [
|
||||
Exception(),
|
||||
@@ -150,7 +148,6 @@ def test_should_go_into_technical_error_if_exceeds_retries_on_deliver_email_task
|
||||
assert sample_notification.status == 'technical-failure'
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Needs updating for TTS: Failing for unknown reason")
|
||||
def test_should_technical_error_and_not_retry_if_EmailClientNonRetryableException(sample_notification, mocker):
|
||||
mocker.patch(
|
||||
'app.delivery.send_to_providers.send_email_to_provider',
|
||||
|
||||
@@ -108,10 +108,9 @@ def test_create_nightly_notification_status_triggers_relevant_tasks(
|
||||
assert types == expected_types_aggregated
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Needs updating for TTS: Timezone handling")
|
||||
def test_create_nightly_billing_for_day_checks_history(
|
||||
sample_service,
|
||||
sample_sms_template,
|
||||
sample_template,
|
||||
mocker
|
||||
):
|
||||
yesterday = datetime.now() - timedelta(days=1)
|
||||
@@ -119,13 +118,13 @@ def test_create_nightly_billing_for_day_checks_history(
|
||||
|
||||
create_notification(
|
||||
created_at=yesterday,
|
||||
template=sample_sms_template,
|
||||
template=sample_template,
|
||||
status='sending',
|
||||
)
|
||||
|
||||
create_notification_history(
|
||||
created_at=yesterday,
|
||||
template=sample_sms_template,
|
||||
template=sample_template,
|
||||
status='delivered',
|
||||
)
|
||||
|
||||
@@ -141,7 +140,6 @@ def test_create_nightly_billing_for_day_checks_history(
|
||||
assert record.notifications_sent == 2
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Needs updating for TTS: Timezone handling")
|
||||
@pytest.mark.parametrize('second_rate, records_num, billable_units, multiplier',
|
||||
[(1.0, 1, 2, [1]),
|
||||
(2.0, 2, 1, [1, 2])])
|
||||
@@ -193,7 +191,6 @@ def test_create_nightly_billing_for_day_sms_rate_multiplier(
|
||||
assert record.rate_multiplier == multiplier[i]
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Needs updating for TTS: Timezone handling")
|
||||
def test_create_nightly_billing_for_day_different_templates(
|
||||
sample_service,
|
||||
sample_template,
|
||||
@@ -240,8 +237,7 @@ def test_create_nightly_billing_for_day_different_templates(
|
||||
assert record.rate_multiplier == multiplier[i]
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Needs updating for TTS: Timezone handling")
|
||||
def test_create_nightly_billing_for_day_different_sent_by(
|
||||
def test_create_nightly_billing_for_day_same_sent_by(
|
||||
sample_service,
|
||||
sample_template,
|
||||
sample_email_template,
|
||||
@@ -276,16 +272,15 @@ def test_create_nightly_billing_for_day_different_sent_by(
|
||||
create_nightly_billing_for_day(str(yesterday.date()))
|
||||
|
||||
records = FactBilling.query.order_by('rate_multiplier').all()
|
||||
assert len(records) == 2
|
||||
assert len(records) == 1
|
||||
|
||||
for _, record in enumerate(records):
|
||||
assert record.local_date == datetime.date(yesterday)
|
||||
assert record.rate == Decimal(1.33)
|
||||
assert record.billable_units == 1
|
||||
assert record.billable_units == 2
|
||||
assert record.rate_multiplier == 1.0
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Needs updating for TTS: Timezone handling")
|
||||
def test_create_nightly_billing_for_day_null_sent_by_sms(
|
||||
sample_service,
|
||||
sample_template,
|
||||
@@ -497,7 +492,6 @@ def test_create_nightly_notification_status_for_service_and_day(notify_db_sessio
|
||||
assert sms_delivered_row.key_type == KEY_TYPE_NORMAL
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Needs updating for TTS: Timezone handling")
|
||||
def test_create_nightly_notification_status_for_service_and_day_overwrites_old_data(notify_db_session):
|
||||
first_service = create_service(service_name='First Service')
|
||||
first_template = create_template(service=first_service)
|
||||
|
||||
@@ -12,6 +12,7 @@ from app.celery.research_mode_tasks import (
|
||||
sns_callback,
|
||||
)
|
||||
from app.config import QueueNames
|
||||
from app.models import NOTIFICATION_DELIVERED, NOTIFICATION_FAILED, Notification
|
||||
from tests.conftest import Matcher
|
||||
|
||||
dvla_response_file_matcher = Matcher(
|
||||
@@ -20,24 +21,33 @@ dvla_response_file_matcher = Matcher(
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Re-enable when SMS receipts exist")
|
||||
def test_make_sns_callback(notify_api, rmock):
|
||||
def test_make_sns_callback(notify_api, rmock, mocker):
|
||||
endpoint = "http://localhost:6011/notifications/sms/sns"
|
||||
get_notification_by_id = mocker.patch('app.celery.research_mode_tasks.get_notification_by_id')
|
||||
n = Notification()
|
||||
n.id = 1234
|
||||
n.status = NOTIFICATION_DELIVERED
|
||||
get_notification_by_id.return_value = n
|
||||
rmock.request(
|
||||
"POST",
|
||||
endpoint,
|
||||
json={"status": "success"},
|
||||
status_code=200)
|
||||
send_sms_response("sns", "1234", "2028675309")
|
||||
send_sms_response("sns", "1234")
|
||||
|
||||
assert rmock.called
|
||||
assert rmock.request_history[0].url == endpoint
|
||||
assert json.loads(rmock.request_history[0].text)['MSISDN'] == '2028675309'
|
||||
assert json.loads(rmock.request_history[0].text)['status'] == 'delivered'
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Re-enable when SMS receipts exist")
|
||||
def test_callback_logs_on_api_call_failure(notify_api, rmock, mocker):
|
||||
endpoint = "http://localhost:6011/notifications/sms/sns"
|
||||
get_notification_by_id = mocker.patch('app.celery.research_mode_tasks.get_notification_by_id')
|
||||
n = Notification()
|
||||
n.id = 1234
|
||||
n.status = NOTIFICATION_FAILED
|
||||
get_notification_by_id.return_value = n
|
||||
|
||||
rmock.request(
|
||||
"POST",
|
||||
endpoint,
|
||||
@@ -46,12 +56,12 @@ def test_callback_logs_on_api_call_failure(notify_api, rmock, mocker):
|
||||
mock_logger = mocker.patch('app.celery.tasks.current_app.logger.error')
|
||||
|
||||
with pytest.raises(HTTPError):
|
||||
send_sms_response("mmg", "1234", "07700900001")
|
||||
send_sms_response("sns", "1234")
|
||||
|
||||
assert rmock.called
|
||||
assert rmock.request_history[0].url == endpoint
|
||||
mock_logger.assert_called_once_with(
|
||||
'API POST request on http://localhost:6011/notifications/sms/mmg failed with status 500'
|
||||
'API POST request on http://localhost:6011/notifications/sms/sns failed with status 500'
|
||||
)
|
||||
|
||||
|
||||
@@ -65,31 +75,13 @@ def test_make_ses_callback(notify_api, mocker):
|
||||
assert mock_task.apply_async.call_args[0][0][0] == ses_notification_callback(some_ref)
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Re-enable when SNS delivery receipts exist")
|
||||
def test_delievered_sns_callback():
|
||||
phone_number = "2028675309"
|
||||
data = json.loads(sns_callback("1234", phone_number))
|
||||
assert data['MSISDN'] == phone_number
|
||||
assert data['status'] == "3"
|
||||
assert data['reference'] == "sns_reference"
|
||||
assert data['CID'] == "1234"
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Re-enable when SNS delivery receipts exist")
|
||||
def test_perm_failure_sns_callback():
|
||||
phone_number = "2028675302"
|
||||
data = json.loads(sns_callback("1234", phone_number))
|
||||
assert data['MSISDN'] == phone_number
|
||||
assert data['status'] == "5"
|
||||
assert data['reference'] == "sns_reference"
|
||||
assert data['CID'] == "1234"
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Re-enable when SNS delivery receipts exist")
|
||||
def test_temp_failure_sns_callback():
|
||||
phone_number = "2028675303"
|
||||
data = json.loads(sns_callback("1234", phone_number))
|
||||
assert data['MSISDN'] == phone_number
|
||||
assert data['status'] == "4"
|
||||
assert data['reference'] == "sns_reference"
|
||||
def test_delivered_sns_callback(mocker):
|
||||
get_notification_by_id = mocker.patch('app.celery.research_mode_tasks.get_notification_by_id')
|
||||
n = Notification()
|
||||
n.id = 1234
|
||||
n.status = NOTIFICATION_DELIVERED
|
||||
get_notification_by_id.return_value = n
|
||||
|
||||
data = json.loads(sns_callback("1234"))
|
||||
assert data['status'] == "delivered"
|
||||
assert data['CID'] == "1234"
|
||||
|
||||
Reference in New Issue
Block a user