source: main/waeup.sirp/trunk/src/waeup/sirp/tests/test_objecthistory.py @ 6467

Last change on this file since 6467 was 6467, checked in by uli, 13 years ago

Add tests for object history.

File size: 2.9 KB
Line 
1"""Test object history.
2"""
3import grok
4import shutil
5import tempfile
6from persistent.list import PersistentList
7from zope.interface.verify import verifyObject, verifyClass
8from zope.security.management import newInteraction, endInteraction
9from zope.security.testing import Principal, Participation
10from waeup.sirp.app import University
11from waeup.sirp.interfaces import IObjectHistory, IWAeUPObject
12from waeup.sirp.testing import FunctionalTestCase, FunctionalLayer
13from waeup.sirp.objecthistory import ObjectHistory
14
15class SampleObject(grok.Model):
16    grok.implements(IWAeUPObject)
17    pass
18
19class ObjectHistoryTests(FunctionalTestCase):
20    # Tests for helpers like get_access_code, disable_accesscode, ...
21
22    layer = FunctionalLayer
23
24    def setUp(self):
25        super(ObjectHistoryTests, self).setUp()
26
27        # Prepopulate ZODB
28        app = University()
29        self.dc_root = tempfile.mkdtemp()
30        app['datacenter'].setStoragePath(self.dc_root)
31
32        # Prepopulate the ZODB...
33        self.getRootFolder()['app'] = app
34        self.app = self.getRootFolder()['app']
35        self.obj = SampleObject()
36
37    def tearDown(self):
38        shutil.rmtree(self.dc_root)
39        super(ObjectHistoryTests, self).tearDown()
40        endInteraction() # Just in case, one is still lingering around
41
42    def test_iface(self):
43        # ObjectHistory class and instances provide the promised ifaces
44        hist = IObjectHistory(self.obj)
45        assert verifyObject(IObjectHistory, hist)
46        assert verifyClass(IObjectHistory, ObjectHistory)
47
48    def test_adapter(self):
49        # We can get a history by adapting to IObjectHistory
50        result = IObjectHistory(self.obj)
51        assert isinstance(result, ObjectHistory)
52
53    def test_add_messages(self):
54        # We can add a message
55        hist = IObjectHistory(self.obj)
56        assert hist.messages == []
57        hist.addMessage('blah')
58        assert 'blah' in ''.join(hist.messages)
59
60    def test_add_messages_timestamp_and_user(self):
61        # Messages added get a timestamp and the current user
62        hist = IObjectHistory(self.obj)
63        hist.addMessage('blah')
64        result = ''.join(hist.messages)
65        self.assertMatches('<YYYY-MM-DD hh:mm:ss> - system - blah', result)
66
67    def test_add_messages_existing_principal(self):
68        principal = Principal('bob')
69        principal.title = 'Bob'
70        newInteraction(Participation(principal)) # set current user
71        hist = IObjectHistory(self.obj)
72        hist.addMessage('blah')
73        result = ''.join(hist.messages)
74        self.assertMatches('<YYYY-MM-DD hh:mm:ss> - bob (Bob) - blah', result)
75
76    def test_messages(self):
77        # we get messages as a persistent list of strings
78        hist = IObjectHistory(self.obj)
79        hist.addMessage('foo')
80        hist.addMessage('bar')
81        assert isinstance(hist.messages, PersistentList)
82        assert 'foo' in hist.messages[0]
83        assert 'bar' in hist.messages[1]
Note: See TracBrowser for help on using the repository browser.