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

Last change on this file since 7194 was 7193, checked in by Henrik Bettermann, 13 years ago

More copyright adjustments.

  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1## $Id: test_objecthistory.py 7193 2011-11-25 07:21:29Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""Test object history.
19"""
20import grok
21import shutil
22import tempfile
23from persistent.list import PersistentList
24from zope.interface.verify import verifyObject, verifyClass
25from zope.security.management import newInteraction, endInteraction
26from zope.security.testing import Principal, Participation
27from waeup.sirp.app import University
28from waeup.sirp.interfaces import IObjectHistory, IWAeUPObject
29from waeup.sirp.testing import FunctionalTestCase, FunctionalLayer
30from waeup.sirp.objecthistory import ObjectHistory
31
32class SampleObject(grok.Model):
33    grok.implements(IWAeUPObject)
34    pass
35
36class ObjectHistoryTests(FunctionalTestCase):
37    # Tests for helpers like get_access_code, disable_accesscode, ...
38
39    layer = FunctionalLayer
40
41    def setUp(self):
42        super(ObjectHistoryTests, self).setUp()
43
44        # Prepopulate ZODB
45        app = University()
46        self.dc_root = tempfile.mkdtemp()
47        app['datacenter'].setStoragePath(self.dc_root)
48
49        # Prepopulate the ZODB...
50        self.getRootFolder()['app'] = app
51        self.app = self.getRootFolder()['app']
52        self.obj = SampleObject()
53
54    def tearDown(self):
55        shutil.rmtree(self.dc_root)
56        super(ObjectHistoryTests, self).tearDown()
57        endInteraction() # Just in case, one is still lingering around
58
59    def test_iface(self):
60        # ObjectHistory class and instances provide the promised ifaces
61        hist = IObjectHistory(self.obj)
62        assert verifyObject(IObjectHistory, hist)
63        assert verifyClass(IObjectHistory, ObjectHistory)
64
65    def test_adapter(self):
66        # We can get a history by adapting to IObjectHistory
67        result = IObjectHistory(self.obj)
68        assert isinstance(result, ObjectHistory)
69
70    def test_add_messages(self):
71        # We can add a message
72        hist = IObjectHistory(self.obj)
73        assert hist.messages == []
74        hist.addMessage('blah')
75        assert 'blah' in ''.join(hist.messages)
76
77    def test_add_messages_timestamp_and_user(self):
78        # Messages added get a timestamp and the current user
79        hist = IObjectHistory(self.obj)
80        hist.addMessage('blah')
81        result = ''.join(hist.messages)
82        self.assertMatches('<YYYY-MM-DD hh:mm:ss> - blah by system', result)
83
84    def test_add_messages_existing_principal(self):
85        principal = Principal('bob')
86        principal.title = 'Bob'
87        newInteraction(Participation(principal)) # set current user
88        hist = IObjectHistory(self.obj)
89        hist.addMessage('blah')
90        result = ''.join(hist.messages)
91        self.assertMatches('<YYYY-MM-DD hh:mm:ss> - blah by bob', result)
92
93    def test_messages(self):
94        # we get messages as a persistent list of strings
95        hist = IObjectHistory(self.obj)
96        hist.addMessage('foo')
97        hist.addMessage('bar')
98        assert isinstance(hist.messages, PersistentList)
99        assert 'foo' in hist.messages[0]
100        assert 'bar' in hist.messages[1]
Note: See TracBrowser for help on using the repository browser.