Link redirect view
------------------

The default view of the Link content type is a script (link_redirect_view.py)
which may redirect to the link target or show a view describing the link.  Let's
make sure this behaves properly...

First create a demo link::

  >>> from plone.app.testing import setRoles
  >>> app = layer['app']
  >>> portal = layer['portal']
  >>> from plone.app.testing import TEST_USER_ID
  >>> setRoles(portal, TEST_USER_ID, ['Manager'])
  >>> _ = portal.invokeFactory('Link', 'link')
  >>> link = portal.link
  >>> link.remoteUrl = 'http://nohost/plone'
  >>> portal.portal_workflow.doActionFor(portal.link, 'publish')
  >>> _ = portal.invokeFactory('Folder', 'news')
  >>> news = portal.news
  >>> portal.portal_workflow.doActionFor(portal.news, 'publish')
  >>> import transaction; transaction.commit()

Now let's visit the link in the test browser.  Nothing should happen, because
the 'redirect_links' property is on by default::

  >>> from plone.testing.zope import Browser
  >>> from plone.app.testing import SITE_OWNER_NAME
  >>> from plone.app.testing import SITE_OWNER_PASSWORD
  >>> browser = Browser(app)
  >>> browser.open('http://nohost/plone/link')
  >>> browser.url
  'http://nohost/plone'
  >>> 'This link will immediately redirect' in browser.contents
  False

But if we turn off 'redirect_links', visiting the link should let us stay on the page::

  >>> portal.portal_registry['plone.redirect_links'] = False
  >>> transaction.commit()
  >>> browser.open('http://nohost/plone/link')
  >>> browser.url
  'http://nohost/plone/link'

Now let's log in as someone who is allowed to edit the link.  They won't get
redirected, even though 'redirect_links' is re-enabled::

  >>> portal.portal_registry['plone.redirect_links'] = True
  >>> transaction.commit()
  >>> browser.open('http://nohost/plone/login')
  >>> browser.getControl('Login Name').value = SITE_OWNER_NAME
  >>> browser.getControl('Password').value = SITE_OWNER_PASSWORD
  >>> browser.getControl('Log in').click()
  >>> browser.open('http://nohost/plone/link')
  >>> browser.url
  'http://nohost/plone/link'

The manager should get a message to help clarify that the link will redirect
for anonymous users::

  >>> "You see this page because you have permission to edit this link." in browser.contents
  True

If we turn redirect_links back off, that message should not be present::

  >>> portal.portal_registry['plone.redirect_links'] = False
  >>> transaction.commit()
  >>> browser.open('http://nohost/plone/link')
  >>> browser.url
  'http://nohost/plone/link'
  >>> "You see this page because you have permission to edit this link." in browser.contents
  False


Relative Links as Default View
``````````````````````````````

Sometimes it's useful to redirect users to a certain element within a folder automatically
using a link set as the default page of a folder.

Therefore we change the location of the link and set it as default view for the portal.

  >>> link = portal.link
  >>> link.remoteUrl = './news'
  >>> portal.setDefaultPage('link')

We turn on the redirect_link option again and create a browser for anonymous users:

  >>> portal.portal_registry['plone.redirect_links'] = True
  >>> transaction.commit()
  >>> anon = Browser(app)
  >>> anon.handleErrors = False

Now opening the portal, will redirect to the `news` section

  >>> anon.open('http://nohost/plone')
  >>> anon.url
  'http://nohost/plone/./news'

Of course opening the link directly will redirect too:

  >>> anon.open('http://nohost/plone/link')
  >>> anon.url
  'http://nohost/plone/./news'
