diff --git a/app/models.py b/app/models.py index 7cb6c2e41..187ba2b3a 100644 --- a/app/models.py +++ b/app/models.py @@ -166,6 +166,33 @@ class Organisation(db.Model): return serialized +class EmailBranding(db.Model): + __tablename__ = 'email_branding' + id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + colour = db.Column(db.String(7), nullable=True) + logo = db.Column(db.String(255), nullable=True) + name = db.Column(db.String(255), nullable=True) + + def serialize(self): + serialized = { + "id": str(self.id), + "colour": self.colour, + "logo": self.logo, + "name": self.name, + } + + return serialized + + +service_email_branding = db.Table( + 'service_email_branding', + db.Model.metadata, + # service_id is a primary key as you can only have one email branding per service + db.Column('service_id', UUID(as_uuid=True), db.ForeignKey('services.id'), primary_key=True, nullable=False), + db.Column('email_branding_id', UUID(as_uuid=True), db.ForeignKey('email_branding.id'), nullable=False), +) + + DVLA_ORG_HM_GOVERNMENT = '001' DVLA_ORG_LAND_REGISTRY = '500' @@ -255,6 +282,12 @@ class Service(db.Model, Versioned): association_proxy('permissions', 'service_permission_types') + email_branding = db.relationship( + 'EmailBranding', + secondary=service_email_branding, + uselist=False, + backref=db.backref('services', lazy='dynamic')) + @classmethod def from_json(cls, data): """ diff --git a/app/schemas.py b/app/schemas.py index ed4c914ef..efb0545c1 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -204,6 +204,7 @@ class ServiceSchema(BaseSchema): branding = field_for(models.Service, 'branding') dvla_organisation = field_for(models.Service, 'dvla_organisation') permissions = fields.Method("service_permissions") + email_branding = field_for(models.Service, 'email_branding') override_flag = False reply_to_email_address = fields.Method(method_name="get_reply_to_email_address") sms_sender = fields.Method(method_name="get_sms_sender") diff --git a/app/service/rest.py b/app/service/rest.py index 9c1763af0..89968aee7 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -66,7 +66,7 @@ from app.errors import ( InvalidRequest, register_errors ) -from app.models import Service +from app.models import Service, EmailBranding from app.schema_validation import validate from app.service import statistics from app.service.service_senders_schema import ( @@ -179,11 +179,16 @@ def update_service(service_id): current_data = dict(service_schema.dump(fetched_service).data.items()) current_data.update(request.get_json()) - update_dict = service_schema.load(current_data).data + service = service_schema.load(current_data).data org_type = req_json.get('organisation_type', None) if org_type: - update_dict.crown = org_type == 'central' - dao_update_service(update_dict) + service.crown = org_type == 'central' + + if 'organisation' in req_json: + org_id = req_json['organisation'] + service.email_branding = None if not org_id else EmailBranding.query.get(org_id) + + dao_update_service(service) if service_going_live: send_notification_to_service_users( diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 35fbc8650..9a30d7e17 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -13,9 +13,16 @@ from app.dao.services_dao import dao_remove_user_from_service from app.dao.templates_dao import dao_redact_template from app.dao.users_dao import save_model_user from app.models import ( - User, Organisation, Service, ServicePermission, Notification, - ServiceEmailReplyTo, ServiceLetterContact, - ServiceSmsSender, InboundNumber, + EmailBranding, + InboundNumber, + Notification, + Organisation, + Service, + ServiceEmailReplyTo, + ServiceLetterContact, + ServicePermission, + ServiceSmsSender, + User, DVLA_ORG_LAND_REGISTRY, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST, EMAIL_TYPE, SMS_TYPE, LETTER_TYPE, INTERNATIONAL_SMS_TYPE, INBOUND_SMS_TYPE @@ -403,6 +410,10 @@ def test_update_service(client, notify_db, sample_service): org = Organisation(colour='#000000', logo='justice-league.png', name='Justice League') notify_db.session.add(org) notify_db.session.commit() + # Need to set this up manually until org->email_branding migration is complete :( + brand = EmailBranding(id=org.id, colour='#000000', logo='justice-league.png', name='Justice League') + notify_db.session.add(brand) + notify_db.session.commit() auth_header = create_authorization_header() resp = client.get( @@ -412,6 +423,8 @@ def test_update_service(client, notify_db, sample_service): json_resp = json.loads(resp.get_data(as_text=True)) assert resp.status_code == 200 assert json_resp['data']['name'] == sample_service.name + assert json_resp['data']['organisation'] is None + assert json_resp['data']['email_branding'] is None data = { 'name': 'updated service name', @@ -434,10 +447,48 @@ def test_update_service(client, notify_db, sample_service): assert result['data']['name'] == 'updated service name' assert result['data']['email_from'] == 'updated.service.name' assert result['data']['organisation'] == str(org.id) + assert result['data']['email_branding'] == str(org.id) assert result['data']['dvla_organisation'] == DVLA_ORG_LAND_REGISTRY assert result['data']['organisation_type'] == 'foo' +def test_update_service_remove_org(admin_request, notify_db, sample_service): + org = Organisation(colour='#000000', logo='justice-league.png', name='Justice League') + notify_db.session.add(org) + notify_db.session.commit() + # Need to set this up manually until org->email_branding migration is complete :( + brand = EmailBranding(id=org.id, colour='#000000', logo='justice-league.png', name='Justice League') + sample_service.organisation = org + sample_service.email_branding = brand + notify_db.session.commit() + + resp = admin_request.post('service.update_service', service_id=sample_service.id, _data={'organisation': None}) + assert resp['data']['organisation'] is None + assert resp['data']['email_branding'] is None + + +def test_update_service_change_org(admin_request, notify_db, sample_service): + org1 = Organisation(colour='#000000', logo='justice-league.png', name='Justice League') + org2 = Organisation(colour='#111111', logo='avengers.png', name='Avengers') + notify_db.session.add_all([org1, org2]) + notify_db.session.commit() + # Need to set this up manually until org->email_branding migration is complete :( + brand1 = EmailBranding(id=org1.id, colour='#000000', logo='justice-league.png', name='Justice League') + brand2 = EmailBranding(id=org2.id, colour='#111111', logo='avengers.png', name='Avengers') + notify_db.session.add_all([brand1, brand2]) + sample_service.organisation = org1 + sample_service.email_branding = brand1 + notify_db.session.commit() + + resp = admin_request.post( + 'service.update_service', + service_id=sample_service.id, + _data={'organisation': str(org2.id)} + ) + assert resp['data']['organisation'] == str(org2.id) + assert resp['data']['email_branding'] == str(brand2.id) + + def test_update_service_flags(client, sample_service): auth_header = create_authorization_header() resp = client.get(