Back to index

plone3  3.1.7
Functions
statusmessages.tests.test_adapter Namespace Reference

Functions

def test_directives
def test_suite

Function Documentation

Test status messages

First some boilerplate.

  >>> from zope.component.testing import setUp
  >>> setUp()

  >>> import Products.Five
  >>> import Products.statusmessages

  >>> from Products.Five import zcml
  >>> zcml.load_config('meta.zcml', Products.Five)
  >>> zcml.load_config('configure.zcml', Products.statusmessages)

Now lets make sure we can actually adapt the request.

  >>> from Products.statusmessages.interfaces import IStatusMessage
  >>> status = IStatusMessage(self.app.REQUEST)
  >>> IStatusMessage.providedBy(status)
  True

We also need the request to be annotatable:

  >>> from zope.interface import directlyProvides
  >>> from zope.annotation.interfaces import IAttributeAnnotatable
  >>> directlyProvides(self.app.REQUEST, IAttributeAnnotatable)

The dummy request we have is a bit limited, so we need a simple method
to fake a real request/response for the cookie handling. Basically it
puts all entries from RESPONSE.cookies into REQUEST.cookies but shifts
the real values into the right place as browsers would do it.
  
  >>> def fakePublish(request):
  ...     cookies = request.RESPONSE.cookies.copy()
  ...     new_cookies = {}
  ...     for key in cookies.keys():
  ...         new_cookies[key] = cookies[key]['value']
  ...     request.cookies = new_cookies
  ...     request.RESPONSE.cookies = {}

  >>> request = self.app.REQUEST
  >>> status = IStatusMessage(request)

Make sure there's no stored message.

  >>> len(status.showStatusMessages())
  0

Add one message
  
  >>> status.addStatusMessage(u'test', type=u'info')

Now check the results

  >>> messages = status.showStatusMessages()
  >>> len(messages)
  1

  >>> messages[0].message
  u'test'

  >>> messages[0].type
  u'info'

Make sure messages are removed

  >>> len(status.showStatusMessages())
  0

Since we accessed the message prior to publishing the page, we must 
ensure that the messages have been removed from the cookies

  >>> fakePublish(request)
  >>> len(status.showStatusMessages())
  0

Now we repeat the test, only this time we publish the page prior to
retrieving the messages

Add one message
  
  >>> status.addStatusMessage(u'test', type=u'info')

Publish the request

  >>> fakePublish(request)

Now check the results

  >>> messages = status.showStatusMessages()
  >>> len(messages)
  1

  >>> messages[0].message
  u'test'

  >>> messages[0].type
  u'info'

Make sure messages are removed

  >>> len(status.showStatusMessages())
  0

Add two messages (without publishing)

  >>> status.addStatusMessage(u'test', type=u'info')
  >>> status.addStatusMessage(u'test1', u'warn')
  
And check the results again

  >>> messages = status.showStatusMessages()
  >>> len(messages)
  2

  >>> test = messages[1]

  >>> test.message
  u'test1'

  >>> test.type
  u'warn'

Make sure messages are removed again

  >>> len(status.showStatusMessages())
  0

Add two messages (with publishing)

  >>> status.addStatusMessage(u'test', type=u'info')
  >>> fakePublish(request)
  >>> status.addStatusMessage(u'test1', u'warn')
  
And check the results again

  >>> fakePublish(request)
  >>> messages = status.showStatusMessages()
  >>> len(messages)
  2

  >>> test = messages[1]

  >>> test.message
  u'test1'

  >>> test.type
  u'warn'

Make sure messages are removed again

  >>> len(status.showStatusMessages())
  0

Add two identical messages

  >>> status.addStatusMessage(u'test', type=u'info')
  >>> status.addStatusMessage(u'test', type=u'info')

And check the results again

  >>> fakePublish(request)
  >>> messages = status.showStatusMessages()
  >>> len(messages)
  1

  >>> test = messages[0]

  >>> test.message
  u'test'

  >>> test.type
  u'info'

Make sure messages are removed again

  >>> len(status.showStatusMessages())
  0

Test incredibly long messages:

  >>> status.addStatusMessage(u'm' * 0x400, type=u't' * 0x20)

  And check the results again

  >>> fakePublish(request)
  >>> messages = status.showStatusMessages()
  >>> len(messages)
  1

  >>> test = messages[0]

  >>> test.message == u'm' * 0x3FF
  True

  >>> test.type == u't' * 0x1F
  True
  
Messages are stored as base64-ed cookie values, so we must make sure we
create proper header values; all ascii characters, and no newlines:

  >>> status.addStatusMessage(u'test' * 40, type=u'info')
  >>> cookies = [c['value'] for c in request.RESPONSE.cookies.values()]
  >>> cookies = ''.join(cookies)
  >>> cookies == unicode(cookies).encode('ASCII')
  True
  >>> '\\n' in cookies
  False

  >>> from zope.component.testing import tearDown
  >>> tearDown()

Definition at line 8 of file test_adapter.py.

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

Definition at line 223 of file test_adapter.py.

00223 
00224 def test_suite():
00225     from Testing.ZopeTestCase import ZopeDocTestSuite
00226     return ZopeDocTestSuite()

Here is the call graph for this function: