diff --git a/.env.sample b/.env.sample index f4cc9703..6829c43b 100644 --- a/.env.sample +++ b/.env.sample @@ -2,5 +2,5 @@ POSTGRES_USERNAME= POSTGRES_PASSWORD= POSTGRES_HOSTNAME=localhost POSTGRES_NAME=laa_crime_application_store -NSM_CASEWORKER_AUTH_ID=secret_auth_key -NSM_CASEWORKER_URL=http://localhost:3000 +APP_CLIENT_ID= +TENANT_ID= diff --git a/README.md b/README.md index 38d42b69..82bc291c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # laa-crime-application-store -LAA Crime Application Store is a service to provide the ability to store and version crime applications from CRM forms. +LAA Crime Application Store is a service to provide the ability to store and version crime applications from CRM forms. ## Setting up the service @@ -44,10 +44,21 @@ pipenv run uvicorn laa_crime_application_store_app.main:app --reload ``` The application will reload on code changes to save on rebuild times -### Running locally with Docker +#### Authenticating Requests + +This application uses [Entra ID](https://www.microsoft.com/en-gb/security/business/identity-access/microsoft-entra-id#overview) +to authenticate API requests through the use of the [fastapi-azure-auth](https://github.com/Intility/fastapi-azure-auth). To be able to authenticate +requests you will need to setup and add your application within Entra ID and add the following environment variables +``` +APP_CLIENT_ID={uuid of the application created} +TENANT_ID={uuid of the tentant that the application was created in} +``` +Once added, calls to the API will require a [bearer token requested from the same app/tenant id within the header](https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-client-creds-grant-flow#use-a-token). + +#### Running locally with Docker To run via a docker container: -1. Perform the docker build with: +1. Perform the docker build with: `docker-compose build app` 2. You can optionally set build arguments by adding: @@ -68,7 +79,8 @@ pipenv run pytest --cov-report term --cov=laa_crime_application_store_app tests/ #### API Tests API testing is done using the Postman tooling. This can be downloaded from [the Postman website() and a free account created. Once this is done you can import the collections and environments found in the postman -folder to begin testing. +folder to begin testing. You will need to get a secret token from Entra ID from the Tenant and Application ID as setup +above to be able to authenticate requests. ### Running linters @@ -77,4 +89,4 @@ Running linters can be done using the following command from the root of the pro pipenv run black . pipenv run isort . pipenv run flake8 -``` \ No newline at end of file +``` diff --git a/laa_crime_application_store_app/routers/v1/application.py b/laa_crime_application_store_app/routers/v1/application.py index c996b38c..6a34a87b 100644 --- a/laa_crime_application_store_app/routers/v1/application.py +++ b/laa_crime_application_store_app/routers/v1/application.py @@ -48,10 +48,13 @@ async def get_applications( @router.get("/application/{app_id}", response_model=App) @auth_logger async def get_application( - request: Request, app_id: UUID | None = None, db: Session = Depends(get_db) + request: Request, + app_id: UUID | None = None, + app_version: int | None = None, + db: Session = Depends(get_db), ): logger.info("GETTING_APPLICATION", application_id=app_id) - application = ApplicationService().get_application(db, app_id) + application = ApplicationService().get_application(db, app_id, app_version) if application is None: return Response(status_code=400) diff --git a/laa_crime_application_store_app/services/v1/application_service.py b/laa_crime_application_store_app/services/v1/application_service.py index c61c0859..ec77e378 100644 --- a/laa_crime_application_store_app/services/v1/application_service.py +++ b/laa_crime_application_store_app/services/v1/application_service.py @@ -22,22 +22,24 @@ class ApplicationService: @staticmethod - def get_application(db: Session, app_id: UUID): + def get_application(db: Session, app_id: UUID, app_version: int | None): application = ApplicationService.__get_application_by_id(db, app_id) if application is None: logger.info("APPLICATION_NOT_FOUND", application_id=app_id) return None + version = application.current_version if app_version is None else app_version + application_version = ApplicationService.__get_application_version( - db, app_id, application.current_version + db, app_id, version ) if application_version is None: logger.info( "APPLICATION_VERSION_NOT_FOUND", application_id=app_id, - version=application.current_version, + version=version, ) return None diff --git a/postman/collections/Application.postman_collection.json b/postman/collections/Application.postman_collection.json index d9f94f68..73905cdc 100644 --- a/postman/collections/Application.postman_collection.json +++ b/postman/collections/Application.postman_collection.json @@ -17,7 +17,19 @@ "const uuid = require('uuid')", "const uuidv4 = uuid.v4()", "", - "pm.environment.set('appId', uuidv4)" + "pm.environment.set('appId', uuidv4)", + "pm.environment.set('laaReference', `LAA-${randomString()}`)", + "", + "", + "function randomString() {", + " const dataSet = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'", + " const length = 6", + " let randomString = \"\";", + "", + " for (let i = 0; i < length; i++)", + " randomString += dataSet.charAt(Math.floor(Math.random() * dataSet.length));", + " return randomString;", + "}" ], "type": "text/javascript" } @@ -71,7 +83,7 @@ ], "body": { "mode": "raw", - "raw": "{\n \"application\": {\n \"id\": \"{{appId}}\",\n \"ufn\": \"123523/002\",\n \"office_code\": \"1A123B\",\n \"status\": \"submitted\",\n \"claim_type\": {\n \"value\": \"non_standard_magistrate\",\n \"en\": \"Non-standard magistrates' court payment\"\n },\n \"rep_order_date\": \"2023-01-01\",\n \"cntp_order\": null,\n \"cntp_date\": null,\n \"created_at\": \"2023-12-14T14:06:40.157Z\",\n \"updated_at\": \"2023-12-19T09:41:05.600Z\",\n \"reasons_for_claim\": [\n {\n \"value\": \"extradition\",\n \"en\": \"Extradition\"\n }\n ],\n \"representation_order_withdrawn_date\": null,\n \"reason_for_claim_other_details\": \"\",\n \"main_offence\": \"Assault (common)\",\n \"main_offence_date\": \"2022-11-02\",\n \"assigned_counsel\": \"no\",\n \"unassigned_counsel\": \"no\",\n \"agent_instructed\": \"no\",\n \"remitted_to_magistrate\": \"no\",\n \"plea\": {\n \"value\": \"guilty\",\n \"en\": \"Guilty plea\"\n },\n \"arrest_warrant_date\": null,\n \"cracked_trial_date\": null,\n \"first_hearing_date\": \"2023-01-01\",\n \"number_of_hearing\": 1,\n \"court\": \"Aldershot Magistrates' Court\",\n \"in_area\": \"no\",\n \"youth_court\": \"no\",\n \"hearing_outcome\": {\n \"value\": \"CP01\",\n \"en\": \"Arrest warrant issued/adjourned indefinitely\"\n },\n \"matter_type\": {\n \"value\": \"1\",\n \"en\": \"Offences against the person\"\n },\n \"prosecution_evidence\": 1,\n \"defence_statement\": 1,\n \"number_of_witnesses\": 1,\n \"supplemental_claim\": \"no\",\n \"time_spent\": null,\n \"other_info\": \"\",\n \"conclusion\": \"\",\n \"concluded\": \"no\",\n \"laa_reference\": \"LAA-x0PlnK\",\n \"work_before_date\": null,\n \"work_after_date\": null,\n \"signatory_name\": \"Test test\",\n \"gender\": {\n \"value\": null,\n \"en\": null\n },\n \"ethnic_group\": {\n \"value\": null,\n \"en\": null\n },\n \"disability\": {\n \"value\": null,\n \"en\": null\n },\n \"send_by_post\": true,\n \"remitted_to_magistrate_date\": null,\n \"preparation_time\": \"no\",\n \"work_before\": \"no\",\n \"work_after\": \"no\",\n \"has_disbursements\": \"yes\",\n \"is_other_info\": \"no\",\n \"answer_equality\": {\n \"value\": \"no\",\n \"en\": \"No, skip the equality questions\"\n },\n \"plea_category\": {\n \"value\": \"guilty_pleas\",\n \"en\": \"Category 1\"\n },\n \"submitted_total\": null,\n \"submitted_total_inc_vat\": null,\n \"adjusted_total\": null,\n \"adjusted_total_inc_vat\": null,\n \"letters_and_calls\": [\n {\n \"type\": {\n \"value\": \"letters\",\n \"en\": \"Letters\"\n },\n \"count\": 2,\n \"pricing\": 4.09,\n \"uplift\": null\n },\n {\n \"type\": {\n \"value\": \"calls\",\n \"en\": \"Calls\"\n },\n \"count\": 2,\n \"pricing\": 4.09,\n \"uplift\": null\n }\n ],\n \"disbursements\": [\n {\n \"id\": \"ef307a55-fa63-4532-80ba-ed7b86bcaabd\",\n \"disbursement_date\": \"1990-01-01\",\n \"disbursement_type\": {\n \"value\": \"bike\",\n \"en\": \"Bike\"\n },\n \"other_type\": {\n \"value\": null,\n \"en\": null\n },\n \"miles\": \"12.0\",\n \"total_cost_without_vat\": 3,\n \"details\": \"rest\",\n \"prior_authority\": \"yes\",\n \"apply_vat\": \"true\",\n \"vat_amount\": 0.6,\n \"pricing\": 0.25,\n \"vat_rate\": 0.2\n }\n ],\n \"work_items\": [\n {\n \"id\": \"805aad38-e9d2-43de-a359-76b687fb6105\",\n \"work_type\": {\n \"value\": \"attendance_without_counsel\",\n \"en\": \"Attendance without counsel\"\n },\n \"time_spent\": 60,\n \"completed_on\": \"2023-01-01\",\n \"fee_earner\": \"PK\",\n \"uplift\": 0,\n \"pricing\": 52.15\n }\n ],\n \"defendants\": [\n {\n \"id\": \"4d864db3-910b-4ed8-8139-f283ccf70329\",\n \"full_name\": \"Test name\",\n \"maat\": \"1234547\",\n \"position\": 1,\n \"main\": true\n }\n ],\n \"firm_office\": {\n \"name\": \"Test Firm\",\n \"account_number\": \"02DCY\",\n \"address_line_1\": \"Test address\",\n \"address_line_2\": \"\",\n \"town\": \"Bradford\",\n \"postcode\": \"BF34 4DS\",\n \"previous_id\": null,\n \"vat_registered\": \"yes\"\n },\n \"solicitor\": {\n \"full_name\": \"Test Solicitor\",\n \"reference_number\": \"02DXYD\",\n \"contact_full_name\": null,\n \"contact_email\": null,\n \"previous_id\": null\n },\n \"submitter\": {\n \"email\": \"provider@example.com\",\n \"description\": null\n },\n \"supporting_evidences\": []\n },\n \"application_id\": \"{{appId}}\",\n \"application_state\": \"submitted\",\n \"application_risk\": \"high\",\n \"application_type\": \"crm7\",\n \"json_schema_version\": 1\n}", + "raw": "{\n \"application\": {\n \"id\": \"{{appId}}\",\n \"ufn\": \"123523/002\",\n \"office_code\": \"1A123B\",\n \"status\": \"submitted\",\n \"claim_type\": {\n \"value\": \"non_standard_magistrate\",\n \"en\": \"Non-standard magistrates' court payment\"\n },\n \"rep_order_date\": \"2023-01-01\",\n \"cntp_order\": null,\n \"cntp_date\": null,\n \"created_at\": \"2023-12-14T14:06:40.157Z\",\n \"updated_at\": \"2023-12-19T09:41:05.600Z\",\n \"reasons_for_claim\": [\n {\n \"value\": \"extradition\",\n \"en\": \"Extradition\"\n }\n ],\n \"representation_order_withdrawn_date\": null,\n \"reason_for_claim_other_details\": \"\",\n \"main_offence\": \"Assault (common)\",\n \"main_offence_date\": \"2022-11-02\",\n \"assigned_counsel\": \"no\",\n \"unassigned_counsel\": \"no\",\n \"agent_instructed\": \"no\",\n \"remitted_to_magistrate\": \"no\",\n \"plea\": {\n \"value\": \"guilty\",\n \"en\": \"Guilty plea\"\n },\n \"arrest_warrant_date\": null,\n \"cracked_trial_date\": null,\n \"first_hearing_date\": \"2023-01-01\",\n \"number_of_hearing\": 1,\n \"court\": \"Aldershot Magistrates' Court\",\n \"in_area\": \"no\",\n \"youth_court\": \"no\",\n \"hearing_outcome\": {\n \"value\": \"CP01\",\n \"en\": \"Arrest warrant issued/adjourned indefinitely\"\n },\n \"matter_type\": {\n \"value\": \"1\",\n \"en\": \"Offences against the person\"\n },\n \"prosecution_evidence\": 1,\n \"defence_statement\": 1,\n \"number_of_witnesses\": 1,\n \"supplemental_claim\": \"no\",\n \"time_spent\": null,\n \"other_info\": \"\",\n \"conclusion\": \"\",\n \"concluded\": \"no\",\n \"laa_reference\": \"{{laaReference}}\",\n \"work_before_date\": null,\n \"work_after_date\": null,\n \"signatory_name\": \"Test test\",\n \"gender\": {\n \"value\": null,\n \"en\": null\n },\n \"ethnic_group\": {\n \"value\": null,\n \"en\": null\n },\n \"disability\": {\n \"value\": null,\n \"en\": null\n },\n \"send_by_post\": true,\n \"remitted_to_magistrate_date\": null,\n \"preparation_time\": \"no\",\n \"work_before\": \"no\",\n \"work_after\": \"no\",\n \"has_disbursements\": \"yes\",\n \"is_other_info\": \"no\",\n \"answer_equality\": {\n \"value\": \"no\",\n \"en\": \"No, skip the equality questions\"\n },\n \"plea_category\": {\n \"value\": \"guilty_pleas\",\n \"en\": \"Category 1\"\n },\n \"submitted_total\": null,\n \"submitted_total_inc_vat\": null,\n \"adjusted_total\": null,\n \"adjusted_total_inc_vat\": null,\n \"letters_and_calls\": [\n {\n \"type\": {\n \"value\": \"letters\",\n \"en\": \"Letters\"\n },\n \"count\": 2,\n \"pricing\": 4.09,\n \"uplift\": null\n },\n {\n \"type\": {\n \"value\": \"calls\",\n \"en\": \"Calls\"\n },\n \"count\": 2,\n \"pricing\": 4.09,\n \"uplift\": null\n }\n ],\n \"disbursements\": [\n {\n \"id\": \"ef307a55-fa63-4532-80ba-ed7b86bcaabd\",\n \"disbursement_date\": \"1990-01-01\",\n \"disbursement_type\": {\n \"value\": \"bike\",\n \"en\": \"Bike\"\n },\n \"other_type\": {\n \"value\": null,\n \"en\": null\n },\n \"miles\": \"12.0\",\n \"total_cost_without_vat\": 3.0,\n \"details\": \"rest\",\n \"prior_authority\": \"yes\",\n \"apply_vat\": \"true\",\n \"vat_amount\": 0.6,\n \"pricing\": 0.25,\n \"vat_rate\": 0.2\n }\n ],\n \"work_items\": [\n {\n \"id\": \"805aad38-e9d2-43de-a359-76b687fb6105\",\n \"work_type\": {\n \"value\": \"attendance_without_counsel\",\n \"en\": \"Attendance without counsel\"\n },\n \"time_spent\": 60,\n \"completed_on\": \"2023-01-01\",\n \"fee_earner\": \"PK\",\n \"uplift\": 0,\n \"pricing\": 52.15\n }\n ],\n \"defendants\": [\n {\n \"id\": \"4d864db3-910b-4ed8-8139-f283ccf70329\",\n \"full_name\": \"Test name\",\n \"maat\": \"1234547\",\n \"position\": 1,\n \"main\": true\n }\n ],\n \"firm_office\": {\n \"name\": \"Test Firm\",\n \"account_number\": \"02DCY\",\n \"address_line_1\": \"Test address\",\n \"address_line_2\": \"\",\n \"town\": \"Bradford\",\n \"postcode\": \"BF34 4DS\",\n \"previous_id\": null,\n \"vat_registered\": \"yes\"\n },\n \"solicitor\": {\n \"full_name\": \"Test Solicitor\",\n \"reference_number\": \"02DXYD\",\n \"contact_full_name\": null,\n \"contact_email\": null,\n \"previous_id\": null\n },\n \"submitter\": {\n \"email\": \"provider@example.com\",\n \"description\": null\n },\n \"supporting_evidences\": [],\n \"cost_totals\": [\n {\n \"id\": \"539ae8df-8f33-433d-9b85-29840f1aeef7\",\n \"cost_type\": \"core_costs\",\n \"amount\": 0.0,\n \"amount_with_vat\": 0.0\n },\n {\n \"id\": \"9fb87a45-22e5-4407-a1f6-0c781990ffc3\",\n \"cost_type\": \"travel_and_waiting\",\n \"amount\": 0.0,\n \"amount_with_vat\": 0.0\n },\n {\n \"id\": \"ab19a6ea-2677-46b8-8709-49f1ba650991\",\n \"cost_type\": \"disbursements\",\n \"amount\": 3.0,\n \"amount_with_vat\": 3.6\n }\n ],\n \"vat_rate\": 0.2\n },\n \"application_id\": \"{{appId}}\",\n \"application_state\": \"submitted\",\n \"application_risk\": \"high\",\n \"application_type\": \"crm7\",\n \"json_schema_version\": 1\n}", "options": { "raw": { "language": "json" @@ -146,7 +158,7 @@ ], "body": { "mode": "raw", - "raw": "{\n \"application\": {\n \"id\": \"{{appId}}\",\n \"ufn\": \"123523/002\",\n \"office_code\": \"1A123B\",\n \"status\": \"granted\",\n \"claim_type\": {\n \"value\": \"non_standard_magistrate\",\n \"en\": \"Non-standard magistrates' court payment\"\n },\n \"rep_order_date\": \"2023-01-01\",\n \"cntp_order\": null,\n \"cntp_date\": null,\n \"created_at\": \"2023-12-14T14:06:40.157Z\",\n \"updated_at\": \"2023-12-19T09:41:05.600Z\",\n \"reasons_for_claim\": [\n {\n \"value\": \"extradition\",\n \"en\": \"Extradition\"\n }\n ],\n \"representation_order_withdrawn_date\": null,\n \"reason_for_claim_other_details\": \"\",\n \"main_offence\": \"Assault (common)\",\n \"main_offence_date\": \"2022-11-02\",\n \"assigned_counsel\": \"no\",\n \"unassigned_counsel\": \"no\",\n \"agent_instructed\": \"no\",\n \"remitted_to_magistrate\": \"no\",\n \"plea\": {\n \"value\": \"guilty\",\n \"en\": \"Guilty plea\"\n },\n \"arrest_warrant_date\": null,\n \"cracked_trial_date\": null,\n \"first_hearing_date\": \"2023-01-01\",\n \"number_of_hearing\": 1,\n \"court\": \"Aldershot Magistrates' Court\",\n \"in_area\": \"no\",\n \"youth_court\": \"no\",\n \"hearing_outcome\": {\n \"value\": \"CP01\",\n \"en\": \"Arrest warrant issued/adjourned indefinitely\"\n },\n \"matter_type\": {\n \"value\": \"1\",\n \"en\": \"Offences against the person\"\n },\n \"prosecution_evidence\": 1,\n \"defence_statement\": 1,\n \"number_of_witnesses\": 1,\n \"supplemental_claim\": \"no\",\n \"time_spent\": null,\n \"other_info\": \"\",\n \"conclusion\": \"\",\n \"concluded\": \"no\",\n \"laa_reference\": \"LAA-x0PlnK\",\n \"work_before_date\": null,\n \"work_after_date\": null,\n \"signatory_name\": \"Test test\",\n \"gender\": {\n \"value\": null,\n \"en\": null\n },\n \"ethnic_group\": {\n \"value\": null,\n \"en\": null\n },\n \"disability\": {\n \"value\": null,\n \"en\": null\n },\n \"send_by_post\": true,\n \"remitted_to_magistrate_date\": null,\n \"preparation_time\": \"no\",\n \"work_before\": \"no\",\n \"work_after\": \"no\",\n \"has_disbursements\": \"yes\",\n \"is_other_info\": \"no\",\n \"answer_equality\": {\n \"value\": \"no\",\n \"en\": \"No, skip the equality questions\"\n },\n \"plea_category\": {\n \"value\": \"guilty_pleas\",\n \"en\": \"Category 1\"\n },\n \"submitted_total\": null,\n \"submitted_total_inc_vat\": null,\n \"adjusted_total\": null,\n \"adjusted_total_inc_vat\": null,\n \"letters_and_calls\": [\n {\n \"type\": {\n \"value\": \"letters\",\n \"en\": \"Letters\"\n },\n \"count\": 2,\n \"pricing\": 4.09,\n \"uplift\": null\n },\n {\n \"type\": {\n \"value\": \"calls\",\n \"en\": \"Calls\"\n },\n \"count\": 2,\n \"pricing\": 4.09,\n \"uplift\": null\n }\n ],\n \"disbursements\": [\n {\n \"id\": \"ef307a55-fa63-4532-80ba-ed7b86bcaabd\",\n \"disbursement_date\": \"1990-01-01\",\n \"disbursement_type\": {\n \"value\": \"bike\",\n \"en\": \"Bike\"\n },\n \"other_type\": {\n \"value\": null,\n \"en\": null\n },\n \"miles\": \"12.0\",\n \"total_cost_without_vat\": 3,\n \"details\": \"rest\",\n \"prior_authority\": \"yes\",\n \"apply_vat\": \"true\",\n \"vat_amount\": 0.6,\n \"pricing\": 0.25,\n \"vat_rate\": 0.2\n }\n ],\n \"work_items\": [\n {\n \"id\": \"805aad38-e9d2-43de-a359-76b687fb6105\",\n \"work_type\": {\n \"value\": \"attendance_without_counsel\",\n \"en\": \"Attendance without counsel\"\n },\n \"time_spent\": 60,\n \"completed_on\": \"2023-01-01\",\n \"fee_earner\": \"PK\",\n \"uplift\": 0,\n \"pricing\": 52.15\n }\n ],\n \"defendants\": [\n {\n \"id\": \"4d864db3-910b-4ed8-8139-f283ccf70329\",\n \"full_name\": \"Test name\",\n \"maat\": \"1234547\",\n \"position\": 1,\n \"main\": true\n }\n ],\n \"firm_office\": {\n \"name\": \"Test Firm\",\n \"account_number\": \"02DCY\",\n \"address_line_1\": \"Test address\",\n \"address_line_2\": \"\",\n \"town\": \"Bradford\",\n \"postcode\": \"BF34 4DS\",\n \"previous_id\": null,\n \"vat_registered\": \"yes\"\n },\n \"solicitor\": {\n \"full_name\": \"Test Solicitor\",\n \"reference_number\": \"02DXYD\",\n \"contact_full_name\": null,\n \"contact_email\": null,\n \"previous_id\": null\n },\n \"submitter\": {\n \"email\": \"provider@example.com\",\n \"description\": null\n },\n \"supporting_evidences\": []\n },\n \"application_id\": \"{{appId}}\",\n \"application_state\": \"granted\",\n \"application_risk\": \"high\",\n \"application_type\": \"crm7\",\n \"json_schema_version\": 1\n}", + "raw": "{\n \"application\": {\n \"id\": \"{{appId}}\",\n \"ufn\": \"123523/002\",\n \"office_code\": \"1A123B\",\n \"status\": \"granted\",\n \"claim_type\": {\n \"value\": \"non_standard_magistrate\",\n \"en\": \"Non-standard magistrates' court payment\"\n },\n \"rep_order_date\": \"2023-01-01\",\n \"cntp_order\": null,\n \"cntp_date\": null,\n \"created_at\": \"2023-12-14T14:06:40.157Z\",\n \"updated_at\": \"2023-12-19T09:41:05.600Z\",\n \"reasons_for_claim\": [\n {\n \"value\": \"extradition\",\n \"en\": \"Extradition\"\n }\n ],\n \"representation_order_withdrawn_date\": null,\n \"reason_for_claim_other_details\": \"\",\n \"main_offence\": \"Assault (common)\",\n \"main_offence_date\": \"2022-11-02\",\n \"assigned_counsel\": \"no\",\n \"unassigned_counsel\": \"no\",\n \"agent_instructed\": \"no\",\n \"remitted_to_magistrate\": \"no\",\n \"plea\": {\n \"value\": \"guilty\",\n \"en\": \"Guilty plea\"\n },\n \"arrest_warrant_date\": null,\n \"cracked_trial_date\": null,\n \"first_hearing_date\": \"2023-01-01\",\n \"number_of_hearing\": 1,\n \"court\": \"Aldershot Magistrates' Court\",\n \"in_area\": \"no\",\n \"youth_court\": \"no\",\n \"hearing_outcome\": {\n \"value\": \"CP01\",\n \"en\": \"Arrest warrant issued/adjourned indefinitely\"\n },\n \"matter_type\": {\n \"value\": \"1\",\n \"en\": \"Offences against the person\"\n },\n \"prosecution_evidence\": 1,\n \"defence_statement\": 1,\n \"number_of_witnesses\": 1,\n \"supplemental_claim\": \"no\",\n \"time_spent\": null,\n \"other_info\": \"\",\n \"conclusion\": \"\",\n \"concluded\": \"no\",\n \"laa_reference\": \"{{laaReference}}\",\n \"work_before_date\": null,\n \"work_after_date\": null,\n \"signatory_name\": \"Test test\",\n \"gender\": {\n \"value\": null,\n \"en\": null\n },\n \"ethnic_group\": {\n \"value\": null,\n \"en\": null\n },\n \"disability\": {\n \"value\": null,\n \"en\": null\n },\n \"send_by_post\": true,\n \"remitted_to_magistrate_date\": null,\n \"preparation_time\": \"no\",\n \"work_before\": \"no\",\n \"work_after\": \"no\",\n \"has_disbursements\": \"yes\",\n \"is_other_info\": \"no\",\n \"answer_equality\": {\n \"value\": \"no\",\n \"en\": \"No, skip the equality questions\"\n },\n \"plea_category\": {\n \"value\": \"guilty_pleas\",\n \"en\": \"Category 1\"\n },\n \"submitted_total\": null,\n \"submitted_total_inc_vat\": null,\n \"adjusted_total\": null,\n \"adjusted_total_inc_vat\": null,\n \"letters_and_calls\": [\n {\n \"type\": {\n \"value\": \"letters\",\n \"en\": \"Letters\"\n },\n \"count\": 2,\n \"pricing\": 4.09,\n \"uplift\": null\n },\n {\n \"type\": {\n \"value\": \"calls\",\n \"en\": \"Calls\"\n },\n \"count\": 2,\n \"pricing\": 4.09,\n \"uplift\": null\n }\n ],\n \"disbursements\": [\n {\n \"id\": \"ef307a55-fa63-4532-80ba-ed7b86bcaabd\",\n \"disbursement_date\": \"1990-01-01\",\n \"disbursement_type\": {\n \"value\": \"bike\",\n \"en\": \"Bike\"\n },\n \"other_type\": {\n \"value\": null,\n \"en\": null\n },\n \"miles\": \"12.0\",\n \"total_cost_without_vat\": 3.0,\n \"details\": \"rest\",\n \"prior_authority\": \"yes\",\n \"apply_vat\": \"true\",\n \"vat_amount\": 0.6,\n \"pricing\": 0.25,\n \"vat_rate\": 0.2\n }\n ],\n \"work_items\": [\n {\n \"id\": \"805aad38-e9d2-43de-a359-76b687fb6105\",\n \"work_type\": {\n \"value\": \"attendance_without_counsel\",\n \"en\": \"Attendance without counsel\"\n },\n \"time_spent\": 60,\n \"completed_on\": \"2023-01-01\",\n \"fee_earner\": \"PK\",\n \"uplift\": 0,\n \"pricing\": 52.15\n }\n ],\n \"defendants\": [\n {\n \"id\": \"4d864db3-910b-4ed8-8139-f283ccf70329\",\n \"full_name\": \"Test name\",\n \"maat\": \"1234547\",\n \"position\": 1,\n \"main\": true\n }\n ],\n \"firm_office\": {\n \"name\": \"Test Firm\",\n \"account_number\": \"02DCY\",\n \"address_line_1\": \"Test address\",\n \"address_line_2\": \"\",\n \"town\": \"Bradford\",\n \"postcode\": \"BF34 4DS\",\n \"previous_id\": null,\n \"vat_registered\": \"yes\"\n },\n \"solicitor\": {\n \"full_name\": \"Test Solicitor\",\n \"reference_number\": \"02DXYD\",\n \"contact_full_name\": null,\n \"contact_email\": null,\n \"previous_id\": null\n },\n \"submitter\": {\n \"email\": \"provider@example.com\",\n \"description\": null\n },\n \"supporting_evidences\": [],\n \"cost_totals\": [\n {\n \"id\": \"539ae8df-8f33-433d-9b85-29840f1aeef7\",\n \"cost_type\": \"core_costs\",\n \"amount\": 0.0,\n \"amount_with_vat\": 0.0\n },\n {\n \"id\": \"9fb87a45-22e5-4407-a1f6-0c781990ffc3\",\n \"cost_type\": \"travel_and_waiting\",\n \"amount\": 0.0,\n \"amount_with_vat\": 0.0\n },\n {\n \"id\": \"ab19a6ea-2677-46b8-8709-49f1ba650991\",\n \"cost_type\": \"disbursements\",\n \"amount\": 3.0,\n \"amount_with_vat\": 3.6\n }\n ],\n \"vat_rate\": 0.2\n },\n \"application_id\": \"{{appId}}\",\n \"application_state\": \"granted\",\n \"application_risk\": \"high\",\n \"application_type\": \"crm7\",\n \"json_schema_version\": 1\n}", "options": { "raw": { "language": "json" diff --git a/tests/routers/conftest.py b/tests/routers/conftest.py index 5454857c..17b50763 100644 --- a/tests/routers/conftest.py +++ b/tests/routers/conftest.py @@ -89,7 +89,7 @@ def seed_application(dbsession): app_id = uuid.uuid4() application = Application( id=app_id, - current_version=1, + current_version=2, application_state="submitted", application_risk="low", application_type="crm7", @@ -101,6 +101,12 @@ def seed_application(dbsession): json_schema_version=1, application={"id": 1}, ) - dbsession.add_all([application, version]) + version_2 = ApplicationVersion( + application_id=app_id, + version=2, + json_schema_version=1, + application={"id": 2}, + ) + dbsession.add_all([application, version, version_2]) dbsession.commit() return app_id diff --git a/tests/routers/v1/application_test.py b/tests/routers/v1/application_test.py index 5ba551b1..079931f7 100644 --- a/tests/routers/v1/application_test.py +++ b/tests/routers/v1/application_test.py @@ -64,6 +64,20 @@ def test_data_returns_200(client: TestClient, seed_application): assert response.status_code == 200 +def test_data_selected_version_returns_200(client: TestClient, seed_application): + response = client.get( + f"/v1/application/{seed_application}", params={"app_version": 2} + ) + assert response.status_code == 200 + + +def test_data_selected_version_returns_400(client: TestClient, seed_application): + response = client.get( + f"/v1/application/{seed_application}", params={"app_version": 3} + ) + assert response.status_code == 400 + + @patch("laa_crime_application_store_app.internal.notifier.Notifier.notify") def test_post_application_returns_200( mock_notify, client: TestClient, dbsession: Session @@ -147,9 +161,9 @@ def test_put_application_create_a_new_version( "application": {"id": 10, "plea": "guilty"}, }, ) - assert dbsession.query(ApplicationVersion).count() == 2 + assert dbsession.query(ApplicationVersion).count() == 3 application = dbsession.query(Application).first() - latest_version = dbsession.query(ApplicationVersion).filter_by(version=2).first() + latest_version = dbsession.query(ApplicationVersion).filter_by(version=3).first() assert latest_version.application == {"id": 10, "plea": "guilty"} assert (datetime.now() - application.updated_at) < timedelta(seconds=3) @@ -194,12 +208,12 @@ def test_put_application_returns_409_when_invalid_data( }, ) - assert dbsession.query(ApplicationVersion).count() == 1 + assert dbsession.query(ApplicationVersion).count() == 2 assert response.status_code == 409 @patch("laa_crime_application_store_app.internal.notifier.Notifier.notify") -def test_put_application_has_no_effect_if_data_is_unchnaged( +def test_put_application_has_no_effect_if_data_is_unchanged( mock_notify, client: TestClient, dbsession: Session, seed_application ): mock_notify.return_value = True @@ -212,11 +226,11 @@ def test_put_application_has_no_effect_if_data_is_unchnaged( "application_state": "submitted", "application_risk": "low", "application_type": "crm7", - "application": {"id": 1}, + "application": {"id": 2}, }, ) - assert dbsession.query(ApplicationVersion).count() == 1 + assert dbsession.query(ApplicationVersion).count() == 2 assert response.status_code == 201