From 1f86c442b1332b217133bbf671e237cebe765f68 Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Fri, 15 Dec 2023 11:53:18 -0500 Subject: [PATCH 1/4] test: add unit test case for unset token in test_entrypoint_base.py Signed-off-by: Jennifer Power --- tests/trestlebot/entrypoints/test_entrypoint_base.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/trestlebot/entrypoints/test_entrypoint_base.py b/tests/trestlebot/entrypoints/test_entrypoint_base.py index 02c4ae30..51bdf146 100644 --- a/tests/trestlebot/entrypoints/test_entrypoint_base.py +++ b/tests/trestlebot/entrypoints/test_entrypoint_base.py @@ -91,3 +91,14 @@ def test_set_git_provider_with_none() -> None: args = argparse.Namespace(target_branch=None, with_token=None) provider = EntrypointBase.set_git_provider(args=args) assert provider is None + + +def test_set_provider_with_no_token() -> None: + """Test set_git_provider function with no token""" + args = argparse.Namespace(target_branch="main", with_token=None) + with pytest.raises( + EntrypointInvalidArgException, + match="Invalid args --with-token: " + "with-token flag must be set when using target-branch", + ): + EntrypointBase.set_git_provider(args=args) From eede6851ea9de4508677f9dbc12cd0865ed3e9c6 Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Fri, 15 Dec 2023 13:04:29 -0500 Subject: [PATCH 2/4] test: adds negative test for get_trestle_model_dir in test_types.py Signed-off-by: Jennifer Power --- tests/trestlebot/tasks/authored/test_types.py | 11 +++++++++++ trestlebot/tasks/authored/types.py | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/trestlebot/tasks/authored/test_types.py b/tests/trestlebot/tasks/authored/test_types.py index edbffbd6..5068a452 100644 --- a/tests/trestlebot/tasks/authored/test_types.py +++ b/tests/trestlebot/tasks/authored/test_types.py @@ -17,6 +17,7 @@ """Test author types for Trestlebot""" import os +from unittest.mock import Mock import pytest @@ -98,3 +99,13 @@ def test_invalid_authored_type(tmp_trestle_dir: str) -> None: match="Invalid authored type fake", ): _ = types.get_authored_object("fake", tmp_trestle_dir, "") + + +def test_get_model_dir_with_invalid_type() -> None: + """Test triggering an error with an invalid type when getting the model dir.""" + with pytest.raises( + AuthoredObjectException, + match="Invalid authored object ", + ): + mock = Mock(spec=AuthoredObjectBase) + _ = types.get_trestle_model_dir(mock) diff --git a/trestlebot/tasks/authored/types.py b/trestlebot/tasks/authored/types.py index 8105989f..e38df2df 100644 --- a/trestlebot/tasks/authored/types.py +++ b/trestlebot/tasks/authored/types.py @@ -67,4 +67,6 @@ def get_trestle_model_dir(authored_object: AuthoredObjectBase) -> str: elif isinstance(authored_object, AuthoredSSP): return const.MODEL_DIR_SSP else: - raise AuthoredObjectException(f"Invalid authored object {authored_object}") + raise AuthoredObjectException( + f"Invalid authored object {type(authored_object)}" + ) From 7aabd1dd7fb6a0bd23580e8ab6cb70a1154fea60 Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Fri, 15 Dec 2023 15:01:06 -0500 Subject: [PATCH 3/4] refactor: adds dynamic list of supported models in autosync CLI Signed-off-by: Jennifer Power --- trestlebot/entrypoints/autosync.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/trestlebot/entrypoints/autosync.py b/trestlebot/entrypoints/autosync.py index 4e2710f8..81b486bf 100644 --- a/trestlebot/entrypoints/autosync.py +++ b/trestlebot/entrypoints/autosync.py @@ -52,6 +52,7 @@ def __init__(self, parser: argparse.ArgumentParser) -> None: """Initialize.""" # Setup base arguments super().__init__(parser) + self.supported_models: List[str] = [model.value for model in types.AuthoredType] self.setup_autosync_arguments() def setup_autosync_arguments(self) -> None: @@ -66,7 +67,7 @@ def setup_autosync_arguments(self) -> None: "--oscal-model", required=True, type=str, - choices=["catalog", "profile", "compdef", "ssp"], + choices=self.supported_models, help="OSCAL model type to run tasks on.", ) self.parser.add_argument( @@ -98,12 +99,12 @@ def setup_autosync_arguments(self) -> None: def validate_args(self, args: argparse.Namespace) -> None: """Validate the arguments for the autosync entrypoint.""" - authored_list: List[str] = [model.value for model in types.AuthoredType] - if args.oscal_model not in authored_list: + supported_models_str = ", ".join(self.supported_models) + if args.oscal_model not in self.supported_models: raise EntrypointInvalidArgException( "--oscal-model", - f"Value {args.oscal_model} is not valid." - f"Please use one of {authored_list}", + f"Invalid value {args.oscal_model}. " + f"Please use one of {supported_models_str}", ) if not args.markdown_path: From 50dcbf714c527c3cda8f2d0c60781f7368256510 Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Fri, 15 Dec 2023 15:01:34 -0500 Subject: [PATCH 4/4] test: adds unit test for validate_args with invalid model Signed-off-by: Jennifer Power --- tests/trestlebot/entrypoints/test_autosync.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/trestlebot/entrypoints/test_autosync.py b/tests/trestlebot/entrypoints/test_autosync.py index 1120da2a..a0fa9927 100644 --- a/tests/trestlebot/entrypoints/test_autosync.py +++ b/tests/trestlebot/entrypoints/test_autosync.py @@ -16,6 +16,7 @@ """Test for Autosync CLI""" +import argparse import logging from typing import Any, Dict from unittest.mock import patch @@ -23,7 +24,9 @@ import pytest from tests.testutils import args_dict_to_list +from trestlebot.entrypoints.autosync import AutoSyncEntrypoint from trestlebot.entrypoints.autosync import main as cli_main +from trestlebot.entrypoints.entrypoint_base import EntrypointInvalidArgException @pytest.fixture @@ -48,6 +51,26 @@ def test_invalid_oscal_model(valid_args_dict: Dict[str, str]) -> None: cli_main() +def test_validate_args_invalid_model(valid_args_dict: Dict[str, str]) -> None: + """ + Test invalid oscal model with validate args function. + This is a separate test from test_invalid_oscal_model because + it args are make invalid after the args are parsed. + """ + args_dict = valid_args_dict + with patch("sys.argv", ["trestlebot", *args_dict_to_list(args_dict)]): + with pytest.raises( + EntrypointInvalidArgException, + match="Invalid args --oscal-model: Invalid value fake. " + "Please use one of catalog, profile, ssp, compdef", + ): + parser = argparse.ArgumentParser() + auto_sync = AutoSyncEntrypoint(parser=parser) + args = parser.parse_args() + args.oscal_model = "fake" + auto_sync.validate_args(args) + + def test_no_ssp_index(valid_args_dict: Dict[str, str], caplog: Any) -> None: """Test missing index file for ssp""" args_dict = valid_args_dict