Back to index

plone3  3.1.7
test_adapter.py
Go to the documentation of this file.
00001 # -*- coding: UTF-8 -*-
00002 """
00003     StatusMessage adapter tests.
00004 """
00005 
00006 import unittest
00007 
00008 def test_directives():
00009     """
00010     Test status messages
00011 
00012     First some boilerplate.
00013 
00014       >>> from zope.component.testing import setUp
00015       >>> setUp()
00016 
00017       >>> import Products.Five
00018       >>> import Products.statusmessages
00019 
00020       >>> from Products.Five import zcml
00021       >>> zcml.load_config('meta.zcml', Products.Five)
00022       >>> zcml.load_config('configure.zcml', Products.statusmessages)
00023 
00024     Now lets make sure we can actually adapt the request.
00025 
00026       >>> from Products.statusmessages.interfaces import IStatusMessage
00027       >>> status = IStatusMessage(self.app.REQUEST)
00028       >>> IStatusMessage.providedBy(status)
00029       True
00030 
00031     We also need the request to be annotatable:
00032 
00033       >>> from zope.interface import directlyProvides
00034       >>> from zope.annotation.interfaces import IAttributeAnnotatable
00035       >>> directlyProvides(self.app.REQUEST, IAttributeAnnotatable)
00036 
00037     The dummy request we have is a bit limited, so we need a simple method
00038     to fake a real request/response for the cookie handling. Basically it
00039     puts all entries from RESPONSE.cookies into REQUEST.cookies but shifts
00040     the real values into the right place as browsers would do it.
00041       
00042       >>> def fakePublish(request):
00043       ...     cookies = request.RESPONSE.cookies.copy()
00044       ...     new_cookies = {}
00045       ...     for key in cookies.keys():
00046       ...         new_cookies[key] = cookies[key]['value']
00047       ...     request.cookies = new_cookies
00048       ...     request.RESPONSE.cookies = {}
00049 
00050       >>> request = self.app.REQUEST
00051       >>> status = IStatusMessage(request)
00052 
00053     Make sure there's no stored message.
00054 
00055       >>> len(status.showStatusMessages())
00056       0
00057 
00058     Add one message
00059       
00060       >>> status.addStatusMessage(u'test', type=u'info')
00061 
00062     Now check the results
00063 
00064       >>> messages = status.showStatusMessages()
00065       >>> len(messages)
00066       1
00067 
00068       >>> messages[0].message
00069       u'test'
00070 
00071       >>> messages[0].type
00072       u'info'
00073 
00074     Make sure messages are removed
00075 
00076       >>> len(status.showStatusMessages())
00077       0
00078 
00079     Since we accessed the message prior to publishing the page, we must 
00080     ensure that the messages have been removed from the cookies
00081 
00082       >>> fakePublish(request)
00083       >>> len(status.showStatusMessages())
00084       0
00085 
00086     Now we repeat the test, only this time we publish the page prior to
00087     retrieving the messages
00088 
00089     Add one message
00090       
00091       >>> status.addStatusMessage(u'test', type=u'info')
00092 
00093     Publish the request
00094 
00095       >>> fakePublish(request)
00096 
00097     Now check the results
00098 
00099       >>> messages = status.showStatusMessages()
00100       >>> len(messages)
00101       1
00102 
00103       >>> messages[0].message
00104       u'test'
00105 
00106       >>> messages[0].type
00107       u'info'
00108 
00109     Make sure messages are removed
00110 
00111       >>> len(status.showStatusMessages())
00112       0
00113 
00114     Add two messages (without publishing)
00115 
00116       >>> status.addStatusMessage(u'test', type=u'info')
00117       >>> status.addStatusMessage(u'test1', u'warn')
00118       
00119     And check the results again
00120 
00121       >>> messages = status.showStatusMessages()
00122       >>> len(messages)
00123       2
00124 
00125       >>> test = messages[1]
00126 
00127       >>> test.message
00128       u'test1'
00129 
00130       >>> test.type
00131       u'warn'
00132 
00133     Make sure messages are removed again
00134 
00135       >>> len(status.showStatusMessages())
00136       0
00137 
00138     Add two messages (with publishing)
00139 
00140       >>> status.addStatusMessage(u'test', type=u'info')
00141       >>> fakePublish(request)
00142       >>> status.addStatusMessage(u'test1', u'warn')
00143       
00144     And check the results again
00145 
00146       >>> fakePublish(request)
00147       >>> messages = status.showStatusMessages()
00148       >>> len(messages)
00149       2
00150 
00151       >>> test = messages[1]
00152 
00153       >>> test.message
00154       u'test1'
00155 
00156       >>> test.type
00157       u'warn'
00158 
00159     Make sure messages are removed again
00160 
00161       >>> len(status.showStatusMessages())
00162       0
00163 
00164     Add two identical messages
00165 
00166       >>> status.addStatusMessage(u'test', type=u'info')
00167       >>> status.addStatusMessage(u'test', type=u'info')
00168 
00169     And check the results again
00170 
00171       >>> fakePublish(request)
00172       >>> messages = status.showStatusMessages()
00173       >>> len(messages)
00174       1
00175 
00176       >>> test = messages[0]
00177 
00178       >>> test.message
00179       u'test'
00180 
00181       >>> test.type
00182       u'info'
00183 
00184     Make sure messages are removed again
00185 
00186       >>> len(status.showStatusMessages())
00187       0
00188 
00189     Test incredibly long messages:
00190 
00191       >>> status.addStatusMessage(u'm' * 0x400, type=u't' * 0x20)
00192 
00193       And check the results again
00194 
00195       >>> fakePublish(request)
00196       >>> messages = status.showStatusMessages()
00197       >>> len(messages)
00198       1
00199 
00200       >>> test = messages[0]
00201 
00202       >>> test.message == u'm' * 0x3FF
00203       True
00204 
00205       >>> test.type == u't' * 0x1F
00206       True
00207       
00208     Messages are stored as base64-ed cookie values, so we must make sure we
00209     create proper header values; all ascii characters, and no newlines:
00210     
00211       >>> status.addStatusMessage(u'test' * 40, type=u'info')
00212       >>> cookies = [c['value'] for c in request.RESPONSE.cookies.values()]
00213       >>> cookies = ''.join(cookies)
00214       >>> cookies == unicode(cookies).encode('ASCII')
00215       True
00216       >>> '\\n' in cookies
00217       False
00218 
00219       >>> from zope.component.testing import tearDown
00220       >>> tearDown()
00221     """
00222 
00223 def test_suite():
00224     from Testing.ZopeTestCase import ZopeDocTestSuite
00225     return ZopeDocTestSuite()
00226 
00227 if __name__ == '__main__':
00228     unittest.main(defaultTest="test_suite")