Skip to content

Commit

Permalink
[Fixes #12713] fix black and flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiagiupponi committed Jan 13, 2025
1 parent e981421 commit 333d604
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 19 deletions.
27 changes: 23 additions & 4 deletions geonode/base/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,14 @@ def test_resource_service_copy_with_perms_dataset_set_default_perms(self):
}
resource.set_permissions(_perms)
# checking that bobby is in the original dataset perms list
self.assertTrue("bobby" in "bobby" in [x.username for x in permissions_registry.get_perms(instance=_resource, include_virtual=True).get("users", [])])
self.assertTrue(
"bobby"
in "bobby"
in [
x.username
for x in permissions_registry.get_perms(instance=resource, include_virtual=True).get("users", [])
]
)
# copying the resource, should remove the perms for bobby
# only the default perms should be available
copy_url = reverse("importer_resource_copy", kwargs={"pk": resource.pk})
Expand All @@ -2389,8 +2396,20 @@ def test_resource_service_copy_with_perms_dataset_set_default_perms(self):
self.assertEqual("finished", self.client.get(response.json().get("status_url")).json().get("status"))
_resource = Dataset.objects.filter(title__icontains="test_copy_with_perms").last()
self.assertIsNotNone(_resource)
self.assertNotIn("bobby", [x.username for x in permissions_registry.get_perms(instance=_resource, include_virtual=True).get("users", [])])
self.assertIn("admin", [x.username for x in permissions_registry.get_perms(instance=_resource, include_virtual=True).get("users", [])])
self.assertNotIn(
"bobby",
[
x.username
for x in permissions_registry.get_perms(instance=_resource, include_virtual=True).get("users", [])
],
)
self.assertIn(
"admin",
[
x.username
for x in permissions_registry.get_perms(instance=_resource, include_virtual=True).get("users", [])
],
)

def test_resource_service_copy_with_perms_doc(self):
files = os.path.join(gisdata.GOOD_DATA, "vector/single_point.shp")
Expand Down Expand Up @@ -3427,7 +3446,7 @@ def test_simple_resourcebase_can_be_created_by_resourcemanager(self):
},
"groups": {anonymous_group: set(["view_resourcebase"])},
}

actual_perms = permissions_registry.get_perms(instance=resource, include_virtual=True).copy()
self.assertIsNotNone(actual_perms)
self.assertTrue(self.user in actual_perms["users"].keys())
Expand Down
2 changes: 1 addition & 1 deletion geonode/documents/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def test_set_document_permissions(self):
self.assertFalse(self.anonymous_user.has_perm("view_resourcebase", document.get_self_resource()))

# Test that previous permissions for users other than ones specified in
# the perm_spec (and the document owner) were
# the perm_spec (and the document owner) were
current_perms = permissions_registry.get_perms(instance=document, include_virtual=True)
self.assertEqual(len(current_perms["users"]), 1)

