1 | """Test object history. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | import shutil |
---|
5 | import tempfile |
---|
6 | from persistent.list import PersistentList |
---|
7 | from zope.interface.verify import verifyObject, verifyClass |
---|
8 | from zope.security.management import newInteraction, endInteraction |
---|
9 | from zope.security.testing import Principal, Participation |
---|
10 | from waeup.sirp.app import University |
---|
11 | from waeup.sirp.interfaces import IObjectHistory, IWAeUPObject |
---|
12 | from waeup.sirp.testing import FunctionalTestCase, FunctionalLayer |
---|
13 | from waeup.sirp.objecthistory import ObjectHistory |
---|
14 | |
---|
15 | class SampleObject(grok.Model): |
---|
16 | grok.implements(IWAeUPObject) |
---|
17 | pass |
---|
18 | |
---|
19 | class 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> - blah by system', 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> - blah by bob', 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] |
---|