def test_should_call_send_sms_response_task_if_research_mode( notify_db, sample_service, sample_notification, mocker, research_mode, key_type): mocker.patch('app.mmg_client.send_sms') mocker.patch('app.delivery.send_to_providers.send_sms_response') 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 not mmg_client.send_sms.called app.delivery.send_to_providers.send_sms_response.assert_called_once_with( 'mmg', str(sample_notification.id), sample_notification.to) persisted_notification = notifications_dao.get_notification_by_id( sample_notification.id) assert persisted_notification.to == sample_notification.to assert persisted_notification.template_id == sample_notification.template_id assert persisted_notification.status == 'sending' assert persisted_notification.sent_at <= datetime.utcnow() assert persisted_notification.sent_by == 'mmg' assert not persisted_notification.personalisation
def test_should_send_personalised_template_to_correct_sms_provider_and_persist( notify_db, notify_db_session, sample_template_with_placeholders, mocker ): db_notification = sample_notification(notify_db, notify_db_session, template=sample_template_with_placeholders, to_field="+447234123123", personalisation={"name": "Jo"}, status='created') mocker.patch('app.mmg_client.send_sms') mocker.patch('app.mmg_client.get_name', return_value="mmg") send_to_providers.send_sms_to_provider( db_notification ) mmg_client.send_sms.assert_called_once_with( to=format_phone_number(validate_phone_number("+447234123123")), content="Sample service: Hello Jo\nYour thing is due soon", reference=str(db_notification.id), sender=None ) notification = Notification.query.filter_by(id=db_notification.id).one() assert notification.status == 'sending' assert notification.sent_at <= datetime.utcnow() assert notification.sent_by == 'mmg' assert notification.billable_units == 1 assert notification.personalisation == {"name": "Jo"}
def deliver_sms(self, notification_id): try: current_app.logger.info( "Start sending SMS for notification id: {}".format( notification_id)) notification = notifications_dao.get_notification_by_id( notification_id) if not notification: raise NoResultFound() send_to_providers.send_sms_to_provider(notification) except Exception as e: if isinstance(e, SmsClientResponseException): current_app.logger.warning( "SMS notification delivery for id: {} failed".format( notification_id)) else: current_app.logger.exception( "SMS notification delivery for id: {} failed".format( notification_id)) try: if self.request.retries == 0: self.retry(queue=QueueNames.RETRY, countdown=0) else: self.retry(queue=QueueNames.RETRY) except self.MaxRetriesExceededError: message = "RETRY FAILED: Max retries reached. The task send_sms_to_provider failed for notification {}. " \ "Notification has been updated to technical-failure".format(notification_id) update_notification_status_by_id(notification_id, NOTIFICATION_TECHNICAL_FAILURE) raise NotificationTechnicalFailureException(message)
def test_should_send_sms_sender_from_service_if_present( notify_db, notify_db_session, sample_service, sample_template, mocker): db_notification = sample_notification(notify_db, notify_db_session, template=sample_template, to_field="+447234123123", status='created') sample_service.sms_sender = 'elevenchars' notify_db.session.add(sample_service) notify_db.session.commit() mocker.patch('app.mmg_client.send_sms') mocker.patch('app.mmg_client.get_name', return_value="mmg") send_to_providers.send_sms_to_provider( db_notification ) mmg_client.send_sms.assert_called_once_with( to=format_phone_number(validate_phone_number("+447234123123")), content="This is a template:\nwith a newline", reference=str(db_notification.id), sender=sample_service.sms_sender )
def test_should_call_send_sms_response_task_if_research_mode(notify_db, sample_service, sample_notification, mocker, research_mode, key_type): 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 not mmg_client.send_sms.called send_to_providers.send_sms_response.apply_async.assert_called_once_with( ('mmg', str(sample_notification.id), sample_notification.to), queue='research-mode' ) persisted_notification = notifications_dao.get_notification_by_id(sample_notification.id) assert persisted_notification.to == sample_notification.to assert persisted_notification.template_id == sample_notification.template_id assert persisted_notification.status == 'sending' assert persisted_notification.sent_at <= datetime.utcnow() assert persisted_notification.sent_by == 'mmg' assert not persisted_notification.personalisation
def test_should_handle_sms_sender_and_prefix_message( notify_db_session, mocker, sms_sender, prefix_sms, expected_sender, expected_content, ): mocker.patch('app.telstra_sms_client.send_sms', return_value=[ 'MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', NOTIFICATION_SENDING ]) service = create_service_with_defined_sms_sender( sms_sender_value=sms_sender, prefix_sms=prefix_sms) template = create_template(service, content='bar') notification = create_notification(template, reply_to_text=sms_sender) send_to_providers.send_sms_to_provider(notification) telstra_sms_client.send_sms.assert_called_once_with( content=expected_content, sender=expected_sender, to=ANY, reference=ANY, )
def test_send_sms_to_provider_should_format_phone_number(sample_notification, mocker): sample_notification.to = "+1 650 253 2222" send_mock = mocker.patch("app.aws_sns_client.send_sms", return_value="message_id_from_sns") send_to_providers.send_sms_to_provider(sample_notification) assert send_mock.call_args[1]["to"] == "+16502532222"
def test_send_sms_to_provider_should_return_template_if_found_in_redis( mocker, client, sample_template): from app.schemas import service_schema, template_schema service_dict = service_schema.dump(sample_template.service).data template_dict = template_schema.dump(sample_template).data mocker.patch( 'app.redis_store.get', side_effect=[ json.dumps({ 'data': service_dict }).encode('utf-8'), json.dumps({ 'data': template_dict }).encode('utf-8'), ], ) mock_get_template = mocker.patch( 'app.dao.templates_dao.dao_get_template_by_id_and_service_id') mock_get_service = mocker.patch( 'app.dao.services_dao.dao_fetch_service_by_id') send_mock = mocker.patch('app.mmg_client.send_sms') notification = create_notification(template=sample_template, to_field='+447700900855', normalised_to='447700900855') send_to_providers.send_sms_to_provider(notification) assert mock_get_template.called is False assert mock_get_service.called is False send_mock.assert_called_once_with(to=notification.normalised_to, content=ANY, reference=str(notification.id), sender=notification.reply_to_text)
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)
def test_send_sms_to_provider_should_format_phone_number(sample_notification, mocker): sample_notification.to = '+44 (7123) 123-123' send_mock = mocker.patch('app.mmg_client.send_sms') send_to_providers.send_sms_to_provider(sample_notification) assert send_mock.call_args[1]['to'] == '447123123123'
def test_should_call_send_sms_response_task_if_research_mode( notify_db, sample_service, sample_notification, mocker, research_mode, key_type ): reference = str(uuid.uuid4()) mocker.patch("app.aws_sns_client.send_sms") mocker.patch("app.delivery.send_to_providers.send_sms_response", return_value=reference) 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 not aws_sns_client.send_sms.called app.delivery.send_to_providers.send_sms_response.assert_called_once_with("sns", sample_notification.to) persisted_notification = notifications_dao.get_notification_by_id(sample_notification.id) assert persisted_notification.to == sample_notification.to assert persisted_notification.template_id == sample_notification.template_id assert persisted_notification.status == "sent" assert persisted_notification.sent_at <= datetime.utcnow() assert persisted_notification.sent_by == "sns" assert persisted_notification.reference == reference assert not persisted_notification.personalisation
def test_should_send_personalised_template_to_correct_sms_provider_and_persist( sample_sms_template_with_html, mocker): db_notification = create_notification( template=sample_sms_template_with_html, to_field="+447234123123", personalisation={"name": "Jo"}, status='created', reply_to_text=sample_sms_template_with_html.service. get_default_sms_sender()) mocker.patch('app.mmg_client.send_sms') send_to_providers.send_sms_to_provider(db_notification) mmg_client.send_sms.assert_called_once_with( to=validate_and_format_phone_number("+447234123123"), content= "Sample service: Hello Jo\nHere is <em>some HTML</em> & entities", reference=str(db_notification.id), sender=current_app.config['FROM_NUMBER']) notification = Notification.query.filter_by(id=db_notification.id).one() assert notification.status == 'sending' assert notification.sent_at <= datetime.utcnow() assert notification.sent_by == 'mmg' assert notification.billable_units == 1 assert notification.personalisation == {"name": "Jo"}
def test_should_send_sms_to_international_providers( restore_provider_details, sample_sms_template_with_html, sample_user, mocker ): mocker.patch('app.provider_details.switch_providers.get_user_by_id', return_value=sample_user) dao_switch_sms_provider_to_provider_with_identifier('firetext') db_notification_uk = create_notification( template=sample_sms_template_with_html, to_field="+16135555555", personalisation={"name": "Jo"}, status='created', international=False, reply_to_text=sample_sms_template_with_html.service.get_default_sms_sender() ) db_notification_international = create_notification( template=sample_sms_template_with_html, to_field="+1613555555", personalisation={"name": "Jo"}, status='created', international=False, reply_to_text=sample_sms_template_with_html.service.get_default_sms_sender() ) mocker.patch('app.aws_sns_client.send_sms') mocker.patch('app.mmg_client.send_sms') send_to_providers.send_sms_to_provider( db_notification_uk ) mmg_client.send_sms.assert_called_once_with( to="16135555555", content=ANY, reference=str(db_notification_uk.id), sender=current_app.config['FROM_NUMBER'] ) send_to_providers.send_sms_to_provider( db_notification_international ) aws_sns_client.send_sms.assert_called_once_with( to="601117224412", content=ANY, reference=str(db_notification_international.id), sender=current_app.config['FROM_NUMBER'] ) notification_uk = Notification.query.filter_by(id=db_notification_uk.id).one() notification_int = Notification.query.filter_by(id=db_notification_international.id).one() assert notification_uk.status == 'sending' assert notification_uk.sent_by == 'firetext' assert notification_int.status == 'sent' assert notification_int.sent_by == 'mmg'
def test_send_sms_should_use_template_version_from_notification_not_latest( sample_template, mocker): db_notification = create_notification( template=sample_template, to_field='+447234123123', status='created', reply_to_text=sample_template.service.get_default_sms_sender()) mocker.patch('app.mmg_client.send_sms') version_on_notification = sample_template.version # Change the template from app.dao.templates_dao import dao_update_template, dao_get_template_by_id sample_template.content = sample_template.content + " another version of the template" dao_update_template(sample_template) t = dao_get_template_by_id(sample_template.id) assert t.version > version_on_notification send_to_providers.send_sms_to_provider(db_notification) mmg_client.send_sms.assert_called_once_with( to=validate_and_format_phone_number("+447234123123"), content="Sample service: This is a template:\nwith a newline", reference=str(db_notification.id), sender=current_app.config['FROM_NUMBER']) persisted_notification = notifications_dao.get_notification_by_id( db_notification.id) assert persisted_notification.to == db_notification.to assert persisted_notification.template_id == sample_template.id assert persisted_notification.template_version == version_on_notification assert persisted_notification.template_version != sample_template.version assert persisted_notification.status == 'sending' assert not persisted_notification.personalisation
def test_should_send_personalised_template_to_correct_sms_provider_and_persist(sample_sms_template_with_html, mocker): db_notification = create_notification( template=sample_sms_template_with_html, to_field="+16502532222", personalisation={"name": "Jo"}, status="created", reply_to_text=sample_sms_template_with_html.service.get_default_sms_sender(), ) statsd_mock = mocker.patch("app.delivery.send_to_providers.statsd_client") mocker.patch("app.aws_sns_client.send_sms", return_value="message_id_from_sns") send_to_providers.send_sms_to_provider(db_notification) aws_sns_client.send_sms.assert_called_once_with( to=validate_and_format_phone_number("+16502532222"), content="Sample service: Hello Jo\nHere is <em>some HTML</em> & entities", reference=str(db_notification.id), sender=current_app.config["FROM_NUMBER"], ) notification = Notification.query.filter_by(id=db_notification.id).one() assert notification.status == "sent" assert notification.sent_at <= datetime.utcnow() assert notification.sent_by == "sns" assert notification.billable_units == 1 assert notification.personalisation == {"name": "Jo"} assert notification.reference == "message_id_from_sns" statsd_timing_calls = statsd_mock.timing_with_dates.call_args_list assert call("sms.total-time", notification.sent_at, notification.created_at) in statsd_timing_calls assert call("sms.process_type-normal", notification.sent_at, notification.created_at) in statsd_timing_calls assert call("sms.process_type-normal") in statsd_mock.incr.call_args_list
def test_send_sms_to_provider_should_format_phone_number( sample_notification, mocker): sample_notification.to = '+1 650 253 2222' send_mock = mocker.patch('app.aws_sns_client.send_sms') send_to_providers.send_sms_to_provider(sample_notification) assert send_mock.call_args[1]['to'] == '+16502532222'
def test_send_sms_to_provider_should_format_phone_number(sample_notification, mock_sms_client): sample_notification.to = '+1 650 253 2222' send_to_providers.send_sms_to_provider(sample_notification) # TODO: don't test the actual return value of notification_utils.recipients.validate_and_format_phone_number # instead, mock that dependency and check that it's used properly assert mock_sms_client.send_sms.call_args[1]['to'] == '+16502532222'
def test_send_email_to_provider_should_not_send_to_provider_when_status_is_not_created(sample_email_template, mocker): notification = create_notification(template=sample_email_template, status="sending") mocker.patch("app.aws_ses_client.send_email") mocker.patch("app.delivery.send_to_providers.send_email_response") send_to_providers.send_sms_to_provider(notification) app.aws_ses_client.send_email.assert_not_called() app.delivery.send_to_providers.send_email_response.assert_not_called()
def test_should_send_sms_to_international_providers( notify_db, restore_provider_details, sample_sms_template_with_html, sample_user, mocker): mocker.patch('app.provider_details.switch_providers.get_user_by_id', return_value=sample_user) dao_switch_sms_provider_to_provider_with_identifier('sap') db_notification_au = create_notification( template=sample_sms_template_with_html, to_field="+61412888999", personalisation={"name": "Jo"}, status='created', international=False, reply_to_text=sample_sms_template_with_html.service. get_default_sms_sender()) db_notification_international = create_notification( template=sample_sms_template_with_html, to_field="+61412111222", personalisation={"name": "Jo"}, status='created', international=True, reply_to_text=sample_sms_template_with_html.service. get_default_sms_sender()) mocker.patch('app.sap_sms_client.send_sms', return_value=[ 'MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', NOTIFICATION_SENDING ]) send_to_providers.send_sms_to_provider(db_notification_au) sap_sms_client.send_sms.assert_called_with( to="+61412888999", content=ANY, reference=str(db_notification_au.id), sender=current_app.config['FROM_NUMBER']) send_to_providers.send_sms_to_provider(db_notification_international) sap_sms_client.send_sms.assert_called_with( to="+61412111222", content=ANY, reference=str(db_notification_international.id), sender=current_app.config['FROM_NUMBER']) notification_au = Notification.query.filter_by( id=db_notification_au.id).one() notification_int = Notification.query.filter_by( id=db_notification_international.id).one() assert notification_au.status == 'sending' assert notification_au.sent_by == 'sap' assert notification_au.reference == 'MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' assert notification_int.status == 'sent' assert notification_int.sent_by == 'sap' assert notification_int.reference == 'MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
def test_should_not_have_sent_status_if_fake_callback_function_fails(sample_notification, mocker): mocker.patch("app.delivery.send_to_providers.send_sms_response", side_effect=HTTPError) sample_notification.key_type = KEY_TYPE_TEST with pytest.raises(HTTPError): send_to_providers.send_sms_to_provider(sample_notification) assert sample_notification.status == "created" assert sample_notification.sent_by is None
def test_send_sms_to_provider_should_format_phone_number( sample_notification, mocker): sample_notification.to = '+44 (7123) 123-123' res = Mock(content=json.dumps({'sid': 1})) send_mock = mocker.patch('app.twilio_client.send_sms', return_value=res) send_to_providers.send_sms_to_provider(sample_notification) assert send_mock.call_args[1]['to'] == '447123123123'
def test_should_not_send_sms_message_when_service_is_inactive_notifcation_is_in_tech_failure( sample_service, sample_notification, mocker, client_send): sample_service.active = False send_mock = mocker.patch(client_send, return_value='reference') with pytest.raises(NotificationTechnicalFailureException) as e: send_to_providers.send_sms_to_provider(sample_notification) assert str(sample_notification.id) in str(e.value) send_mock.assert_not_called() assert Notification.query.get(sample_notification.id).status == 'technical-failure'
def test_should_send_sms_to_international_providers( sample_template, sample_user, mocker ): mocker.patch('app.mmg_client.send_sms') mocker.patch('app.firetext_client.send_sms') # set firetext to active get_provider_details_by_identifier('firetext').priority = 100 get_provider_details_by_identifier('mmg').priority = 0 notification_uk = create_notification( template=sample_template, to_field="+447234123999", personalisation={"name": "Jo"}, status='created', international=False, reply_to_text=sample_template.service.get_default_sms_sender() ) notification_international = create_notification( template=sample_template, to_field="+6011-17224412", personalisation={"name": "Jo"}, status='created', international=True, reply_to_text=sample_template.service.get_default_sms_sender() ) send_to_providers.send_sms_to_provider( notification_uk ) firetext_client.send_sms.assert_called_once_with( to="447234123999", content=ANY, reference=str(notification_uk.id), sender=current_app.config['FROM_NUMBER'] ) send_to_providers.send_sms_to_provider( notification_international ) mmg_client.send_sms.assert_called_once_with( to="601117224412", content=ANY, reference=str(notification_international.id), sender=current_app.config['FROM_NUMBER'] ) assert notification_uk.status == 'sending' assert notification_uk.sent_by == 'firetext' assert notification_international.status == 'sent' assert notification_international.sent_by == 'mmg'
def test_should_not_update_notification_if_research_mode_on_exception(sample_service, sample_notification, mocker): mock_send_sms = mocker.patch("app.delivery.send_to_providers.send_sms_response", side_effect=Exception()) sample_service.research_mode = True sample_notification.billable_units = 0 with pytest.raises(Exception): send_to_providers.send_sms_to_provider(sample_notification) persisted_notification = notifications_dao.get_notification_by_id(sample_notification.id) assert persisted_notification.billable_units == 0 assert mock_send_sms.called
def test_should_have_sent_status_if_fake_callback_function_fails(sample_notification, mocker, mock_sms_client): mocker.patch('app.delivery.send_to_providers.send_sms_response', side_effect=HTTPError) sample_notification.key_type = KEY_TYPE_TEST with pytest.raises(HTTPError): send_to_providers.send_sms_to_provider( sample_notification ) assert sample_notification.status == 'sent' assert sample_notification.sent_by == mock_sms_client.get_name()
def test_send_sms_should_use_service_sms_sender(sample_service, sample_template, mocker): mocker.patch("app.aws_sns_client.send_sms", return_value="message_id_from_sns") sms_sender = create_service_sms_sender(service=sample_service, sms_sender="123456", is_default=False) db_notification = create_notification(template=sample_template, reply_to_text=sms_sender.sms_sender) send_to_providers.send_sms_to_provider( db_notification, ) app.aws_sns_client.send_sms.assert_called_once_with(to=ANY, content=ANY, reference=ANY, sender=sms_sender.sms_sender)
def test_send_sms_to_provider_should_use_normalised_to(mocker, client, sample_template): send_mock = mocker.patch('app.mmg_client.send_sms') notification = create_notification(template=sample_template, to_field='+447700900855', normalised_to='447700900855') send_to_providers.send_sms_to_provider(notification) send_mock.assert_called_once_with(to=notification.normalised_to, content=ANY, reference=str(notification.id), sender=notification.reply_to_text)
def test_send_sms_to_provider_should_format_phone_number( notify_db, sample_notification, mocker, ): sample_notification.to = '+61 (412) 345-678' send_mock = mocker.patch('app.twilio_sms_client.send_sms') send_to_providers.send_sms_to_provider(sample_notification) assert send_mock.call_args[1]['to'] == '61412345678'
def test_should_not_send_to_provider_when_status_is_not_created( sample_template, mocker): notification = create_notification(template=sample_template, status='sending') mocker.patch('app.mmg_client.send_sms') response_mock = mocker.patch( 'app.delivery.send_to_providers.send_sms_response') send_to_providers.send_sms_to_provider(notification) app.mmg_client.send_sms.assert_not_called() response_mock.assert_not_called()
def test_should_send_international_sms_with_formatted_phone_number( notify_db, sample_template, mocker): notification = create_notification(template=sample_template, to_field="+6011-17224412", international=True) send_notification_mock = mocker.patch('app.twilio_sms_client.send_sms') mocker.patch('app.delivery.send_to_providers.send_sms_response') send_to_providers.send_sms_to_provider(notification) assert send_notification_mock.called is True
def test_should_set_international_phone_number_to_sent_status( notify_db, sample_template, mocker): notification = create_notification(template=sample_template, to_field="+61-412-345-678", international=True) mocker.patch('app.twilio_sms_client.send_sms') mocker.patch('app.delivery.send_to_providers.send_sms_response') send_to_providers.send_sms_to_provider(notification) assert notification.status == 'sent'
def test_should_not_set_billable_units_if_research_mode( notify_db, sample_service, sample_notification, mocker): mocker.patch('app.twilio_sms_client.send_sms') mocker.patch('app.delivery.send_to_providers.send_sms_response') sample_service.research_mode = True notify_db.session.add(sample_service) notify_db.session.commit() send_to_providers.send_sms_to_provider(sample_notification) persisted_notification = notifications_dao.get_notification_by_id( sample_notification.id) assert persisted_notification.billable_units == 0
def test_send_sms_should_use_service_sms_sender(sample_service, sample_template, mock_sms_client): sms_sender = create_service_sms_sender(service=sample_service, sms_sender='123456', is_default=False) db_notification = create_notification(template=sample_template, reply_to_text=sms_sender.sms_sender) send_to_providers.send_sms_to_provider(db_notification, ) mock_sms_client.send_sms.assert_called_once_with( to=ANY, content=ANY, reference=ANY, sender=sms_sender.sms_sender)
def test_should_set_notification_billable_units_if_sending_to_provider_fails( sample_notification, mocker, ): mocker.patch("app.aws_sns_client.send_sms", side_effect=Exception()) mock_toggle_provider = mocker.patch("app.delivery.send_to_providers.dao_toggle_sms_provider") sample_notification.billable_units = 0 assert sample_notification.sent_by is None with pytest.raises(Exception): send_to_providers.send_sms_to_provider(sample_notification) assert sample_notification.billable_units == 1 assert mock_toggle_provider.called
def test_should_not_set_billable_units_if_research_mode(notify_db, sample_service, sample_notification, mocker): 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') sample_service.research_mode = True notify_db.session.add(sample_service) notify_db.session.commit() send_to_providers.send_sms_to_provider( sample_notification ) persisted_notification = notifications_dao.get_notification_by_id(sample_notification.id) assert persisted_notification.billable_units == 0
def test_should_not_send_to_provider_when_status_is_not_created(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='sending') 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') send_to_providers.send_sms_to_provider( notification ) app.mmg_client.send_sms.assert_not_called() app.celery.research_mode_tasks.send_sms_response.apply_async.assert_not_called()
def deliver_sms(self, notification_id): try: notification = notifications_dao.get_notification_by_id(notification_id) if not notification: raise NoResultFound() send_to_providers.send_sms_to_provider(notification) except Exception as e: try: current_app.logger.exception( "RETRY: SMS notification {} failed".format(notification_id) ) self.retry(queue="retry", countdown=retry_iteration_to_delay(self.request.retries)) except self.MaxRetriesExceededError: current_app.logger.exception( "RETRY FAILED: task send_sms_to_provider failed for notification {}".format(notification_id), ) update_notification_status_by_id(notification_id, 'technical-failure')
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): 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 notifications_dao.get_notification_by_id(sample_notification.id).billable_units == 0
def test_send_sms_should_use_template_version_from_notification_not_latest( notify_db, notify_db_session, sample_template, mocker): db_notification = sample_notification(notify_db, notify_db_session, template=sample_template, to_field='+447234123123', status='created') mocker.patch('app.mmg_client.send_sms') mocker.patch('app.mmg_client.get_name', return_value="mmg") version_on_notification = sample_template.version # Change the template from app.dao.templates_dao import dao_update_template, dao_get_template_by_id sample_template.content = sample_template.content + " another version of the template" dao_update_template(sample_template) t = dao_get_template_by_id(sample_template.id) assert t.version > version_on_notification send_to_providers.send_sms_to_provider( db_notification ) mmg_client.send_sms.assert_called_once_with( to=format_phone_number(validate_phone_number("+447234123123")), content="Sample service: This is a template:\nwith a newline", reference=str(db_notification.id), sender=None ) persisted_notification = notifications_dao.get_notification_by_id(db_notification.id) assert persisted_notification.to == db_notification.to assert persisted_notification.template_id == sample_template.id assert persisted_notification.template_version == version_on_notification assert persisted_notification.template_version != sample_template.version assert persisted_notification.status == 'sending' assert not persisted_notification.personalisation