diff --git a/app/__init__.py b/app/__init__.py index be9f8aa95..af3f15c74 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -71,6 +71,7 @@ def create_app(app_name=None): from app.events.rest import events as events_blueprint from app.provider_details.rest import provider_details as provider_details_blueprint from app.spec.rest import spec as spec_blueprint + from app.organisation.rest import organisation_blueprint application.register_blueprint(service_blueprint, url_prefix='/service') application.register_blueprint(user_blueprint, url_prefix='/user') @@ -85,6 +86,7 @@ def create_app(app_name=None): application.register_blueprint(events_blueprint) application.register_blueprint(provider_details_blueprint, url_prefix='/provider-details') application.register_blueprint(spec_blueprint, url_prefix='/spec') + application.register_blueprint(organisation_blueprint, url_prefix='/organisation') return application diff --git a/app/dao/organisation_dao.py b/app/dao/organisation_dao.py new file mode 100644 index 000000000..804fd8a1d --- /dev/null +++ b/app/dao/organisation_dao.py @@ -0,0 +1,9 @@ +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() diff --git a/app/organisation/__init__.py b/app/organisation/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/app/organisation/rest.py b/app/organisation/rest.py new file mode 100644 index 000000000..1bdfbff74 --- /dev/null +++ b/app/organisation/rest.py @@ -0,0 +1,20 @@ +from flask import Blueprint, jsonify + +from app.dao.organisation_dao import dao_get_organisations, dao_get_organisation_by_id +from app.schemas import organisation_schema +from app.errors import register_errors + +organisation_blueprint = Blueprint('organisation', __name__) +register_errors(organisation_blueprint) + + +@organisation_blueprint.route('', methods=['GET']) +def get_organisations(): + data = organisation_schema.dump(dao_get_organisations(), many=True).data + return jsonify(organisations=data) + + +@organisation_blueprint.route('/', methods=['GET']) +def get_organisation_by_id(org_id): + data = organisation_schema.dump(dao_get_organisation_by_id(org_id)).data + return jsonify(organisation=data) diff --git a/app/schemas.py b/app/schemas.py index ea71ac341..168faa02c 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -440,6 +440,12 @@ class EventSchema(BaseSchema): strict = True +class OrganisationSchema(BaseSchema): + class Meta: + model = models.Organisation + strict = True + + class FromToDateSchema(ma.Schema): class Meta: @@ -533,6 +539,7 @@ service_history_schema = ServiceHistorySchema() api_key_history_schema = ApiKeyHistorySchema() template_history_schema = TemplateHistorySchema() event_schema = EventSchema() +organisation_schema = OrganisationSchema() from_to_date_schema = FromToDateSchema() provider_details_schema = ProviderDetailsSchema() week_aggregate_notification_statistics_schema = WeekAggregateNotificationStatisticsSchema() diff --git a/tests/app/organisation/__init__.py b/tests/app/organisation/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/app/organisation/test_rest.py b/tests/app/organisation/test_rest.py new file mode 100644 index 000000000..f024edd31 --- /dev/null +++ b/tests/app/organisation/test_rest.py @@ -0,0 +1,39 @@ +from flask import json + +from app.models import Organisation + +from tests import create_authorization_header + + +def test_get_organisations(notify_api, 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() + + with notify_api.test_request_context(), notify_api.test_client() as client: + auth_header = create_authorization_header() + response = client.get('/organisation', headers=[auth_header]) + + assert response.status_code == 200 + organisations = json.loads(response.get_data(as_text=True))['organisations'] + assert len(organisations) == 2 + assert {org['id'] for org in organisations} == {str(org1.id), str(org2.id)} + + +def test_get_organisation_by_id(notify_api, 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() + + with notify_api.test_request_context(), notify_api.test_client() as client: + auth_header = create_authorization_header() + response = client.get('/organisation/{}'.format(org.id), headers=[auth_header]) + + assert response.status_code == 200 + organisation = json.loads(response.get_data(as_text=True))['organisation'] + assert set(organisation.keys()) == {'colour', 'logo', 'name', 'id'} + assert organisation['colour'] == '#FFFFFF' + assert organisation['logo'] == '/path/image.png' + assert organisation['name'] == 'My Org' + assert organisation['id'] == str(org.id)