From 7a7183c5b9ba5cdb18a64c478b47edf4bd7fe739 Mon Sep 17 00:00:00 2001 From: Mayank Dhiman Date: Thu, 19 Nov 2020 12:39:09 -0800 Subject: [PATCH 1/3] expose permission's audit value --- groupy/resources.py | 13 ++++++++----- groupy/version.py | 2 +- tests/fixtures.py | 16 ++++++++++++++++ tests/test_resources.py | 14 ++++++++++++++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/groupy/resources.py b/groupy/resources.py index 5b028ba..8b93092 100644 --- a/groupy/resources.py +++ b/groupy/resources.py @@ -104,26 +104,28 @@ class ServiceAccount(User): class Permission(object): - def __init__(self, groups): - # type: (Dict[str, Dict[str, Any]]) -> None + def __init__(self, groups, audited): + # type: (Dict[str, Dict[str, Any]], Dict[str, Any]) -> None self.groups = { groupname: Group.from_payload({"data": groups[groupname]}) for groupname in groups } + self.audited = audited.get("audited") @classmethod def from_payload(cls, payload): # type: (Dict[str, Any]) -> Permission - return cls(payload["data"]["groups"]) + return cls(payload["data"]["groups"], payload["data"]["audited"],) class MappedPermission(object): - def __init__(self, permission, argument, granted_on, distance, path): - # type: (str, str, float, Optional[int], Optional[List[str]]) -> None + def __init__(self, permission, argument, granted_on, distance, path, audited): + # type: (str, str, float, Optional[int], Optional[List[str]], Optional[bool]) -> None self.permission = permission self.argument = argument self.granted_on = granted_on self.distance = distance self.path = path + self.audited = audited @classmethod def from_payload(cls, payload): @@ -134,6 +136,7 @@ def from_payload(cls, payload): payload["granted_on"], payload.get("distance"), payload.get("path"), + payload.get("audited"), ) diff --git a/groupy/version.py b/groupy/version.py index 7225152..43a1e95 100644 --- a/groupy/version.py +++ b/groupy/version.py @@ -1 +1 @@ -__version__ = "0.5.2" +__version__ = "0.5.3" diff --git a/tests/fixtures.py b/tests/fixtures.py index 5b1a3b4..73668dc 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -34,6 +34,22 @@ def service_account_response(request): } +@pytest.fixture +def permission_response(request): + # type: (str) -> Dict[Text, Any] + return { + u"checkpoint": 3, + u"checkpoint_time": 1605842894, + u"data": { + u"permission": {u"name": "grouper.audit.security"}, + u"groups": {}, + u"service_accounts": {}, + u"audited": {u"audited": False,}, + }, + u"status": u"ok", + } + + @pytest.fixture def user_response(request): # type: (str) -> Dict[Text, Any] diff --git a/tests/test_resources.py b/tests/test_resources.py index 9488815..4b51ae4 100644 --- a/tests/test_resources.py +++ b/tests/test_resources.py @@ -4,6 +4,7 @@ from mock import Mock, patch from groupy.client import Groupy, HTTPClient +from tests.fixtures import permission_response # noqa: F401 from tests.fixtures import service_account_response # noqa: F401 if TYPE_CHECKING: @@ -32,3 +33,16 @@ def test_service_account(service_account_response): # noqa: F811 expected = service_account_response["data"]["user"]["service_account"] assert service.service_account == expected + + +def test_permission(permission_response): # noqa: F811 + # type: (Dict[Text, Any]) -> None + res = Mock() + res.body = json.dumps(permission_response) + mock_fetch = Mock() + mock_fetch.side_effect = [res] + with patch.object(HTTPClient, "fetch", mock_fetch): + client = Groupy(["localhost:8000"]) + permission = client.permissions.get("grouper.audit.security") + assert permission.groups == {} + assert permission.audited is False From 30671a914ab6ae6463511d8a02370386618f41b7 Mon Sep 17 00:00:00 2001 From: Mayank Dhiman Date: Thu, 19 Nov 2020 14:30:40 -0800 Subject: [PATCH 2/3] Flake8 typo fix --- tests/fixtures.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures.py b/tests/fixtures.py index 73668dc..1c93696 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -44,7 +44,7 @@ def permission_response(request): u"permission": {u"name": "grouper.audit.security"}, u"groups": {}, u"service_accounts": {}, - u"audited": {u"audited": False,}, + u"audited": {u"audited": False}, }, u"status": u"ok", } From 4c5d9c3540efb1c6958be1d2e02046b548207c21 Mon Sep 17 00:00:00 2001 From: Mayank Dhiman Date: Thu, 19 Nov 2020 23:28:01 -0800 Subject: [PATCH 3/3] unnest audited dict --- groupy/resources.py | 6 +++--- tests/fixtures.py | 2 +- tests/test_resources.py | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/groupy/resources.py b/groupy/resources.py index 8b93092..7148fc4 100644 --- a/groupy/resources.py +++ b/groupy/resources.py @@ -105,16 +105,16 @@ class ServiceAccount(User): class Permission(object): def __init__(self, groups, audited): - # type: (Dict[str, Dict[str, Any]], Dict[str, Any]) -> None + # type: (Dict[str, Dict[str, Any]], bool) -> None self.groups = { groupname: Group.from_payload({"data": groups[groupname]}) for groupname in groups } - self.audited = audited.get("audited") + self.audited = audited @classmethod def from_payload(cls, payload): # type: (Dict[str, Any]) -> Permission - return cls(payload["data"]["groups"], payload["data"]["audited"],) + return cls(payload["data"]["groups"], payload["data"]["audited"]) class MappedPermission(object): diff --git a/tests/fixtures.py b/tests/fixtures.py index 1c93696..d035f3e 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -44,7 +44,7 @@ def permission_response(request): u"permission": {u"name": "grouper.audit.security"}, u"groups": {}, u"service_accounts": {}, - u"audited": {u"audited": False}, + u"audited": False, }, u"status": u"ok", } diff --git a/tests/test_resources.py b/tests/test_resources.py index 4b51ae4..089e6ba 100644 --- a/tests/test_resources.py +++ b/tests/test_resources.py @@ -45,4 +45,5 @@ def test_permission(permission_response): # noqa: F811 client = Groupy(["localhost:8000"]) permission = client.permissions.get("grouper.audit.security") assert permission.groups == {} + assert isinstance(permission.audited, bool) assert permission.audited is False