Browsing Ikoba
*************

Here we visit all parts of a Ikoba portal using a browser.

Institution
==========

We can watch universities in the browser.

We create an institution object and put into the ZODB root::

  >>> root = getRootFolder()
  >>> list(root)
  []

  >>> from waeup.ikoba.app import Institution
  >>> u = Institution()
  >>> root['myinstitution'] = u
  >>> list(root)
  [u'myinstitution']

  >>> from zope.component.hooks import setSite
  >>> setSite(root['myinstitution'])

To make sure, we can 'watch' pages, we first have to initialize out
test browser::

  >>> from zope.testbrowser.testing import Browser
  >>> browser = Browser()

Let's get the default view of a institution::

  >>> browser.open('http://localhost/myinstitution')
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Welcome to WAeUP.Ikoba...
  ...

We can change to German::
  >>> browser.getLink('de').click()
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Anfragen...
  ...

And then change back to English by clicking on the third link containing 'en'
behind 'Anfragen' and 'Einloggen'::
  >>> browser.getLink('en', index=2).click()
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Enquiries...
  ...

The contact form for anonymous users is called 'Enquiries'::

  >>> browser.open('http://localhost/myinstitution')
  >>> browser.getLink('Enquiries').click()
  >>> browser.getControl('Send').click()
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Required input is missing...
  ...

  >>> browser.getControl(name='form.fullname').value = "Bob Tester"
  >>> browser.getControl(name='form.email_from').value = "xx@yy.zz"
  >>> browser.getControl(name='form.body').value = u'test message'
  >>> browser.getControl('Send').click()
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Your message has been sent...
  ...

Registered users with an email address can request a password change:

  >>> root['myinstitution']['users'].addUser('forgetful', 'secret',title='Bob Forgetful',
  ...                               description='A forgetful user', email='aa@aa.ng')
  >>> browser.open('http://localhost/myinstitution/changepw')
  >>> browser.getControl(name="form.identifier").value = 'forgetful'
  >>> browser.getControl(name="form.email").value = 'aa@aa.ng'
  >>> browser.getControl("Send login credentials").click()
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...An email with your user name and password has been sent to aa@aa.ng...

Now we login as manager::

  >>> browser.addHeader('Authorization', 'Basic mgr:mgrpw')
  >>> browser.handleErrors = False

We can then get an edit view of the configuration container::

  >>> browser.open('http://localhost/myinstitution/configuration')
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...<form action="http://localhost/myinstitution/configuration/@@index"
  ...

The edit form contains the default value for the institution name::

  >>> 'Sample Institution' in browser.contents
  True

We can perform several actions on the edit form::

  >>> browser.getControl("Save", index=0).click()
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Form has been saved...
  ...

  >>> browser.open('http://localhost/myinstitution/configuration')
  >>> browser.getControl("Update plugins").click()
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Plugins were updated. See log file for details...
  ...

The default frontpage HTML has been saved in a dictionary
and is properly rendered on the frontpage of the portal:

  >>> browser.open('http://localhost/myinstitution')
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...<h1>Welcome to WAeUP.Ikoba...</h1>
  ...

The German part is really not being rendered:

  >>> 'Willkommen' in browser.contents
  False


If we change to German so that the German part of frontpage.rst is rendered:

  >>> browser.open('http://localhost/myinstitution//@@change_language?lang=de')
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...<h1>Willkommen auf WAeUP.Ikoba...</h1>
  ...

The English part is really not being rendered:

  >>> 'Welcome' in browser.contents
  False

Switch back to English:

  >>> browser.open('http://localhost/myinstitution//@@change_language?lang=en')


Portal Users
============

  >>> browser.open('http://localhost/myinstitution')
  >>> browser.getLink('Portal Users').click()
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Portal Users...
  ...

We can add users:

  >>> browser.getLink("Add user").click()
  >>> browser.getControl(name="form.name").value = 'bob'
  >>> browser.getControl(name="form.title").value = 'Bob The User'
  >>> browser.getControl(name="password").value = 'secret'
  >>> browser.getControl(name="control_password").value = 'secret'
  >>> browser.getControl(name="form.email").value = 'xx@yy.zz'
  >>> browser.getControl(name="form.phone.country").value = ['+234']
  >>> browser.getControl(name="form.phone.area").value = '123'
  >>> browser.getControl(name="form.phone.ext").value = '45678'
  >>> browser.getControl("Add user").click()
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...<td>bob</td>
  ...

We can edit user bob:

  >>> browser.getControl("Manage", index=0).click()
  >>> browser.getControl("Save", index=0).click()
  >>> browser.getControl("Cancel", index=0).click()

