Merge branch 'master' into async-workers

This commit is contained in:
Athanasios Voutsadakis
2018-02-06 14:42:40 +00:00
18 changed files with 327 additions and 279 deletions

View File

@@ -96,7 +96,7 @@ def register_blueprint(application):
from app.template_statistics.rest import template_statistics as template_statistics_blueprint
from app.events.rest import events as events_blueprint
from app.provider_details.rest import provider_details as provider_details_blueprint
from app.organisation.rest import organisation_blueprint
from app.email_branding.rest import email_branding_blueprint
from app.dvla_organisation.rest import dvla_organisation_blueprint
from app.delivery.rest import delivery_blueprint
from app.inbound_number.rest import inbound_number_blueprint
@@ -159,8 +159,10 @@ def register_blueprint(application):
provider_details_blueprint.before_request(requires_admin_auth)
application.register_blueprint(provider_details_blueprint, url_prefix='/provider-details')
organisation_blueprint.before_request(requires_admin_auth)
application.register_blueprint(organisation_blueprint, url_prefix='/organisation')
email_branding_blueprint.before_request(requires_admin_auth)
application.register_blueprint(email_branding_blueprint, url_prefix='/email-branding')
# TODO: remove this route after admin is updated to refer to email branding
application.register_blueprint(email_branding_blueprint, url_prefix='/organisation')
dvla_organisation_blueprint.before_request(requires_admin_auth)
application.register_blueprint(dvla_organisation_blueprint, url_prefix='/dvla_organisations')

View File

@@ -0,0 +1,23 @@
from app import db
from app.dao.dao_utils import transactional
from app.models import EmailBranding
def dao_get_email_branding_options():
return EmailBranding.query.all()
def dao_get_email_branding_by_id(email_branding_id):
return EmailBranding.query.filter_by(id=email_branding_id).one()
@transactional
def dao_create_email_branding(email_branding):
db.session.add(email_branding)
@transactional
def dao_update_email_branding(email_branding, **kwargs):
for key, value in kwargs.items():
setattr(email_branding, key, value)
db.session.add(email_branding)

View File

@@ -1,23 +0,0 @@
from app import db
from app.dao.dao_utils import transactional
from app.models import Organisation
def dao_get_organisations():
return Organisation.query.all()
def dao_get_organisation_by_id(org_id):
return Organisation.query.filter_by(id=org_id).one()
@transactional
def dao_create_organisation(organisation):
db.session.add(organisation)
@transactional
def dao_update_organisation(organisation, **kwargs):
for key, value in kwargs.items():
setattr(organisation, key, value)
db.session.add(organisation)

View File

@@ -195,17 +195,17 @@ def get_logo_url(base_url, logo_file):
def get_html_email_options(service):
govuk_banner = service.branding not in (BRANDING_ORG, BRANDING_ORG_BANNER)
brand_banner = service.branding == BRANDING_ORG_BANNER
if service.organisation and service.branding != BRANDING_GOVUK:
if service.branding != BRANDING_GOVUK and service.email_branding:
logo_url = get_logo_url(
current_app.config['ADMIN_BASE_URL'],
service.organisation.logo
) if service.organisation.logo else None
service.email_branding.logo
) if service.email_branding.logo else None
branding = {
'brand_colour': service.organisation.colour,
'brand_colour': service.email_branding.colour,
'brand_logo': logo_url,
'brand_name': service.organisation.name,
'brand_name': service.email_branding.name,
}
else:
branding = {}

View File

