-*- coding: utf-8 -*-

============
WebDAV tests
============

ATContentTypes supports WebDAV and FTP uploads with small differences
from ordinary AT based content types.

First, we are going to setup an environment so we can test that stuff
is acquired or not acquired at the right times.

  >>> import os
  >>> from plone.app.testing import TEST_USER_ID
  >>> from plone.app.testing import TEST_USER_NAME
  >>> from plone.app.testing import TEST_USER_PASSWORD
  >>> from Products.ATContentTypes.tests.atcttestcase import test_home
  >>> portal = layer['portal']
  >>> portal_name = portal.getId()

  Input directory with test files
  >>> input_dir = os.path.join(test_home, 'input')

  Use the member's home folder as play ground for the tests
  >>> folder = portal.portal_membership.getHomeFolder(TEST_USER_ID)
  >>> fpath = '/'.join(folder.getPhysicalPath())

  Plone's default error message template is too verbose and far too long
  >>> portal.default_error_message = None

WebDAV PUT
==========

ATContentTypes registers several file extensions and mimetypes in the content
type registry. WebDAV and FTP uploads should create the rigt content types.

Document
--------

  >>> input = open(os.path.join(input_dir, 'test-document.txt'))

  Create object by content type

  >>> from Testing.ZopeTestCase.zopedoctest.functional import http
  >>> from Testing.ZopeTestCase.sandbox import AppZapper
  >>> AppZapper().set(layer['app'])

  >>> input.seek(0)
  >>> print http(r"""
  ... PUT /%s/test-document HTTP/1.1
  ... Authorization: Basic %s:%s
  ... Content-Type: text/html; charset="utf-8"
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, input.read()))
  HTTP/1.1 201 Created
  ...

  >>> document = folder['test-document']
  >>> input.seek(0)

  >>> print document.getPortalTypeName()
  Document

  >>> print document.Title()
  test document

  >>> print document.getText()
  <em>test document body text</em>
  <BLANKLINE>

  >>> print document.Description()
  test document description

  >>> print document.getContentType()
  text/html

  >>> print document.Subject()
  ('test keyword 1', 'test keyword 2')

  >>> print document.Contributors()
  ('John Dow', 'Example User')

  >>> print document.Creators()
  ('test_user_1_',)

  >>> print document.effective_date.ISO()
  2005-01-01...00:00:00...

  >>> print document.expiration_date.ISO()
  2005-02-01...00:00:00...

  >>> print document.Language()
  de

  >>> print document.Rights()
  GPL

  >>> print document.getLayout()
  document_view

  >>> del document

  Create object by file extension

  >>> print http(r"""
  ... PUT /%s/test-document2.txt HTTP/1.1
  ... Authorization: Basic %s:%s
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, "a test"))
  HTTP/1.1 201 Created
  ...

  >>> document = folder['test-document2.txt']
  >>> input.seek(0)

  >>> print document.getPortalTypeName()
  Document

  >>> print document.Title()
  test-document2.txt

  >>> print document.getText()
  <p>a test</p>

  >>> del document
  >>> input.close()

Event
-----

  >>> input = open(os.path.join(input_dir, 'test-event.txt'), 'rb')

  Create an object by extension

  >>> input.seek(0)
  >>> print http(r"""
  ... PUT /%s/test-event.event HTTP/1.1
  ... Authorization: Basic %s:%s
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, input.read()))
  HTTP/1.1 201 Created
  ...

  XXX: .event should be removed?

  >>> event = folder['test-event.event']

  >>> print event.getPortalTypeName()
  Event

  >>> print event.Title()
  test event

  >>> del event
  >>> input.close()

File
----

  >>> input = open(os.path.join(input_dir, 'test.zip'), 'rb')

  >>> input.seek(0)
  >>> print http(r"""
  ... PUT /%s/test-file HTTP/1.1
  ... Authorization: Basic %s:%s
  ... Content-Type: application/zip
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, input.read()))
  HTTP/1.1 201 Created
  ...

  >>> file = folder['test-file']

  >>> print file.getPortalTypeName()
  File

  manage_afterPUT sets the Title according to the file name

  >>> print file.Title()
  test-file

  >>> input.seek(0)
  >>> len(str(file.getFile())), len(input.read())
  (145, 145)
  >>> input.seek(0)
  >>> file.getFile().data == input.read()
  True

  >>> print file.getContentType()
  application/zip

  >>> del file

