From e507d3fd17dd2226c7777e3bec8e713951370f60 Mon Sep 17 00:00:00 2001 From: Taylor Steinberg Date: Fri, 8 Nov 2024 09:35:53 -0500 Subject: [PATCH] fix: remove all warnings from attribute access (#323) Removes all warning messages created by direct attribute access. --- .../posit/connect/oauth/test_associations.py | 4 +- .../posit/connect/oauth/test_integrations.py | 2 +- .../tests/posit/connect/test_groups.py | 4 +- src/posit/connect/bundles.py | 8 +- src/posit/connect/content.py | 16 +-- src/posit/connect/groups.py | 2 +- src/posit/connect/permissions.py | 10 +- src/posit/connect/tasks.py | 2 +- src/posit/connect/users.py | 2 +- .../posit/connect/oauth/test_integrations.py | 68 ++--------- tests/posit/connect/oauth/test_sessions.py | 40 +------ tests/posit/connect/test_bundles.py | 76 ++---------- tests/posit/connect/test_client.py | 2 +- tests/posit/connect/test_content.py | 22 ++-- tests/posit/connect/test_groups.py | 6 +- tests/posit/connect/test_permissions.py | 4 +- tests/posit/connect/test_tasks.py | 8 +- tests/posit/connect/test_users.py | 112 +++--------------- 18 files changed, 84 insertions(+), 304 deletions(-) diff --git a/integration/tests/posit/connect/oauth/test_associations.py b/integration/tests/posit/connect/oauth/test_associations.py index 46035fc5..6ec3759d 100644 --- a/integration/tests/posit/connect/oauth/test_associations.py +++ b/integration/tests/posit/connect/oauth/test_associations.py @@ -87,7 +87,9 @@ def test_find_update_by_content(self): updated_associations = self.content.oauth.associations.find() assert len(updated_associations) == 1 assert updated_associations[0]["app_guid"] == self.content["guid"] - assert updated_associations[0]["oauth_integration_guid"] == self.another_integration.guid + assert ( + updated_associations[0]["oauth_integration_guid"] == self.another_integration["guid"] + ) # unset content association self.content.oauth.associations.delete() diff --git a/integration/tests/posit/connect/oauth/test_integrations.py b/integration/tests/posit/connect/oauth/test_integrations.py index 245b8d45..25720bb6 100644 --- a/integration/tests/posit/connect/oauth/test_integrations.py +++ b/integration/tests/posit/connect/oauth/test_integrations.py @@ -88,7 +88,7 @@ def test_create_update_delete(self): created.update(name="updated integration name") updated = self.client.oauth.integrations.get(integration["guid"]) - assert updated.name == "updated integration name" + assert updated["name"] == "updated integration name" # delete the new integration diff --git a/integration/tests/posit/connect/test_groups.py b/integration/tests/posit/connect/test_groups.py index ba2c0e94..6c64dd81 100644 --- a/integration/tests/posit/connect/test_groups.py +++ b/integration/tests/posit/connect/test_groups.py @@ -2,10 +2,12 @@ class TestGroups: + @classmethod def setup_class(cls): cls.client = connect.Client() cls.item = cls.client.groups.create(name="Friends") + @classmethod def teardown_class(cls): cls.item.delete() assert cls.client.groups.count() == 0 @@ -14,7 +16,7 @@ def test_count(self): assert self.client.groups.count() == 1 def test_get(self): - assert self.client.groups.get(self.item.guid) + assert self.client.groups.get(self.item["guid"]) def test_find(self): assert self.client.groups.find() == [self.item] diff --git a/src/posit/connect/bundles.py b/src/posit/connect/bundles.py index 4d970e99..c6a8a265 100644 --- a/src/posit/connect/bundles.py +++ b/src/posit/connect/bundles.py @@ -19,7 +19,7 @@ def metadata(self) -> BundleMetadata: def delete(self) -> None: """Delete the bundle.""" - path = f"v1/content/{self.content_guid}/bundles/{self.id}" + path = f"v1/content/{self['content_guid']}/bundles/{self['id']}" url = self.params.url + path self.params.session.delete(url) @@ -39,9 +39,9 @@ def deploy(self) -> tasks.Task: >>> task.wait_for() None """ - path = f"v1/content/{self.content_guid}/deploy" + path = f"v1/content/{self['content_guid']}/deploy" url = self.params.url + path - response = self.params.session.post(url, json={"bundle_id": self.id}) + response = self.params.session.post(url, json={"bundle_id": self["id"]}) result = response.json() ts = tasks.Tasks(self.params) return ts.get(result["task_id"]) @@ -77,7 +77,7 @@ def download(self, output: io.BufferedWriter | str) -> None: f"download() expected argument type 'io.BufferedWriter` or 'str', but got '{type(output).__name__}'", ) - path = f"v1/content/{self.content_guid}/bundles/{self.id}/download" + path = f"v1/content/{self['content_guid']}/bundles/{self['id']}/download" url = self.params.url + path response = self.params.session.get(url, stream=True) if isinstance(output, io.BufferedWriter): diff --git a/src/posit/connect/content.py b/src/posit/connect/content.py index d2ca16cb..9dc51fad 100644 --- a/src/posit/connect/content.py +++ b/src/posit/connect/content.py @@ -55,7 +55,7 @@ def oauth(self) -> ContentItemOAuth: def delete(self) -> None: """Delete the content item.""" - path = f"v1/content/{self.guid}" + path = f"v1/content/{self['guid']}" url = self.params.url + path self.params.session.delete(url) @@ -75,7 +75,7 @@ def deploy(self) -> tasks.Task: >>> task.wait_for() None """ - path = f"v1/content/{self.guid}/deploy" + path = f"v1/content/{self['guid']}/deploy" url = self.params.url + path response = self.params.session.post(url, json={"bundle_id": None}) result = response.json() @@ -99,7 +99,7 @@ def render(self) -> Task: if self.is_rendered: variants = self._variants.find() - variants = [variant for variant in variants if variant.is_default] + variants = [variant for variant in variants if variant["is_default"]] if len(variants) != 1: raise RuntimeError( f"Found {len(variants)} default variants. Expected 1. Without a single default variant, the content cannot be refreshed. This is indicative of a corrupted state.", @@ -108,7 +108,7 @@ def render(self) -> Task: return variant.render() else: raise ValueError( - f"Render not supported for this application mode: {self.app_mode}. Did you need to use the 'restart()' method instead? Note that some application modes do not support 'render()' or 'restart()'.", + f"Render not supported for this application mode: {self['app_mode']}. Did you need to use the 'restart()' method instead? Note that some application modes do not support 'render()' or 'restart()'.", ) def restart(self) -> None: @@ -132,12 +132,12 @@ def restart(self) -> None: self.environment_variables.create(key, unix_epoch_in_seconds) self.environment_variables.delete(key) # GET via the base Connect URL to force create a new worker thread. - url = posixpath.join(dirname(self.params.url), f"content/{self.guid}") + url = posixpath.join(dirname(self.params.url), f"content/{self['guid']}") self.params.session.get(url) return None else: raise ValueError( - f"Restart not supported for this application mode: {self.app_mode}. Did you need to use the 'render()' method instead? Note that some application modes do not support 'render()' or 'restart()'.", + f"Restart not supported for this application mode: {self['app_mode']}. Did you need to use the 'render()' method instead? Note that some application modes do not support 'render()' or 'restart()'.", ) @overload @@ -276,7 +276,7 @@ def _variants(self) -> Variants: @property def is_interactive(self) -> bool: - return self.app_mode in { + return self["app_mode"] in { "api", "jupyter-voila", "python-api", @@ -293,7 +293,7 @@ def is_interactive(self) -> bool: @property def is_rendered(self) -> bool: - return self.app_mode in { + return self["app_mode"] in { "rmd-static", "jupyter-static", "quarto-static", diff --git a/src/posit/connect/groups.py b/src/posit/connect/groups.py index 3956e0bb..217f47ee 100644 --- a/src/posit/connect/groups.py +++ b/src/posit/connect/groups.py @@ -14,7 +14,7 @@ class Group(Resource): def delete(self) -> None: """Delete the group.""" - path = f"v1/groups/{self.guid}" + path = f"v1/groups/{self['guid']}" url = self.params.url + path self.params.session.delete(url) diff --git a/src/posit/connect/permissions.py b/src/posit/connect/permissions.py index 6b06c56f..c5c9a268 100644 --- a/src/posit/connect/permissions.py +++ b/src/posit/connect/permissions.py @@ -12,7 +12,7 @@ class Permission(Resource): def delete(self) -> None: """Delete the permission.""" - path = f"v1/content/{self.content_guid}/permissions/{self.id}" + path = f"v1/content/{self['content_guid']}/permissions/{self['id']}" url = self.params.url + path self.params.session.delete(url) @@ -33,13 +33,13 @@ def update(self, *args, **kwargs) -> None: def update(self, *args, **kwargs) -> None: """Update the permission.""" body = { - "principal_guid": self.principal_guid, - "principal_type": self.principal_type, - "role": self.role, + "principal_guid": self.get("principal_guid"), + "principal_type": self.get("principal_type"), + "role": self.get("role"), } body.update(dict(*args)) body.update(**kwargs) - path = f"v1/content/{self.content_guid}/permissions/{self.id}" + path = f"v1/content/{self['content_guid']}/permissions/{self['id']}" url = self.params.url + path response = self.params.session.put( url, diff --git a/src/posit/connect/tasks.py b/src/posit/connect/tasks.py index a4827142..0319b05a 100644 --- a/src/posit/connect/tasks.py +++ b/src/posit/connect/tasks.py @@ -90,7 +90,7 @@ def update(self, *args, **kwargs) -> None: ] """ params = dict(*args, **kwargs) - path = f"v1/tasks/{self.id}" + path = f"v1/tasks/{self['id']}" url = self.params.url + path response = self.params.session.get(url, params=kwargs) result = response.json() diff --git a/src/posit/connect/users.py b/src/posit/connect/users.py index a17ea6a2..4b085702 100644 --- a/src/posit/connect/users.py +++ b/src/posit/connect/users.py @@ -43,7 +43,7 @@ def lock(self, *, force: bool = False): >>> user.lock(force=True) """ _me = me.get(self.params) - if _me.guid == self["guid"] and not force: + if _me["guid"] == self["guid"] and not force: raise RuntimeError( "You cannot lock your own account. Set force=True to override this behavior.", ) diff --git a/tests/posit/connect/oauth/test_integrations.py b/tests/posit/connect/oauth/test_integrations.py index c513fcea..a2a6d223 100644 --- a/tests/posit/connect/oauth/test_integrations.py +++ b/tests/posit/connect/oauth/test_integrations.py @@ -1,61 +1,11 @@ -from unittest import mock - import responses from responses import matchers from posit.connect.client import Client -from posit.connect.oauth.associations import IntegrationAssociations -from posit.connect.oauth.integrations import Integration from ..api import load_mock # type: ignore -class TestIntegrationAttributes: - @classmethod - def setup_class(cls): - guid = "22644575-a27b-4118-ad06-e24459b05126" - fake_item = load_mock(f"v1/oauth/integrations/{guid}.json") - cls.item = Integration(mock.Mock(), **fake_item) - - def test_id(self): - assert self.item.id == "3" - - def test_guid(self): - assert self.item.guid == "22644575-a27b-4118-ad06-e24459b05126" - - def test_name(self): - assert self.item.name == "keycloak integration" - - def test_description(self): - assert self.item.description == "integration description" - - def test_template(self): - assert self.item.template == "custom" - - def test_config(self): - assert self.item.config["auth_mode"] == "Confidential" - assert ( - self.item.config["authorization_uri"] - == "http://keycloak:8080/realms/rsconnect/protocol/openid-connect/auth" - ) - assert self.item.config["client_id"] == "rsconnect-oidc" - assert self.item.config["scopes"] == "email" - assert self.item.config["token_endpoint_auth_method"] == "client_secret_basic" - assert ( - self.item.config["token_uri"] - == "http://keycloak:8080/realms/rsconnect/protocol/openid-connect/token" - ) - - def test_created_time(self): - assert self.item.created_time == "2024-07-16T19:28:05Z" - - def test_updated_time(self): - assert self.item.updated_time == "2024-07-17T19:28:05Z" - - def test_associations(self): - assert isinstance(self.item.associations, IntegrationAssociations) - - class TestIntegrationDelete: @responses.activate def test(self): @@ -96,7 +46,7 @@ def test(self): c = Client("https://connect.example", "12345") c.ctx.version = None integration = c.oauth.integrations.get(guid) - assert integration.guid == guid + assert integration["guid"] == guid new_name = "New Name" @@ -111,7 +61,7 @@ def test(self): integration.update(name=new_name) assert mock_update.call_count == 1 - assert integration.name == new_name + assert integration["name"] == new_name class TestIntegrationsCreate: @@ -151,10 +101,10 @@ def test(self): # assert assert mock_create.call_count == 1 - assert integration.name == fake_integration["name"] - assert integration.description == fake_integration["description"] - assert integration.template == fake_integration["template"] - assert integration.config == fake_integration["config"] + assert integration["name"] == fake_integration["name"] + assert integration["description"] == fake_integration["description"] + assert integration["template"] == fake_integration["template"] + assert integration["config"] == fake_integration["config"] class TestIntegrationsFind: @@ -176,8 +126,8 @@ def test(self): # assert assert mock_get.call_count == 1 assert len(integrations) == 2 - assert integrations[0].id == "3" - assert integrations[1].id == "4" + assert integrations[0]["id"] == "3" + assert integrations[1]["id"] == "4" class TestIntegrationsGet: @@ -197,4 +147,4 @@ def test(self): integration = c.oauth.integrations.get(guid) assert mock_get.call_count == 1 - assert integration.guid == guid + assert integration["guid"] == guid diff --git a/tests/posit/connect/oauth/test_sessions.py b/tests/posit/connect/oauth/test_sessions.py index 09cd8300..9af1656e 100644 --- a/tests/posit/connect/oauth/test_sessions.py +++ b/tests/posit/connect/oauth/test_sessions.py @@ -1,43 +1,11 @@ -from unittest import mock - import responses from responses import matchers from posit.connect.client import Client -from posit.connect.oauth.sessions import Session from ..api import load_mock # type: ignore -class TestOAuthSessionAttributes: - @classmethod - def setup_class(cls): - guid = "32c04dc6-0318-41b7-bc74-7e321b196f14" - fake_item = load_mock(f"v1/oauth/sessions/{guid}.json") - cls.item = Session(mock.Mock(), **fake_item) - - def test_id(self): - assert self.item.id == "54" - - def test_guid(self): - assert self.item.guid == "32c04dc6-0318-41b7-bc74-7e321b196f14" - - def test_user_guid(self): - assert self.item.user_guid == "217be1f2-6a32-46b9-af78-e3f4b89f2e74" - - def test_oauth_integration_guid(self): - assert self.item.oauth_integration_guid == "967f0ad3-3e3b-4491-8539-1a193b35a415" - - def test_has_refresh_token(self): - assert self.item.has_refresh_token - - def test_created_time(self): - assert self.item.created_time == "2024-07-24T15:59:51Z" - - def test_updated_time(self): - assert self.item.updated_time == "2024-07-24T16:59:51Z" - - class TestSessionDelete: @responses.activate def test(self): @@ -82,9 +50,9 @@ def test(self): # assert assert mock_get.call_count == 1 assert len(sessions) == 3 - assert sessions[0].id == "54" - assert sessions[1].id == "55" - assert sessions[2].id == "56" + assert sessions[0]["id"] == "54" + assert sessions[1]["id"] == "55" + assert sessions[2]["id"] == "56" @responses.activate def test_params_all(self): @@ -126,4 +94,4 @@ def test(self): # assert assert mock_get.call_count == 1 - assert session.guid == guid + assert session["guid"] == guid diff --git a/tests/posit/connect/test_bundles.py b/tests/posit/connect/test_bundles.py index cc6dec97..2f8031e2 100644 --- a/tests/posit/connect/test_bundles.py +++ b/tests/posit/connect/test_bundles.py @@ -6,73 +6,10 @@ from responses import matchers from posit.connect import Client -from posit.connect.bundles import Bundle from .api import get_path, load_mock # type: ignore -class TestBundleProperties: - def setup_class(cls): - cls.bundle = Bundle( - mock.Mock(), - **load_mock("v1/content/f2f37341-e21d-3d80-c698-a935ad614066/bundles/101.json"), - ) - - def test_id(self): - assert self.bundle.id == "101" - - def test_content_guid(self): - assert self.bundle.content_guid == "f2f37341-e21d-3d80-c698-a935ad614066" - - def test_created_time(self): - assert self.bundle.created_time == "2006-01-02T15:04:05Z07:00" - - def test_cluster_name(self): - assert self.bundle.cluster_name == "Local" - - def test_image_name(self): - assert self.bundle.image_name == "Local" - - def test_r_version(self): - assert self.bundle.r_version == "3.5.1" - - def test_r_environment_management(self): - assert self.bundle.r_environment_management is True - - def test_py_version(self): - assert self.bundle.py_version == "3.8.2" - - def test_py_environment_management(self): - assert self.bundle.py_environment_management is True - - def test_quarto_version(self): - assert self.bundle.quarto_version == "0.2.22" - - def test_active(self): - assert self.bundle.active is False - - def test_size(self): - assert self.bundle.size == 1000000 - - def test_metadata_source(self): - assert self.bundle.metadata.source == "string" - - def test_metadata_source_repo(self): - assert self.bundle.metadata.source_repo == "string" - - def test_metadata_source_branch(self): - assert self.bundle.metadata.source_branch == "string" - - def test_metadata_source_commit(self): - assert self.bundle.metadata.source_commit == "string" - - def test_metadata_archive_md5(self): - assert self.bundle.metadata.archive_md5 == "37324238a80595c453c706b22adb83d3" - - def test_metadata_archive_sha1(self): - assert self.bundle.metadata.archive_sha1 == "a2f7d13d87657df599aeeabdb70194d508cfa92f" - - class TestBundleDelete: @responses.activate def test(self): @@ -144,7 +81,7 @@ def test(self): task = bundle.deploy() # assert - assert task.id == task_id + assert task["id"] == task_id assert mock_content_get.call_count == 1 assert mock_bundle_get.call_count == 1 assert mock_bundle_deploy.call_count == 1 @@ -290,7 +227,7 @@ def test(self): bundle = content.bundles.create(data) # # assert - assert bundle.id == "101" + assert bundle["id"] == "101" assert mock_content_get.call_count == 1 assert mock_bundle_post.call_count == 1 @@ -322,7 +259,7 @@ def test_kwargs_pathname(self): bundle = content.bundles.create(pathname) # # assert - assert bundle.id == "101" + assert bundle["id"] == "101" assert mock_content_get.call_count == 1 assert mock_bundle_post.call_count == 1 @@ -371,7 +308,7 @@ def test(self): assert mock_content_get.call_count == 1 assert mock_bundles_get.call_count == 1 assert len(bundles) == 1 - assert bundles[0].id == "101" + assert bundles[0]["id"] == "101" class TestBundlesFindOne: @@ -399,7 +336,8 @@ def test(self): # assert assert mock_content_get.call_count == 1 assert mock_bundles_get.call_count == 1 - assert bundle.id == "101" + assert bundle + assert bundle["id"] == "101" class TestBundlesGet: @@ -428,4 +366,4 @@ def test(self): # assert assert mock_content_get.call_count == 1 assert mock_bundle_get.call_count == 1 - assert bundle.id == bundle_id + assert bundle["id"] == bundle_id diff --git a/tests/posit/connect/test_client.py b/tests/posit/connect/test_client.py index 89f9373c..5425a301 100644 --- a/tests/posit/connect/test_client.py +++ b/tests/posit/connect/test_client.py @@ -127,7 +127,7 @@ def test_me_request(self): ) con = Client(api_key="12345", url="https://connect.example/") - assert con.me.username == "carlos12" + assert con.me["username"] == "carlos12" def test_request(self, MockSession): api_key = "12345" diff --git a/tests/posit/connect/test_content.py b/tests/posit/connect/test_content.py index 8aee9602..964d4258 100644 --- a/tests/posit/connect/test_content.py +++ b/tests/posit/connect/test_content.py @@ -88,7 +88,7 @@ def test(self): task = content.deploy() # assert - assert task.id == task_id + assert task["id"] == task_id assert mock_content_get.call_count == 1 assert mock_content_deploy.call_count == 1 assert mock_tasks_get.call_count == 1 @@ -104,7 +104,7 @@ def test_update(self): ) con = Client("https://connect.example", "12345") content = con.content.get(guid) - assert content.guid == guid + assert content["guid"] == guid new_name = "New Name" fake_content = load_mock(f"v1/content/{guid}.json") @@ -115,7 +115,7 @@ def test_update(self): ) content.update(name=new_name) - assert content.name == new_name + assert content["name"] == new_name class TestContentCreate: @@ -139,7 +139,7 @@ def test(self): content_item = client.content.create(name=fake_content_item["name"]) # assert - assert content_item.name == fake_content_item["name"] + assert content_item["name"] == fake_content_item["name"] class TestContentsFind: @@ -160,9 +160,9 @@ def test(self): # assert assert mock_get.call_count == 1 assert len(content) == 3 - assert content[0].name == "team-admin-dashboard" - assert content[1].name == "Performance-Data-1671216053560" - assert content[2].name == "My-Streamlit-app" + assert content[0]["name"] == "team-admin-dashboard" + assert content[1]["name"] == "Performance-Data-1671216053560" + assert content[2]["name"] == "My-Streamlit-app" @responses.activate def test_params_include(self): @@ -237,7 +237,7 @@ def test(self): # assert assert mock_get.call_count == 1 assert content - assert content.name == "team-admin-dashboard" + assert content["name"] == "team-admin-dashboard" @responses.activate def test_miss(self): @@ -297,7 +297,7 @@ def test_owner_guid(self): # assert assert mock_get.call_count == 1 assert content_item - assert content_item.owner_guid == owner_guid + assert content_item["owner_guid"] == owner_guid @responses.activate def test_name(self): @@ -319,7 +319,7 @@ def test_name(self): # assert assert mock_get.call_count == 1 assert content_item - assert content_item.name == name + assert content_item["name"] == name @responses.activate def test_params_include(self): @@ -367,7 +367,7 @@ def test(self): ) con = Client("https://connect.example", "12345") get_one = con.content.get("f2f37341-e21d-3d80-c698-a935ad614066") - assert get_one.name == "Performance-Data-1671216053560" + assert get_one["name"] == "Performance-Data-1671216053560" class TestContentsCount: diff --git a/tests/posit/connect/test_groups.py b/tests/posit/connect/test_groups.py index d63beed8..5ad02912 100644 --- a/tests/posit/connect/test_groups.py +++ b/tests/posit/connect/test_groups.py @@ -17,10 +17,10 @@ def setup_class(cls): cls.item = Group(mock.Mock(), **fake_item) def test_guid(self): - assert self.item.guid == "6f300623-1e0c-48e6-a473-ddf630c0c0c3" + assert self.item["guid"] == "6f300623-1e0c-48e6-a473-ddf630c0c0c3" def test_name(self): - assert self.item.name == "Friends" + assert self.item["name"] == "Friends" def test_owner_guid(self): - assert self.item.owner_guid == "20a79ce3-6e87-4522-9faf-be24228800a4" + assert self.item["owner_guid"] == "20a79ce3-6e87-4522-9faf-be24228800a4" diff --git a/tests/posit/connect/test_permissions.py b/tests/posit/connect/test_permissions.py index dad2a12e..b8ebac16 100644 --- a/tests/posit/connect/test_permissions.py +++ b/tests/posit/connect/test_permissions.py @@ -116,9 +116,9 @@ def test_role_update(self): permission = Permission(params, id=uid, content_guid=content_guid, role=old_role) # assert role change with respect to api response - assert permission.role == old_role + assert permission["role"] == old_role permission.update(role=new_role) - assert permission.role == new_role + assert permission["role"] == new_role class TestPermissionsCount: diff --git a/tests/posit/connect/test_tasks.py b/tests/posit/connect/test_tasks.py index 28cdd718..556727d1 100644 --- a/tests/posit/connect/test_tasks.py +++ b/tests/posit/connect/test_tasks.py @@ -17,13 +17,13 @@ def setup_class(cls): ) def test_id(self): - assert self.task.id == "jXhOhdm5OOSkGhJw" + assert self.task["id"] == "jXhOhdm5OOSkGhJw" def test_is_finished(self): assert self.task.is_finished def test_output(self): - assert self.task.output == [ + assert self.task["output"] == [ "Building static content...", "Launching static content...", ] @@ -38,7 +38,7 @@ def test_error_message(self): ) def test_result(self): - assert self.task.result is None + assert self.task["result"] is None class TestTaskUpdate: @@ -152,5 +152,5 @@ def test(self): task = c.tasks.get(uid) # assert - assert task.id == uid + assert task["id"] == uid assert mock_tasks_get.call_count == 1 diff --git a/tests/posit/connect/test_users.py b/tests/posit/connect/test_users.py index c414a5c7..525ddeba 100644 --- a/tests/posit/connect/test_users.py +++ b/tests/posit/connect/test_users.py @@ -1,4 +1,3 @@ -from unittest import mock from unittest.mock import Mock import pytest @@ -7,7 +6,6 @@ from responses import matchers from posit.connect.client import Client -from posit.connect.users import User from .api import load_mock # type: ignore @@ -15,85 +13,6 @@ url = Mock() -class TestUserAttributes: - def test_guid(self): - user = User(mock.Mock()) - assert hasattr(user, "guid") - assert user.guid is None - user = User(mock.Mock(), guid="test_guid") - assert user.guid == "test_guid" - - def test_email(self): - user = User(mock.Mock()) - assert hasattr(user, "email") - assert user.email is None - user = User(mock.Mock(), email="test@example.com") - assert user.email == "test@example.com" - - def test_username(self): - user = User(mock.Mock()) - assert hasattr(user, "username") - assert user.username is None - user = User(mock.Mock(), username="test_user") - assert user.username == "test_user" - - def test_first_name(self): - user = User(mock.Mock()) - assert hasattr(user, "first_name") - assert user.first_name is None - user = User(mock.Mock(), first_name="John") - assert user.first_name == "John" - - def test_last_name(self): - user = User(mock.Mock()) - assert hasattr(user, "last_name") - assert user.last_name is None - user = User(mock.Mock(), last_name="Doe") - assert user.last_name == "Doe" - - def test_user_role(self): - user = User(mock.Mock()) - assert hasattr(user, "user_role") - assert user.user_role is None - user = User(mock.Mock(), user_role="admin") - assert user.user_role == "admin" - - def test_created_time(self): - user = User(mock.Mock()) - assert hasattr(user, "created_time") - assert user.created_time is None - user = User(mock.Mock(), created_time="2022-01-01T00:00:00") - assert user.created_time == "2022-01-01T00:00:00" - - def test_updated_time(self): - user = User(mock.Mock()) - assert hasattr(user, "updated_time") - assert user.updated_time is None - user = User(mock.Mock(), updated_time="2022-01-01T00:00:00") - assert user.updated_time == "2022-01-01T00:00:00" - - def test_active_time(self): - user = User(mock.Mock()) - assert hasattr(user, "active_time") - assert user.active_time is None - user = User(mock.Mock(), active_time="2022-01-01T00:00:00") - assert user.active_time == "2022-01-01T00:00:00" - - def test_confirmed(self): - user = User(mock.Mock()) - assert hasattr(user, "confirmed") - assert user.confirmed is None - user = User(mock.Mock(), confirmed=True) - assert user.confirmed is True - - def test_locked(self): - user = User(mock.Mock()) - assert hasattr(user, "locked") - assert user.locked is None - user = User(mock.Mock(), locked=False) - assert user.locked is False - - class TestUserContent: """Check behavior of content attribute.""" @@ -139,7 +58,7 @@ def test_lock(self): ) c = Client(api_key="12345", url="https://connect.example/") user = c.users.get("a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6") - assert user.guid == "a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6" + assert user["guid"] == "a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6" responses.get( "https://connect.example/__api__/v1/user", @@ -150,7 +69,7 @@ def test_lock(self): match=[responses.matchers.json_params_matcher({"locked": True})], ) user.lock() - assert user.locked + assert user["locked"] @responses.activate def test_lock_self_true(self): @@ -160,7 +79,7 @@ def test_lock_self_true(self): ) c = Client(api_key="12345", url="https://connect.example/") user = c.users.get("20a79ce3-6e87-4522-9faf-be24228800a4") - assert user.guid == "20a79ce3-6e87-4522-9faf-be24228800a4" + assert user["guid"] == "20a79ce3-6e87-4522-9faf-be24228800a4" responses.get( "https://connect.example/__api__/v1/user", @@ -171,7 +90,7 @@ def test_lock_self_true(self): match=[responses.matchers.json_params_matcher({"locked": True})], ) user.lock(force=True) - assert user.locked + assert user["locked"] @responses.activate def test_lock_self_false(self): @@ -181,7 +100,7 @@ def test_lock_self_false(self): ) c = Client(api_key="12345", url="https://connect.example/") user = c.users.get("20a79ce3-6e87-4522-9faf-be24228800a4") - assert user.guid == "20a79ce3-6e87-4522-9faf-be24228800a4" + assert user["guid"] == "20a79ce3-6e87-4522-9faf-be24228800a4" responses.get( "https://connect.example/__api__/v1/user", @@ -193,7 +112,7 @@ def test_lock_self_false(self): ) with pytest.raises(RuntimeError): user.lock(force=False) - assert not user.locked + assert not user["locked"] class TestUserUnlock: @@ -205,14 +124,14 @@ def test_unlock(self): ) c = Client(api_key="12345", url="https://connect.example/") user = c.users.get("20a79ce3-6e87-4522-9faf-be24228800a4") - assert user.guid == "20a79ce3-6e87-4522-9faf-be24228800a4" + assert user["guid"] == "20a79ce3-6e87-4522-9faf-be24228800a4" responses.post( "https://connect.example/__api__/v1/users/20a79ce3-6e87-4522-9faf-be24228800a4/lock", match=[responses.matchers.json_params_matcher({"locked": False})], ) user.unlock() - assert not user.locked + assert not user["locked"] class TestUsers: @@ -225,9 +144,9 @@ def test_users_get(self): con = Client(api_key="12345", url="https://connect.example/") carlos = con.users.get("20a79ce3-6e87-4522-9faf-be24228800a4") - assert carlos.username == "carlos12" - assert carlos.first_name == "Carlos" - assert carlos.created_time == "2019-09-09T15:24:32Z" + assert carlos["username"] == "carlos12" + assert carlos["first_name"] == "Carlos" + assert carlos["created_time"] == "2019-09-09T15:24:32Z" @responses.activate def test_users_get_extra_fields(self): @@ -242,7 +161,7 @@ def test_users_get_extra_fields(self): con = Client(api_key="12345", url="https://connect.example/") carlos = con.users.get("20a79ce3-6e87-4522-9faf-be24228800a4") - assert carlos.username == "carlos12" + assert carlos["username"] == "carlos12" assert carlos["some_new_field"] == "some_new_value" @responses.activate @@ -261,12 +180,12 @@ def test_user_update(self): carlos = con.users.get("20a79ce3-6e87-4522-9faf-be24228800a4") assert patch_request.call_count == 0 - assert carlos.first_name == "Carlos" + assert carlos["first_name"] == "Carlos" carlos.update(first_name="Carlitos") assert patch_request.call_count == 1 - assert carlos.first_name == "Carlitos" + assert carlos["first_name"] == "Carlitos" @responses.activate def test_user_update_server_error(self): @@ -306,7 +225,8 @@ def test_default(self): ) con = Client(api_key="12345", url="https://connect.example/") user = con.users.find_one() - assert user.username == "al" + assert user + assert user["username"] == "al" assert len(responses.calls) == 1 @responses.activate