Ejemplo n.º 1
0
def test_no_census_params_and_no_schema_name_raises_error(fake_census_metadata_runner):
    fake_census_metadata_runner.pop("schema_name", None)
    fake_census_metadata_runner.pop("region_code", None)
    fake_census_metadata_runner.pop("form_type", None)
    fake_census_metadata_runner.pop("survey", None)

    with pytest.raises(ValidationError):
        validate_runner_claims(fake_census_metadata_runner)
Ejemplo n.º 2
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

    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 claims.get("account_service_url"):
        cookie_session["account_service_url"] = claims.get(
            "account_service_url")

    if claims.get("account_service_log_out_url"):
        cookie_session["account_service_log_out_url"] = claims.get(
            "account_service_log_out_url")

    return redirect(url_for("questionnaire.get_questionnaire"))
Ejemplo n.º 3
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}
Ejemplo n.º 4
0
def test_empty_strings_are_not_valid(fake_metadata_runner):
    fake_metadata_runner["schema_name"] = ""

    with pytest.raises(ValidationError):
        validate_runner_claims(fake_metadata_runner)
Ejemplo n.º 5
0
def test_business_params_without_schema_name(fake_business_metadata_runner):
    claims = validate_runner_claims(fake_business_metadata_runner)

    assert claims["schema_name"] == "mbs_0253"
Ejemplo n.º 6
0
def test_minimum_length_on_runner_metadata(fake_metadata_runner):
    validate_runner_claims(fake_metadata_runner)

    fake_metadata_runner["schema_name"] = ""
    with pytest.raises(ValidationError):
        validate_runner_claims(fake_metadata_runner)
Ejemplo n.º 7
0
def test_unknown_claims_are_not_deserialized(fake_metadata_runner):
    fake_metadata_runner["unknown_key"] = "some value"
    claims = validate_runner_claims(fake_metadata_runner)
    assert "unknown_key" not in claims
Ejemplo n.º 8
0
def test_uuid_deserialisation(fake_metadata_runner):
    claims = validate_runner_claims(fake_metadata_runner)

    assert isinstance(claims["tx_id"], str)
Ejemplo n.º 9
0
def test_spaces_are_stripped_from_string_fields(fake_metadata_runner):
    fake_metadata_runner["collection_exercise_sid"] = "  stripped     "

    output = validate_runner_claims(fake_metadata_runner)

    assert output["collection_exercise_sid"] == "stripped"
Ejemplo n.º 10
0
def test_bad_region_code_parameter(fake_census_metadata_runner):
    fake_census_metadata_runner["region_code"] = "bad_region_code"
    with pytest.raises(ValidationError):
        validate_runner_claims(fake_census_metadata_runner)
Ejemplo n.º 11
0
def test_bad_form_type_parameter(fake_census_metadata_runner):
    fake_census_metadata_runner["form_type"] = "bad_form_type"
    with pytest.raises(ValidationError):
        validate_runner_claims(fake_census_metadata_runner)
Ejemplo n.º 12
0
def test_bad_survey_parameter(fake_census_metadata_runner):
    fake_census_metadata_runner["survey"] = "bad_survey"
    with pytest.raises(ValidationError):
        validate_runner_claims(fake_census_metadata_runner)
Ejemplo n.º 13
0
def test_survey_parameter_allows_ccs(fake_census_metadata_runner):
    fake_census_metadata_runner["survey"] = "CCS"
    claims = validate_runner_claims(fake_census_metadata_runner)

    assert claims["schema_name"] == "ccs_individual_gb_eng"
Ejemplo n.º 14
0
def test_survey_parameter_defaults_to_census(fake_census_metadata_runner):
    del fake_census_metadata_runner["survey"]
    claims = validate_runner_claims(fake_census_metadata_runner)

    assert claims["schema_name"] == "census_individual_gb_eng"
Ejemplo n.º 15
0
def test_census_params_without_schema_name(fake_census_metadata_runner):
    claims = validate_runner_claims(fake_census_metadata_runner)

    assert claims["schema_name"] == "census_individual_gb_eng"