File, by Extension
------------------

  >>> input.seek(0)
  >>> print http(r"""
  ... PUT /%s/test-file.zip HTTP/1.1
  ... Authorization: Basic %s:%s
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, input.read()))
  HTTP/1.1 201 Created
  ...

  >>> file = folder['test-file.zip']

  >>> print file.getPortalTypeName()
  File

  >>> print file.Title()
  test-file.zip

  >>> print file.getContentType()
  application/zip

  >>> del file
  >>> input.close()

OpenOffice 1.0 Text Document, no Content-Type, Extension
--------------------------------------------------------

  >>> input = open(os.path.join(input_dir, 'test.sxw'), 'rb')

  >>> input.seek(0)
  >>> print http(r"""
  ... PUT /%s/test-file1.sxw HTTP/1.1
  ... Authorization: Basic %s:%s
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, input.read()))
  HTTP/1.1 201 Created
  ...

  >>> file = folder['test-file1.sxw']

  >>> print file.getPortalTypeName()
  File

  manage_afterPUT sets the Title according to the file name

  >>> print file.Title()
  test-file1.sxw

  >>> input.seek(0)
  >>> len(str(file.getFile())), len(input.read())
  (9241, 9241)

  >>> input.seek(0)
  >>> file.getFile().data == input.read()
  True

  >>> print file.getContentType()
  application/vnd.sun.xml.writer

  >>> del file
  >>> input.close()

OpenOffice 1.0 Text Document, no Content-Type, No Extension
--------------------------------------------------------

  >>> input = open(os.path.join(input_dir, 'test.sxw'), 'rb')

  >>> input.seek(0)
  >>> print http(r"""
  ... PUT /%s/test-file2 HTTP/1.1
  ... Authorization: Basic %s:%s
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, input.read()))
  HTTP/1.1 201 Created
  ...

  >>> file = folder['test-file2']

  >>> print file.getPortalTypeName()
  File

  manage_afterPUT sets the Title according to the file name

  >>> print file.Title()
  test-file2

  >>> input.seek(0)
  >>> len(str(file.getFile())), len(input.read())
  (9241, 9241)

  >>> input.seek(0)
  >>> file.getFile().data == input.read()
  True

  XXX Broken. MimetypesRegistry detects the file as 'text/xml'.

  # >>> print file.getContentType()
  # application/vnd.sun.xml.writer

  >>> del file
  >>> input.close()

OpenOffice 1.0 Text Document, w/ Content-Type, No Extension
--------------------------------------------------------

  >>> input = open(os.path.join(input_dir, 'test.sxw'), 'rb')

  >>> input.seek(0)
  >>> print http(r"""
  ... PUT /%s/test-file3 HTTP/1.1
  ... Content-Type: application/vnd.sun.xml.writer
  ... Authorization: Basic %s:%s
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, input.read()))
  HTTP/1.1 201 Created
  ...

  >>> file = folder['test-file3']

  >>> print file.getPortalTypeName()
  File

  manage_afterPUT sets the Title according to the file name

  >>> print file.Title()
  test-file3

  >>> input.seek(0)
  >>> len(str(file.getFile())), len(input.read())
  (9241, 9241)

  >>> input.seek(0)
  >>> file.getFile().data == input.read()
  True

  >>> print file.getContentType()
  application/vnd.sun.xml.writer

  >>> del file
  >>> input.close()

Image
-----

  >>> input = open(os.path.join(input_dir, 'test.gif'), 'rb')

  Create an object by content type

  >>> input.seek(0)
  >>> print http(r"""
  ... PUT /%s/test-image HTTP/1.1
  ... Authorization: Basic %s:%s
  ... Content-Type: image/gif
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, input.read()))
  HTTP/1.1 201 Created
  ...

  >>> image = folder['test-image']

  >>> print image.getPortalTypeName()
  Image

  manage_afterPUT sets the Title according to the file name

  >>> print image.Title()
  test-image

  >>> input.seek(0)
  >>> len(str(image.getImage().data)), len(input.read())
  (105, 105)
  >>> input.seek(0)
  >>> image.getImage().data == input.read()
  True

  >>> print image.getContentType()
  image/gif

  >>> del image

Image, by Extension
-------------------

  >>> input.seek(0)
  >>> print http(r"""
  ... PUT /%s/test-image.gif HTTP/1.1
  ... Authorization: Basic %s:%s
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, input.read()))
  HTTP/1.1 201 Created
  ...

  >>> image = folder['test-image.gif']

  >>> print image.getPortalTypeName()
  Image

  >>> print image.Title()
  test-image.gif

  >>> print image.getContentType()
  image/gif

  >>> del image

  Now test the name mangeling. White spaces are replaces by a dash.

  >>> input.seek(0)
  >>> print http(r"""
  ... PUT /%s/test%%20my%%20image.gif HTTP/1.1
  ... Authorization: Basic %s:%s
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, input.read()))
  HTTP/1.1 201 Created
  ...

  XXX: Note that name mangeling for WebDAV PUTs are not supported at the
  moment!

  >>> image = folder['test my image.gif']

  >>> print image.getPortalTypeName()
  Image

  >>> print image.Title()
  test my image.gif

  >>> print image.getContentType()
  image/gif

  >>> del image
  >>> input.close()

Link
----

  >>> input = open(os.path.join(input_dir, 'test-link.txt'), 'rb')

  >>> input.seek(0)
  >>> print http(r"""
  ... PUT /%s/test-link.link HTTP/1.1
  ... Authorization: Basic %s:%s
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, input.read()))
  HTTP/1.1 201 Created
  ...

  XXX: .link should be removed?

  >>> link = folder['test-link.link']

  >>> print link.getPortalTypeName()
  Link

  >>> print link.Title()
  test link

  >>> del link
  >>> input.close()

