Skip to content

Commit 2f01f79

Browse files
committed
DELETE /api/v2/manage/customers/{identifier} should return 400 when customer is referenced in a case
1 parent 6a55d46 commit 2f01f79

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

source/app/blueprints/rest/v2/manage_routes/customers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from app.blueprints.rest.endpoints import response_api_not_found
2727
from app.blueprints.rest.endpoints import response_api_deleted
2828
from app.blueprints.access_controls import ac_api_requires
29+
from app.datamgmt.exceptions.ElementExceptions import ElementInUseException
2930
from app.models.authorization import Permissions
3031
from app.schema.marshables import CustomerSchema
3132
from app.business.errors import ObjectNotFoundError
@@ -81,7 +82,8 @@ def delete(self, identifier):
8182

8283
except ObjectNotFoundError:
8384
return response_api_not_found()
84-
85+
except ElementInUseException as e:
86+
return response_api_error('Cannot delete a referenced customer')
8587

8688
customers_blueprint = Blueprint('customers_rest_v2', __name__, url_prefix='/customers')
8789

tests/iris.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ def create_dummy_customer(self) -> int:
104104
response = self.create('/manage/customers/add', {'customer_name': f'customer{uuid4()}'}).json()
105105
return response['data']['customer_id']
106106

107-
def create_dummy_case(self):
107+
def create_dummy_case(self, customer_identifier=IRIS_INITIAL_CUSTOMER_IDENTIFIER):
108108
body = {
109109
'case_name': 'case name',
110110
'case_description': 'description',
111-
'case_customer_id': 1,
111+
'case_customer_id': customer_identifier,
112112
'case_soc_id': ''
113113
}
114114
response = self._api.post('/api/v2/cases', body).json()

tests/tests_rest_customers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,17 @@ def test_delete_customer_should_return_204(self):
141141

142142
response = self._subject.delete(f'/api/v2/manage/customers/{identifier}')
143143
self.assertEqual(204, response.status_code)
144+
145+
def test_delete_customer_should_return_400_when_referenced_by_a_customer(self):
146+
body = {'customer_name': 'customer'}
147+
response = self._subject.create('/api/v2/manage/customers', body).json()
148+
identifier = response['customer_id']
149+
150+
self._subject.create_dummy_case(identifier)
151+
152+
# TODO currently, to remove a customer, no user should have any access to it. I am not sure this is the optimum behavior.
153+
body = {'customers_membership': [IRIS_INITIAL_CUSTOMER_IDENTIFIER]}
154+
self._subject.create(f'/manage/users/{ADMINISTRATOR_USER_IDENTIFIER}/customers/update', body)
155+
156+
response = self._subject.delete(f'/api/v2/manage/customers/{identifier}')
157+
self.assertEqual(400, response.status_code)

0 commit comments

Comments
 (0)