==============================================================================
Doctest for attachment upload 
Based on the test tutorial: http://plone.org/documentation/tutorial/testing/doctests 
and the AddMoveAndDeleteDocument.txt doc test from CMFPlone
==============================================================================

Go the member folder and add a Rich document    

    >>> browser = self.getBrowser()
    >>> browser.open('http://nohost/plone/Members/test_user_1_')
    >>> browser.getLink('Add new').click()
    >>> 'Add new item' in browser.contents
    True

    >>> browser.getControl('Rich document').click()
    >>> browser.getControl('Add').click()
    >>> #'Edit Page' in browser.contents # ouch, Plone has <span> around 'Page'
    >>> browser.url
    'http://nohost/plone/Members/test_user_1_/portal_factory/RichDocument/.../edit'

Edit simple stuff like title and description

    >>> browser.getControl('Title').value = 'Rich document'
    >>> browser.getControl('Description').value = 'This is a rich document'
    >>> browser.getControl('Save').click()
    >>> browser.open('http://nohost/plone/Members/test_user_1_/rich-document')
    >>> 'Rich document' in browser.contents
    True


Prepare attachment upload, create a fake file.
This is taken from an doc test of Infrae:
https://svn.infrae.com/documentlibrary/trunk/src/documentlibrary/core/ftests/conversion_document_tests.txt

    >>> from StringIO import StringIO
    >>> file_contents = (r"""{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0
    ...                      Hello!\par This is some {\b bold} text.\par
    ...                      }""")

Lets upload an attachment
and don't use: browser.getControl('Upload').click()

Which is output from the testrecorder, use instead:
browser.getControl(name='form.button.UploadAttachment').click()

As always FireBug dom inspect is your friend :)

    >>> browser.getLink('Edit').click()
    >>> browser.getControl(name='attachmentTitle').value = 'Attachment 1'
    >>> upload = browser.getControl(name='attachmentFile')
    >>> upload.add_file(StringIO(file_contents),
    ...                 'text/rtf', 'test_file.rtf')
    >>> browser.getControl(name='form.button.UploadAttachment').click()

Lets check if the attachment was uploaded at all

    >>> browser.open('http://nohost/plone/Members/test_user_1_/rich-document')
    >>> 'Attachment 1' in browser.contents
    True
    
And check that the attachment is available for download    
    
    >>> browser.getLink('Attachment 1').click()
    >>> browser.url
    'http://nohost/plone/Members/test_user_1_/rich-document/test_file.rtf'
    
 
Lets add a second attachment with an identical file name as the first attachment

    >>> browser.open('http://nohost/plone/Members/test_user_1_/rich-document')
    >>> browser.getLink('Edit').click()
    >>> browser.getControl(name='attachmentTitle').value = 'Attachment 2'
    >>> upload = browser.getControl(name='attachmentFile')
    >>> upload.add_file(StringIO(file_contents),
    ...                 'text/rtf', 'test_file.rtf')
    >>> browser.getControl(name='form.button.UploadAttachment').click()

Lets check if the attachment was uploaded at all

    >>> browser.open('http://nohost/plone/Members/test_user_1_/rich-document')
    >>> 'Attachment 2' in browser.contents
    True

And check that the attachment is available for download    
    
    >>> browser.getLink('Attachment 2').click()
    >>> browser.url
    'http://nohost/plone/Members/test_user_1_/rich-document/test_file.0.rtf'

Lets add a third attachment with an identical file name as the first and second attachment
This should crash the plone site / the test

    >>> browser.open('http://nohost/plone/Members/test_user_1_/rich-document')
    >>> browser.getLink('Edit').click()
    >>> browser.getControl(name='attachmentTitle').value = 'Attachment 3'
    >>> upload = browser.getControl(name='attachmentFile')
    >>> upload.add_file(StringIO(file_contents),
    ...                 'text/rtf', 'test_file.rtf')
    >>> browser.getControl(name='form.button.UploadAttachment').click()

Lets add a fourth attachment with an identical file name as the first and second attachment
This should crash the plone site / the test

    >>> browser.open('http://nohost/plone/Members/test_user_1_/rich-document')
    >>> browser.getLink('Edit').click()
    >>> browser.getControl(name='attachmentTitle').value = 'Attachment 4'
    >>> upload = browser.getControl(name='attachmentFile')
    >>> upload.add_file(StringIO(file_contents),
    ...                 'text/rtf', 'test_file.rtf')
    >>> browser.getControl(name='form.button.UploadAttachment').click()
