def sample_email_template( notify_db, notify_db_session, template_name="Email Template Name", template_type="email", user=None, content="This is a template", subject_line='Email Subject', service=None): if user is None: user = sample_user(notify_db, notify_db_session) if service is None: service = sample_service(notify_db, notify_db_session) data = { 'name': template_name, 'template_type': template_type, 'content': content, 'service': service, 'created_by': user } if subject_line: data.update({ 'subject': subject_line }) template = Template(**data) dao_create_template(template) return template
def test_create_template_creates_a_history_record_with_current_data( sample_service, sample_user): assert Template.query.count() == 0 assert TemplateHistory.query.count() == 0 data = { "name": "Sample Template", "template_type": "email", "subject": "subject", "content": "Template content", "service": sample_service, "created_by": sample_user, } template = Template(**data) dao_create_template(template) assert Template.query.count() == 1 template_from_db = Template.query.first() template_history = TemplateHistory.query.first() assert template_from_db.id == template_history.id assert template_from_db.name == template_history.name assert template_from_db.version == 1 assert template_from_db.version == template_history.version assert sample_user.id == template_history.created_by_id assert template_from_db.created_by.id == template_history.created_by_id
def create_template(service_id): fetched_service = dao_fetch_service_by_id(service_id=service_id) # permissions needs to be placed here otherwise marshmallow will intefere with versioning permissions = fetched_service.permissions new_template = template_schema.load(request.get_json()).data if not service_has_permission(new_template.template_type, permissions): message = "Creating {} templates is not allowed".format( get_public_notify_type_text(new_template.template_type)) errors = {'template_type': [message]} raise InvalidRequest(errors, 403) new_template.service = fetched_service over_limit = _content_count_greater_than_limit(new_template.content, new_template.template_type) if over_limit: char_count_limit = SMS_CHAR_COUNT_LIMIT message = 'Content has a character count greater than the limit of {}'.format( char_count_limit) errors = {'content': [message]} raise InvalidRequest(errors, status_code=400) check_reply_to(service_id, new_template.reply_to, new_template.template_type) dao_create_template(new_template) return jsonify(data=template_schema.dump(new_template).data), 201
def test_update_template_in_a_folder_to_archived(sample_service, sample_user): template_data = { 'name': 'Sample Template', 'template_type': "sms", 'content': "Template content", 'service': sample_service, 'created_by': sample_user } template = Template(**template_data) template_folder_data = { 'name': 'My Folder', 'service_id': sample_service.id, } template_folder = TemplateFolder(**template_folder_data) template.folder = template_folder dao_create_template(template) template.archived = True dao_update_template(template) template_folder = TemplateFolder.query.one() archived_template = Template.query.one() assert template_folder assert not archived_template.folder
def test_update_template_in_a_folder_to_archived(sample_service, sample_user): template_data = { "name": "Sample Template", "template_type": "sms", "content": "Template content", "service": sample_service, "created_by": sample_user, } template = Template(**template_data) template_folder_data = { "name": "My Folder", "service_id": sample_service.id, } template_folder = TemplateFolder(**template_folder_data) template.folder = template_folder dao_create_template(template) template.archived = True dao_update_template(template) template_folder = TemplateFolder.query.one() archived_template = Template.query.one() assert template_folder assert not archived_template.folder
def test_dao_update_template_reply_to_some_to_none(sample_service, sample_user): letter_contact = create_letter_contact(sample_service, "Edinburgh, ED1 1AA") data = { "name": "Sample Template", "template_type": "letter", "content": "Template content", "service": sample_service, "created_by": sample_user, "service_letter_contact_id": letter_contact.id, "postage": "second", } template = Template(**data) dao_create_template(template) created = Template.query.get(template.id) dao_update_template_reply_to(template_id=created.id, reply_to=None) updated = Template.query.get(template.id) assert updated.reply_to is None assert updated.version == 2 assert updated.updated_at history = TemplateHistory.query.filter_by(id=created.id, version=2).one() assert history.service_letter_contact_id is None assert history.updated_at == updated.updated_at
def create_template(service_id): fetched_service = dao_fetch_service_by_id(service_id=service_id) # permissions needs to be placed here otherwise marshmallow will interfere with versioning permissions = [p.permission for p in fetched_service.permissions] template_json = validate(request.get_json(), post_create_template_schema) folder = validate_parent_folder(template_json=template_json) new_template = Template.from_json(template_json, folder) if not service_has_permission(new_template.template_type, permissions): message = "Creating {} templates is not allowed".format( get_public_notify_type_text(new_template.template_type)) errors = {'template_type': [message]} raise InvalidRequest(errors, 403) if not new_template.postage and new_template.template_type == LETTER_TYPE: new_template.postage = SECOND_CLASS new_template.service = fetched_service over_limit = _content_count_greater_than_limit(new_template.content, new_template.template_type) if over_limit: message = 'Content has a character count greater than the limit of {}'.format( SMS_CHAR_COUNT_LIMIT) errors = {'content': [message]} raise InvalidRequest(errors, status_code=400) check_reply_to(service_id, new_template.reply_to, new_template.template_type) dao_create_template(new_template) return jsonify(data=template_schema.dump(new_template).data), 201
def sample_email_template( notify_db, notify_db_session, template_name="Email Template Name", template_type="email", user=None, content="This is a template", subject_line="Email Subject", service=None, permissions=[EMAIL_TYPE, SMS_TYPE], ): if user is None: user = create_user() if service is None: service = create_service( user=user, service_permissions=permissions, check_if_service_exists=True, ) data = { "name": template_name, "template_type": template_type, "content": content, "service": service, "created_by": user, "subject": subject_line, } template = Template(**data) dao_create_template(template) return template
def test_dao_update_template_reply_to_some_to_some(sample_service, sample_user): letter_contact = create_letter_contact(sample_service, 'Edinburgh, ED1 1AA') letter_contact_2 = create_letter_contact(sample_service, 'London, N1 1DE') data = { 'name': 'Sample Template', 'template_type': "letter", 'content': "Template content", 'service': sample_service, 'created_by': sample_user, 'service_letter_contact_id': letter_contact.id, 'postage': 'second', } template = Template(**data) dao_create_template(template) created = Template.query.get(template.id) dao_update_template_reply_to(template_id=created.id, reply_to=letter_contact_2.id) updated = Template.query.get(template.id) assert updated.reply_to == letter_contact_2.id assert updated.version == 2 assert updated.updated_at updated_history = TemplateHistory.query.filter_by(id=created.id, version=2).one() assert updated_history.service_letter_contact_id == letter_contact_2.id assert updated_history.updated_at == updated_history.updated_at
def sample_email_template(notify_db, notify_db_session, template_name="Email Template Name", template_type="email", user=None, content="This is a template", subject_line='Email Subject', service=None, permissions=[EMAIL_TYPE, SMS_TYPE]): if user is None: user = create_user() if service is None: service = create_service(user=user, service_permissions=permissions, check_if_service_exists=True, smtp_user="******") data = { 'name': template_name, 'template_type': template_type, 'content': content, 'service': service, 'created_by': user, 'subject': subject_line } template = Template(**data) dao_create_template(template) return template
def sample_template(notify_db, notify_db_session, template_name="Template Name", template_type="sms", content="This is a template:\nwith a newline", archived=False, subject_line='Subject', user=None, service=None, created_by=None): if user is None: user = sample_user(notify_db, notify_db_session) if service is None: service = sample_service(notify_db, notify_db_session) if created_by is None: created_by = sample_user(notify_db, notify_db_session) data = { 'name': template_name, 'template_type': template_type, 'content': content, 'service': service, 'created_by': created_by, 'archived': archived } if template_type in ['email', 'letter']: data.update({ 'subject': subject_line }) template = Template(**data) dao_create_template(template) return template
def test_update_template_creates_a_history_record_with_current_data( sample_service, sample_user): assert Template.query.count() == 0 assert TemplateHistory.query.count() == 0 data = { "name": "Sample Template", "template_type": "email", "subject": "subject", "content": "Template content", "service": sample_service, "created_by": sample_user, } template = Template(**data) dao_create_template(template) created = dao_get_all_templates_for_service(sample_service.id)[0] assert created.name == "Sample Template" assert Template.query.count() == 1 assert Template.query.first().version == 1 assert TemplateHistory.query.count() == 1 created.name = "new name" dao_update_template(created) assert Template.query.count() == 1 assert TemplateHistory.query.count() == 2 template_from_db = Template.query.first() assert template_from_db.version == 2 assert TemplateHistory.query.filter_by( name="Sample Template").one().version == 1 assert TemplateHistory.query.filter_by(name="new name").one().version == 2
def test_create_template_creates_a_history_record_with_current_data(sample_service, sample_user): assert Template.query.count() == 0 assert TemplateHistory.query.count() == 0 data = { 'name': 'Sample Template', 'template_type': "email", 'subject': "subject", 'content': "Template content", 'service': sample_service, 'created_by': sample_user } template = Template(**data) dao_create_template(template) assert Template.query.count() == 1 template_from_db = Template.query.first() template_history = TemplateHistory.query.first() assert template_from_db.id == template_history.id assert template_from_db.name == template_history.name assert template_from_db.version == 1 assert template_from_db.version == template_history.version assert sample_user.id == template_history.created_by_id assert template_from_db.created_by.id == template_history.created_by_id
def test_update_template_creates_a_history_record_with_current_data(sample_service, sample_user): assert Template.query.count() == 0 assert TemplateHistory.query.count() == 0 data = { 'name': 'Sample Template', 'template_type': "email", 'subject': "subject", 'content': "Template content", 'service': sample_service, 'created_by': sample_user } template = Template(**data) dao_create_template(template) created = dao_get_all_templates_for_service(sample_service.id)[0] assert created.name == 'Sample Template' assert Template.query.count() == 1 assert Template.query.first().version == 1 assert TemplateHistory.query.count() == 1 created.name = 'new name' dao_update_template(created) assert Template.query.count() == 1 assert TemplateHistory.query.count() == 2 template_from_db = Template.query.first() assert template_from_db.version == 2 assert TemplateHistory.query.filter_by(name='Sample Template').one().version == 1 assert TemplateHistory.query.filter_by(name='new name').one().version == 2
def test_template_postage_constraint_on_create(sample_service, sample_user, template_type, postage): data = { 'name': 'Sample Template', 'template_type': template_type, 'content': "Template content", 'service': sample_service, 'created_by': sample_user, 'postage': postage } template = Template(**data) with pytest.raises(expected_exception=SQLAlchemyError): dao_create_template(template)
def test_template_postage_constraint_on_create(sample_service, sample_user, template_type, postage): data = { "name": "Sample Template", "template_type": template_type, "content": "Template content", "service": sample_service, "created_by": sample_user, "postage": postage, } template = Template(**data) with pytest.raises(expected_exception=SQLAlchemyError): dao_create_template(template)
def test_template_with_no_given_provider_id_has_null_provider_id(sample_service, sample_user): data = { 'name': 'Sample Template', 'template_type': "email", 'content': "Template content", 'service': sample_service, 'created_by': sample_user, } template = Template(**data) dao_create_template(template) assert Template.query.filter_by(id=template.id).one().provider_id is None
def sample_email_template(notify_db_session): user = create_user() service = create_service(user=user, service_permissions=[EMAIL_TYPE, SMS_TYPE], check_if_service_exists=True) data = { 'name': 'Email Template Name', 'template_type': EMAIL_TYPE, 'content': 'This is a template', 'service': service, 'created_by': user, 'subject': 'Email Subject' } template = Template(**data) dao_create_template(template) return template
def test_template_with_provider_id_persists_provider_id(sample_service, sample_user, ses_provider): data = { 'name': 'Sample Template', 'template_type': "email", 'content': "Template content", 'service': sample_service, 'created_by': sample_user, 'provider_id': ses_provider.id } template = Template(**data) dao_create_template(template) assert Template.query.filter_by(id=template.id).one().provider_id == ses_provider.id
def create_template(service_id): fetched_service = dao_fetch_service_by_id(service_id=service_id) new_template = template_schema.load(request.get_json()).data new_template.service = fetched_service new_template.content = _strip_html(new_template.content) over_limit = _content_count_greater_than_limit(new_template.content, new_template.template_type) if over_limit: char_count_limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT') message = 'Content has a character count greater than the limit of {}'.format(char_count_limit) errors = {'content': [message]} raise InvalidRequest(errors, status_code=400) dao_create_template(new_template) return jsonify(data=template_schema.dump(new_template).data), 201
def test_create_template_with_reply_to(sample_service, sample_user): letter_contact = create_letter_contact(sample_service, 'Edinburgh, ED1 1AA') data = { 'name': 'Sample Template', 'template_type': "letter", 'content': "Template content", 'service': sample_service, 'created_by': sample_user, 'reply_to': letter_contact.id, } template = Template(**data) dao_create_template(template) assert dao_get_all_templates_for_service(sample_service.id)[0].reply_to == letter_contact.id
def test_update_template(sample_service, sample_user): data = { 'name': 'Sample Template', 'template_type': "sms", 'content': "Template content", 'service': sample_service, 'created_by': sample_user } template = Template(**data) dao_create_template(template) created = dao_get_all_templates_for_service(sample_service.id)[0] assert created.name == 'Sample Template' created.name = 'new name' dao_update_template(created) assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'new name'
def test_create_template(sample_service, sample_user, template_type, subject): data = { 'name': 'Sample Template', 'template_type': template_type, 'content': "Template content", 'service': sample_service, 'created_by': sample_user } if subject: data.update({'subject': subject}) template = Template(**data) dao_create_template(template) assert Template.query.count() == 1 assert len(dao_get_all_templates_for_service(sample_service.id)) == 1 assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'Sample Template'
def test_update_template(sample_service, sample_user): data = { "name": "Sample Template", "template_type": "sms", "content": "Template content", "service": sample_service, "created_by": sample_user, } template = Template(**data) dao_create_template(template) created = dao_get_all_templates_for_service(sample_service.id)[0] assert created.name == "Sample Template" created.name = "new name" dao_update_template(created) assert dao_get_all_templates_for_service( sample_service.id)[0].name == "new name"
def test_template_postage_constraint_on_update(sample_service, sample_user): data = { 'name': 'Sample Template', 'template_type': "letter", 'content': "Template content", 'service': sample_service, 'created_by': sample_user, 'postage': 'second' } template = Template(**data) dao_create_template(template) created = dao_get_all_templates_for_service(sample_service.id)[0] assert created.name == 'Sample Template' created.postage = 'third' with pytest.raises(expected_exception=SQLAlchemyError): dao_update_template(created)
def test_create_template(sample_service, sample_user, template_type, subject): data = { 'name': 'Sample Template', 'template_type': template_type, 'content': "Template content", 'service': sample_service, 'created_by': sample_user } if subject: data.update({'subject': subject}) template = Template(**data) dao_create_template(template) assert Template.query.count() == 1 assert len(dao_get_all_templates_for_service(sample_service.id)) == 1 assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'Sample Template' assert dao_get_all_templates_for_service(sample_service.id)[0].process_type == 'normal'
def test_template_postage_constraint_on_update(sample_service, sample_user): data = { "name": "Sample Template", "template_type": "letter", "content": "Template content", "service": sample_service, "created_by": sample_user, "postage": "second", } template = Template(**data) dao_create_template(template) created = dao_get_all_templates_for_service(sample_service.id)[0] assert created.name == "Sample Template" created.postage = "third" with pytest.raises(expected_exception=SQLAlchemyError): dao_update_template(created)
def test_create_template_with_reply_to(sample_service, sample_user): letter_contact = create_letter_contact(sample_service, "Edinburgh, ED1 1AA") data = { "name": "Sample Template", "template_type": "letter", "content": "Template content", "service": sample_service, "created_by": sample_user, "reply_to": letter_contact.id, "postage": "second", } template = Template(**data) dao_create_template(template) assert dao_get_all_templates_for_service( sample_service.id)[0].reply_to == letter_contact.id
def sample_template(notify_db_session): user = create_user() service = create_service(service_permissions=[EMAIL_TYPE, SMS_TYPE], check_if_service_exists=True) data = { 'name': 'Template Name', 'template_type': 'sms', 'content': 'This is a template:\nwith a newline', 'service': service, 'created_by': user, 'archived': False, 'hidden': False, 'process_type': 'normal' } template = Template(**data) dao_create_template(template) return template
def sample_template( notify_db, notify_db_session, template_name="Template Name", template_type="sms", content="This is a template:\nwith a newline", archived=False, hidden=False, subject_line='Subject', user=None, service=None, created_by=None, process_type='normal', permissions=[EMAIL_TYPE, SMS_TYPE] ): if user is None: user = create_user() if service is None: service = Service.query.filter_by(name='Sample service').first() if not service: service = create_service(service_permissions=permissions, check_if_service_exists=True) if created_by is None: created_by = create_user() data = { 'name': template_name, 'template_type': template_type, 'content': content, 'service': service, 'created_by': created_by, 'archived': archived, 'hidden': hidden, 'process_type': process_type } if template_type in ['email', 'letter']: data.update({ 'subject': subject_line }) if template_type == 'letter': data['postage'] = 'second' template = Template(**data) dao_create_template(template) return template
def sample_template( notify_db, notify_db_session, template_name="Template Name", template_type="sms", content="This is a template:\nwith a newline", archived=False, hidden=False, subject_line="Subject", user=None, service=None, created_by=None, process_type="normal", permissions=[EMAIL_TYPE, SMS_TYPE], ): if user is None: user = create_user() if service is None: service = Service.query.filter_by(name="Sample service").first() if not service: service = create_service(service_permissions=permissions, check_if_service_exists=True) if created_by is None: created_by = create_user() data = { "name": template_name, "template_type": template_type, "content": content, "service": service, "created_by": created_by, "archived": archived, "hidden": hidden, "process_type": process_type, } if template_type in ["email", "letter"]: data.update({"subject": subject_line}) if template_type == "letter": data["postage"] = "second" template = Template(**data) dao_create_template(template) return template
def get_precompiled_letter_template(service_id): template = Template.query.filter_by(service_id=service_id, template_type=LETTER_TYPE, hidden=True).first() if template is not None: return template template = Template( name='Pre-compiled PDF', created_by=get_user_by_id(current_app.config['NOTIFY_USER_ID']), service_id=service_id, template_type=LETTER_TYPE, hidden=True, subject='Pre-compiled PDF', content='', ) dao_create_template(template) return template
def test_create_template(sample_service, sample_user, template_type, subject): data = { "name": "Sample Template", "template_type": template_type, "content": "Template content", "service": sample_service, "created_by": sample_user, } if template_type == "letter": data["postage"] = "second" if subject: data.update({"subject": subject}) template = Template(**data) dao_create_template(template) assert Template.query.count() == 1 assert len(dao_get_all_templates_for_service(sample_service.id)) == 1 assert dao_get_all_templates_for_service( sample_service.id)[0].name == "Sample Template" assert dao_get_all_templates_for_service( sample_service.id)[0].process_type == "normal"
def sample_template( notify_db, notify_db_session, template_name="Template Name", template_type="sms", content="This is a template:\nwith a newline", archived=False, hidden=False, subject_line='Subject', user=None, service=None, created_by=None, process_type='normal', permissions=[EMAIL_TYPE, SMS_TYPE] ): if user is None: user = create_user() if service is None: service = sample_service(notify_db, notify_db_session, permissions=permissions) if created_by is None: created_by = create_user() data = { 'name': template_name, 'template_type': template_type, 'content': content, 'service': service, 'created_by': created_by, 'archived': archived, 'hidden': hidden, 'process_type': process_type } if template_type in ['email', 'letter']: data.update({ 'subject': subject_line }) template = Template(**data) dao_create_template(template) return template