def test_process_sms_does_not_update_sent_by_if_already_set( mocker, notify_db, sample_notification): mock_update = mocker.patch( 'app.notifications.process_client_response.set_notification_sent_by') sample_notification.sent_by = 'MMG' process_sms_client_response(status='3', provider_reference=str(sample_notification.id), client_name='MMG') assert not mock_update.called
def test_process_sms_response_raises_client_exception_for_unknown_status( mocker): with pytest.raises(ClientException) as e: process_sms_client_response(status='000', provider_reference=str(uuid.uuid4()), client_name='Firetext') assert "{} callback failed: status {} not found.".format( 'Firetext', '000') in str(e.value)
def test_process_sms_response_does_not_send_status_update_for_pending( sample_notification, mocker): send_mock = mocker.patch( 'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async' ) process_sms_client_response(status='2', provider_reference=str(sample_notification.id), client_name='firetext') send_mock.assert_not_called()
def test_sms_resonse_does_not_call_send_callback_if_no_db_entry(sample_notification, mocker): mocker.patch( 'app.notifications.process_client_response.notifications_dao.update_notification_status_by_id', return_value=sample_notification ) send_mock = mocker.patch( 'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async' ) reference = str(uuid.uuid4()) process_sms_client_response(status='3', provider_reference=reference, client_name='MMG') send_mock.assert_not_called()
def process_telstra_response(notification_id): client_name = 'telstra' data = request.json errors = validate_callback_data(data=data, fields=['messageId', 'deliveryStatus'], client_name=client_name) if errors: raise InvalidRequest(errors, status_code=400) success, errors = process_sms_client_response( status=str(data.get('deliveryStatus')), provider_reference=notification_id, client_name=client_name) redacted_data = data.copy() redacted_data.pop("to") current_app.logger.debug( "Full delivery response from {} for notification: {}\n{}".format( client_name, notification_id, redacted_data)) if errors: raise InvalidRequest(errors, status_code=400) return jsonify(result='success', message=success), 200
def test_outcome_statistics_called_for_successful_callback( sample_notification, mocker): mocker.patch( 'app.notifications.process_client_response.notifications_dao.update_notification_status_by_id', return_value=sample_notification) send_mock = mocker.patch( 'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async' ) callback_api = create_service_callback_api( service=sample_notification.service, url="https://original_url.com", notification_statuses=NOTIFICATION_STATUS_TYPES) reference = str(uuid.uuid4()) success, error = process_sms_client_response(status='3', provider_reference=reference, client_name='MMG') assert success == "MMG callback succeeded. reference {} updated".format( str(reference)) assert error is None encrypted_data = create_delivery_status_callback_data( sample_notification, callback_api) send_mock.assert_called_once_with( [str(sample_notification.id), encrypted_data], queue="service-callbacks")
def process_twilio_response(notification_id): client_name = 'Twilio' data = request.values errors = validate_callback_data(data=data, fields=['MessageStatus', 'MessageSid'], client_name=client_name) if errors: raise InvalidRequest(errors, status_code=400) success, errors = process_sms_client_response( status=data.get('MessageStatus'), provider_reference=notification_id, client_name=client_name) redacted_data = dict(data.items()) redacted_data.pop('To', None) current_app.logger.debug( "Full delivery response from {} for notification: {}\n{}".format( client_name, notification_id, redacted_data)) if errors: raise InvalidRequest(errors, status_code=400) else: return jsonify(result='success', message=success), 200
def test_process_sms_updates_sent_by_with_client_name_if_not_in_noti(sample_notification): sample_notification.sent_by = None success, error = process_sms_client_response( status='3', provider_reference=str(sample_notification.id), client_name='MMG') assert error is None assert success == 'MMG callback succeeded. reference {} updated'.format(sample_notification.id) assert sample_notification.sent_by == 'mmg'
def test_process_sms_updates_billable_units_if_zero(sample_notification): sample_notification.billable_units = 0 success, error = process_sms_client_response( status='3', provider_reference=str(sample_notification.id), client_name='MMG') assert error is None assert success == 'MMG callback succeeded. reference {} updated'.format(sample_notification.id) assert sample_notification.billable_units == 1
def test_process_sms_response_returns_error_bad_reference(mocker): success, error = process_sms_client_response( status='000', provider_reference='something-bad', client_name='sms-client') assert success is None assert error == "{} callback with invalid reference {}".format( 'sms-client', 'something-bad')
def test_process_sms_response_return_success_for_send_sms_code_reference( mocker): success, error = process_sms_client_response( status='000', provider_reference='send-sms-code', client_name='sms-client') assert success == "{} callback succeeded: send-sms-code".format( 'sms-client') assert error is None
def test_process_sms_response_raises_client_exception_for_unknown_sms_client( mocker): success, error = process_sms_client_response(status='000', provider_reference=str( uuid.uuid4()), client_name='sms-client') assert success is None assert error == 'unknown sms client: {}'.format('sms-client')
def process_firetext_response(): client_name = 'Firetext' errors = validate_callback_data(data=request.form, fields=['status', 'reference'], client_name=client_name) if errors: raise InvalidRequest(errors, status_code=400) safe_to_log = dict(request.form).copy() safe_to_log.pop('mobile') current_app.logger.debug( "Full delivery response from {} for notification: {}\n{}".format(client_name, request.form.get('reference'), safe_to_log)) success, errors = process_sms_client_response(status=request.form.get('status'), provider_reference=request.form.get('reference'), client_name=client_name) if errors: raise InvalidRequest(errors, status_code=400) else: return jsonify(result='success', message=success), 200
def process_mmg_response(): client_name = 'MMG' data = json.loads(request.data) errors = validate_callback_data(data=data, fields=['status', 'CID'], client_name=client_name) if errors: raise InvalidRequest(errors, status_code=400) success, errors = process_sms_client_response(status=str(data.get('status')), provider_reference=data.get('CID'), client_name=client_name) safe_to_log = data.copy() safe_to_log.pop("MSISDN") current_app.logger.debug( "Full delivery response from {} for notification: {}\n{}".format(client_name, request.form.get('CID'), safe_to_log)) if errors: raise InvalidRequest(errors, status_code=400) else: return jsonify(result='success', message=success), 200
def test_delivery_status_callback_calls_for_notification_with_callback( sample_notification_with_callback, mocker): mocker.patch( 'app.notifications.process_client_response.notifications_dao.update_notification_status_by_id', return_value=sample_notification_with_callback) send_mock = mocker.patch( 'app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async' ) reference = str(uuid.uuid4()) success, error = process_sms_client_response(status='delivered', provider_reference=reference, client_name='twilio') assert success == "twilio callback succeeded. reference {} updated".format( str(reference)) assert error is None encrypted_data = create_delivery_status_callback_data( sample_notification_with_callback, None) send_mock.assert_called_once_with( [str(sample_notification_with_callback.id), encrypted_data], queue="service-callbacks", )
def _process_sap_response(client_name, notification_id, data): errors = validate_callback_data(data=data, fields=['messageId', 'status'], client_name=client_name) if errors: raise InvalidRequest(errors, status_code=400) success, errors = process_sms_client_response( status=str(data.get('status')), provider_reference=notification_id, client_name=client_name) redacted_data = data.copy() redacted_data.pop("recipient") redacted_data.pop("message") current_app.logger.debug( "Full delivery response from {} for notification: {}\n{}".format( client_name, notification_id, redacted_data)) if errors: raise InvalidRequest(errors, status_code=400) return success
def process_pinpoint_response(): # setting provider_reference = 'send-sms-code' mocks a successful response success, errors = process_sms_client_response(None, 'send-sms-code', 'pinpoint') return jsonify(result='success', message=success), 200
def record_notification_status(provider, reference, response): notification = get_notification_by_reference(reference) return process_sms_client_response(status=response, provider_reference=str(notification.id), client_name=provider)
def test_process_sms_response_return_success_for_send_sms_code_reference(): success, error = process_sms_client_response(status="000", reference="send-sms-code", client_name="sms-client") assert success == "{} callback succeeded: send-sms-code".format("sms-client") assert error is None
def test_process_sms_response_returns_error_for_unknown_status(): success, error = process_sms_client_response(status="000", reference=str(uuid.uuid4()), client_name="Firetext") assert success is None assert error == "{} callback failed: status {} not found.".format("Firetext", "000")
def test_process_sms_response_returns_error_for_unknown_sms_client(): success, error = process_sms_client_response(status="000", reference=str(uuid.uuid4()), client_name="sms-client") assert success is None assert error == "unknown sms client: {}".format("sms-client")
def test_process_sms_response_returns_error_bad_reference(): success, error = process_sms_client_response(status="000", reference="something-bad", client_name="sms-client") assert success is None assert error == "{} callback with invalid reference {}".format("sms-client", "something-bad")