testShaDir.py 8.58 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
import json
import transaction
35
import random
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from ShaDirMixin import ShaDirMixin


class TestShaDir(ShaDirMixin, ERP5TypeTestCase):
  """
    ShaDir - HTTP Information Cache server
  """

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

51
  def postInformation(self, key=None, data=None):
52 53 54 55
    """
      Post the information calling the Python Script.
      It simulates the real usage.
    """
56 57
    parsed = urlparse.urlparse(self.shadir_url)
    connection = httplib.HTTPConnection(parsed.hostname, parsed.port)
58
    try:
59 60 61 62
      connection.request('PUT', '/'.join([parsed.path, key or self.key]),
        data or self.data, self.header_dict)
      result = connection.getresponse()
      data = result.read()
63
    finally:
64
      connection.close()
65 66
    self.assertEqual(result.status, httplib.CREATED)
    self.assertEqual(data, '')
67 68 69 70 71 72

  def getInformation(self, key=None):
    """
      Get the information calling the Python Script.
      It simulates the real usage.
    """
73 74 75 76 77 78 79 80 81 82
    parsed = urlparse.urlparse(self.shadir_url)
    connection = httplib.HTTPConnection(parsed.hostname, parsed.port)
    try:
      connection.request('GET', '/'.join([parsed.path, key or self.key]),
        self.data, self.header_dict)
      result = connection.getresponse()
      data = result.read()
    finally:
      connection.close()
    return result.status, data
83 84 85 86 87 88 89 90 91

  def beforeTearDown(self):
    """
      Clear everything for next test.
    """
    for module in ('data_set_module',
                   'document_module',):
      folder = self.portal[module]
      folder.manage_delObjects(list(folder.objectIds()))
92
    self.portal.portal_caches.clearAllCache()
93 94 95 96 97 98 99
    transaction.commit()
    self.tic()

  def test_post_information(self):
    """
      Check if posting information is working.
    """
100
    self.postInformation()
101 102
    transaction.commit()
    self.tic()
103 104

    # Asserting Data Set
105 106
    data_set = self.portal.portal_catalog.getResultValue(
      reference=self.key)
107
    self.assertEquals(self.key, data_set.getReference())
108
    self.assertEquals('published', data_set.getValidationState())
109 110

    # Asserting Document
111 112
    document = self.portal.portal_catalog.getResultValue(
      reference=self.sha512sum)
113 114 115 116
    self.assertEquals(self.sha512sum, document.getTitle())
    self.assertEquals(self.sha512sum, document.getReference())
    self.assertEquals(self.data, document.getData())
    self.assertEquals(data_set, document.getFollowUpValue())
117 118
    self.assertEquals(str(self.expiration_date),
                                    str(document.getExpirationDate()))
119
    self.assertEquals('application/json', document.getContentType())
120
    self.assertEquals('Published', document.getValidationStateTitle())
121 122 123 124 125 126 127

  def test_get_information(self):
    """
      check if return the temp document with text content.
    """
    self.postInformation()

128 129 130 131 132
    transaction.commit()
    self.tic()

    result, data = self.getInformation()
    self.assertEqual(result, httplib.OK)
133 134 135 136 137 138 139 140 141 142

    information_list = json.loads(data)

    self.assertEquals(1, len(information_list))
    self.assertEquals(json.dumps(information_list[0]), self.data)

  def test_post_information_more_than_once(self):
    """
      Check if posting information is working.
    """
143
    self.postInformation()
144 145
    transaction.commit()
    self.tic()
146

147
    self.postInformation()
148 149
    transaction.commit()
    self.tic()
150

151 152 153 154 155 156
    self.assertEqual(1, self.portal.portal_catalog.countResults(
      reference=self.key)[0][0])
    data_set = self.portal.portal_catalog.getResultValue(
      reference=self.key)
    self.assertEqual(self.key, data_set.getReference())
    self.assertEqual('published', data_set.getValidationState())
157

158 159 160 161 162 163
    document_list = data_set.getFollowUpRelatedValueList()

    self.assertEqual([self.sha512sum, self.sha512sum], [q.getReference() for q \
        in document_list])
    self.assertEqual(sorted(['published', 'archived']), sorted([
        q.getValidationState() for q in document_list]))
164

Łukasz Nowak's avatar
Łukasz Nowak committed
165 166 167 168 169 170 171
    result, data = self.getInformation()
    self.assertEqual(result, httplib.OK)
    information_list = json.loads(data)

    self.assertEquals(1, len(information_list))
    self.assertEquals(json.dumps(information_list[0]), self.data)

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  def test_post_information_more_than_once_no_tic(self):
    """
      Check if posting information is working.
    """
    self.postInformation()
    transaction.commit()

    self.postInformation()
    transaction.commit()
    self.tic()

    self.assertEqual(1, self.portal.portal_catalog.countResults(
      reference=self.key)[0][0])
    data_set = self.portal.portal_catalog.getResultValue(
      reference=self.key)
    self.assertEqual(self.key, data_set.getReference())
    self.assertEqual('published', data_set.getValidationState())

    document_list = data_set.getFollowUpRelatedValueList()

    self.assertEqual([self.sha512sum, self.sha512sum], [q.getReference() for q \
        in document_list])
    self.assertEqual(sorted(['published', 'archived']), sorted([
        q.getValidationState() for q in document_list]))

197 198 199 200 201
  def test_get_information_for_single_data_set(self):
    """
      check if return the temp document with text content.
    """
    self.postInformation()
202 203
    transaction.commit()
    self.tic()
204

205 206
    result, data = self.getInformation()
    self.assertEqual(result, httplib.OK)
207 208 209 210 211 212 213
    information_list = json.loads(data)

    self.assertEquals(1, len(information_list))
    self.assertEquals(json.dumps(information_list[0]), self.data)

  def test_get_information_from_different_data_set(self):
    """
214
      POST information with two different keys
215 216 217 218 219 220 221 222
      It must create two Data Set and two Text documents.

      When the user retrieve the content of a given key,
      it must return only the Text document related to the key.

      This relation is controlled by Data Set object.
    """
    self.postInformation()
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    transaction.commit()
    self.tic()

    urlmd5_2 = 'anotherurlmd5' + str(random.random())
    sha512_2 = 'anothersha512_2' + str(random.random())
    key_2 = 'another_key' + str(random.random())
    data_list_2 = [{'file': self.file_name,
                      'urlmd5': urlmd5_2,
                      'sha512': sha512_2,
                      'creation_date': str(self.creation_date),
                      'expiration_date': str(self.expiration_date),
                      'distribution': self.distribution,
                      'architecture': self.architecture},
                      "User SIGNATURE goes here."]
    data_2 = json.dumps(data_list_2)
238
    self.postInformation(key_2, data_2)
239 240 241
    transaction.commit()
    self.tic()

242 243 244
    self.assertEquals(2, len(self.portal.data_set_module))
    self.assertEquals(2, len(self.portal.document_module))

245 246
    result, document = self.getInformation()
    self.assertEquals(1, len(json.loads(document)))
247

248 249
    result, document2 = self.getInformation(key_2)
    self.assertEquals(1, len(json.loads(document2)))
250

251
    self.postInformation()
252 253
    transaction.commit()
    self.tic()
254 255 256
    self.assertEquals(2, len(self.portal.data_set_module))
    self.assertEquals(3, len(self.portal.document_module))

257 258
    result, document3 = self.getInformation()
    self.assertEquals(2, len(json.loads(document3)))