|
| 1 | +import json |
| 2 | + |
| 3 | +from features.environment import CIS2_USERS |
| 4 | + |
| 5 | +from methods.api.common_api_methods import get, get_headers |
| 6 | +from methods.shared.common import assert_that |
| 7 | + |
| 8 | +PRESCRIPTION_ID_NOT_EXIST = "111111-222222-333333" |
| 9 | + |
| 10 | + |
| 11 | +def get_prescription_list(context, identifier): |
| 12 | + match identifier.lower(): |
| 13 | + case "nhs number": |
| 14 | + url = f"{context.cpts_fhir_base_url}/RequestGroup?nhsNumber={context.nhs_number}" |
| 15 | + case "prescription id": |
| 16 | + url = f"{context.cpts_fhir_base_url}/RequestGroup?prescriptionId={context.prescription_id}" |
| 17 | + case "nhs number and prescription id": |
| 18 | + url = ( |
| 19 | + f"{context.cpts_fhir_base_url}/RequestGroup?prescriptionId={context.prescription_id}" |
| 20 | + f"&nhsNumber={context.nhs_number}" |
| 21 | + ) |
| 22 | + case _: |
| 23 | + raise AssertionError("Unknown Identifier {}".format(identifier)) |
| 24 | + |
| 25 | + print(url) |
| 26 | + additional_headers = { |
| 27 | + "Content-Type": "application/json", |
| 28 | + "nhsd-organization-uuid": "A83008", |
| 29 | + "nhsd-session-jobrole": "S0030:G0100:R0570", |
| 30 | + } |
| 31 | + headers = get_headers(context, context.auth_method, additional_headers) |
| 32 | + |
| 33 | + context.response = get(url=url, context=context, headers=headers) |
| 34 | + |
| 35 | + |
| 36 | +def assert_prescription_list(context): |
| 37 | + json_response = json.loads(context.response.content) |
| 38 | + expected_nhs_number = context.nhs_number |
| 39 | + expected_prescription_id = context.prescription_id |
| 40 | + assert_that( |
| 41 | + json_response["entry"][0]["resource"]["identifier"][0]["value"] |
| 42 | + ).is_equal_to(expected_nhs_number) |
| 43 | + assert_that( |
| 44 | + json_response["entry"][1]["resource"]["identifier"][0]["value"] |
| 45 | + ).is_equal_to(expected_prescription_id) |
| 46 | + |
| 47 | + |
| 48 | +def assert_empty_prescription_list(context): |
| 49 | + json_response = json.loads(context.response.content) |
| 50 | + assert_that(len(json_response["entry"])).is_equal_to(0) |
| 51 | + |
| 52 | + |
| 53 | +def assert_both_identifier_error(context): |
| 54 | + json_response = json.loads(context.response.content) |
| 55 | + assert_that(json_response["issue"][0]["diagnostics"]).is_equal_to( |
| 56 | + "Invalid query string parameters; only prescriptionId or nhsNumber must be provided, not both." |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def get_prescription_details(context, issue_number): |
| 61 | + query_params = f"?issueNumber={issue_number}" if issue_number else "" |
| 62 | + url = f"{context.cpts_fhir_base_url}/RequestGroup/{context.prescription_id}{query_params}" |
| 63 | + print(url) |
| 64 | + additional_headers = { |
| 65 | + "Content-Type": "application/json", |
| 66 | + "nhsd-organization-uuid": "A83008", |
| 67 | + "nhsd-session-urid": CIS2_USERS["prescriber"]["role_id"], |
| 68 | + "nhsd-session-jobrole": "S0030:G0100:R0570", |
| 69 | + } |
| 70 | + headers = get_headers(context, context.auth_method, additional_headers) |
| 71 | + |
| 72 | + context.response = get(url=url, context=context, headers=headers) |
| 73 | + |
| 74 | + |
| 75 | +def assert_prescription_details(context, issue_number): |
| 76 | + json_response = json.loads(context.response.content) |
| 77 | + expected_nhs_number = context.nhs_number |
| 78 | + expected_prescription_id = context.prescription_id |
| 79 | + patient_resource = next( |
| 80 | + bundle_entry |
| 81 | + for bundle_entry in json_response["entry"] |
| 82 | + if bundle_entry["resource"]["resourceType"] == "Patient" |
| 83 | + ) |
| 84 | + request_group = next( |
| 85 | + bundle_entry |
| 86 | + for bundle_entry in json_response["entry"] |
| 87 | + if bundle_entry["resource"]["resourceType"] == "RequestGroup" |
| 88 | + ) |
| 89 | + |
| 90 | + assert_that(patient_resource["resource"]["identifier"][0]["value"]).is_equal_to( |
| 91 | + expected_nhs_number |
| 92 | + ) |
| 93 | + assert_that(request_group["resource"]["identifier"][0]["value"]).is_equal_to( |
| 94 | + expected_prescription_id |
| 95 | + ) |
| 96 | + if issue_number: |
| 97 | + repeat_information = next( |
| 98 | + extension |
| 99 | + for extension in request_group["resource"]["extension"] |
| 100 | + if extension["url"] |
| 101 | + == "https://fhir.nhs.uk/StructureDefinition/Extension-EPS-RepeatInformation" |
| 102 | + ) |
| 103 | + number_of_repeats_issued = next( |
| 104 | + extension |
| 105 | + for extension in repeat_information["extension"] |
| 106 | + if extension["url"] == "numberOfRepeatsIssued" |
| 107 | + ) |
| 108 | + assert_that(number_of_repeats_issued["valueInteger"]).is_equal_to(issue_number) |
| 109 | + |
| 110 | + |
| 111 | +def get_prescription_not_found(context): |
| 112 | + url = f"{context.cpts_fhir_base_url}/RequestGroup/{PRESCRIPTION_ID_NOT_EXIST}" |
| 113 | + print(url) |
| 114 | + additional_headers = { |
| 115 | + "Content-Type": "application/json", |
| 116 | + "nhsd-organization-uuid": "A83008", |
| 117 | + "nhsd-session-urid": CIS2_USERS["prescriber"]["role_id"], |
| 118 | + "nhsd-session-jobrole": "S0030:G0100:R0570", |
| 119 | + } |
| 120 | + headers = get_headers(context, context.auth_method, additional_headers) |
| 121 | + |
| 122 | + context.response = get(url=url, context=context, headers=headers) |
| 123 | + |
| 124 | + |
| 125 | +def assert_prescription_not_found(context): |
| 126 | + json_response = json.loads(context.response.content) |
| 127 | + print(json_response) |
| 128 | + assert_that(json_response["issue"][0]["code"]).is_equal_to("not-found") |
| 129 | + |
| 130 | + |
| 131 | +def get_path_parameter_not_provided(context): |
| 132 | + url = f"{context.cpts_fhir_base_url}/RequestGroup/" |
| 133 | + print(url) |
| 134 | + |
| 135 | + headers = get_headers(context, context.auth_method) |
| 136 | + |
| 137 | + context.response = get(url=url, context=context, headers=headers) |
| 138 | + |
| 139 | + |
| 140 | +def assert_path_parameter_not_provided(context): |
| 141 | + json_response = json.loads(context.response.content) |
| 142 | + print(json_response) |
| 143 | + assert_that(json_response["issue"][0]["code"]).is_equal_to("value") |
0 commit comments