From 3ac2feacfe7501f95cfa7493a311c2a24053127a Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Mon, 10 Jul 2023 14:21:36 -0700 Subject: [PATCH] fix tests --- app/dao/organization_dao.py | 5 ----- app/organization/rest.py | 23 ++++++++++++++++++++--- tests/app/organization/test_rest.py | 24 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/app/dao/organization_dao.py b/app/dao/organization_dao.py index c0233d0bd..0a001ccca 100644 --- a/app/dao/organization_dao.py +++ b/app/dao/organization_dao.py @@ -62,24 +62,19 @@ def dao_create_organization(organization): @autocommit def dao_update_organization(organization_id, **kwargs): - domains = kwargs.pop('domains', None) - num_updated = Organization.query.filter_by(id=organization_id).update( kwargs ) if isinstance(domains, list): - Domain.query.filter_by(organization_id=organization_id).delete() - db.session.bulk_save_objects([ Domain(domain=domain.lower(), organization_id=organization_id) for domain in domains ]) organization = Organization.query.get(organization_id) - if 'organization_type' in kwargs: _update_organization_services(organization, 'organization_type', only_where_none=False) diff --git a/app/organization/rest.py b/app/organization/rest.py index 03f604a1c..6e589743f 100644 --- a/app/organization/rest.py +++ b/app/organization/rest.py @@ -1,6 +1,6 @@ from flask import Blueprint, abort, current_app, jsonify, request -from sqlalchemy.exc import IntegrityError +from sqlalchemy.exc import IntegrityError, SQLAlchemyError from app.config import QueueNames from app.dao.annual_billing_dao import set_default_free_allowance_for_service @@ -92,7 +92,15 @@ def create_organization(): data = request.get_json() validate(data, post_create_organization_schema) organization = Organization(**data) - dao_create_organization(organization) + try: + dao_create_organization(organization) + except SQLAlchemyError as e: + error = str(e.__dict__['orig']) + if "duplicate key" in error: + return jsonify(result='error', message='Organization name already exists'), 400 + else: + return jsonify(result='error', message='Error'), 400 + return jsonify(organization.serialize()), 201 @@ -101,7 +109,16 @@ def update_organization(organization_id): data = request.get_json() validate(data, post_update_organization_schema) - result = dao_update_organization(organization_id, **data) + try: + result = dao_update_organization(organization_id, **data) + except SQLAlchemyError as e: + error = str(e.__dict__['orig']) + if "duplicate key" in error and "domain_pkey" in error: + return jsonify(result='error', message='Domain already exists'), 400 + elif "duplicate key" in error and "organisation_name" in error: + return jsonify(result='error', message='Organization name already exists'), 400 + else: + return jsonify(result='error', message='Error'), 400 if data.get('agreement_signed') is True: # if a platform admin has manually adjusted the organization, don't tell people diff --git a/tests/app/organization/test_rest.py b/tests/app/organization/test_rest.py index 2b39a47ca..ae7e9318c 100644 --- a/tests/app/organization/test_rest.py +++ b/tests/app/organization/test_rest.py @@ -205,6 +205,9 @@ def test_post_create_organization_sets_default_nhs_branding_for_nhs_orgs( def test_post_create_organization_existing_name_raises_400(admin_request, sample_organization): + organization = Organization.query.all() + assert len(organization) == 1 + data = { 'name': sample_organization.name, 'active': True, @@ -223,6 +226,27 @@ def test_post_create_organization_existing_name_raises_400(admin_request, sample assert response['message'] == 'Organization name already exists' +def test_post_create_organization_works(admin_request, sample_organization): + organization = Organization.query.all() + assert len(organization) == 1 + + data = { + 'name': "org 2", + 'active': True, + 'organization_type': 'federal', + } + + admin_request.post( + 'organization.create_organization', + _data=data, + _expected_status=201 + ) + + organization = Organization.query.all() + + assert len(organization) == 2 + + @pytest.mark.parametrize('data, expected_error', ( ({ 'active': False,