Skip to content

Commit

Permalink
Use object representation to log life cycle events
Browse files Browse the repository at this point in the history
This fixes an issue with Archetypes-based objects being dumped to the log.
  • Loading branch information
hvelarde committed May 6, 2016
1 parent 1b3effa commit 9fe1a77
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 24 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Expand Up @@ -4,6 +4,10 @@ Changelog
1.0b4 (unreleased)
------------------

- Use object representation to log life cycle events;
this fixes an issue with Archetypes-based objects being dumped to the log (refs. `#8`_ and fixes `#38`_).
[hvelarde]

- Package is now compatible with Plone 5.0 and Plone 5.1.
[hvelarde]

Expand Down Expand Up @@ -84,7 +88,9 @@ Changelog
.. _`#1`: https://github.com/collective/collective.fingerpointing/issues/1
.. _`#2`: https://github.com/collective/collective.fingerpointing/issues/2
.. _`#4`: https://github.com/collective/collective.fingerpointing/issues/4
.. _`#8`: https://github.com/collective/collective.fingerpointing/issues/8
.. _`#15`: https://github.com/collective/collective.fingerpointing/issues/15
.. _`#18`: https://github.com/collective/collective.fingerpointing/issues/18
.. _`#25`: https://github.com/collective/collective.fingerpointing/issues/25
.. _`#30`: https://github.com/collective/collective.fingerpointing/issues/30
.. _`#38`: https://github.com/collective/collective.fingerpointing/issues/38
6 changes: 3 additions & 3 deletions src/collective/fingerpointing/subscribers/lifecycle_logger.py
Expand Up @@ -28,12 +28,12 @@ def lifecycle_logger(obj, event):

if IObjectCreatedEvent.providedBy(event):
action = 'create'
extras = 'object={0}'.format(obj)
extras = 'object={0}'.format(repr(obj))
if IObjectModifiedEvent.providedBy(event):
action = 'modify'
extras = 'object={0}'.format(obj)
extras = 'object={0}'.format(repr(obj))
if IObjectRemovedEvent.providedBy(event):
action = 'remove'
extras = 'object={0}'.format(obj)
extras = 'object={0}'.format(repr(obj))

log_info(AUDIT_MESSAGE.format(user, ip, action, extras))
2 changes: 1 addition & 1 deletion src/collective/fingerpointing/testing.py
Expand Up @@ -21,7 +21,7 @@
from plone.app.contenttypes.testing import PLONE_APP_CONTENTTYPES_FIXTURE as PLONE_FIXTURE


PLONE_VERSION = api.env.plone_version()
IS_PLONE_5 = api.env.plone_version().startswith('5')


class Fixture(PloneSandboxLayer):
Expand Down
76 changes: 56 additions & 20 deletions src/collective/fingerpointing/tests/test_lifecycle_subscriber.py
@@ -1,12 +1,12 @@
# -*- coding: utf-8 -*-
"""Tests for lifecycle subscriber.
Events are slightly different among Plone 4 and Plone 5. Also, Plone 5
content types have different names.
Events are slightly different among Archetypes and Dexterity.
Also, Dexterity content types have different names.
"""
from collective.fingerpointing.config import PROJECTNAME
from collective.fingerpointing.testing import INTEGRATION_TESTING
from collective.fingerpointing.testing import PLONE_VERSION
from collective.fingerpointing.testing import IS_PLONE_5
from logging import INFO
from plone import api
from testfixtures import LogCapture
Expand All @@ -25,38 +25,73 @@ def setUp(self):
with api.env.adopt_roles(['Manager']):
self.folder = api.content.create(self.portal, 'Folder', 'folder')

def test_object_created(self):
if PLONE_VERSION >= '5.0':
def test_file_lifecicle(self):
if IS_PLONE_5:
expected = (
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=create object=<File at foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<Folder at /plone/folder>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=remove object=<File at /plone/folder/foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<Folder at /plone/folder>'),
)
else:
expected = (
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=create object=<ATFile at /plone/folder/foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<ATFolder at /plone/folder>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<ATFile at /plone/folder/foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=remove object=<ATFile at /plone/folder/foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<ATFolder at /plone/folder>'),
)

with LogCapture('collective.fingerpointing', level=INFO) as log:
obj = api.content.create(self.folder, 'File', 'foo')
obj.reindexObject()
api.content.delete(obj)
log.check(*expected)

def test_folder_lifecicle(self):
if IS_PLONE_5:
expected = (
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=create object=<Folder at foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<Folder at folder>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<Folder at /plone/folder>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=remove object=<Folder at /plone/folder/foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<Folder at /plone/folder>'),
)
else:
expected = (
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=create object=<ATFolder at foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<ATFolder at folder>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<ATFolder at foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=create object=<ATFolder at /plone/folder/foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<ATFolder at /plone/folder>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<ATFolder at /plone/folder/foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=remove object=<ATFolder at /plone/folder/foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<ATFolder at /plone/folder>'),
)

with LogCapture('collective.fingerpointing', level=INFO) as log:
api.content.create(self.folder, 'Folder', 'foo')
obj = api.content.create(self.folder, 'Folder', 'foo')
obj.reindexObject()
api.content.delete(obj)
log.check(*expected)

def test_object_removed(self):
if PLONE_VERSION >= '5.0':
def test_image_lifecicle(self):
if IS_PLONE_5:
expected = (
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=remove object=<Folder at foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<Folder at folder>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=create object=<Image at foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<Folder at /plone/folder>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=remove object=<Image at /plone/folder/foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<Folder at /plone/folder>'),
)
else:
expected = (
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=remove object=<ATFolder at foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<ATFolder at folder>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=create object=<ATImage at /plone/folder/foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<ATFolder at /plone/folder>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<ATImage at /plone/folder/foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=remove object=<ATImage at /plone/folder/foo>'),
('collective.fingerpointing', 'INFO', 'user=test ip=127.0.0.1 action=modify object=<ATFolder at /plone/folder>'),
)

api.content.create(self.folder, 'Folder', 'foo')
with LogCapture('collective.fingerpointing', level=INFO) as log:
api.content.delete(self.folder['foo'])
obj = api.content.create(self.folder, 'Image', 'foo')
obj.reindexObject()
api.content.delete(obj)
log.check(*expected)

def test_susbcriber_ignored_when_package_not_installed(self):
Expand All @@ -67,5 +102,6 @@ def test_susbcriber_ignored_when_package_not_installed(self):
with api.env.adopt_roles(['Manager']):
qi.uninstallProducts(products=[PROJECTNAME])

api.content.create(self.folder, 'Folder', 'foo')
api.content.delete(self.folder['foo'])
obj = api.content.create(self.folder, 'Folder', 'foo')
obj.reindexObject()
api.content.delete(obj)

0 comments on commit 9fe1a77

Please sign in to comment.