We can add site roles which are then displayed on the user container page.
Since the test browser does not use javascript, we have to add site roles
manually by setting the roles attribute:

  >>> print root['myinstitution']['users']['bob'].roles
  []
  >>> root['myinstitution']['users']['bob'].roles = ['waeup.UsersManager']
  >>> print root['myinstitution']['users']['bob'].roles
  ['waeup.UsersManager']
  >>> browser.open('http://localhost/myinstitution/users')
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...<td nowrap>Users Manager <br /></td>
  ...

Users can't be added twice:

  >>> browser.open('http://localhost/myinstitution/users/add')
  >>> browser.getControl(name="form.name").value = 'bob'
  >>> browser.getControl(name="form.title").value = 'Bob The User'
  >>> browser.getControl(name="password").value = 'secret'
  >>> browser.getControl(name="control_password").value = 'secret'
  >>> browser.getControl(name="form.email").value = 'xx@yy.zz'
  >>> browser.getControl(name="form.phone.country").value = ['+234']
  >>> browser.getControl(name="form.phone.area").value = '123'
  >>> browser.getControl(name="form.phone.ext").value = '45678'
  >>> browser.getControl("Add user").click()
  >>> 'The userid chosen already exists' in browser.contents
  True

Users can be deleted:

  >>> browser.open('http://localhost/myinstitution/users')
  >>> browser.getControl("Remove", index=0).click()
  >>> 'User account bob successfully deleted' in browser.contents
  True



Contact Form
============

Let's enter the contact form::

  >>> browser.open('http://localhost/myinstitution/contactadmin')
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Contact
  ...

We fill the form  (this will send a real message to
contact@waeup.org)::

  >>> browser.open('http://localhost/myinstitution/contactadmin')
  >>> browser.getControl(name='form.body').value = "test message"
  >>> browser.getControl('Send').click()
  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
  ...Your message has been sent...
  ...

If this test fails, chances are, that the local machine has no SMTP
server installed.


Data Center
===========

The data center helps us uploading files for later import or similar.

  >>> browser.open('http://localhost/myinstitution')
  >>> browser.getLink('Data Center').click()

Setting the file path
---------------------

A datacenter stores files in a path in filesystem. By default this is
a directory in the sources:

  >>> print browser.contents
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  ...
  <b>Storage path:</b> <span>.../parts/test/datacenter</span>
  ...

Going to 'Settings` we can change the path:

  >>> browser.getLink('Edit settings').click()
  >>> pathsetting = browser.getControl(name='newpath')

We create a directory and set it as our upload path:

  >>> import os
  >>> cwd = os.getcwd()
  >>> uploadpath = os.path.join(cwd, 'testfiles')
  >>> os.mkdir(uploadpath)
  >>> pathsetting.value = uploadpath

And submit the form:

  >>> browser.getControl(name='save').click()

We clean up the set directory path, as there might be some files be
copied some files from installation:

  >>> files = os.listdir(uploadpath)
  >>> for filename in files:
  ...   if os.path.isdir(os.path.join(uploadpath, filename)):
  ...     continue
  ...   os.unlink(os.path.join(uploadpath, filename))

We also remove any existing 'accesscodes' subdir:

  >>> import shutil
  >>> for filename in files:
  ...   if not os.path.isdir(os.path.join(uploadpath, filename)):
  ...     continue
  ...   if filename != 'accesscodes':
  ...     continue
  ...   shutil.rmtree(os.path.join(uploadpath, filename))

The new upload directory is now empty, except from the logs and other
standard dirs, which are created automatically:

  >>> sorted(os.listdir(uploadpath))
  ['deleted', 'finished', 'logs', 'unfinished']


Uploading files
---------------

Now we can upload files. Most interesting files might be CSV files,
that can be imported lateron. We create a CSV file containing faculty
descriptions:

  >>> open('faculties.csv', 'wb').write(
  ... """code,title,title_prefix
  ... FA,Arts,faculty
  ... FS,Sciences,faculty
  ... """)

Now we can upload this file. To do this, we first go to the upload
page:

  >>> browser.getLink('Upload data').click()

and enter the appropriate data in the form:

  >>> filewidget = browser.getControl(name='uploadfile:file')
  >>> filewidget
  <Control name='uploadfile:file' type='file'>

A sidenote for developers: by marking the filewidget with the
``:file`` extension, we tell Zope to handle this field as a file
widget.

  >>> import cStringIO
  >>> filecontents = cStringIO.StringIO(
  ...   open('faculties.csv', 'rb').read())
  >>> filewidget.add_file(filecontents, 'text/plain', 'myfaculties.csv')

  >>> browser.getControl(name='SUBMIT').click()

The file was indeed uploaded, with the current userid inserted:

  >>> sorted(os.listdir(uploadpath))
  ['deleted', 'finished', 'logs', 'myfaculties_zope.mgr.csv', 'unfinished']



Importing a CSV file
--------------------

The import of CSV files is described in batchprocessing.txt.


Clean up:

  >>> import os
  >>> import shutil
  >>> shutil.rmtree(uploadpath)

