mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-28 11:49:42 -04:00
Merge branch 'master' into refactor-send-tasks-into-shared-code
Conflicts: app/celery/provider_tasks.py tests/app/celery/test_provider_tasks.py
This commit is contained in:
@@ -142,7 +142,9 @@ def test_should_add_to_retry_queue_if_notification_not_found_in_send_email_to_pr
|
||||
app.celery.provider_tasks.send_email_to_provider.retry.assert_called_with(queue="retry", countdown=10)
|
||||
|
||||
|
||||
def test_should_go_into_technical_error_if_exceeds_retries(
|
||||
# DO THESE FOR THE 4 TYPES OF TASK
|
||||
|
||||
def test_should_go_into_technical_error_if_exceeds_retries_on_send_sms_to_provider_task(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service,
|
||||
@@ -164,7 +166,28 @@ def test_should_go_into_technical_error_if_exceeds_retries(
|
||||
assert db_notification.status == 'technical-failure'
|
||||
|
||||
|
||||
def test_send_email_to_provider_should_go_into_technical_error_if_exceeds_retries(
|
||||
def test_should_go_into_technical_error_if_exceeds_retries_on_deliver_sms_task(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service,
|
||||
mocker):
|
||||
notification = sample_notification(notify_db=notify_db, notify_db_session=notify_db_session,
|
||||
service=sample_service, status='created')
|
||||
|
||||
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())
|
||||
|
||||
deliver_sms(
|
||||
notification.id
|
||||
)
|
||||
|
||||
provider_tasks.deliver_sms.retry.assert_called_with(queue='retry', countdown=10)
|
||||
|
||||
db_notification = Notification.query.filter_by(id=notification.id).one()
|
||||
assert db_notification.status == 'technical-failure'
|
||||
|
||||
|
||||
def test_send_email_to_provider_should_go_into_technical_error_if_exceeds_retries_on_send_email_to_provider_task(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service,
|
||||
@@ -185,3 +208,24 @@ def test_send_email_to_provider_should_go_into_technical_error_if_exceeds_retrie
|
||||
|
||||
db_notification = Notification.query.filter_by(id=notification.id).one()
|
||||
assert db_notification.status == 'technical-failure'
|
||||
|
||||
|
||||
def test_should_go_into_technical_error_if_exceeds_retries_on_deliver_email_task(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service,
|
||||
mocker):
|
||||
notification = sample_notification(notify_db=notify_db, notify_db_session=notify_db_session,
|
||||
service=sample_service, status='created')
|
||||
|
||||
mocker.patch('app.delivery.send_to_providers.send_email_to_provider', side_effect=Exception("EXPECTED"))
|
||||
mocker.patch('app.celery.provider_tasks.deliver_email.retry', side_effect=MaxRetriesExceededError())
|
||||
|
||||
deliver_email(
|
||||
notification.id
|
||||
)
|
||||
|
||||
provider_tasks.deliver_email.retry.assert_called_with(queue='retry', countdown=10)
|
||||
|
||||
db_notification = Notification.query.filter_by(id=notification.id).one()
|
||||
assert db_notification.status == 'technical-failure'
|
||||
|
||||
@@ -7,10 +7,10 @@ from mock import ANY
|
||||
import app
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
from app import mmg_client
|
||||
from app.dao import (provider_details_dao, notifications_dao, provider_statistics_dao)
|
||||
from app.dao.provider_statistics_dao import get_provider_statistics
|
||||
from app.dao import (provider_details_dao, notifications_dao)
|
||||
from app.delivery import send_to_providers
|
||||
from app.models import Notification, KEY_TYPE_NORMAL, KEY_TYPE_TEST, BRANDING_ORG, BRANDING_BOTH, Organisation
|
||||
from app.models import Notification, KEY_TYPE_NORMAL, KEY_TYPE_TEST, BRANDING_ORG, BRANDING_BOTH, Organisation, \
|
||||
KEY_TYPE_TEAM
|
||||
from tests.app.conftest import sample_notification
|
||||
|
||||
from notifications_utils.recipients import validate_phone_number, format_phone_number
|
||||
@@ -197,8 +197,6 @@ def test_should_call_send_sms_response_task_if_research_mode(notify_db, sample_s
|
||||
])
|
||||
def test_should_set_billable_units_to_zero_in_research_mode_or_test_key(
|
||||
notify_db, sample_service, sample_notification, mocker, research_mode, key_type):
|
||||
provider_stats = provider_statistics_dao.get_provider_statistics(sample_service).all()
|
||||
assert len(provider_stats) == 0
|
||||
|
||||
mocker.patch('app.mmg_client.send_sms')
|
||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
||||
@@ -292,9 +290,6 @@ def test_send_email_to_provider_should_call_research_mode_task_response_task_if_
|
||||
sample_service.research_mode = True
|
||||
notify_db.session.add(sample_service)
|
||||
notify_db.session.commit()
|
||||
assert not get_provider_statistics(
|
||||
sample_email_template.service,
|
||||
providers=[ses_provider.identifier]).first()
|
||||
|
||||
send_to_providers.send_email_to_provider(
|
||||
notification
|
||||
@@ -303,9 +298,6 @@ def test_send_email_to_provider_should_call_research_mode_task_response_task_if_
|
||||
send_to_providers.send_email_response.apply_async.assert_called_once_with(
|
||||
('ses', str(reference), 'john@smith.com'), queue="research-mode"
|
||||
)
|
||||
assert not get_provider_statistics(
|
||||
sample_email_template.service,
|
||||
providers=[ses_provider.identifier]).first()
|
||||
persisted_notification = Notification.query.filter_by(id=notification.id).one()
|
||||
|
||||
assert persisted_notification.to == 'john@smith.com'
|
||||
@@ -315,6 +307,7 @@ def test_send_email_to_provider_should_call_research_mode_task_response_task_if_
|
||||
assert persisted_notification.created_at <= datetime.utcnow()
|
||||
assert persisted_notification.sent_by == 'ses'
|
||||
assert persisted_notification.reference == str(reference)
|
||||
assert persisted_notification.billable_units == 0
|
||||
|
||||
|
||||
def test_send_email_to_provider_should_not_send_to_provider_when_status_is_not_created(notify_db, notify_db_session,
|
||||
@@ -415,3 +408,39 @@ def test_should_not_set_billable_units_if_research_mode(notify_db, sample_servic
|
||||
|
||||
persisted_notification = notifications_dao.get_notification_by_id(sample_notification.id)
|
||||
assert persisted_notification.billable_units == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize('research_mode,key_type, billable_units', [
|
||||
(True, KEY_TYPE_NORMAL, 0),
|
||||
(False, KEY_TYPE_NORMAL, 1),
|
||||
(False, KEY_TYPE_TEST, 0),
|
||||
(True, KEY_TYPE_TEST, 0),
|
||||
(True, KEY_TYPE_TEAM, 0),
|
||||
(False, KEY_TYPE_TEAM, 1)
|
||||
])
|
||||
def test_should_update_billable_units_according_to_research_mode_and_key_type(notify_db,
|
||||
sample_service,
|
||||
sample_notification,
|
||||
mocker,
|
||||
research_mode,
|
||||
key_type,
|
||||
billable_units):
|
||||
|
||||
assert Notification.query.count() == 1
|
||||
|
||||
mocker.patch('app.mmg_client.send_sms')
|
||||
mocker.patch('app.mmg_client.get_name', return_value="mmg")
|
||||
mocker.patch('app.celery.research_mode_tasks.send_sms_response.apply_async')
|
||||
if research_mode:
|
||||
sample_service.research_mode = True
|
||||
notify_db.session.add(sample_service)
|
||||
notify_db.session.commit()
|
||||
|
||||
sample_notification.key_type = key_type
|
||||
|
||||
send_to_providers.send_sms_to_provider(
|
||||
sample_notification
|
||||
)
|
||||
|
||||
assert Notification.query.get(sample_notification.id).billable_units == billable_units, \
|
||||
"Research mode: {0}, key type: {1}, billable_units: {2}".format(research_mode, key_type, billable_units)
|
||||
|
||||
@@ -3,7 +3,7 @@ import uuid
|
||||
from datetime import datetime
|
||||
from flask import json
|
||||
from freezegun import freeze_time
|
||||
from mock import call
|
||||
from unittest.mock import call
|
||||
|
||||
import app.celery.tasks
|
||||
from app.dao.notifications_dao import (
|
||||
|
||||
@@ -14,6 +14,17 @@
|
||||
},
|
||||
"links": {
|
||||
"type": "object",
|
||||
"properties" : {
|
||||
"prev" : {
|
||||
"type" : "string"
|
||||
},
|
||||
"next" : {
|
||||
"type" : "string"
|
||||
},
|
||||
"last": {
|
||||
"type" : "string"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
},
|
||||
"page_size": {"type": "number"},
|
||||
|
||||
@@ -1059,6 +1059,27 @@ def test_get_all_notifications_for_service_including_ones_made_by_jobs(
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_get_only_api_created_notifications_for_service(
|
||||
client,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service
|
||||
):
|
||||
with_job = sample_notification_with_job(notify_db, notify_db_session, service=sample_service)
|
||||
without_job = create_sample_notification(notify_db, notify_db_session, service=sample_service)
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
path='/service/{}/notifications?include_jobs=false'.format(sample_service.id),
|
||||
headers=[auth_header])
|
||||
|
||||
resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(resp['notifications']) == 1
|
||||
assert resp['notifications'][0]['id'] == str(without_job.id)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_set_sms_sender_for_service(notify_api, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
Reference in New Issue
Block a user