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

Clear State before raising errors #34

Merged
merged 5 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions pyracf/common/security_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def _build_bool_segment_dictionaries(self, segments: List[str]) -> None:
else:
bad_segments.append(segment)
if bad_segments:
self.__clear_state(SecurityRequest)
raise SegmentError(bad_segments, self._profile_type)
# preserve segment traits for debug logging.
self.__preserved_segment_traits = self._segment_traits
Expand All @@ -287,6 +288,7 @@ def _build_segment_dictionaries(self, traits: dict) -> None:
if not trait_valid:
bad_traits.append(trait)
if bad_traits:
self.__clear_state(SecurityRequest)
raise SegmentTraitError(bad_traits, self._profile_type)

# preserve segment traits for debug logging.
Expand Down
43 changes: 43 additions & 0 deletions tests/user/test_user_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,30 @@ def test_user_admin_build_add_request_with_bad_segment_traits(self):
+ f"combination for '{self.user_admin._profile_type}'.\n",
)

def test_user_admin_cleans_up_after_build_add_request_with_bad_segment_traits(self):
bad_trait = "omvs:bad_trait"
user_admin = UserAdmin(
generate_requests_only=True,
)
with self.assertRaises(SegmentTraitError) as exception:
user_admin.add(
"squidwrd", TestUserConstants.TEST_ADD_USER_REQUEST_BAD_TRAITS
)
self.assertEqual(
exception.exception.message,
"Unable to build Security Request.\n\n"
+ f"'{bad_trait}' is not a known segment-trait "
+ f"combination for '{self.user_admin._profile_type}'.\n",
)
result = user_admin.extract("squidwrd", segments=["omvs"])
lcarcaramo marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(
result, TestUserConstants.TEST_EXTRACT_USER_REQUEST_BASE_OMVS_XML
)
result = self.user_admin.add(
"squidwrd", traits=TestUserConstants.TEST_ADD_USER_REQUEST_TRAITS
)
self.assertEqual(result, TestUserConstants.TEST_ADD_USER_REQUEST_XML)

def test_user_admin_build_extract_request_with_bad_segment_name(self):
bad_segment = "bad_segment"
user_admin = UserAdmin(
Expand All @@ -152,3 +176,22 @@ def test_user_admin_build_extract_request_with_bad_segment_name(self):
"Unable to build Security Request.\n\n"
+ f"'{bad_segment}' is not a known segment for '{self.user_admin._profile_type}'.\n",
)

def test_user_admin_cleans_up_after_build_extract_request_with_bad_segment_name(
self,
):
bad_segment = "bad_segment"
user_admin = UserAdmin(
generate_requests_only=True,
)
with self.assertRaises(SegmentError) as exception:
user_admin.extract("squidwrd", segments=["tso", bad_segment])
self.assertEqual(
exception.exception.message,
"Unable to build Security Request.\n\n"
+ f"'{bad_segment}' is not a known segment for '{self.user_admin._profile_type}'.\n",
)
result = user_admin.extract("squidwrd", segments=["omvs"])
self.assertEqual(
result, TestUserConstants.TEST_EXTRACT_USER_REQUEST_BASE_OMVS_XML
)