fix tests

This commit is contained in:
Kenneth Kehl
2023-07-10 14:21:36 -07:00
parent 6e442d71e6
commit 3ac2feacfe
3 changed files with 44 additions and 8 deletions

View File

@@ -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)

View File

@@ -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)
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)
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

View File

@@ -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,