diff --git a/app/email_branding/email_branding_schema.py b/app/email_branding/email_branding_schema.py index 7a70c70db..6c0dcdaeb 100644 --- a/app/email_branding/email_branding_schema.py +++ b/app/email_branding/email_branding_schema.py @@ -1,3 +1,5 @@ +from app.models import BRANDING_TYPES + post_create_email_branding_schema = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "POST schema for getting email_branding", @@ -10,6 +12,7 @@ post_create_email_branding_schema = { "text": {"type": ["string", "null"]}, "logo": {"type": ["string", "null"]}, "domain": {"type": ["string", "null"]}, + "brand_type": {"enum": BRANDING_TYPES}, }, "required": [] } @@ -26,6 +29,7 @@ post_update_email_branding_schema = { "text": {"type": ["string", "null"]}, "logo": {"type": ["string", "null"]}, "domain": {"type": ["string", "null"]}, + "brand_type": {"enum": BRANDING_TYPES}, }, "required": [] } diff --git a/app/models.py b/app/models.py index fa62a6a04..2a9aa68e3 100644 --- a/app/models.py +++ b/app/models.py @@ -192,6 +192,7 @@ BRANDING_GOVUK = 'govuk' BRANDING_ORG = 'org' BRANDING_BOTH = 'both' BRANDING_ORG_BANNER = 'org_banner' +BRANDING_TYPES = [BRANDING_GOVUK, BRANDING_ORG, BRANDING_BOTH, BRANDING_ORG_BANNER] class BrandingTypes(db.Model): @@ -209,6 +210,13 @@ class EmailBranding(db.Model): name = db.Column(db.String(255), nullable=True) text = db.Column(db.String(255), nullable=True) domain = db.Column(db.Text, nullable=True) + brand_type = db.Column( + db.String(255), + db.ForeignKey('branding_type.name'), + index=True, + nullable=True, + default=BRANDING_GOVUK + ) def serialize(self): serialized = { @@ -219,7 +227,8 @@ class EmailBranding(db.Model): "text": self.text, "banner_colour": self.banner_colour, "single_id_colour": self.single_id_colour, - "domain": self.domain + "domain": self.domain, + "brand_type": self.brand_type } return serialized diff --git a/migrations/versions/0214_email_brand_type.py b/migrations/versions/0214_email_brand_type.py new file mode 100644 index 000000000..a704b0753 --- /dev/null +++ b/migrations/versions/0214_email_brand_type.py @@ -0,0 +1,25 @@ +""" + +Revision ID: 0214_email_brand_type +Revises: 0213_brand_colour_domain +Create Date: 2018-08-23 11:48:00.800968 + +""" +from alembic import op +import sqlalchemy as sa + +revision = '0214_email_brand_type' +down_revision = '0213_brand_colour_domain' + + +def upgrade(): + + op.add_column('email_branding', sa.Column('brand_type', sa.String(length=255), nullable=True)) + op.create_index(op.f('ix_email_branding_brand_type'), 'email_branding', ['brand_type'], unique=False) + op.create_foreign_key(None, 'email_branding', 'branding_type', ['brand_type'], ['name']) + + +def downgrade(): + op.drop_constraint("email_branding_brand_type_fkey", 'email_branding', type_='foreignkey') + op.drop_index(op.f('ix_email_branding_brand_type'), table_name='email_branding') + op.drop_column('email_branding', 'brand_type') diff --git a/tests/app/email_branding/test_rest.py b/tests/app/email_branding/test_rest.py index c27969693..9ee6505d0 100644 --- a/tests/app/email_branding/test_rest.py +++ b/tests/app/email_branding/test_rest.py @@ -1,6 +1,7 @@ import pytest -from app.models import EmailBranding +from app.models import EmailBranding, BRANDING_GOVUK, BRANDING_ORG +from tests.app.db import create_email_branding def test_get_email_branding_options(admin_request, notify_db, notify_db_session): @@ -33,12 +34,13 @@ def test_get_email_branding_by_id(admin_request, notify_db, notify_db_session): ) assert set(response['email_branding'].keys()) == {'colour', 'logo', 'name', 'id', 'text', - 'banner_colour', 'single_id_colour', 'domain'} + 'banner_colour', 'single_id_colour', 'domain', 'brand_type'} assert response['email_branding']['colour'] == '#FFFFFF' assert response['email_branding']['logo'] == '/path/image.png' assert response['email_branding']['name'] == 'Some Org' assert response['email_branding']['text'] == 'My Org' assert response['email_branding']['id'] == str(email_branding.id) + assert response['email_branding']['brand_type'] == str(email_branding.brand_type) def test_post_create_email_branding(admin_request, notify_db_session): @@ -48,7 +50,8 @@ def test_post_create_email_branding(admin_request, notify_db_session): 'banner_colour': '#808080', 'single_id_colour': '#FF0000', 'logo': '/images/test_x2.png', - 'domain': 'gov.uk' + 'domain': 'gov.uk', + 'brand_type': BRANDING_ORG } response = admin_request.post( 'email_branding.create_email_branding', @@ -62,6 +65,24 @@ def test_post_create_email_branding(admin_request, notify_db_session): assert data['logo'] == response['data']['logo'] assert data['name'] == response['data']['text'] assert data['domain'] == response['data']['domain'] + assert data['brand_type'] == response['data']['brand_type'] + + +def test_post_create_email_branding_without_brand_type_defaults(admin_request, notify_db_session): + data = { + 'name': 'test email_branding', + 'colour': '#0000ff', + 'banner_colour': '#808080', + 'single_id_colour': '#FF0000', + 'logo': '/images/test_x2.png', + 'domain': 'gov.uk', + } + response = admin_request.post( + 'email_branding.create_email_branding', + _data=data, + _expected_status=201 + ) + assert BRANDING_GOVUK == response['data']['brand_type'] def test_post_create_email_branding_without_logo_is_ok(admin_request, notify_db_session): @@ -69,11 +90,12 @@ def test_post_create_email_branding_without_logo_is_ok(admin_request, notify_db_ 'name': 'test email_branding', 'colour': '#0000ff', } - admin_request.post( + response = admin_request.post( 'email_branding.create_email_branding', _data=data, _expected_status=201, ) + assert not response['data']['logo'] def test_post_create_email_branding_without_name_or_colour_is_valid(admin_request, notify_db_session): @@ -150,6 +172,7 @@ def test_post_create_email_branding_with_text_as_none_and_name(admin_request, no ({'logo': 'images/text_x3.png', 'colour': '#ffffff'}), ({'logo': 'images/text_x3.png', 'banner_colour': '#ffffff', 'single_id_colour': '#808080'}), ({'logo': 'images/text_x3.png', 'banner_colour': '#ffffff', 'single_id_colour': '#808080', 'domain': 'gov.uk'}), + ({'logo': 'images/text_x3.png', 'banner_colour': '#ffffff', 'single_id_colour': '#808080', 'brand_type': 'org'}), ]) def test_post_update_email_branding_updates_field(admin_request, notify_db_session, data_update): data = { @@ -164,7 +187,7 @@ def test_post_update_email_branding_updates_field(admin_request, notify_db_sessi email_branding_id = response['data']['id'] - response = admin_request.post( + admin_request.post( 'email_branding.update_email_branding', _data=data_update, email_branding_id=email_branding_id @@ -197,7 +220,7 @@ def test_post_update_email_branding_updates_field_with_text(admin_request, notif email_branding_id = response['data']['id'] - response = admin_request.post( + admin_request.post( 'email_branding.update_email_branding', _data=data_update, email_branding_id=email_branding_id @@ -209,3 +232,34 @@ def test_post_update_email_branding_updates_field_with_text(admin_request, notif assert str(email_branding[0].id) == email_branding_id for key in data_update.keys(): assert getattr(email_branding[0], key) == data_update[key] + + +def test_create_email_branding_reject_invalid_brand_type(admin_request): + data = { + 'name': 'test email_branding', + 'brand_type': 'NOT A TYPE' + + } + response = admin_request.post( + 'email_branding.create_email_branding', + _data=data, + _expected_status=400 + ) + + assert response['errors'][0]['message'] == 'brand_type NOT A TYPE is not one of [govuk, org, both, org_banner]' + + +def test_update_email_branding_reject_invalid_brand_type(admin_request, notify_db_session): + email_branding = create_email_branding() + data = { + 'brand_type': 'NOT A TYPE' + + } + response = admin_request.post( + 'email_branding.update_email_branding', + _data=data, + _expected_status=400, + email_branding_id=email_branding.id + ) + + assert response['errors'][0]['message'] == 'brand_type NOT A TYPE is not one of [govuk, org, both, org_banner]'