Expand Down
4 changes: 3 additions & 1 deletion geonode/layers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ def set_datasets_permissions(
# for the selected resource
resource = resource.first()
# getting the actual permissions available for the dataset
original_perms = PermSpec(permissions_registry.get_perms(instance=resource, include_virtual=False), resource)
original_perms = PermSpec(
permissions_registry.get_perms(instance=resource, include_virtual=False), resource
)
new_perms_payload = {"organizations": [], "users": [], "groups": []}
# if the username is specified, we add them to the payload with the compact
# perm value
Expand Down
2 changes: 1 addition & 1 deletion geonode/security/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get_perms(self, instance, user=None, include_virtual=True, *args, **kwargs):

for handler in self.REGISTRY:
payload = handler.get_perms(instance, payload, user, include_virtual=include_virtual, *args, **kwargs)

if user:
return payload["users"][user]
return payload
Expand Down
38 changes: 27 additions & 11 deletions geonode/security/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2313,7 +2313,10 @@ def test_if_anonymoys_default_perms_is_false_should_not_assign_perms_to_user_gro
"""

resource = resource_manager.create(str(uuid.uuid4), Dataset, defaults={"owner": self.group_member})
self.assertFalse(self.group_profile.group in permissions_registry.get_perms(instance=resource, include_virtual=True)["groups"].keys())
self.assertFalse(
self.group_profile.group
in permissions_registry.get_perms(instance=resource, include_virtual=True)["groups"].keys()
)

@override_settings(DEFAULT_ANONYMOUS_DOWNLOAD_PERMISSION=False)
def test_if_anonymoys_default_download_perms_is_false_should_not_assign_perms_to_user_group(self):
Expand All @@ -2322,7 +2325,10 @@ def test_if_anonymoys_default_download_perms_is_false_should_not_assign_perms_to
"""

resource = resource_manager.create(str(uuid.uuid4), Dataset, defaults={"owner": self.group_member})
self.assertFalse(self.group_profile.group in permissions_registry.get_perms(instance=resource, include_virtual=True)["groups"].keys())
self.assertFalse(
self.group_profile.group
in permissions_registry.get_perms(instance=resource, include_virtual=True)["groups"].keys()
)

@override_settings(DEFAULT_ANONYMOUS_DOWNLOAD_PERMISSION=False)
@override_settings(RESOURCE_PUBLISHING=True)
Expand All @@ -2333,8 +2339,13 @@ def test_if_anonymoys_default_perms_is_false_should_assign_perms_to_user_group_i
"""

resource = resource_manager.create(str(uuid.uuid4), Dataset, defaults={"owner": self.group_member})
self.assertTrue(self.group_profile.group in permissions_registry.get_perms(instance=resource, include_virtual=True)["groups"].keys())
group_val = permissions_registry.get_perms(instance=resource, include_virtual=True)["groups"][self.group_profile.group]
self.assertTrue(
self.group_profile.group
in permissions_registry.get_perms(instance=resource, include_virtual=True)["groups"].keys()
)
group_val = permissions_registry.get_perms(instance=resource, include_virtual=True)["groups"][
self.group_profile.group
]
self.assertSetEqual({"view_resourcebase", "download_resourcebase"}, set(group_val))

@override_settings(DEFAULT_ANONYMOUS_VIEW_PERMISSION=False)
Expand All @@ -2349,8 +2360,13 @@ def test_if_anonymoys_default_perms_is_false_should_assign_perms_to_user_group_i

resource = resource_manager.create(str(uuid.uuid4), Dataset, defaults={"owner": self.group_member})

self.assertTrue(self.group_profile.group in permissions_registry.get_perms(instance=resource, include_virtual=True)["groups"].keys())
group_val = permissions_registry.get_perms(instance=resource, include_virtual=True)["groups"][self.group_profile.group]
self.assertTrue(
self.group_profile.group
in permissions_registry.get_perms(instance=resource, include_virtual=True)["groups"].keys()
)
group_val = permissions_registry.get_perms(instance=resource, include_virtual=True)["groups"][
self.group_profile.group
]
self.assertSetEqual({"view_resourcebase", "download_resourcebase"}, set(group_val))


Expand Down Expand Up @@ -2411,7 +2427,7 @@ def setUp(self):
assign_perm(perm, self.member_with_perms, self.resource.get_self_resource())

# Assert inital assignment of permissions to groups and users
resource_perm_specs = self.permissions_registry.get_perms(instance=resource, include_virtual=True)
resource_perm_specs = self.permissions_registry.get_perms(instance=self.resource, include_virtual=True)
self.assertSetEqual(
set(resource_perm_specs["users"][self.author]), set(self.owner_perms + self.edit_perms + self.dataset_perms)
)
Expand Down Expand Up @@ -2460,7 +2476,7 @@ def test_owner_is_group_manager(self):
# Admin publishes and approves the resource
response = self.admin_approve_and_publish_resource()
self.assertEqual(response.status_code, 200)
resource_perm_specs = self.permissions_registry.get_perms(instance=resource, include_virtual=True)
resource_perm_specs = self.permissions_registry.get_perms(instance=self.resource, include_virtual=True)

# Once a resource has been published, the 'publish_resourcebase' permission should be removed anyway
self.assertSetEqual(
Expand All @@ -2471,7 +2487,7 @@ def test_owner_is_group_manager(self):
# Admin un-approves and un-publishes the resource
response = self.admin_unapprove_and_unpublish_resource()
self.assertEqual(response.status_code, 200)
resource_perm_specs = self.permissions_registry.get_perms(instance=resource, include_virtual=True)
resource_perm_specs = self.permissions_registry.get_perms(instance=self.resource, include_virtual=True)

self.assertSetEqual(
set(resource_perm_specs["users"][self.author]),
Expand All @@ -2481,7 +2497,7 @@ def test_owner_is_group_manager(self):
GroupMember.objects.get(group=self.owner_group, user=self.author).demote()

def assertions_for_approved_or_published_is_true(self):
resource_perm_specs = self.permissions_registry.get_perms(instance=resource, include_virtual=True)
resource_perm_specs = self.permissions_registry.get_perms(instance=self.resource, include_virtual=True)
self.assertSetEqual(set(resource_perm_specs["users"][self.author]), set(self.owner_perms))
self.assertSetEqual(
set(resource_perm_specs["users"][self.member_with_perms]), set(self.owner_perms + self.dataset_perms)
Expand All @@ -2498,7 +2514,7 @@ def assertions_for_approved_or_published_is_true(self):
self.assertSetEqual(set(resource_perm_specs["groups"][self.resource_group.group]), set(self.safe_perms))

def assertions_for_approved_and_published_is_false(self):
resource_perm_specs = self.permissions_registry.get_perms(instance=resource, include_virtual=True)
resource_perm_specs = self.permissions_registry.get_perms(instance=self.resource, include_virtual=True)
self.assertSetEqual(
set(resource_perm_specs["users"][self.author]), set(self.owner_perms + self.edit_perms + self.dataset_perms)
)
Expand Down
4 changes: 3 additions & 1 deletion geonode/security/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,9 @@ def get_permissions(
_permissions = copy.deepcopy(permissions)

if _resource:
perm_spec = _permissions or copy.deepcopy(permissions_registry.get_perms(instance=_resource, include_virtual=True))
perm_spec = _permissions or copy.deepcopy(
permissions_registry.get_perms(instance=_resource, include_virtual=True)
)

# Sanity checks
if isinstance(perm_spec, str):
Expand Down

0 comments on commit 333d604

Please sign in to comment.