testShaCache.py 4.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2011 Nexedi SA and Contributors. All Rights Reserved.
#                    Lucas Carvalho <lucas@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################


import transaction
32
import httplib
33
import urlparse
34 35
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from ShaCacheMixin import ShaCacheMixin
36
from Products.ERP5Type.tests.backportUnittest import expectedFailure
37 38 39 40 41 42 43 44 45 46 47 48

class TestShaCache(ShaCacheMixin, ERP5TypeTestCase):
  """
    ShaCache - HTTP File Cache server
  """

  def getTitle(self):
    """
      Return the title of the current test set.
    """
    return "SHACACHE - HTTP File Cache Server"

49
  def postFile(self, key=None):
50 51 52
    """
      Post the file
    """
53 54
    parsed = urlparse.urlparse(self.shacache_url)
    connection = httplib.HTTPConnection(parsed.hostname, parsed.port)
55
    try:
56 57 58
      connection.request('POST', parsed.path, self.data, self.header_dict)
      result = connection.getresponse()
      data = result.read()
59
    finally:
60 61
      connection.close()
    return result.status, data
62 63 64 65 66 67 68 69 70

  def getFile(self, key=None):
    """
      Get the file calling the Python Script.
      It simulates the real usage.
    """
    if key is None:
      key = self.key

71 72 73 74 75 76 77 78 79 80
    parsed = urlparse.urlparse(self.shacache_url)
    connection = httplib.HTTPConnection(parsed.hostname, parsed.port)
    try:
      connection.request('GET', '/'.join([parsed.path, key]), None,
        self.header_dict)
      result = connection.getresponse()
      data = result.read()
    finally:
      connection.close()
    return result.status, data
81

82
  def test_put_file(self):
83
    """
84
      Check if the PUT method is creating an object.
85
    """
86 87 88 89 90 91
    result, data = self.postFile()
    self.assertEqual(result, httplib.CREATED)
    self.assertEqual(data, self.key)

    transaction.commit()
    self.tic()
92

93 94
    document = self.portal.portal_catalog.getResultValue(reference=self.key)
    self.assertNotEqual(None, document)
95 96 97 98
    self.assertEquals(self.key, document.getTitle())
    self.assertEquals(self.key, document.getReference())
    self.assertEquals(self.data, document.getData())
    self.assertEquals('application/octet-stream', document.getContentType())
99
    self.assertEquals('Published', document.getValidationStateTitle())
100 101 102 103 104

  def test_get_file(self):
    """
      Check if the file returned is the correct.
    """
105 106 107 108 109 110
    result, data = self.postFile()
    self.assertEqual(result, httplib.CREATED)
    self.assertEqual(data, self.key)

    transaction.commit()
    self.tic()
111

112 113
    document = self.portal.portal_catalog.getResultValue(reference=self.key)
    self.assertNotEqual(None, document)
114

115 116 117
    result, data = self.getFile()
    self.assertEqual(result, httplib.OK)
    self.assertEquals(data, self.data)
118 119 120 121 122

  def test_put_file_twice(self):
    """
      Check if is allowed to put the same file twice.
    """
123 124 125 126
    self.postFile()
    transaction.commit()
    self.tic()
    document = self.portal.portal_catalog.getResultValue(reference=self.key)
Łukasz Nowak's avatar
Łukasz Nowak committed
127
    self.assertEquals('published', document.getValidationState())
128

129 130 131 132 133
    self.postFile()
    transaction.commit()
    self.tic()
    self.assertEquals(2, self.portal.portal_catalog.countResults(
      reference=self.key)[0][0])
134

135 136
    document2 = self.portal.portal_catalog.getResultValue(reference=self.key,
      sort_on=(('uid', 'ASC'),))
Łukasz Nowak's avatar
Łukasz Nowak committed
137
    self.assertEquals('published', document2.getValidationState())
138
    self.assertEquals('archived', document.getValidationState())
139

140
  @expectedFailure
141 142 143 144 145 146 147 148
  def test_put_file_twice_no_tic(self):
    self.postFile()
    transaction.commit()
    self.postFile()
    transaction.commit()
    self.tic()

    document_list = self.portal.portal_catalog(reference=self.key)
149

150 151 152
    self.assertEqual(2, len(document_list))
    self.assertEqual(sorted(['archived', 'published']), sorted([
        q.getValidationState() for q in document_list]))