From 0cd4904abb826776a2d480179057a19db1ed8ba6 Mon Sep 17 00:00:00 2001 From: rverdile Date: Tue, 19 Nov 2024 10:19:15 -0500 Subject: [PATCH] normalize type to empty string cleanup --- src/rhsmlib/dbus/objects/register.py | 11 ++++++- src/rhsmlib/services/environment.py | 11 +++---- test/rhsmlib/dbus/test_register.py | 47 +++++++++++++++++++++++----- 3 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/rhsmlib/dbus/objects/register.py b/src/rhsmlib/dbus/objects/register.py index b516f0d8c5..0fe312094f 100644 --- a/src/rhsmlib/dbus/objects/register.py +++ b/src/rhsmlib/dbus/objects/register.py @@ -193,10 +193,19 @@ def get_environments(self, options: dict) -> List[dict]: environments = environment_service.list(options["org_id"], typed_environments=True) - keys_to_remove: List[str] = ["created", "updated", "contentPrefix", "owner", "environmentContent"] + keys_to_remove: List[str] = [ + "created", + "updated", + "contentPrefix", + "owner", + "environmentContent", + ] + for environment in environments: for key in keys_to_remove: environment.pop(key, None) + if ("type" not in environment) or (environment["type"] is None): + environment["type"] = "" return environments diff --git a/src/rhsmlib/services/environment.py b/src/rhsmlib/services/environment.py index 1af6c04b06..7602b7498c 100644 --- a/src/rhsmlib/services/environment.py +++ b/src/rhsmlib/services/environment.py @@ -41,15 +41,12 @@ def list(self, org_id: str, typed_environments: bool = True) -> List[dict]: :param typed_environments: Whether output should include typed environments :return: List of environments. """ - + list_all = False if typed_environments: - has_typed_environments = self.cp.has_capability("typed_environments") - - if not has_typed_environments: + list_all = self.cp.has_capability("typed_environments") + if not list_all: log.debug("candlepin does not have typed_environments capability") - environments = self.cp.getEnvironmentList(org_id, list_all=has_typed_environments) - else: - environments = self.cp.getEnvironmentList(org_id) + environments = self.cp.getEnvironmentList(org_id, list_all=list_all) return environments diff --git a/test/rhsmlib/dbus/test_register.py b/test/rhsmlib/dbus/test_register.py index d7408ea7b2..3715d17c25 100644 --- a/test/rhsmlib/dbus/test_register.py +++ b/test/rhsmlib/dbus/test_register.py @@ -25,6 +25,7 @@ from unittest import mock from test.rhsmlib.base import SubManDBusFixture + CONSUMER_CONTENT_JSON_SCA = """{"hypervisorId": null, "serviceLevel": "", "autoheal": true, @@ -150,12 +151,49 @@ "name": "test-environment-3", "description": "test description", "contentPrefix": null, - "type": "content-template", + "type": null, + "environmentContent": [] + }, + { + "created": "2024-11-07T20:01:47+0000", + "updated": "2024-11-07T20:01:47+0000", + "id": "fake-id-4", + "name": "test-environment-4", + "description": "test description", + "contentPrefix": null, "environmentContent": [] } ] """ +ENVIRONMENTS_DBUS_JSON = """[ + { + "id": "fake-id", + "name": "test-environment", + "description": "test description", + "type": "content-template" + }, + { + "id": "fake-id-2", + "name": "test-environment-2", + "description": "test description", + "type": "content-template" + }, + { + "id": "fake-id-3", + "name": "test-environment-3", + "description": "test description", + "type": "" + }, + { + "id": "fake-id-4", + "name": "test-environment-4", + "description": "test description", + "type": "" + } +] +""" + class RegisterDBusObjectTest(SubManDBusFixture): socket_dir: Optional[tempfile.TemporaryDirectory] = None @@ -350,12 +388,7 @@ def test_GetEnvironments(self): mock_cp.getEnvironmentList.return_value = json.loads(ENVIRONMENTS_CONTENT_JSON) self.patches["build_uep"].return_value = mock_cp - expected = json.loads(ENVIRONMENTS_CONTENT_JSON) - keys_to_remove = ["created", "updated", "contentPrefix", "owner", "environmentContent"] - for environment in expected: - for key in keys_to_remove: - environment.pop(key, None) - + expected = json.loads(ENVIRONMENTS_DBUS_JSON) result = self.impl.get_environments( {"username": "username", "password": "password", "org_id": "org_id"} )