Skin Switching based on a custom admin flag in the request header
=================================================================

Tests for new feature by Ed Crewe, Internet Development (University
of Bristol) October 2008

This tests switching to the edit skin when a custom admin header is added.
The admin header checked for is HTTP_PLONEADMIN.

A custom header can be added via a proxy server in front of plone, eg.  Apache
A common use case may be when you want to use the same domain for both the 
default skin and edit skin. So for example if you wanted to have ...

www.example.com as the default skin
www.example.com/admin as the edit skin

The Apache2 config could be: 

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^/admin(.*) http://127.0.0.1:8080/VirtualHostBase/http/www.example.com/plone/VirtualHostRoot/_vh_admin$1 [last,proxy,E=PLONEADMIN:ON]
RewriteRule ^/(.*) http://127.0.0.1:8080/VirtualHostBase/http/www.example.com/plone/VirtualHostRoot/$1 [last,proxy]
RequestHeader set PLONEADMIN on env=PLONEADMIN

This proxies the plone site root at www.example.com with no custom header
and at www.example.com/admin with HTTP_PLONEADMIN in the request.

First, we must perform some setup of the test browser.

    >>> from Products.Five.testbrowser import Browser
    >>> portal_url = portal.absolute_url()
    >>> browser = Browser()

The following is useful when writing and debugging testbrowser
tests. It lets us see all error messages in the error_log.

    >>> browser.handleErrors = False
    >>> self.portal.error_log._ignored_exceptions = ()

And we make sure we are logged out:

    >>> browser.open(portal_url + '/logout')
 
Skin switching is done when the switch_skin_action is 'based on admin header'
so lets switch on skin changing due to admin header:

    >>> from Products.CMFCore.utils import getToolByName
    >>> portal_props = getToolByName(portal, 'portal_properties')
    >>> editskin_props = portal_props.get('editskin_switcher')
    >>> editskin_props.switch_skin_action = 'based on admin header'
    >>> switch_skin_action = editskin_props.getProperty('switch_skin_action')
    >>> switch_skin_action == 'based on admin header'
    True

On localhost we show visitors the default skin.  We test that by
faking traversal:

    >>> from collective.editskinswitcher.tests.utils import TestRequest
    >>> from collective.editskinswitcher.tests.utils import FakeTraversalEvent
    >>> from collective.editskinswitcher.traversal import switch_skin
    >>> request = TestRequest(SERVER_URL='http://localhost')
    >>> event = FakeTraversalEvent(portal, request)
    >>> switch_skin(portal, event)

Now lets check that our test skin is installed:

    >>> testskin = portal.portal_skins.restrictedTraverse('editskinswitcher_tests')
    >>> str(testskin.objectIds())
    "['accessibility-info']"
    >>> portal.getCurrentSkinName()
    'Monty Python Skin'
    >>> request.get('editskinswitched',0)
    0

Check that the default skin is really the Monty Python skin via the
browser by testing the changed template in the skin:

    >>> browser.open(portal_url + '/accessibility-info')
    >>> portal.portal_skins.getCurrentSkinName()
    'Monty Python Skin'
    >>> 'Monty Python' in browser.contents
    True

When accessing the edit skin users go via the /admin path 
or whatever you configure in your proxy server this then sets the 
admin header ... so here we just do this directly since 
we are not testing the proxy server!

    >>> request = TestRequest(SERVER_URL='http://127.0.0.1')
    >>> request.set('HTTP_PLONEADMIN','on')
    >>> event = FakeTraversalEvent(portal, request)
    >>> switch_skin(portal, event)
    >>> portal.getCurrentSkinName()
    'Plone Default'

Confirm that via the browser testing the changed template in the skin
is not changed.

    >>> browser.open(portal_url + '/accessibility-info')
    >>> 'Monty Python' in browser.contents
    False

In these tests we need to manually switch the skin back to our
default, which normally happens automatically when your browser makes
a new request.

    >>> portal.changeSkin('Monty Python Skin', portal.REQUEST)

Visitors on localhost still see our fabulous Monty Python Skin")

    >>> request = TestRequest(SERVER_URL='http://localhost')
    >>> event = FakeTraversalEvent(portal, request)
    >>> switch_skin(portal, event)
    >>> portal.getCurrentSkinName()
    'Monty Python Skin'
