==============
 Address book
==============

Person data (name, address, ...) is stored inside an address book.

>>> from z3c.etestbrowser.wsgi import ExtendedTestBrowser as Browser
>>> browser = Browser()

Create an address book
======================

As only managers are allowed to create address books we have to login:

>>> browser.addHeader('Authorization', 'Basic mgr:mgrpw')
>>> browser.open('http://localhost/++skin++AddressBook/')

On the start page there is a link to add an address book:

>>> from icemac.addressbook.testing import get_messages
>>> browser.getLink('address book').click()
>>> browser.getControl('title').value = 'test book'
>>> browser.getControl('Add').click()
>>> get_messages(browser)
['"test book" added.']
>>> address_book_url = browser.url
>>> address_book_url
'http://localhost/++skin++AddressBook/AddressBook'


Editing address book's data
===========================

The manager can also edit the address book's data. The edit form shows
the formerly entred data:

>>> browser.getLink('Edit address book').click()
>>> browser.url
'http://localhost/++skin++AddressBook/AddressBook/@@edit.html'
>>> edit_address_book_url = browser.url
>>> browser.getControl('title').value
'test book'
>>> browser.getControl('title').value = 'ftest book'
>>> browser.getControl('Apply').click()
>>> get_messages(browser)
['Data successfully updated.']

The edit form submits to itself and shows the stored data:

>>> browser.getControl('title').value
'ftest book'


Canceling edit of address book data
===================================

If the manager decides to cancel editing values are not changed:

>>> browser.getControl('title').value = 'fancy book'
>>> browser.getControl('Cancel').click()
>>> get_messages(browser)
['No changes were applied.']
>>> browser.getControl('title').value
'ftest book'


Person list
===========

Initially there are no persons existing:

>>> browser.getLink('Person list').click()
>>> print browser.contents
<!DOCTYPE...
There are no persons entered yet, click on "Add person" to create one...

There is also an add link in the add menu to add a person:

>>> browser.getLink('person')
<Link text='person'
      url='http://localhost/++skin++AddressBook/AddressBook/@@addPerson.html'>


Editors
=======

Editors can add new persons:

>>> editor_browser = Browser()
>>> editor_browser.addHeader('Authorization', 'Basic editor:editor')
>>> editor_browser.open(address_book_url)
>>> editor_browser.getLink('person').click()

But there is no link to edit the address book's data (title)
because the editor is not allowed to do so:

>>> editor_browser.goBack()
>>> editor_browser.getLink('Edit address book')
Traceback (most recent call last):
LinkNotFoundError

Even opening the URL is not possible:

>>> editor_browser.open(edit_address_book_url)
Traceback (most recent call last):
HTTPError: HTTP Error 403: Forbidden


Visitors
========

Visitors can only visit data but are not allowed to change anything:

>>> visitor_browser = Browser()
>>> visitor_browser.addHeader('Authorization', 'Basic visitor:visitor')
>>> visitor_browser.open(address_book_url)
>>> visitor_browser.getLink('person')
Traceback (most recent call last):
LinkNotFoundError

There is also no link to edit the address book's data (title)
because the visitor is not allowed to do so:

>>> visitor_browser.getLink('Edit address book')
Traceback (most recent call last):
LinkNotFoundError

Even opening the URL is not possible:

>>> visitor_browser.open(edit_address_book_url)
Traceback (most recent call last):
HTTPError: HTTP Error 403: Forbidden


Delete all persons in addressbook
=================================

The Administrator is able to delete all persons in the address book on
the edit form. Users are not deleted because the are referenced as a
safty belt so the current user does not delete his credencials.

Editors and visitors are not allowed to do so, even they know the URL.

Editor
------

>>> delete_address_book_url = address_book_url + '/@@delete_content.html'
>>> editor_browser.open(delete_address_book_url)
Traceback (most recent call last):
HTTPError: HTTP Error 403: Forbidden

Visitor
-------

>>> visitor_browser.open(delete_address_book_url)
Traceback (most recent call last):
HTTPError: HTTP Error 403: Forbidden


Administrator
-------------

Make sure there are some persons (with addresses) and some users in the
address book:

>>> from icemac.addressbook.testing import (create_person, create_user,
...     create_postal_address)
>>> ab = layer['rootFolder']['AddressBook']
>>> create_person(ab, ab, last_name=u'Tester', return_obj=False)
>>> create_person(ab, ab, last_name=u'Tester 2', return_obj=False)
>>> create_user(ab, ab, u'Hans', u'User', u'hans@user.de', u'asdf', ['Visitor'])
>>> create_user(ab, ab, u'Kurt', u'Utzr', u'kurt@utzr.ch', u'asdf', ['Editor'])
>>> t3 = create_person(ab, ab, last_name=u'Tester 3')
>>> create_postal_address(ab, t3, city=u'Hettstedt', return_obj=False)

To delete all persons in the address book, the Adminstrator has to
open the `edit address book` form and select the `Delete all persons
in address book` button there. An `are you sure` form is displayed:

>>> browser.getLink('Edit address book').click()
>>> browser.getControl('Delete all persons in address book').click()
>>> browser.url
'http://localhost/++skin++AddressBook/AddressBook/@@delete_content.html'

When the adminstrator decides not to delete the persons he is led back
to the address book's edit form:

>>> print browser.contents
<!DOCTYPE ...
      <h1>Do you really want to delete all persons in this address book?</h1>...
                  <span>number of persons</span>...
    <span id="form-widgets-count"
          class="text-widget int-field">5</span>...
>>> browser.getControl('No, cancel').click()
>>> get_messages(browser)
['Deletion canceled.']
>>> browser.url == edit_address_book_url
True

When he decides to delete all persons he is led back to the person
list where only the users are still shown:

>>> browser.getControl('Delete all persons in address book').click()
>>> browser.getControl('Yes, delete').click()
>>> get_messages(browser)
['Address book contents deleted.']
>>> browser.url
'http://localhost/++skin++AddressBook/AddressBook'
>>> print browser.contents
<!DOCTYPE ...
  <h2>Persons</h2>
  <table>
  <thead>
    <tr>
      <th><a href="?table-sortOrder=descending&table-sortOn=table-last_name-0" title="Sort">last name</a></th>
      <th><a href="?table-sortOrder=ascending&table-sortOn=table-first_name-1" title="Sort">first name</a></th>
    </tr>
  </thead>
  <tbody>
    <tr class="table-even-row">
      <td><a href="http://localhost/++skin++AddressBook/AddressBook/Person-3">User</a></td>
      <td><a href="http://localhost/++skin++AddressBook/AddressBook/Person-3">Hans</a></td>
    </tr>
    <tr class="table-odd-row">
      <td><a href="http://localhost/++skin++AddressBook/AddressBook/Person-4">Utzr</a></td>
      <td><a href="http://localhost/++skin++AddressBook/AddressBook/Person-4">Kurt</a></td>
    </tr>
  </tbody>
</table>
  ...
