def test_validate_address(notify_db_session, key, address_line_3, expected_postage): service = create_service(service_permissions=[LETTER_TYPE, INTERNATIONAL_LETTERS]) data = { 'address_line_1': 'Prince Harry', 'address_line_2': 'Toronto', key: address_line_3, } postage = validate_address(service, data) assert postage == expected_postage
def send_one_off_notification(service_id, post_data): service = dao_fetch_service_by_id(service_id) template = dao_get_template_by_id_and_service_id( template_id=post_data['template_id'], service_id=service_id) personalisation = post_data.get('personalisation', None) validate_template(template.id, personalisation, service, template.template_type) check_service_over_daily_message_limit(KEY_TYPE_NORMAL, service) validate_and_format_recipient( send_to=post_data['to'], key_type=KEY_TYPE_NORMAL, service=service, notification_type=template.template_type, allow_guest_list_recipients=False, ) postage = None if template.template_type == LETTER_TYPE: # Validate address and set postage to europe|rest-of-world if international letter, # otherwise persist_notification with use template postage postage = validate_address(service, personalisation) if not postage: postage = template.postage validate_created_by(service, post_data['created_by']) sender_id = post_data.get('sender_id', None) reply_to = get_reply_to_text(notification_type=template.template_type, sender_id=sender_id, service=service, template=template) notification = persist_notification( template_id=template.id, template_version=template.version, recipient=post_data['to'], service=service, personalisation=personalisation, notification_type=template.template_type, api_key_id=None, key_type=KEY_TYPE_NORMAL, created_by_id=post_data['created_by'], reply_to_text=reply_to, reference=create_one_off_reference(template.template_type), postage=postage) queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None if template.template_type == LETTER_TYPE and service.research_mode: _update_notification_status( notification, NOTIFICATION_DELIVERED, ) else: send_notification_to_queue( notification=notification, research_mode=service.research_mode, queue=queue_name, ) return {'id': str(notification.id)}
def process_letter_notification(*, letter_data, api_key, service, template, template_with_content, reply_to_text, precompiled=False): if api_key.key_type == KEY_TYPE_TEAM: raise BadRequestError( message='Cannot send letters with a team api key', status_code=403) if not service.research_mode and service.restricted and api_key.key_type != KEY_TYPE_TEST: raise BadRequestError( message='Cannot send letters when service is in trial mode', status_code=403) if precompiled: return process_precompiled_letter_notifications( letter_data=letter_data, api_key=api_key, service=service, template=template, reply_to_text=reply_to_text) postage = validate_address(service, letter_data['personalisation']) test_key = api_key.key_type == KEY_TYPE_TEST status = NOTIFICATION_CREATED updated_at = None if test_key: # if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up if current_app.config['NOTIFY_ENVIRONMENT'] in [ 'preview', 'development' ]: status = NOTIFICATION_SENDING # mark test letter as delivered and do not create a fake response later else: status = NOTIFICATION_DELIVERED updated_at = datetime.utcnow() queue = QueueNames.CREATE_LETTERS_PDF if not test_key else QueueNames.RESEARCH_MODE notification = create_letter_notification(letter_data=letter_data, service=service, template=template, api_key=api_key, status=status, reply_to_text=reply_to_text, updated_at=updated_at, postage=postage) get_pdf_for_templated_letter.apply_async([str(notification.id)], queue=queue) if test_key and current_app.config['NOTIFY_ENVIRONMENT'] in [ 'preview', 'development' ]: create_fake_letter_response_file.apply_async( (notification.reference, ), queue=queue) resp = create_response_for_post_notification( notification_id=notification.id, client_reference=notification.client_reference, template_id=notification.template_id, template_version=notification.template_version, notification_type=notification.notification_type, reply_to=reply_to_text, service_id=notification.service_id, template_with_content=template_with_content) return resp