Functional test for local sites
===============================

This tests how local site managers are found during traversal and how
that affects component lookup (depending on whether a site is
traversed or not, local components might or might not be found).

First, we set up all of Five:

  >>> import Products.Five
  >>> from Products.Five import zcml
  >>> zcml.load_config("configure.zcml", Products.Five)

Next we turn our DummySite class into a site in ZCML (and register
some views that will provide us with some test info),

  >>> zcml_text = """
  ... <configure xmlns="http://namespaces.zope.org/zope"
  ...            xmlns:meta="http://namespaces.zope.org/meta"
  ...            xmlns:five="http://namespaces.zope.org/five"
  ...            xmlns:browser="http://namespaces.zope.org/browser">
  ... 
  ...   <!-- make the zope2.Public permission work -->
  ...   <meta:redefinePermission from="zope2.Public" to="zope.Public" />
  ... 
  ...   <five:localsite class="Products.Five.site.tests.dummy.DummySite" />
  ...
  ...   <browser:page
  ...       for="Products.Five.site.tests.dummy.IDummySite"
  ...       name="checkServices.html"
  ...       class="Products.Five.site.tests.test_functional.CheckServicesView"
  ...       permission="zope2.Public"
  ...       />
  ...
  ...   <browser:page
  ...       for="Products.Five.site.tests.dummy.IDummySite"
  ...       name="lookupUtilities.html"
  ...       class="Products.Five.site.tests.test_functional.LookupUtilitiesView"
  ...       permission="zope2.Public"
  ...       />
  ... 
  ... </configure>"""
  >>> zcml.load_string(zcml_text)

then we add an instance to our folder:

  >>> from Products.Five.site.tests.dummy import manage_addDummySite
  >>> nothing = manage_addDummySite(self.folder, 'site')

Now we check what the info view tells us about local component lookup:

  >>> print http(r'''
  ... GET /test_folder_1_/site/@@checkServices.html HTTP/1.1
  ... ''')
  HTTP/1.1 200 OK
  ...
  zapi.getServices() is zapi.getGlobalServices(): True
  IFiveUtilityService.providedBy(utility_service): False
  isinstance(zapi.getServices(), FiveSiteManager): False

We see that we have no local component lookup yet, because we haven't
set the site.  Therefore, enable the traversal hook by using the view
that's provided for this task (we first need to create a manager
account in order to be able to access it):

  >>> uf = self.folder.acl_users
  >>> uf._doAddUser('manager', 'r00t', ['Manager'], [])

  >>> print http(r'''
  ... POST /test_folder_1_/site/@@manage_site.html HTTP/1.1
  ... Authorization: Basic manager:r00t
  ... Content-Length: 25
  ... 
  ... UPDATE_MAKESITE=Make site''')
  HTTP/1.1 200 OK
  ...

Now we call the info view again and find that local component lookup
is working:

  >>> print http(r'''
  ... GET /test_folder_1_/site/@@checkServices.html HTTP/1.1
  ... ''')
  HTTP/1.1 200 OK
  ...
  zapi.getServices() is zapi.getGlobalServices(): False
  IFiveUtilityService.providedBy(utility_service): True
  isinstance(zapi.getServices(), FiveSiteManager): True

Of course, sites are only active *during* traversal; after traversal
they're gone:

  >>> from zope.app.component.hooks import getSite
  >>> getSite() is None
  True


We can also register utilities now:

  >>> from zope.app import zapi
  >>> sm = self.folder.site.getSiteManager()

  >>> from Products.Five.site.tests.dummy import IDummyUtility, DummyUtility
  >>> dummy = DummyUtility()
  >>> sm.registerUtility(IDummyUtility, dummy)

and find them being looked up just fine:

  >>> print http(r'''
  ... GET /test_folder_1_/site/@@lookupUtilities.html HTTP/1.1
  ... ''')
  HTTP/1.1 200 OK
  ...
  zapi.getUtility(IDummyUtility) == dummy: True

Of course, we can't look it up once the request has ended, because we
lose the local site setup:

  >>> zapi.getUtility(IDummyUtility)
  Traceback (most recent call last):
  ...
  ComponentLookupError: (<InterfaceClass Products.Five.site.tests.dummy.IDummyUtility>, '')

At last we can "unmake" the site using the browser view provided by
Five:

  >>> print http(r'''
  ... POST /test_folder_1_/site/@@manage_site.html HTTP/1.1
  ... Authorization: Basic manager:r00t
  ... Content-Length: 29
  ... 
  ... UPDATE_UNMAKESITE=Unmake site''')
  HTTP/1.1 200 OK
  ...

And everything is back to normal with respect to local component
lookup:

  >>> print http(r'''
  ... GET /test_folder_1_/site/@@checkServices.html HTTP/1.1
  ... ''')
  HTTP/1.1 200 OK
  ...
  zapi.getServices() is zapi.getGlobalServices(): True
  IFiveUtilityService.providedBy(utility_service): False
  isinstance(zapi.getServices(), FiveSiteManager): False

Finally, global services and the monkeys:

  >>> from zope.app.tests.placelesssetup import tearDown
  >>> tearDown()