@@ -1,6 +1,6 @@
post_create_organisation_schema = {
post_create_email_branding_schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST schema for getting organisation",
"description": "POST schema for getting email_branding",
"type": "object",
"properties": {
"colour": {"type": ["string", "null"]},
@@ -10,9 +10,9 @@ post_create_organisation_schema = {
"required": []
}
post_update_organisation_schema = {
post_update_email_branding_schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST schema for getting organisation",
"description": "POST schema for getting email_branding",
"type": "object",
"properties": {
"colour": {"type": ["string", "null"]},

View File

@@ -0,0 +1,58 @@
from flask import Blueprint, jsonify, request
from app.dao.email_branding_dao import (
dao_create_email_branding,
dao_get_email_branding_options,
dao_get_email_branding_by_id,
dao_update_email_branding
)
from app.errors import register_errors
from app.models import EmailBranding
from app.email_branding.email_branding_schema import (
post_create_email_branding_schema,
post_update_email_branding_schema
)
from app.schema_validation import validate
email_branding_blueprint = Blueprint('email_branding', __name__)
register_errors(email_branding_blueprint)
@email_branding_blueprint.route('', methods=['GET'])
def get_email_branding_options():
email_branding_options = [o.serialize() for o in dao_get_email_branding_options()]
key = 'organisations' if request.path.startswith('/organisation') else 'email_branding'
return jsonify(**{key: email_branding_options})
return jsonify(email_branding=email_branding_options)
@email_branding_blueprint.route('/<uuid:email_branding_id>', methods=['GET'])
def get_email_branding_by_id(email_branding_id):
email_branding = dao_get_email_branding_by_id(email_branding_id)
# TODO: remove this switch after admin is updated to refer to email branding
key = 'organisation' if request.path.startswith('/organisation') else 'email_branding'
return jsonify(**{key: email_branding.serialize()})
@email_branding_blueprint.route('', methods=['POST'])
def create_email_branding():
data = request.get_json()
validate(data, post_create_email_branding_schema)
email_branding = EmailBranding(**data)
dao_create_email_branding(email_branding)
return jsonify(data=email_branding.serialize()), 201
@email_branding_blueprint.route('/<uuid:email_branding_id>', methods=['POST'])
def update_email_branding(email_branding_id):
data = request.get_json()
validate(data, post_update_email_branding_schema)
fetched_email_branding = dao_get_email_branding_by_id(email_branding_id)
dao_update_email_branding(fetched_email_branding, **data)
return jsonify(data=fetched_email_branding.serialize()), 200

View File

@@ -148,6 +148,7 @@ class BrandingTypes(db.Model):
name = db.Column(db.String(255), primary_key=True)
# TODO: remove this model after admin is updated to refer to email branding
class Organisation(db.Model):
__tablename__ = 'organisation'
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)

View File

@@ -1,51 +0,0 @@
from flask import Blueprint, jsonify, request
from app.dao.organisations_dao import (
dao_create_organisation,
dao_get_organisations,
dao_get_organisation_by_id,
dao_update_organisation
)
from app.errors import register_errors
from app.models import Organisation
from app.organisation.organisation_schema import post_create_organisation_schema, post_update_organisation_schema
from app.schema_validation import validate
organisation_blueprint = Blueprint('organisation', __name__)
register_errors(organisation_blueprint)
@organisation_blueprint.route('', methods=['GET'])
def get_organisations():
organisations = [o.serialize() for o in dao_get_organisations()]
return jsonify(organisations=organisations)
@organisation_blueprint.route('/<uuid:org_id>', methods=['GET'])
def get_organisation_by_id(org_id):
organisation = dao_get_organisation_by_id(org_id)
return jsonify(organisation=organisation.serialize())
@organisation_blueprint.route('', methods=['POST'])
def create_organisation():
data = request.get_json()
validate(data, post_create_organisation_schema)
organisation = Organisation(**data)
dao_create_organisation(organisation)
return jsonify(data=organisation.serialize()), 201
@organisation_blueprint.route('/<uuid:organisation_id>', methods=['POST'])
def update_organisation(organisation_id):
data = request.get_json()
validate(data, post_update_organisation_schema)
fetched_organisation = dao_get_organisation_by_id(organisation_id)
dao_update_organisation(fetched_organisation, **data)
return jsonify(data=fetched_organisation.serialize()), 200

View File

@@ -199,11 +199,12 @@ class ProviderDetailsHistorySchema(BaseSchema):
class ServiceSchema(BaseSchema):
created_by = field_for(models.Service, 'created_by', required=True)
organisation = field_for(models.Service, 'organisation')
organisation_type = field_for(models.Service, 'organisation_type')
branding = field_for(models.Service, 'branding')
dvla_organisation = field_for(models.Service, 'dvla_organisation')
permissions = fields.Method("service_permissions")
# TODO: remove this variable after admin is updated to refer to email branding
organisation = field_for(models.Service, 'organisation')
email_branding = field_for(models.Service, 'email_branding')
override_flag = False
reply_to_email_address = fields.Method(method_name="get_reply_to_email_address")
@@ -284,11 +285,13 @@ class DetailedServiceSchema(BaseSchema):
'template_statistics',
'service_provider_stats',
'service_notification_stats',
# TODO: remove this field after admin is updated to refer to email branding
'organisation',
'email_branding',
'service_sms_senders',
'monthly_billing',
'reply_to_email_addresses',
'letter_contact_block', # new exclude from here
'letter_contact_block',
'message_limit',
'email_from',
'inbound_api',

View File

@@ -184,10 +184,15 @@ def update_service(service_id):
if org_type:
service.crown = org_type == 'central'
# TODO: remove this block after admin is updated to refer to email branding
if 'organisation' in req_json:
org_id = req_json['organisation']
service.email_branding = None if not org_id else EmailBranding.query.get(org_id)
if 'email_branding' in req_json:
email_branding_id = req_json['email_branding']
service.email_branding = None if not email_branding_id else EmailBranding.query.get(email_branding_id)
dao_update_service(service)
if service_going_live:

View File

@@ -0,0 +1,44 @@
from app.dao.email_branding_dao import (
dao_get_email_branding_options,
dao_get_email_branding_by_id,
dao_update_email_branding,
)
from app.models import EmailBranding
from tests.app.db import create_email_branding
def test_get_email_branding_options_gets_all_email_branding(notify_db, notify_db_session):
email_branding_1 = create_email_branding(name='test_email_branding_1')
email_branding_2 = create_email_branding(name='test_email_branding_2')
email_branding = dao_get_email_branding_options()
assert len(email_branding) == 2
assert email_branding_1 == email_branding[0]
assert email_branding_2 == email_branding[1]
def test_get_email_branding_by_id_gets_correct_email_branding(notify_db, notify_db_session):
email_branding = create_email_branding()
email_branding_from_db = dao_get_email_branding_by_id(email_branding.id)
assert email_branding_from_db == email_branding
def test_update_email_branding(notify_db, notify_db_session):
updated_name = 'new name'
create_email_branding()
email_branding = EmailBranding.query.all()
assert len(email_branding) == 1
assert email_branding[0].name != updated_name
dao_update_email_branding(email_branding[0], name=updated_name)
email_branding = EmailBranding.query.all()
assert len(email_branding) == 1
assert email_branding[0].name == updated_name

View File

@@ -1,60 +0,0 @@
from app.dao.organisations_dao import (
dao_get_organisations,
dao_get_organisation_by_id,
dao_update_organisation,
)
from app.models import Organisation
from tests.app.db import create_organisation
def test_create_organisation(notify_db, notify_db_session):
organisation = create_organisation()
assert Organisation.query.count() == 1
organisation_from_db = Organisation.query.first()
assert organisation == organisation_from_db
def test_create_organisation_without_name_or_colour_is_valid(notify_db, notify_db_session):
organisation = create_organisation(logo=None, name=None, colour=None)
assert Organisation.query.count() == 1
organisation_from_db = Organisation.query.first()
assert organisation == organisation_from_db
def test_get_organisations_gets_all_organisations(notify_db, notify_db_session):
org_1 = create_organisation(name='test_org_1')
org_2 = create_organisation(name='test_org_2')
organisations = dao_get_organisations()
assert len(organisations) == 2
assert org_1 == organisations[0]
assert org_2 == organisations[1]
def test_get_organisation_by_id_gets_correct_organisation(notify_db, notify_db_session):
organisation = create_organisation()
organisation_from_db = dao_get_organisation_by_id(organisation.id)
assert organisation_from_db == organisation
def test_update_organisation(notify_db, notify_db_session):
updated_name = 'new name'
create_organisation()
organisations_1 = Organisation.query.all()
assert len(organisations_1) == 1
assert organisations_1[0].name != updated_name
dao_update_organisation(organisations_1[0], name=updated_name)
organisations_2 = Organisation.query.all()
assert len(organisations_2) == 1
assert organisations_2[0].name == updated_name

View File

@@ -13,7 +13,7 @@ from app.models import (
Job,
MonthlyBilling,
Notification,
Organisation,
EmailBranding,
Rate,
Service,
ServiceEmailReplyTo,
@@ -40,7 +40,7 @@ from app.dao.templates_dao import dao_create_template
from app.dao.services_dao import dao_create_service
from app.dao.service_permissions_dao import dao_add_service_permission
from app.dao.inbound_sms_dao import dao_create_inbound_sms
from app.dao.organisations_dao import dao_create_organisation
from app.dao.email_branding_dao import dao_create_email_branding
def create_user(mobile_number="+447700900986", email="notify@digital.cabinet-office.gov.uk", state='active'):
@@ -316,16 +316,16 @@ def create_service_callback_api(
return service_callback_api
def create_organisation(colour='blue', logo='test_x2.png', name='test_org_1'):
def create_email_branding(colour='blue', logo='test_x2.png', name='test_org_1'):
data = {
'colour': colour,
'logo': logo,
'name': name
}
organisation = Organisation(**data)
dao_create_organisation(organisation)
email_branding = EmailBranding(**data)
dao_create_email_branding(email_branding)
return organisation
return email_branding
def create_rate(start_date, value, notification_type):

View File

@@ -15,7 +15,7 @@ from app.dao.provider_details_dao import dao_switch_sms_provider_to_provider_wit
from app.delivery import send_to_providers
from app.models import (
Notification,
Organisation,
EmailBranding,
KEY_TYPE_NORMAL,
KEY_TYPE_TEST,
KEY_TYPE_TEAM,
@@ -439,9 +439,9 @@ def test_get_html_email_renderer_should_return_for_normal_service(sample_service
])
def test_get_html_email_renderer_with_branding_details(branding_type, govuk_banner, notify_db, sample_service):
sample_service.branding = branding_type
org = Organisation(colour='#000000', logo='justice-league.png', name='Justice League')
sample_service.organisation = org
notify_db.session.add_all([sample_service, org])
email_branding = EmailBranding(colour='#000000', logo='justice-league.png', name='Justice League')
sample_service.email_branding = email_branding
notify_db.session.add_all([sample_service, email_branding])
notify_db.session.commit()
options = send_to_providers.get_html_email_options(sample_service)
@@ -458,9 +458,9 @@ def test_get_html_email_renderer_with_branding_details(branding_type, govuk_bann
def test_get_html_email_renderer_with_branding_details_and_render_govuk_banner_only(notify_db, sample_service):
sample_service.branding = BRANDING_GOVUK
org = Organisation(colour='#000000', logo='justice-league.png', name='Justice League')
sample_service.organisation = org
notify_db.session.add_all([sample_service, org])
email_branding = EmailBranding(colour='#000000', logo='justice-league.png', name='Justice League')
sample_service.email_branding = email_branding
notify_db.session.add_all([sample_service, email_branding])
notify_db.session.commit()
options = send_to_providers.get_html_email_options(sample_service)
@@ -469,23 +469,23 @@ def test_get_html_email_renderer_with_branding_details_and_render_govuk_banner_o
def test_get_html_email_renderer_prepends_logo_path(notify_api):
Service = namedtuple('Service', ['branding', 'organisation'])
Organisation = namedtuple('Organisation', ['colour', 'name', 'logo'])
Service = namedtuple('Service', ['branding', 'email_branding'])
EmailBranding = namedtuple('EmailBranding', ['colour', 'name', 'logo'])
org = Organisation(colour='#000000', logo='justice-league.png', name='Justice League')
service = Service(branding=BRANDING_ORG, organisation=org)
email_branding = EmailBranding(colour='#000000', logo='justice-league.png', name='Justice League')
service = Service(branding=BRANDING_ORG, email_branding=email_branding)
renderer = send_to_providers.get_html_email_options(service)
assert renderer['brand_logo'] == 'http://static-logos.notify.tools/justice-league.png'
def test_get_html_email_renderer_handles_org_without_logo(notify_api):
Service = namedtuple('Service', ['branding', 'organisation'])
Organisation = namedtuple('Organisation', ['colour', 'name', 'logo'])
def test_get_html_email_renderer_handles_email_branding_without_logo(notify_api):
Service = namedtuple('Service', ['branding', 'email_branding'])
EmailBranding = namedtuple('EmailBranding', ['colour', 'name', 'logo'])
org = Organisation(colour='#000000', logo=None, name='Justice League')
service = Service(branding=BRANDING_ORG, organisation=org)
email_branding = EmailBranding(colour='#000000', logo=None, name='Justice League')
service = Service(branding=BRANDING_ORG, email_branding=email_branding)
renderer = send_to_providers.get_html_email_options(service)

View File

@@ -0,0 +1,155 @@
import json
import pytest
from app.models import EmailBranding
from tests import create_authorization_header
def test_get_email_branding_options(admin_request, notify_db, notify_db_session):
email_branding1 = EmailBranding(colour='#FFFFFF', logo='/path/image.png', name='Org1')
email_branding2 = EmailBranding(colour='#000000', logo='/path/other.png', name='Org2')
notify_db.session.add_all([email_branding1, email_branding2])
notify_db.session.commit()
email_branding = admin_request.get(
'email_branding.get_email_branding_options'
)['email_branding']
assert len(email_branding) == 2
assert {
email_branding['id'] for email_branding in email_branding
} == {
str(email_branding1.id), str(email_branding2.id)
}
def test_get_email_branding_options_from_old_endpoint(client, notify_db, notify_db_session):
email_branding1 = EmailBranding(colour='#FFFFFF', logo='/path/image.png', name='Org1')
email_branding2 = EmailBranding(colour='#000000', logo='/path/other.png', name='Org2')
notify_db.session.add_all([email_branding1, email_branding2])
notify_db.session.commit()
response = client.get(
'/organisation',
headers=[create_authorization_header()]
)
assert response.status_code == 200
json_resp = json.loads(response.get_data(as_text=True))
email_branding = json_resp['organisations']
assert len(email_branding) == 2
assert {
email_branding['id'] for email_branding in email_branding
} == {
str(email_branding1.id), str(email_branding2.id)
}
def test_get_email_branding_by_id(admin_request, notify_db, notify_db_session):
email_branding = EmailBranding(colour='#FFFFFF', logo='/path/image.png', name='My Org')
notify_db.session.add(email_branding)
notify_db.session.commit()
response = admin_request.get(
'email_branding.get_email_branding_by_id',
_expected_status=200,
email_branding_id=email_branding.id
)
assert set(response['email_branding'].keys()) == {'colour', 'logo', 'name', 'id'}
assert response['email_branding']['colour'] == '#FFFFFF'
assert response['email_branding']['logo'] == '/path/image.png'
assert response['email_branding']['name'] == 'My Org'
assert response['email_branding']['id'] == str(email_branding.id)
def test_get_email_branding_by_id_from_old_endpoint(client, notify_db, notify_db_session):
email_branding = EmailBranding(colour='#FFFFFF', logo='/path/image.png', name='My Org')
notify_db.session.add(email_branding)
notify_db.session.commit()
response = client.get(
'/organisation/{}'.format(email_branding.id),
headers=[create_authorization_header()]
)
assert response.status_code == 200
json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['organisation']['id'] == str(email_branding.id)
def test_post_create_email_branding(admin_request, notify_db_session):
data = {
'name': 'test email_branding',
'colour': '#0000ff',
'logo': '/images/test_x2.png'
}
response = admin_request.post(
'email_branding.create_email_branding',
_data=data,
_expected_status=201
)
assert data['name'] == response['data']['name']
assert data['colour'] == response['data']['colour']
assert data['logo'] == response['data']['logo']
def test_post_create_email_branding_without_logo_is_ok(admin_request, notify_db_session):
data = {
'name': 'test email_branding',
'colour': '#0000ff',
}
admin_request.post(
'email_branding.create_email_branding',
_data=data,
_expected_status=201,
)
def test_post_create_email_branding_without_name_or_colour_is_valid(admin_request, notify_db_session):
data = {
'logo': 'images/text_x2.png'
}
response = admin_request.post(
'email_branding.create_email_branding',
_data=data,
_expected_status=201
)
assert response['data']['logo'] == data['logo']
assert response['data']['name'] is None
assert response['data']['colour'] is None
@pytest.mark.parametrize('data_update', [
({'name': 'test email_branding 1'}),
({'logo': 'images/text_x3.png', 'colour': '#ffffff'}),
])
def test_post_update_email_branding_updates_field(admin_request, notify_db_session, data_update):
data = {
'name': 'test email_branding',
'logo': 'images/text_x2.png'
}
response = admin_request.post(
'email_branding.create_email_branding',
_data=data,
_expected_status=201
)
email_branding_id = response['data']['id']
response = admin_request.post(
'email_branding.update_email_branding',
_data=data_update,
email_branding_id=email_branding_id
)
email_branding = EmailBranding.query.all()
assert len(email_branding) == 1
assert str(email_branding[0].id) == email_branding_id
for key in data_update.keys():
assert getattr(email_branding[0], key) == data_update[key]

View File

@@ -1,109 +0,0 @@
import pytest
from app.models import Organisation
def test_get_organisations(admin_request, notify_db, notify_db_session):
org1 = Organisation(colour='#FFFFFF', logo='/path/image.png', name='Org1')
org2 = Organisation(colour='#000000', logo='/path/other.png', name='Org2')
notify_db.session.add_all([org1, org2])
notify_db.session.commit()
organisations = admin_request.get(
'organisation.get_organisations'
)['organisations']
assert len(organisations) == 2
assert {org['id'] for org in organisations} == {str(org1.id), str(org2.id)}
def test_get_organisation_by_id(admin_request, notify_db, notify_db_session):
org = Organisation(colour='#FFFFFF', logo='/path/image.png', name='My Org')
notify_db.session.add(org)
notify_db.session.commit()
response = admin_request.get(
'organisation.get_organisation_by_id',
_expected_status=200,
org_id=org.id
)
assert set(response['organisation'].keys()) == {'colour', 'logo', 'name', 'id'}
assert response['organisation']['colour'] == '#FFFFFF'
assert response['organisation']['logo'] == '/path/image.png'
assert response['organisation']['name'] == 'My Org'
assert response['organisation']['id'] == str(org.id)
def test_post_create_organisation(admin_request, notify_db_session):
data = {
'name': 'test organisation',
'colour': '#0000ff',
'logo': '/images/test_x2.png'
}
response = admin_request.post(
'organisation.create_organisation',
_data=data,
_expected_status=201
)
assert data['name'] == response['data']['name']
assert data['colour'] == response['data']['colour']
assert data['logo'] == response['data']['logo']
def test_post_create_organisation_without_logo_is_ok(admin_request, notify_db_session):
data = {
'name': 'test organisation',
'colour': '#0000ff',
}
admin_request.post(
'organisation.create_organisation',
_data=data,
_expected_status=201,
)
def test_post_create_organisation_without_name_or_colour_is_valid(admin_request, notify_db_session):
data = {
'logo': 'images/text_x2.png'
}
response = admin_request.post(
'organisation.create_organisation',
_data=data,
_expected_status=201
)
assert response['data']['logo'] == data['logo']
assert response['data']['name'] is None
assert response['data']['colour'] is None
@pytest.mark.parametrize('data_update', [
({'name': 'test organisation 1'}),
({'logo': 'images/text_x3.png', 'colour': '#ffffff'}),
])
def test_post_update_organisation_updates_field(admin_request, notify_db_session, data_update):
data = {
'name': 'test organisation',
'logo': 'images/text_x2.png'
}
response = admin_request.post(
'organisation.create_organisation',
_data=data,
_expected_status=201
)
org_id = response['data']['id']
response = admin_request.post(
'organisation.update_organisation',
_data=data_update,
organisation_id=org_id
)
organisations = Organisation.query.all()
assert len(organisations) == 1
assert str(organisations[0].id) == org_id
for key in data_update.keys():
assert getattr(organisations[0], key) == data_update[key]