Skip to content

Commit

Permalink
chore: users cannot update unregistered operations
Browse files Browse the repository at this point in the history
  • Loading branch information
BCerki committed Jan 16, 2025
1 parent 59acbb0 commit a82c8aa
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from registration.models import (
UserOperator,
)
from registration.models.operation import Operation
from registration.tests.constants import MOCK_DATA_URL
from registration.tests.utils.helpers import CommonTestSetup, TestUtils
from registration.tests.utils.bakers import operation_baker, operator_baker
Expand Down Expand Up @@ -106,7 +107,9 @@ def test_operations_with_documents_endpoint_get_success(self):

def test_operations_endpoint_put_success(self):
approved_user_operator = baker.make_recipe('utils.approved_user_operator', user=self.user)
operation = baker.make_recipe('utils.operation', operator=approved_user_operator.operator)
operation = baker.make_recipe(
'utils.operation', operator=approved_user_operator.operator, status=Operation.Statuses.REGISTERED
)
contact = baker.make_recipe('utils.contact')
self.test_payload["operation_representatives"] = [contact.id]
response = TestUtils.mock_put_with_auth_role(
Expand Down
5 changes: 1 addition & 4 deletions bc_obps/service/contact_service_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@
from service.data_access_service.contact_service import ContactDataAccessService
from service.data_access_service.user_service import UserDataAccessService
from ninja import Query
from uuid import UUID


from registration.constants import UNAUTHORIZED_MESSAGE
from registration.models.contact import Contact
from registration.schema.v1.contact import ContactOut
from service.data_access_service.contact_service import ContactDataAccessService
from service.data_access_service.user_service import UserDataAccessService
from ninja import Schema


Expand Down Expand Up @@ -51,6 +47,7 @@ def list_contacts_v2(
)
return cast(QuerySet[Contact], queryset)

@classmethod
def get_if_authorized_v2(cls, user_guid: UUID, contact_id: int) -> Optional[Contact]:
user = UserDataAccessService.get_by_guid(user_guid)
user_contacts = ContactDataAccessService.get_all_contacts_for_user(user)
Expand Down
12 changes: 9 additions & 3 deletions bc_obps/service/operation_service_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,20 @@ def update_operation(
payload: OperationInformationIn,
operation_id: UUID,
) -> Operation:
OperationService.get_if_authorized(user_guid, operation_id)
"""
This service is used for updating an operation after it's been registered. During registration, we use the endpoints in cas-registration/bc_obps/registration/api/v2/_operations/_operation_id/_registration
"""
operation = OperationService.get_if_authorized(user_guid, operation_id)

operation: Operation = cls.create_or_update_operation_v2(
if not operation.status == Operation.Statuses.REGISTERED:
raise Exception('Operation must be registered')

updated_operation: Operation = cls.create_or_update_operation_v2(
user_guid,
payload,
operation_id,
)
return operation
return updated_operation

@classmethod
def is_operation_opt_in_information_complete(cls, operation: Operation) -> bool:
Expand Down
9 changes: 4 additions & 5 deletions bc_obps/service/tests/test_contact_service_v2.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from registration.schema.v1.contact import ContactFilterSchema
import pytest
from service.contact_service_v2 import ContactServiceV2
from service.contact_service_v2 import ContactServiceV2, PlacesAssigned
from model_bakery import baker
from registration.models.business_role import BusinessRole
import pytest
from model_bakery import baker
from service.contact_service_v2 import ContactServiceV2

pytestmark = pytest.mark.django_db

Expand Down Expand Up @@ -52,7 +49,9 @@ def test_get_with_places_assigned_with_contacts():

result = ContactServiceV2.get_with_places_assigned_v2(approved_user_operator.user.user_guid, contact.id)
assert result.places_assigned == [
f"Operation Representative - {operation.name}",
PlacesAssigned(
role_name=contact.business_role.role_name, operation_name=operation.name, operation_id=operation.id
)
]

@staticmethod
Expand Down
36 changes: 33 additions & 3 deletions bc_obps/service/tests/test_operation_service_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ def test_update_operation_with_operation_representative_without_address():
)
with pytest.raises(
Exception,
match=f'The contact "{contact.first_name}" "{contact.last_name}"is missing address information. Please return to Contacts and fill in their address information before assigning them as an Operation Representative here.',
match=f'The contact {contact.first_name} {contact.last_name} is missing address information. Please return to Contacts and fill in their address information before assigning them as an Operation Representative here.',
):
OperationServiceV2.create_or_update_operation_v2(
approved_user_operator.user.user_guid,
Expand All @@ -625,10 +625,36 @@ def test_update_operation_with_operation_representative_without_address():


class TestOperationServiceV2UpdateOperation:
def test_update_operation_fails_if_operation_not_registered(self):
approved_user_operator = baker.make_recipe('utils.approved_user_operator')
existing_operation = baker.make_recipe(
'utils.operation',
operator=approved_user_operator.operator,
created_by=approved_user_operator.user,
status=Operation.Statuses.DRAFT,
)
payload = OperationInformationIn(
registration_purpose='Potential Reporting Operation',
regulated_products=[1],
name="Test Update Operation Name",
type="SFO",
naics_code_id=1,
secondary_naics_code_id=1,
tertiary_naics_code_id=2,
activities=[2],
process_flow_diagram=MOCK_DATA_URL,
boundary_map=MOCK_DATA_URL,
)
with pytest.raises(Exception, match='Operation must be registered'):
OperationServiceV2.update_operation(approved_user_operator.user.user_guid, payload, existing_operation.id)

def test_update_operation(self):
approved_user_operator = baker.make_recipe('utils.approved_user_operator')
existing_operation = baker.make_recipe(
'utils.operation', operator=approved_user_operator.operator, created_by=approved_user_operator.user
'utils.operation',
operator=approved_user_operator.operator,
created_by=approved_user_operator.user,
status=Operation.Statuses.REGISTERED,
)
payload = OperationInformationIn(
registration_purpose='Potential Reporting Operation',
Expand Down Expand Up @@ -658,7 +684,10 @@ def test_update_operation(self):
def test_update_operation_with_no_regulated_products(self):
approved_user_operator = baker.make_recipe('utils.approved_user_operator')
existing_operation = baker.make_recipe(
'utils.operation', operator=approved_user_operator.operator, created_by=approved_user_operator.user
'utils.operation',
operator=approved_user_operator.operator,
created_by=approved_user_operator.user,
status=Operation.Statuses.REGISTERED,
)
payload = OperationInformationIn(
registration_purpose='OBPS Regulated Operation',
Expand Down Expand Up @@ -691,6 +720,7 @@ def test_update_operation_with_new_entrant_application_data(self):
operator=approved_user_operator.operator,
created_by=approved_user_operator.user,
date_of_first_shipment=Operation.DateOfFirstShipmentChoices.ON_OR_AFTER_APRIL_1_2024,
status=Operation.Statuses.REGISTERED,
)
payload = OperationInformationIn(
registration_purpose='New Entrant Operation',
Expand Down

0 comments on commit a82c8aa

Please sign in to comment.