test.erp5.testShaCache.py 4.76 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
from unittest import expectedFailure
34
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
35
from erp5.component.test.ShaCacheMixin import ShaCacheMixin
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
    parsed = urlparse.urlparse(self.shacache_url)
    connection = httplib.HTTPConnection(parsed.hostname, parsed.port)
    try:
73
      connection.request('GET', '/'.join([parsed.path, key]), None, {})
74 75 76 77 78
      result = connection.getresponse()
      data = result.read()
    finally:
      connection.close()
    return result.status, data
79

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

    self.tic()
89

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

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

    self.tic()
107

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

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

  def test_put_file_twice(self):
    """
      Check if is allowed to put the same file twice.
    """
119 120 121
    self.postFile()
    self.tic()
    document = self.portal.portal_catalog.getResultValue(reference=self.key)
122
    self.assertEqual('published', document.getValidationState())
123

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

129 130
    document2 = self.portal.portal_catalog.getResultValue(reference=self.key,
      sort_on=(('uid', 'ASC'),))
131 132
    self.assertEqual('published', document2.getValidationState())
    self.assertEqual('archived', document.getValidationState())
133

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

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

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