def test_consent_get_with_consent(self):
        # Pre-Req
        Utils.send_consent_post(self.token)

        # Given
        expected_status_code = 200

        # When
        response = requests.get(
            url=config.REASONABLE_ADJUSTMENTS_CONSENT,
            params={
                'patient': config.TEST_PATIENT_NHS_NUMBER,
                'category': 'https://fhir.nhs.uk/STU3/CodeSystem/RARecord-FlagCategory-1|NRAF',
                'status': 'active',
                '_from': 'json'
            },
            headers={
                'Authorization': f'Bearer {self.token}',
                'nhsd-session-urid': config.TEST_NHSD_SESSION_URID,
                'x-request-id': str(uuid.uuid4()),
                'Accept': 'application/fhir+json'
            }
        )

        # Then
        result_dict = json.loads(response.text)
        assert_that(expected_status_code).is_equal_to(response.status_code)
        assert_that(result_dict['total']).is_equal_to(1)  # Validate patient record contains a consent flag
예제 #2
0
def setup(request):
    """This function is called before each test is executed"""

    # Get the name of the current test and attach it the the test instance
    name = (request.node.name,
            request.node.originalname)[request.node.originalname is not None]
    setattr(request.cls, "name", name)

    yield  # Handover to test
    time.sleep(1)

    # Teardown
    # Return patient to previous state

    if hasattr(request.cls, 'token'):
        # Call this regardless whether any flags exist
        Utils.send_raremoverecord_post(request.cls.token)

    try:
        # Close any lingering sessions
        request.cls.test.session.close()
    except AttributeError:
        # Probably failed during setup
        # so nothing to teardown
        pass
    def test_list_put(self):
        # Pre-Req
        Utils.send_consent_post(self.token)
        Utils.send_list_post(self.token)
        get_list_response = Utils.send_list_get(self.token)
        list_id = get_list_response['id']
        version_id = get_list_response['version']

        # Given
        expected_status_code = 200
        req_body = request_bank.get_body(Request.LIST_PUT)
        req_body['id'] = list_id

        # When
        response = requests.put(
            url=config.REASONABLE_ADJUSTMENTS_LIST + '/' + list_id,
            headers={
                'Authorization': f'Bearer {self.token}',
                'nhsd-session-urid': config.TEST_NHSD_SESSION_URID,
                'x-request-id': str(uuid.uuid4()),
                'content-type': 'application/fhir+json',
                'accept': 'application/fhir+json',
                'if-match': version_id,
            },
            data=json.dumps(req_body)
        )

        # Then
        assert_that(expected_status_code).is_equal_to(response.status_code)
    def test_flag_put(self):
        # Pre-Req: Patient has both a consent and flag
        Utils.send_consent_post(self.token)
        Utils.send_flag_post(self.token)
        get_flag_response = Utils.send_flag_get(self.token)

        # Given
        expected_status_code = 200
        flag_id = get_flag_response['id']
        version_id = get_flag_response['version']

        # When
        response = requests.put(
            url=config.REASONABLE_ADJUSTMENTS_FLAG + '/' + flag_id,
            headers={
                'Authorization': f'Bearer {self.token}',
                'nhsd-session-urid': config.TEST_NHSD_SESSION_URID,
                'x-request-id': str(uuid.uuid4()),
                'content-type': 'application/fhir+json',
                'Accept': 'application/fhir+json',
                'If-match': version_id,
            },
            json=request_bank.get_body(Request.FLAG_PUT)
        )

        # Then
        assert_that(expected_status_code).is_equal_to(response.status_code)
    def test_consent_put(self):
        # Pre-Req
        Utils.send_consent_post(self.token)

        # Given
        expected_status_code = 200

        # And
        consent = Utils.send_consent_get(self.token)
        consent_id = consent['id']
        version_id = consent['version']

        # When
        response = requests.put(
            url=config.REASONABLE_ADJUSTMENTS_CONSENT + '/' + consent_id,
            json=request_bank.get_body(Request.CONSENT_PUT),
            headers={
                'Authorization': f'Bearer {self.token}',
                'nhsd-session-urid': config.TEST_NHSD_SESSION_URID,
                'x-request-id': str(uuid.uuid4()),
                'content-type': 'application/fhir+json',
                'If-Match': version_id
            }
        )

        # Then
        assert_that(expected_status_code).is_equal_to(response.status_code)
    def test_valid_ods(self):
        # Given
        debug_session = ApigeeDebugApi(REASONABLE_ADJUSTMENTS_PROXY_NAME)
        expected_ods = 'D82106'

        # When
        Utils.send_request(self)

        # Then
        actual_ods = debug_session.get_apigee_variable(
            'verifyapikey.VerifyAPIKey.CustomAttributes.ods')

        assert_that(expected_ods).is_equal_to(actual_ods)
    def test_x_request_id_equals_nhsd_request_id(self):
        # Given
        debug_session = ApigeeDebugApi(
            config.REASONABLE_ADJUSTMENTS_PROXY_NAME)

        # When
        Utils.send_request(self)

        # Then
        trace_id = debug_session.get_apigee_header('NHSD-Request-ID')
        x_request_id = debug_session.get_apigee_header('x-request-id')

        assert_that(trace_id).is_equal_to(x_request_id)
    def test_jwt(self):
        # Given
        debug_session = ApigeeDebugApi(REASONABLE_ADJUSTMENTS_PROXY_NAME)
        expected_jwt_claims = {
            'reason_for_request': 'directcare',
            'scope': 'user/Consent.read',
            'requesting_organization':
            'https://fhir.nhs.uk/Id/ods-organization-code|D82106',
            'requesting_system':
            'https://fhir.nhs.uk/Id/accredited-system|200000001390',
            'requesting_user':
            f'https://fhir.nhs.uk/Id/sds-role-profile-id|{config.TEST_NHSD_SESSION_URID}',
            'sub':
            f'https://fhir.nhs.uk/Id/sds-role-profile-id|{config.TEST_NHSD_SESSION_URID}',
            'iss': 'http://api.service.nhs.uk',
            'aud': f'/{REASONABLE_ADJUSTMENTS_PROXY_PATH}/Consent'
        }

        # When
        Utils.send_request(self)

        # Then
        # We should pull Authorization header instead but Apigee mask that value so we get spineJwt variable instead
        actual_jwt = debug_session.get_apigee_variable('spineJwt')

        # We manually decode jwt because, jwt library requires all three segments but we only have two (no signature).
        jwt_segments = actual_jwt.split('.')
        actual_jwt_claims = json.loads(base64.b64decode(jwt_segments[1]))

        assert_that(expected_jwt_claims['reason_for_request']
                    ).is_equal_to_ignoring_case(
                        actual_jwt_claims['reason_for_request'])
        assert_that(expected_jwt_claims['scope']).is_equal_to_ignoring_case(
            actual_jwt_claims['scope'])
        assert_that(expected_jwt_claims['requesting_organization']
                    ).is_equal_to_ignoring_case(
                        actual_jwt_claims['requesting_organization'])
        assert_that(expected_jwt_claims['requesting_system']
                    ).is_equal_to_ignoring_case(
                        actual_jwt_claims['requesting_system'])
        assert_that(
            expected_jwt_claims['requesting_user']).is_equal_to_ignoring_case(
                actual_jwt_claims['requesting_user'])
        assert_that(expected_jwt_claims['sub']).is_equal_to_ignoring_case(
            actual_jwt_claims['sub'])
        assert_that(expected_jwt_claims['iss']).is_equal_to_ignoring_case(
            actual_jwt_claims['iss'])
        assert_that(expected_jwt_claims['aud']).is_equal_to_ignoring_case(
            actual_jwt_claims['aud'])
    def test_ASID_fetch(self):
        # Given
        debug_session = ApigeeDebugApi(
            config.REASONABLE_ADJUSTMENTS_PROXY_NAME)
        expected_value = '200000001390'

        # When
        Utils.send_request(self)

        # Then
        actual_asid = debug_session.get_apigee_variable(
            'verifyapikey.VerifyAPIKey.CustomAttributes.asid')
        assert_that(expected_value).is_equal_to(actual_asid)

        actual_header_value = debug_session.get_apigee_header('NHSD-ASID')
        assert_that(expected_value).is_equal_to(actual_header_value)
    def test_outgoing_request_contains_nhsd_correlation_id_header(self):
        # Given
        debug_session = ApigeeDebugApi(
            config.REASONABLE_ADJUSTMENTS_PROXY_NAME)

        # When
        Utils.send_request(self)

        # Then
        apigee_message_id = debug_session.get_apigee_variable('messageid')
        x_correlation_id_header = debug_session.get_apigee_header(
            'x-correlation-id')
        x_request_id_header = debug_session.get_apigee_header('x-request-id')
        nhsd_correlation_id_header = debug_session.get_apigee_header(
            'NHSD-Correlation-ID')

        expected_correlation_id_header = x_request_id_header + '.' + x_correlation_id_header + '.' + apigee_message_id

        assert_that(expected_correlation_id_header).is_equal_to(
            nhsd_correlation_id_header)
    def test_remove_ra_record_post(self):
        # Pre_Req : Patient record with a consent
        Utils.send_consent_post(self.token)

        # Given
        expected_status_code = 200

        # When
        response = requests.post(
            url=config.REASONABLE_ADJUSTMENTS_REMOVE_RA_RECORD,
            headers={
                'Authorization': f'Bearer {self.token}',
                'nhsd-session-urid': config.TEST_NHSD_SESSION_URID,
                'x-request-id': str(uuid.uuid4()),
                'content-type': 'application/fhir+json',
                'If-Match': 'W/"1"'
            },
            json=request_bank.get_body(Request.REMOVE_RA_RECORD_POST))

        # Then
        assert_that(expected_status_code).is_equal_to(response.status_code)
    def test_list_post(self):
        # Pre-Req - Patient has consent
        Utils.send_consent_post(self.token)

        # Given
        expected_status_code = 201

        # When
        response = requests.post(url=config.REASONABLE_ADJUSTMENTS_LIST,
                                 headers={
                                     'Authorization': f'Bearer {self.token}',
                                     'nhsd-session-urid':
                                     config.TEST_NHSD_SESSION_URID,
                                     'x-request-id': str(uuid.uuid4()),
                                     'content-type': 'application/fhir+json',
                                     'Accept': 'application/fhir+json'
                                 },
                                 json=request_bank.get_body(Request.LIST_POST))

        # Then
        assert_that(expected_status_code).is_equal_to(response.status_code)
    def test_missing_asid(self):
        # Given
        expected_status_code = 500
        expected_response = {
            'resourceType':
            'OperationOutcome',
            'issue': [{
                'severity':
                'error',
                'code':
                'value',
                'details': {
                    'coding': [{
                        'system':
                        'https://fhir.nhs.uk/R4/CodeSystem/Spine-ErrorOrWarningCode',
                        'version': '1',
                        'code': 'INTERNAL_SERVER_ERROR',
                        'display': 'Unexpected internal server error'
                    }]
                },
                'diagnostics':
                'An internal server error occurred. Missing ASID. Contact us for assistance diagnosing this issue: https://digital.nhs.uk/developer/help-and-support quoting Message ID'
            }]
        }

        # When
        response = Utils.send_request(self)

        actual_response = json.loads(response.text)

        # Then
        assert_that(expected_status_code).is_equal_to(response.status_code)
        # assert_that(actual_response['message_id']).is_not_empty()
        assert_that(
            expected_response['resourceType']).is_equal_to_ignoring_case(
                actual_response['resourceType'])
        assert_that(
            expected_response['issue'][0]['code']).is_equal_to_ignoring_case(
                actual_response['issue'][0]['code'])
        assert_that(
            expected_response['issue'][0]['details']['coding'][0]
            ['code']).is_equal_to_ignoring_case(
                actual_response['issue'][0]['details']['coding'][0]['code'])
        assert_that(
            expected_response['issue'][0]['details']['coding'][0]
            ['display']).is_equal_to_ignoring_case(
                actual_response['issue'][0]['details']['coding'][0]['display'])
        assert_that(expected_response['issue'][0]
                    ['diagnostics']).is_equal_to_ignoring_case(
                        actual_response['issue'][0]['diagnostics'])
    def test_duplicate_consent_record(self):
        # Pre-Req
        Utils.send_consent_post(self.token)

        # Given
        expected_status_code = 422

        # When
        response = requests.post(url=config.REASONABLE_ADJUSTMENTS_CONSENT,
                                 json=request_bank.get_body(
                                     Request.CONSENT_POST),
                                 headers={
                                     'Authorization': f'Bearer {self.token}',
                                     'nhsd-session-urid': '093895563513',
                                     'x-request-id': str(uuid.uuid4()),
                                     'content-type': 'application/fhir+json',
                                     'Accept': 'application/fhir+json'
                                 })

        response_body = json.loads(response.text)

        # Then
        assert_that(expected_status_code).is_equal_to(response.status_code)
        assert_that('duplicate').is_equal_to(response_body['issue'][0]['code'])
    def test_missing_ods(self):
        # Given
        expected_status_code = 500
        expected_response = {
            'error':
            'missing ODS',
            'error_description':
            'An internal server error occurred. Missing ODS. Contact us for assistance diagnosing this issue: https://digital.nhs.uk/developer/help-and-support quoting Message ID',
        }

        # When
        response = Utils.send_request(self)
        actual_response = json.loads(response.text)

        # Then
        assert_that(expected_status_code).is_equal_to(response.status_code)
        assert_that(actual_response['message_id']).is_not_empty()
        assert_that(expected_response['error']).is_equal_to_ignoring_case(
            actual_response['error'])
        assert_that(
            expected_response['error_description']).is_equal_to_ignoring_case(
                actual_response['error_description'])