News Item
---------

  >>> input = open(os.path.join(input_dir, 'test-news-item.txt'), 'rb')

  >>> input.seek(0)
  >>> print http(r"""
  ... PUT /%s/test-newsitem.news HTTP/1.1
  ... Authorization: Basic %s:%s
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, input.read()))
  HTTP/1.1 201 Created
  ...

  XXX: .news should be removed?

  >>> news = folder['test-newsitem.news']

  >>> print news.getPortalTypeName()
  News Item

  >>> print news.Title()
  test news item

  >>> del news
  >>> input.close()

HTML Page - verify that title is extracted
-------------------------------------------

  >>> input = open(os.path.join(input_dir, 'test-html-title.html'), 'rb')

  >>> input.seek(0)
  >>> print http(r"""
  ... PUT /%s/test-html-title.html HTTP/1.1
  ... Authorization: Basic %s:%s
  ... Depth: 0
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, input.read()))
  HTTP/1.1 201 Created
  ...

  >>> htdoc = folder['test-html-title.html']
  >>> print htdoc.Title()
  HTML Title
  >>> print htdoc.Format()
  text/html

XHTML Page - verify that title and body are extracted
------------------------------------------------------

  >>> input = open(os.path.join(input_dir, 'test-xhtml-title.html'), 'rb')

  >>> input.seek(0)
  >>> print http(r"""
  ... PUT /%s/test-xhtml-title.html HTTP/1.1
  ... Authorization: Basic %s:%s
  ... Depth: 0
  ...
  ... %s""" % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD, input.read()))
  HTTP/1.1 201 Created
  ...

  >>> htdoc = folder['test-xhtml-title.html']
  >>> print htdoc.Title()
  HTML Title
  >>> print htdoc.Format()
  text/html

  >>> print htdoc.CookedBody()
  <BLANKLINE>
  <h1>A body</h1>
  <BLANKLINE>

WebDAV MKCOL
============

Need manager roles here:

  >>> from plone.app.testing import setRoles
  >>> from plone.app.testing import TEST_USER_ID
  >>> setRoles(portal, TEST_USER_ID, ['Manager'])

Folder
------

  >>> print http(r"""
  ... MKCOL /%s/test-folder-name HTTP/1.1
  ... Authorization: Basic %s:%s
  ...
  ... """ % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD))
  HTTP/1.1 201 Created
  ...

  >>> folder['test-folder-name'].Title()
  'test-folder-name'

PUT Mangling
------------

Now for the real thing, doing a PUT request, back to Member role:

  >>> setRoles(portal, TEST_USER_ID, ['Member'])

  >>> print http(r"""
  ... PUT /%s/Foto%%20do%%20Parana.jpg HTTP/1.1
  ... Authorization: Basic %s:%s
  ...
  ... """ % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD))
  HTTP/1.1 201 Created
  ...

  >>> 'Foto do Parana.jpg' in folder
  True

  >>> image = folder['Foto do Parana.jpg']

  >>> print image.getPortalTypeName()
  Image

  >>> print image.Title()
  Foto do Parana.jpg

  >>> setRoles(portal, TEST_USER_ID, ['Manager'])

MKCOL Mangling
--------------

  >>> print http(r"""
  ... MKCOL /%s/Fotos%%20de%%20Jurere HTTP/1.1
  ... Authorization: Basic %s:%s
  ...
  ... """ % (fpath, TEST_USER_NAME, TEST_USER_PASSWORD))
  HTTP/1.1 201 Created
  ...

  >>> 'Fotos de Jurere' in folder
  True

  >>> print folder['Fotos de Jurere'].Title()
  Fotos de Jurere

  >>> AppZapper().clear()
