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

Fix ResourceWarnings for unclosed files and fix DeprecationWarnings. #97

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions news/4090.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ResourceWarnings for unclosed files and fix DeprecationWarnings. [maurits]
2 changes: 1 addition & 1 deletion src/Products/PlonePAS/interfaces/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def getRolesInContext(user, object):
o Include *both* local roles assigned directly to us *and* those
assigned to our groups.

o Ripped off from AccessControl.User.BasicUser, which provides
o Ripped off from AccessControl.users.BasicUser, which provides
no other extension mechanism. :(
"""

Expand Down
2 changes: 1 addition & 1 deletion src/Products/PlonePAS/pas.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def _getAllLocalRoles(self, context):


def authenticate(self, name, password, request):
"""See AccessControl.User.BasicUserFolder.authenticate
"""See AccessControl.userfolder:BasicUserFolder.authenticate

Products.PluggableAuthService.PluggableAuthService does not provide this
method, BasicUserFolder documents it as "Private UserFolder object
Expand Down
22 changes: 17 additions & 5 deletions src/Products/PlonePAS/tests/test_membershiptool.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from AccessControl import getSecurityManager
from AccessControl import Unauthorized
from AccessControl.User import nobody
from AccessControl.users import nobody
from Acquisition import aq_base
from Acquisition import aq_parent
from DateTime import DateTime
from io import BytesIO
from OFS.Image import Image
from pathlib import Path
from plone.app.testing import login
from plone.app.testing import logout
from plone.app.testing import PLONE_SITE_ID
Expand All @@ -28,7 +29,6 @@
from zope.component import adapter
from zope.component import getGlobalSiteManager

import os
import unittest


Expand Down Expand Up @@ -205,10 +205,10 @@ def addMember(self, username, fullname, email, roles, last_login_time):
)

def makeRealImage(self):
# Note: the caller is responsible for calling 'close' in this image after use.
import Products.PlonePAS as ppas

pas_path = os.path.dirname(ppas.__file__)
path = os.path.join(pas_path, "tool.gif")
path = Path(ppas.__file__).parent / "tool.gif"
image = open(path, "rb")
image_upload = dummy.FileUpload(dummy.FieldStorage(image))
return image_upload
Expand Down Expand Up @@ -242,6 +242,7 @@ def testChangeOwnMemberPortrait(self):
self.assertEqual(
self.membership.getPersonalPortrait(TEST_USER_ID).meta_type, "Image"
)
image.close()

def testChangeOwnMemberPortraitWithEmailUsers(self):
member_id = "[email protected]"
Expand All @@ -264,6 +265,7 @@ def testChangeOwnMemberPortraitWithEmailUsers(self):
self.assertEqual(
self.membership.getPersonalPortrait(member_id).meta_type, "Image"
)
image.close()

def testCannotChangeOtherMemberPortrait(self):
# A normal member should not be able to change the portrait of
Expand All @@ -274,6 +276,7 @@ def testCannotChangeOtherMemberPortrait(self):
self.assertRaises(
Unauthorized, self.membership.changeMemberPortrait, image, "joe"
)
image.close()

def testChangeMemberPortraitAsManager(self):
# Managers should be able to change the portrait of another
Expand All @@ -285,6 +288,7 @@ def testChangeMemberPortraitAsManager(self):
self.membership.changeMemberPortrait(image, "joe")
self.assertEqual(self.membership.getPersonalPortrait("joe").getId(), "joe")
self.assertEqual(self.membership.getPersonalPortrait("joe").meta_type, "Image")
image.close()

def testDeleteOwnPersonalPortrait(self):
# Should delete the portrait image
Expand All @@ -297,6 +301,7 @@ def testDeleteOwnPersonalPortrait(self):
self.assertEqual(
self.membership.getPersonalPortrait(TEST_USER_ID).getId(), "defaultUser.png"
)
image.close()

def testCannotDeleteOtherPersonalPortrait(self):
# A normal member should not be able to delete the portrait of
Expand All @@ -307,6 +312,7 @@ def testCannotDeleteOtherPersonalPortrait(self):
self.membership.changeMemberPortrait(image, "joe")
setRoles(self.portal, TEST_USER_ID, ["Member"])
self.assertRaises(Unauthorized, self.membership.deletePersonalPortrait, "joe")
image.close()

def testDeleteOtherPersonalPortraitAsManager(self):
# Managers should be able to change the portrait of another
Expand All @@ -319,13 +325,15 @@ def testDeleteOtherPersonalPortraitAsManager(self):
self.assertEqual(
self.membership.getPersonalPortrait("joe").getId(), "defaultUser.png"
)
image.close()

def testGetPersonalPortraitWithoutPassingId(self):
# Should return the logged in users portrait if no id is given
image = self.makeRealImage()
self.membership.changeMemberPortrait(image, TEST_USER_ID)
self.assertEqual(self.membership.getPersonalPortrait().getId(), TEST_USER_ID)
self.assertEqual(self.membership.getPersonalPortrait().meta_type, "Image")
image.close()

def testPortraitForNonStandardUserId(self):
# Some characters in a user id can give problems for getting
Expand All @@ -346,6 +354,7 @@ def testPortraitForNonStandardUserId(self):
self.membership.changeMemberPortrait(image, user_id)
self.assertEqual(self.membership.getPersonalPortrait().getId(), safe_id)
self.assertEqual(self.membership.getPersonalPortrait().meta_type, "Image")
image.close()

# Other users should be able to see your portrait.
login(self.portal, TEST_USER_NAME)
Expand All @@ -372,6 +381,7 @@ def testPortraitForNonStandardUserId(self):
self.assertEqual(
self.membership.getPersonalPortrait(user_id).getId(), "defaultUser.png"
)
manager_image.close()

def testListMembers(self):
# Should return the members list
Expand Down Expand Up @@ -670,8 +680,10 @@ def testGetBadMembers(self):
self.portal.portal_memberdata._setPortrait(empty_file, TEST_USER_ID)
self.assertEqual(self.membership.getBadMembers(), [])
# And a good image
self.membership.changeMemberPortrait(self.makeRealImage(), TEST_USER_ID)
image = self.makeRealImage()
self.membership.changeMemberPortrait(image, TEST_USER_ID)
self.assertEqual(self.membership.getBadMembers(), [])
image.close()

def test_credentials_updated_event(self):
events_fired = []
Expand Down
19 changes: 9 additions & 10 deletions src/Products/PlonePAS/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,19 @@ def scale_image(image_file, max_size=None, default_format=None):
from an input file

>>> from Products.PlonePAS import config
>>> import os
>>> from pathlib import Path
>>> from io import BytesIO
>>> from Products.PlonePAS.utils import scale_image
>>> from PIL import Image

Let's make a couple test images and see how it works (all are
100x100), the gif is paletted mode::

>>> pas_path = os.path.dirname(config.__file__)
>>> pjoin = os.path.join
>>> path = pjoin(pas_path, 'tests', 'images')
>>> orig_jpg = open(pjoin(path, 'test.jpg'), 'rb')
>>> orig_png = open(pjoin(path, 'test.png'), 'rb')
>>> orig_gif = open(pjoin(path, 'test.gif'), 'rb')
>>> pas_path = Path(config.__file__).parent
>>> path = pas_path / 'tests' / 'images'
>>> orig_jpg = (path / 'test.jpg').open('rb')
>>> orig_png = (path / 'test.png').open('rb')
>>> orig_gif = (path / 'test.gif').open('rb')

We'll also make some evil non-images, including one which
masquerades as a jpeg (which would trick OFS.Image)::
Expand Down Expand Up @@ -133,9 +132,9 @@ def scale_image(image_file, max_size=None, default_format=None):
>>> img.size
(50, 50)

orig_jpg.close()
orig_png.close()
orig_gif.close()
>>> orig_jpg.close()
>>> orig_png.close()
>>> orig_gif.close()

"""
from PIL import Image
Expand Down
Loading