testShaCache.py 4.81 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
# -*- 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.
#
##############################################################################


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

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"

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

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

70 71 72 73 74 75 76 77 78 79
    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
80

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

    self.tic()
90

91 92
    document = self.portal.portal_catalog.getResultValue(reference=self.key)
    self.assertNotEqual(None, document)
93 94 95 96
    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())
97
    self.assertEquals('Published', document.getValidationStateTitle())
98 99 100 101 102

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

    self.tic()
108

109 110
    document = self.portal.portal_catalog.getResultValue(reference=self.key)
    self.assertNotEqual(None, document)
111

112 113 114
    result, data = self.getFile()
    self.assertEqual(result, httplib.OK)
    self.assertEquals(data, self.data)
115 116 117 118 119

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

125 126 127 128
    self.postFile()
    self.tic()
    self.assertEquals(2, self.portal.portal_catalog.countResults(
      reference=self.key)[0][0])
129

130 131
    document2 = self.portal.portal_catalog.getResultValue(reference=self.key,
      sort_on=(('uid', 'ASC'),))
Łukasz Nowak's avatar
Łukasz Nowak committed
132
    self.assertEquals('published', document2.getValidationState())
133
    self.assertEquals('archived', document.getValidationState())
134

135 136
  def test_put_file_twice_no_tic(self):
    self.postFile()
137
    self.commit()
138 139 140 141
    self.postFile()
    self.tic()

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

143
    self.assertEqual(2, len(document_list))
144 145
    expectedFailure(self.assertEqual)(sorted(['archived', 'published']),
        sorted(q.getValidationState() for q in document_list))