def test_read_template_file_invalid_file(caplog, monkeypatch):
    """
    GIVEN `read_template_file` is called
    WHEN a non existent file is provided
    THEN exit with an error of 1
    """

    monkeypatch.setenv("CFN_TEMPLATE_FILE_LOCATION", "/tmp/x.yaml")

    c = CcValidator()

    with pytest.raises(SystemExit):
        c.read_template_file()

    assert "Template file does not exist" in caplog.text
def test_read_template_file_valid_file():
    """
    GIVEN `read_template_file` is called
    WHEN a file is provided
    THEN return the file contents as a string
    """

    c = CcValidator()
    template_str = c.read_template_file()

    assert "Resources" in template_str
def test_generate_payload():
    """
    GIVEN `generate_payload` is called
    WHEN a valid template is provided
    THEN return the payload which will be sent to Conformity
    """

    c = CcValidator()
    template_str = c.read_template_file()
    payload = c.generate_payload(template_str)

    assert "data" in payload
def test_run_validation_no_profile_id():
    """
    GIVEN `run_validation` is called
    WHEN no profile ID is provided
    THEN return the payload provided by Conformity
    """

    c = CcValidator()
    template_str = c.read_template_file()
    payload = c.generate_payload(template_str)
    validation = c.run_validation(payload)

    assert "data" in validation
def test_run_validation_invalid_profile_id(monkeypatch):
    """
    GIVEN `run_validation` is called
    WHEN an invalid profile ID is provided
    THEN exit with an error of 1
    """

    monkeypatch.setenv("CC_PROFILE_ID", "x")

    c = CcValidator()
    template_str = c.read_template_file()
    payload = c.generate_payload(template_str)
    validation = c.run_validation(payload)

    assert "errors" in validation
def test_run_validation_invalid_api_key(caplog, monkeypatch):
    """
    GIVEN `run_validation` is called
    WHEN an invalid API key is provided
    THEN exit with an error of 1
    """

    monkeypatch.setenv("CC_API_KEY", "x")

    c = CcValidator()
    template_str = c.read_template_file()
    payload = c.generate_payload(template_str)

    with pytest.raises(SystemExit):
        c.run_validation(payload)

    assert "User is not authorized to access this resource with an explicit deny" in caplog.text
def test_run_validation_valid_profile_id():
    """
    GIVEN `run_validation` is called
    WHEN a valid profile ID is provided
    THEN return the payload provided by Conformity
    """

    # ensure a valid profile ID is provided
    if not os.environ.get("CC_PROFILE_ID"):
        assert False is True

    c = CcValidator()
    template_str = c.read_template_file()
    payload = c.generate_payload(template_str)
    validation = c.run_validation(payload)

    assert "data" in validation