def test_get_codes(mock_database, caplog, case_data: CaseDataGetter):
    (
        test_data,
        code,
        key,
        expected_result,
        expected_result_count,
        expected_logger_message,
    ) = case_data.get()
    # Set up test data
    insert_test_data(test_data=test_data)

    # Run test function
    db = boto3.resource("dynamodb")
    result = code_generator.get_codes(database=db, code=code, key=key)

    assert len(result) == expected_result_count

    if expected_result_count == 0:
        with caplog.at_level(logging.INFO):
            assert expected_logger_message in caplog.text

    for row in result:

        assert isinstance(row["active"], bool)
        if code:
            assert code == row["code"]
        if key:
            assert key["lpa"] == row["lpa"]
            assert key["actor"] == row["actor"]

    remove_test_data(test_data)
def test_update_codes_by_key(mock_database, case_data: CaseDataGetter):
    test_data, key, code, status, expected_result = case_data.get()

    before_test_data = insert_test_data(test_data=test_data)

    # Set some expectations
    should_get_updated = [r["code"] for r in before_test_data if r["active"] != status]
    status_should_be = status
    last_updated_date_should_be = date_formatter(datetime.datetime.now())

    # Run test function
    db = boto3.resource("dynamodb")
    test_result = update_codes(database=db, key=key, code=code, status=status)

    # Test db contents after function

    after_test_data = get_codes(database=db, key=key, code=code)

    for row in after_test_data:
        assert isinstance(row["active"], bool)
        assert row["active"] == status_should_be
        if row["code"] in should_get_updated:
            assert row["last_updated_date"] == last_updated_date_should_be

    assert test_result == expected_result

    remove_test_data(test_data)
Exemplo n.º 3
0
def test_checklist_put(server, case_data: CaseDataGetter):
    (
        test_data,
        test_headers,
        test_report_id,
        test_checklist_id,
        test_case_ref,
        expected_response_status_code,
        expected_response_data,
    ) = case_data.get()

    with server.app_context():

        print(f"test_case_ref: {test_case_ref}")
        print(f"test_report_id: {test_report_id}")
        print(f"test_data: {test_data}")

        r = requests.put(
            f"{server.url}/clients/{test_case_ref}/reports/"
            f"{test_checklist_id}/checklists/{test_checklist_id}",
            headers=test_headers,
            data=json.dumps(test_data),
        )

        assert r.status_code == expected_response_status_code
        assert r.json() == expected_response_data
Exemplo n.º 4
0
def test_supporting_docs_errors(server, case_data: CaseDataGetter):
    (
        test_data,
        test_headers,
        test_report_id,
        test_case_ref,
        expected_response_status_code,
        expected_response_data,
    ) = case_data.get()

    with server.app_context():

        print(f"test_case_ref: {test_case_ref}")
        print(f"test_report_id: {test_report_id}")
        print(f"test_data: {test_data}")

        r = requests.post(
            f"{server.url}/clients/{test_case_ref}/reports/"
            f"{test_report_id}/supportingdocuments",
            headers=test_headers,
            data=json.dumps(test_data),
        )

        assert r.status_code == expected_response_status_code
        if r.status_code in [400]:
            assert r.json()["body"]["error"]["code"] == expected_response_data
def test_post(mock_database, mock_generate_code, case_data: CaseDataGetter):
    test_data, data, expected_result, expected_status_code = case_data.get()

    result, status_code = handle_create(data=data)

    assert result == expected_result
    assert status_code == expected_status_code

    if expected_result["codes"] is not None:
        remove_test_data(expected_result["codes"])
Exemplo n.º 6
0
def test_validate_payload(case_data: CaseDataGetter):
    body, case_ref, report_id, expected_result = case_data.get()
    path_params = {"caseref": case_ref, "id": report_id}
    event = build_aws_event(event_body=json.dumps(body),
                            event_path_parementers=path_params,
                            as_json=False)

    valid_event, errors = validate_event(event)

    assert valid_event == expected_result[0]
    assert sorted(errors) == sorted(expected_result[1])
def test_post(mock_database, case_data: CaseDataGetter):
    test_data, data, expected_result, expected_status_code = case_data.get()
    # Set up test data
    insert_test_data(test_data=test_data)

    result, status_code = handle_validate(data=data)

    assert result == expected_result
    assert status_code == expected_status_code

    remove_test_data(test_data)
def test_submit_errors(monkeypatch, case_data: CaseDataGetter):
    (data, method, endpoint, url_params, expected_responses) = case_data.get()

    status_code, response = new_submit_document_to_sirius(
        data=data, method=method, endpoint=endpoint, url_params=url_params)

    print(f"status_code: {status_code}")
    print(f"response: {response}")

    assert (any(message in response
                for message in expected_responses[status_code]) is True)
def test_check_code_unique(mock_database, caplog, case_data: CaseDataGetter):
    test_data, code, logger_message, expected_result = case_data.get()

    insert_test_data(test_data=test_data)
    db = boto3.resource("dynamodb")
    result = check_code_unique(database=db, code=code)

    assert result == expected_result
    with caplog.at_level(logging.INFO):
        assert logger_message in caplog.text

    remove_test_data(test_data)
