def test_dao_get_reply_to_by_id_raises_sqlalchemy_error_when_service_does_not_exist( sample_service): reply_to = create_reply_to_email(service=sample_service, email_address='*****@*****.**') with pytest.raises(SQLAlchemyError): dao_get_reply_to_by_id(service_id=uuid.uuid4(), reply_to_id=reply_to.id)
def test_dao_get_reply_to_by_id_raises_sqlalchemy_error_when_reply_to_is_archived(sample_service): create_reply_to_email(service=sample_service, email_address='*****@*****.**') archived_reply_to = create_reply_to_email( service=sample_service, email_address='*****@*****.**', is_default=False, archived=True) with pytest.raises(SQLAlchemyError): dao_get_reply_to_by_id(service_id=sample_service.id, reply_to_id=archived_reply_to.id)
def check_service_email_reply_to_id(service_id, reply_to_id, notification_type): if reply_to_id: try: return dao_get_reply_to_by_id(service_id, reply_to_id).email_address except NoResultFound: message = 'email_reply_to_id {} does not exist in database for service id {}'\ .format(reply_to_id, service_id) raise BadRequestError(message=message)
def save_email(self, service_id, notification_id, encrypted_notification, sender_id=None): notification = encryption.decrypt(encrypted_notification) service = dao_fetch_service_by_id(service_id) template = dao_get_template_by_id(notification["template"], version=notification["template_version"]) if sender_id: reply_to_text = dao_get_reply_to_by_id(service_id, sender_id).email_address else: reply_to_text = template.get_reply_to_text() if not service_allowed_to_send_to( notification["to"], service, notification.get("key_type", KEY_TYPE_NORMAL)): current_app.logger.info( "Email {} failed as restricted service".format(notification_id)) return check_service_over_daily_message_limit( notification.get("key_type", KEY_TYPE_NORMAL), service) try: # this task is used by two main things... process_job and process_sms_or_email_notification # if the data is not present in the encrypted data then fallback on whats needed for process_job saved_notification = persist_notification( notification_id=notification.get("id", notification_id), template_id=notification["template"], template_version=notification["template_version"], recipient=notification["to"], service=service, personalisation=notification.get("personalisation"), notification_type=EMAIL_TYPE, api_key_id=notification.get("api_key", None), key_type=notification.get("key_type", KEY_TYPE_NORMAL), created_at=datetime.utcnow(), job_id=notification.get("job", None), simulated=notification.get("simulated", None), job_row_number=notification.get("row_number", None), reply_to_text=reply_to_text, client_reference=notification.get("client_reference", None), ) send_notification_to_queue( saved_notification, service.research_mode, queue=notification.get("queue") or template.queue_to_use(), ) current_app.logger.debug("Email {} created at {}".format( saved_notification.id, saved_notification.created_at)) except SQLAlchemyError as e: handle_exception(self, notification, notification_id, e)
def get_reply_to_text(notification_type, sender_id, service, template): reply_to = None if sender_id: if notification_type == EMAIL_TYPE: reply_to = dao_get_reply_to_by_id(service.id, sender_id).email_address elif notification_type == SMS_TYPE: reply_to = dao_get_service_sms_senders_by_id(service.id, sender_id).get_reply_to_text() else: reply_to = template.get_reply_to_text() return reply_to
def save_email(self, service_id, notification_id, encrypted_notification, sender_id=None): notification = encryption.decrypt(encrypted_notification) service = SerialisedService.from_id(service_id) template = SerialisedTemplate.from_id_and_service_id( notification['template'], service_id=service.id, version=notification['template_version'], ) if sender_id: reply_to_text = dao_get_reply_to_by_id(service_id, sender_id).email_address else: reply_to_text = template.reply_to_text if not service_allowed_to_send_to(notification['to'], service, KEY_TYPE_NORMAL): current_app.logger.info( "Email {} failed as restricted service".format(notification_id)) return try: saved_notification = persist_notification( template_id=notification['template'], template_version=notification['template_version'], recipient=notification['to'], service=service, personalisation=notification.get('personalisation'), notification_type=EMAIL_TYPE, api_key_id=None, key_type=KEY_TYPE_NORMAL, created_at=datetime.utcnow(), job_id=notification.get('job', None), job_row_number=notification.get('row_number', None), notification_id=notification_id, reply_to_text=reply_to_text) provider_tasks.deliver_email.apply_async( [str(saved_notification.id)], queue=QueueNames.SEND_EMAIL if not service.research_mode else QueueNames.RESEARCH_MODE) current_app.logger.debug("Email {} created at {}".format( saved_notification.id, saved_notification.created_at)) except SQLAlchemyError as e: handle_exception(self, notification, notification_id, e)
def get_reply_to_text(notification_type, sender_id, service, template): reply_to = None if sender_id: try: if notification_type == EMAIL_TYPE: message = "Reply to email address not found" reply_to = dao_get_reply_to_by_id(service.id, sender_id).email_address elif notification_type == SMS_TYPE: message = "SMS sender not found" reply_to = dao_get_service_sms_senders_by_id(service.id, sender_id).get_reply_to_text() except NoResultFound: raise BadRequestError(message=message) else: reply_to = template.get_reply_to_text() return reply_to
def save_email(self, service_id, notification_id, encrypted_notification, sender_id=None): notification = encryption.decrypt(encrypted_notification) service = dao_fetch_service_by_id(service_id) template = dao_get_template_by_id(notification['template'], version=notification['template_version']) if sender_id: reply_to_text = dao_get_reply_to_by_id(service_id, sender_id).email_address else: reply_to_text = template.get_reply_to_text() if not service_allowed_to_send_to(notification['to'], service, KEY_TYPE_NORMAL): current_app.logger.info( "Email {} failed as restricted service".format(notification_id)) return try: saved_notification = persist_notification( template_id=notification['template'], template_version=notification['template_version'], recipient=notification['to'], service=service, personalisation=notification.get('personalisation'), notification_type=EMAIL_TYPE, api_key_id=None, key_type=KEY_TYPE_NORMAL, created_at=datetime.utcnow(), job_id=notification.get('job', None), job_row_number=notification.get('row_number', None), notification_id=notification_id, reply_to_text=reply_to_text) send_notification_to_queue(saved_notification, service.research_mode) current_app.logger.debug("Email {} created at {}".format( saved_notification.id, saved_notification.created_at)) except SQLAlchemyError as e: handle_exception(self, notification, notification_id, e)
def get_email_reply_to_address(service_id, reply_to_id): result = dao_get_reply_to_by_id(service_id=service_id, reply_to_id=reply_to_id) return jsonify(result.serialize()), 200
def test_dao_get_reply_to_by_id_raises_sqlalchemy_error_when_reply_to_does_not_exist( sample_service): with pytest.raises(SQLAlchemyError): dao_get_reply_to_by_id(service_id=sample_service.id, reply_to_id=uuid.uuid4())
def test_dao_get_reply_to_by_id(sample_service): reply_to = create_reply_to_email(service=sample_service, email_address='*****@*****.**') result = dao_get_reply_to_by_id(service_id=sample_service.id, reply_to_id=reply_to.id) assert result == reply_to