Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSCE-257: feat(authored): adds rules view creation for component definitions #59

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 35 additions & 45 deletions tests/trestlebot/tasks/authored/test_compdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
"""Test for Trestle Bot Authored Compdef."""

import pathlib
import re

import pytest
from trestle.common.model_utils import ModelUtils
from trestle.core.models.file_content_type import FileContentType
from trestle.oscal.component import ComponentDefinition

from tests import testutils
from trestlebot.const import RULES_VIEW_DIR, YAML_EXTENSION
from trestlebot.tasks.authored.base_authored import AuthoredObjectException
from trestlebot.tasks.authored.compdef import AuthoredComponentsDefinition
from trestlebot.transformers.yaml_transformer import ToRulesYAMLTransformer


test_prof = "simplified_nist_profile"
Expand All @@ -41,54 +41,44 @@ def test_create_new_default(tmp_trestle_dir: str) -> None:

authored_comp.create_new_default(test_prof, test_comp, "test", "My desc", "service")

comp, _ = ModelUtils.load_model_for_class(
trestle_root, test_comp, ComponentDefinition, FileContentType.JSON
)

assert comp is not None

assert comp.components is not None
assert comp.components[0] is not None
assert comp.components[0].control_implementations is not None

assert (
len(comp.components[0].control_implementations[0].implemented_requirements)
== 12
)


def test_create_new_default_existing(tmp_trestle_dir: str) -> None:
"""Test creating new default component in existing component definition"""
# Prepare the workspace
trestle_root = pathlib.Path(tmp_trestle_dir)
_ = testutils.setup_for_compdef(trestle_root, test_comp, "")
authored_comp = AuthoredComponentsDefinition(tmp_trestle_dir)

authored_comp.create_new_default(test_prof, test_comp, "test", "My desc", "service")
rules_view_dir = trestle_root / RULES_VIEW_DIR
assert rules_view_dir.exists()

comp, _ = ModelUtils.load_model_for_class(
trestle_root, test_comp, ComponentDefinition, FileContentType.JSON
)
compdef_dir = rules_view_dir / test_comp
assert compdef_dir.exists()

assert comp is not None
comp_dir = compdef_dir / "test"
assert comp_dir.exists()

# Check new component
assert comp.components is not None
assert comp.components[1] is not None
assert comp.components[1].control_implementations is not None
yaml_files = list(comp_dir.glob(f"*{YAML_EXTENSION}"))
assert len(yaml_files) == 12

assert (
len(comp.components[1].control_implementations[0].implemented_requirements)
== 12
rule_file = next(
(file for file in yaml_files if re.search(r"ac-5", file.stem)), None
)

# Check existing component
assert comp.components[0] is not None
assert comp.components[0].control_implementations is not None

assert (
len(comp.components[0].control_implementations[0].implemented_requirements) == 2
assert rule_file is not None

# Read one of the files and check the content
rule_path = pathlib.Path(rule_file)
rule_stream = rule_path.read_text()

transformer = ToRulesYAMLTransformer()
rule = transformer.transform(rule_stream)

assert rule.name == "rule-ac-5"
assert rule.description == "Rule for ac-5"
assert rule.component.name == "test"
assert rule.component.type == "service"
assert rule.component.description == "My desc"
assert rule.parameter is None
assert rule.profile.description == (
"NIST Special Publication 800-53 Revision 5 MODERATE IMPACT \
BASELINE"
)
assert rule.profile.href == "profiles/simplified_nist_profile/profile.json"
assert rule.profile.include_controls is not None
assert len(rule.profile.include_controls) == 1
assert rule.profile.include_controls[0].id == "ac-5"


def test_create_new_default_no_profile(tmp_trestle_dir: str) -> None:
Expand Down
98 changes: 0 additions & 98 deletions tests/trestlebot/transformers/test_csv_to_yaml.py

This file was deleted.

5 changes: 1 addition & 4 deletions tests/trestlebot/transformers/test_yaml_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ def test_read_write_integration(test_rule: TrestleRule) -> None:
to_rules_transformer = ToRulesYAMLTransformer()

yaml_data = from_rules_transformer.transform(test_rule)

blob = yaml_data.getvalue()

read_rule = to_rules_transformer.transform(blob)
read_rule = to_rules_transformer.transform(yaml_data)

assert read_rule == test_rule
Loading
Loading