コード例 #1
0
def test_validation_does_not_change_metadata(
        fake_metadata_full, fake_questionnaire_metadata_requirements_full):
    fake_metadata_copy = deepcopy(fake_metadata_full)
    validate_questionnaire_claims(
        fake_metadata_full, fake_questionnaire_metadata_requirements_full)

    assert fake_metadata_full == fake_metadata_copy
コード例 #2
0
def test_validation_no_error_when_optional_field_not_passed(
        fake_metadata_runner):
    field_specification = [{
        "name": "optional_field",
        "type": "string",
        "optional": True
    }]

    validate_questionnaire_claims(fake_metadata_runner, field_specification)
コード例 #3
0
def test_maximum_length(fake_metadata_runner):
    field_specification = [{
        "name": "some_field",
        "type": "string",
        "max_length": 5
    }]

    fake_metadata_runner["some_field"] = "1234"

    validate_questionnaire_claims(fake_metadata_runner, field_specification)

    fake_metadata_runner["some_field"] = "123456"

    with pytest.raises(ValidationError):
        validate_questionnaire_claims(fake_metadata_runner,
                                      field_specification)
コード例 #4
0
def test_deserialisation_iso_8601_dates(fake_metadata_runner):
    """Runner cannot currently handle date objects in metadata"""
    field_specification = [{"name": "birthday", "type": "date"}]

    fake_metadata_runner["birthday"] = "2019-11-1"
    claims = validate_questionnaire_claims(fake_metadata_runner,
                                           field_specification)

    assert isinstance(claims["birthday"], str)
コード例 #5
0
def login():
    """
    Initial url processing - expects a token parameter and then will authenticate this token. Once authenticated
    it will be placed in the users session
    :return: a 302 redirect to the next location for the user
    """
    # logging in again clears any session state
    if cookie_session:
        cookie_session.clear()

    decrypted_token = decrypt_token(request.args.get("token"))

    validate_jti(decrypted_token)

    try:
        runner_claims = validate_runner_claims(decrypted_token)
    except ValidationError as e:
        raise InvalidTokenException("Invalid runner claims") from e
    # pylint: disable=assigning-non-slot
    g.schema = load_schema_from_metadata(runner_claims)
    schema_metadata = g.schema.json["metadata"]

    try:
        questionnaire_claims = validate_questionnaire_claims(
            decrypted_token, schema_metadata
        )
    except ValidationError as e:
        raise InvalidTokenException("Invalid questionnaire claims") from e

    claims = {**runner_claims, **questionnaire_claims}

    schema_name = claims["schema_name"]
    tx_id = claims["tx_id"]
    ru_ref = claims["ru_ref"]
    case_id = claims["case_id"]

    logger.bind(
        schema_name=schema_name,
        tx_id=tx_id,
        ru_ref=ru_ref,
        case_id=case_id,
    )
    logger.info("decrypted token and parsed metadata")

    store_session(claims)

    cookie_session["theme"] = g.schema.json["theme"]
    cookie_session["survey_title"] = g.schema.json["title"]
    cookie_session["expires_in"] = get_session_timeout_in_seconds(g.schema)

    if account_service_url := claims.get("account_service_url"):
        cookie_session["account_service_base_url"] = account_service_url
コード例 #6
0
 def parse_metadata(claims, schema_metadata):
     runner_claims = validate_runner_claims(claims)
     questionnaire_claims = validate_questionnaire_claims(
         claims, schema_metadata)
     return {**runner_claims, **questionnaire_claims}
コード例 #7
0
def test_validation_field_required_by_default(fake_metadata_runner):
    field_specification = [{"name": "required_field", "type": "string"}]

    with pytest.raises(ValidationError):
        validate_questionnaire_claims(fake_metadata_runner,
                                      field_specification)