======================
Deleting found persons
======================

The administrator is able to delete the persons he found using a search.

Set up
======

The layer defined an address book, some keywords and some persons. We need a
user, too:

>>> from icemac.addressbook.testing import create_user
>>> ab = layer['rootFolder']['ab']
>>> create_user(ab, ab, u'Ben',
...     u'Utzer', u'ben@example.com', u'12345678', [],
...     keywords=set([layer['kw_church']]))

>>> from icemac.addressbook.testing import Browser
>>> browser = Browser()

Only administrators are allowed to delete, so we log in as an administrator:

>>> browser.login('mgr')
>>> browser.open('http://localhost/++skin++AddressBook/ab')

There is a link in the global navigation which leads to the searches
overview providing links to the offered search types, which lead to the
searches:

>>> browser.getLink('Search').click()
>>> browser.getLink('Keyword search').click()

Deletion
========

When selecting a keyword which leads to results they are displayed as a
table with check boxes to select the found persons. The check boxes are
selected by default:

>>> browser.getControl('church').click()
>>> browser.getControl('Search').click()
>>> print browser.contents
<!DOCTYPE ...
<table>
  <thead>
    <tr>
      <th></th>
      <th>last name</th>
      <th>first name</th>
    </tr>
  </thead>
  <tbody>
    <tr class="table-even-row">
      <td><input type="checkbox" class="checkbox-widget" name="persons:list" value="Person-2" checked="checked" /></td>
      <td><a href="http://localhost/++skin++AddressBook/ab/Person-2">Koch</a></td>
      <td></td>
    </tr>
    <tr class="table-odd-row">
      <td><input type="checkbox" class="checkbox-widget" name="persons:list" value="Person-4" checked="checked" /></td>
      <td><a href="http://localhost/++skin++AddressBook/ab/Person-4">Liebig</a></td>
      <td></td>
    </tr>
    <tr class="table-even-row">
      <td><input type="checkbox" class="checkbox-widget" name="persons:list" value="Person-6" checked="checked" /></td>
      <td><a href="http://localhost/++skin++AddressBook/ab/Person-6">Utzer</a></td>
      <td><a href="http://localhost/++skin++AddressBook/ab/Person-6">Ben</a></td>
    </tr>
    <tr class="table-odd-row">
      <td><input type="checkbox" class="checkbox-widget" name="persons:list" value="Person-3" checked="checked" /></td>
      <td><a href="http://localhost/++skin++AddressBook/ab/Person-3">Velleuer</a></td>
      <td></td>
    </tr>
 </tbody>
</table>...

To delete the selected persons is shown the administrator has to choose the
adequate entry in the `handele` menu:

>>> 'Deletion (Deletes selected persons.)' in browser.getControl(
...     'Apply on selected persons').displayOptions
True

Selecting this button leads to an are-you-sure question:

>>> browser.getControl('Apply on selected persons').displayValue = [
...     'Delete']
>>> browser.getControl(name='form.buttons.apply').click()
>>> delete_url = browser.url
>>> delete_url
'http://localhost/++skin++AddressBook/ab/@@delete_persons.html'

The number of persons for deletion is shown on the question screen:

>>> print browser.contents
<!DOCTYPE ...
         <div id="form-widgets-count-row" class="row">
             <div class="label">
               <label for="form-widgets-count">
                 <span>number of persons</span>
               </label>
             </div>
             <div class="widget">
    <span id="form-widgets-count"
          class="text-widget int-field">4</span>
</div>
         </div>
  ...

Seleting the `cancel` button leads to the person list without deleting
anybody:

>>> from icemac.addressbook.testing import get_messages
>>> browser.getControl('No, cancel').click()
>>> get_messages(browser)
['Deletion canceled.']
>>> browser.url
'http://localhost/++skin++AddressBook/ab'
>>> 'Koch' in browser.contents
True
>>> 'Liebig' in browser.contents
True
>>> 'Velleuer' in browser.contents
True

Security
========

For editors and visiors the delete menu item is not shown. Deletion is not
possible even if the user knows the URL:

Editor
------

>>> editor = Browser()
>>> editor.login('editor')
>>> editor.open('http://localhost/++skin++AddressBook/ab')
>>> editor.getLink('Search').click()
>>> editor.getLink('Keyword search').click()
>>> editor.getControl('church').click()
>>> editor.getControl('Search').click()
>>> editor.getControl('Apply on selected persons').displayOptions
['XLS export main (Exports person data and main addresses resp. phone numbers.)',
 'XLS export complete (Exports person data and all addresses resp. phone numbers.)',
 'E-Mail (Creates a link to send e-mails.)']

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

Visitor
------

>>> visitor = Browser()
>>> visitor.login('visitor')
>>> visitor.open('http://localhost/++skin++AddressBook/ab')
>>> visitor.getLink('Search').click()
>>> visitor.getLink('Keyword search').click()
>>> visitor.getControl('church').click()
>>> visitor.getControl('Search').click()
>>> visitor.getControl('Search').click()
>>> visitor.getControl('Apply on selected persons').displayOptions
['XLS export main (Exports person data and main addresses resp. phone numbers.)',
 'XLS export complete (Exports person data and all addresses resp. phone numbers.)',
 'E-Mail (Creates a link to send e-mails.)']

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

Real deletion
=============

We have to do the search again because after cancelation we are on the
person list:

>>> browser.getLink('Search').click()
>>> browser.getLink('Keyword search').click()
>>> browser.getControl('church').click()
>>> browser.getControl('Search').click()

Only the selected persons get deleted. When the administrator chooses
"Person-2" the user "Koch" will not get deleted as all persons are selected
by default:

>>> browser.getControl(name='persons:list').getControl(value="Person-2").click()
>>> browser.getControl('Apply on selected persons').displayValue = [
...     'Delete']
>>> browser.getControl(name='form.buttons.apply').click()
>>> print browser.contents
<!DOCTYPE ...
         <div id="form-widgets-count-row" class="row">
             <div class="label">
               <label for="form-widgets-count">
                 <span>number of persons</span>
               </label>
             </div>
             <div class="widget">
    <span id="form-widgets-count"
          class="text-widget int-field">3</span>
</div>
         </div>
  ...

Seleting the `delete` button leads to the person list after deleting the selected persons:

>>> browser.getControl('Yes, delete').click()
>>> get_messages(browser)
['Selected persons deleted: 2']
>>> browser.url
'http://localhost/++skin++AddressBook/ab'
>>> 'Koch' in browser.contents
True
>>> 'Liebig' in browser.contents
False
>>> 'Velleuer' in browser.contents
False
>>> 'Utzer' in browser.contents
True