def test_data(mock_database, mock_generate_code, case_data: CaseDataGetter):
    test_data, data, expected_result, expected_status_code = case_data.get()

    insert_test_data(test_data=test_data)

    codes_created, status_code = handle_create(data=data)

    for i, item in enumerate(data):

        try:
            lpa = data["lpas"][i]["lpa"]

            actor = data["lpas"][i]["actor"]

            dob = data["lpas"][i]["dob"]
        except KeyError:
            assert codes_created == expected_result
            assert status_code == expected_status_code
            break

        if "" not in [lpa, actor, dob]:

            db = boto3.resource("dynamodb")
            table = db.Table(lpa_codes_table())

            query_result = table.query(
                IndexName="key_index",
                KeyConditionExpression=Key("lpa").eq(lpa) & Key("actor").eq(actor),
            )

            print(f"query_result: {query_result}")

            for item in query_result["Items"]:

                if item["code"] == test_constants["DEFAULT_CODE"]:
                    assert item["dob"] == dob
                    assert item["active"] is True
                    assert item["status_details"] == "Generated"
                    assert item["last_updated_date"] == test_constants["TODAY_ISO"]
                    assert item["generated_date"] == test_constants["TODAY_ISO"]
                    assert item["expiry_date"] == test_constants["EXPIRY_DATE"]
                else:
                    assert item["active"] is False
                    assert item["status_details"] == "Superseded"
        else:
            assert codes_created == expected_result
            break

    if expected_result["codes"] is not None:
        remove_test_data(expected_result["codes"] + test_data)
    else:
        remove_test_data(test_data)
def test_new_format_sirius_response(case_data: CaseDataGetter):
    (
        sirius_response_code,
        sirius_response,
        api_response_code,
        api_response,
    ) = case_data.get()

    formatted_status_code, formatted_response_text = format_sirius_success(
        sirius_response_code, sirius_response)

    assert formatted_response_text == api_response
    assert formatted_status_code == api_response_code
def test_get_codes_broken(
    mock_database,
    mock_generate_code,
    broken_get_code,
    caplog,
    case_data: CaseDataGetter,
):
    test_data, data, expected_result, expected_status_code = case_data.get()

    result, status_code = handle_create(data=data)

    assert status_code == 500
    with caplog.at_level(logging.ERROR):
        assert "get_codes" in caplog.text
def test_submit_success(monkeypatch, case_data: CaseDataGetter):
    (
        data,
        method,
        endpoint,
        url_params,
        expected_status_code,
        expected_response,
    ) = case_data.get()

    status_code, response = new_submit_document_to_sirius(
        data=data, method=method, endpoint=endpoint, url_params=url_params)

    assert response == expected_response
Exemplo n.º 14
0
def test_bad_payload(case_data: CaseDataGetter, test_config):
    (url, method, payload, expected_status_code) = case_data.get(test_config)

    status, response = send_a_request(url=url,
                                      method=method,
                                      payload=payload,
                                      test_config=test_config)

    assert status == expected_status_code
    response_data = json.loads(response)
    if "errors" in response_data:
        for error in response_data["errors"]:
            assert error["code"] == "OPGDATA-API-INVALIDREQUEST"
    else:
        assert response_data["body"]["error"][
            "code"] == "OPGDATA-API-INVALIDREQUEST"
def test_new_format_sirius_response_error(caplog, case_data: CaseDataGetter):
    (
        sirius_response_code,
        sirius_response,
        error_details,
        api_response_code,
        api_response,
    ) = case_data.get()

    formatted_status_code, formatted_response_text = handle_sirius_error(
        sirius_response_code, sirius_response, error_details)
    print(f"formatted_response_text: {formatted_response_text}")
    assert formatted_response_text == api_response
    assert formatted_status_code == api_response_code

    with caplog.at_level(logging.ERROR):
        assert api_response in caplog.text
def test_submit_env_vars_broken(monkeypatch, case_data: CaseDataGetter):
    (
        data,
        method,
        endpoint,
        url_params,
        env_var,
        expected_status_code,
        expected_response,
    ) = case_data.get()

    if env_var:
        monkeypatch.delenv(env_var)

    status_code, response = new_submit_document_to_sirius(
        data=data, method=method, endpoint=endpoint, url_params=url_params)

    assert response == expected_response
def test_post(mock_database, case_data: CaseDataGetter):
    (
        test_data,
        data,
        expected_result,
        expected_last_updated_date,
        expected_status_code,
    ) = case_data.get()
    # Set up test data
    insert_test_data(test_data=test_data)

    # Perform revoke & check return
    result, status_code = handle_revoke(data=data)
    assert result == expected_result
    assert status_code == expected_status_code

    # Check the data after revoke has been performed
    db = boto3.resource("dynamodb")
    after_revoke = code_generator.get_codes(database=db, code=data["code"])

    if after_revoke:
        lpa = after_revoke[0]["lpa"]
        actor = after_revoke[0]["actor"]

        db = boto3.resource("dynamodb")
        table = db.Table(lpa_codes_table())

        query_result = table.query(
            IndexName="key_index",
            KeyConditionExpression=Key("lpa").eq(lpa) & Key("actor").eq(actor),
        )

        row_data = query_result["Items"][0]

        if data["code"] == row_data["code"]:
            assert row_data["last_updated_date"] == expected_last_updated_date
            assert row_data["status_details"] in [
                "Revoked",
                "Superseded",
            ]
            assert row_data["active"] is False

    remove_test_data(test_data)
Exemplo n.º 18
0
def test_custom_errors(server, case_data: CaseDataGetter):
    (
        test_url,
        test_headers,
        test_data,
        test_method,
        expected_response_status_code,
        expected_response_data,
    ) = case_data.get()

    test_url = test_url["url"]

    with server.app_context():

        if test_method == "NOT POST":
            r = requests.get(url=f"{server.url}/{test_url}")
        elif test_method == "PUT":
            data = test_data

            r = requests.put(
                f"{server.url}/clients/{test_url}",
                headers=test_headers,
                data=data,
            )
        else:

            data = test_data

            r = requests.post(
                f"{server.url}/clients/{test_url}",
                headers=test_headers,
                data=data,
            )

        print(f"r.text: {r.text}")

        assert r.status_code == expected_response_status_code
        assert r.json()["body"]["error"]["code"] == expected_response_data