testShaDir.py 7.87 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 65
      connection.close()
    return result.status, data
66 67 68 69 70 71

  def getInformation(self, key=None):
    """
      Get the information calling the Python Script.
      It simulates the real usage.
    """
72 73 74 75 76 77 78 79 80 81
    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
82 83 84 85 86 87 88 89 90

  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()))
91
    self.portal.portal_caches.clearAllCache()
92 93 94 95 96 97 98
    transaction.commit()
    self.tic()

  def test_post_information(self):
    """
      Check if posting information is working.
    """
99 100 101 102 103 104 105
    result, data = self.postInformation()

    self.assertEqual(result, httplib.CREATED)
    self.assertEqual(data, '')

    transaction.commit()
    self.tic()
106 107

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

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

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

131 132 133 134 135
    transaction.commit()
    self.tic()

    result, data = self.getInformation()
    self.assertEqual(result, httplib.OK)
136 137 138 139 140 141 142 143 144 145

    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.
    """
146 147 148 149
    result, data = self.postInformation()

    self.assertEqual(result, httplib.CREATED)
    self.assertEqual(data, '')
150 151
    transaction.commit()
    self.tic()
152

153 154 155 156
    result, data = self.postInformation()

    self.assertEqual(result, httplib.CREATED)
    self.assertEqual(data, '')
157 158
    transaction.commit()
    self.tic()
159

160 161 162 163 164 165
    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())
166

Łukasz Nowak's avatar
Łukasz Nowak committed
167 168 169 170 171 172
    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]))
173 174 175 176 177 178 179

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

180 181 182 183
    transaction.commit()
    self.tic()
    result, data = self.getInformation()
    self.assertEqual(result, httplib.OK)
184 185 186 187 188 189 190
    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):
    """
191
      POST information with two different keys
192 193 194 195 196 197 198 199 200
      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()

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    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)
    result, data = self.postInformation(key_2, data_2)
    self.assertEqual(result, httplib.CREATED)

    transaction.commit()
    self.tic()

222 223 224
    self.assertEquals(2, len(self.portal.data_set_module))
    self.assertEquals(2, len(self.portal.document_module))

225 226
    result, document = self.getInformation()
    self.assertEquals(1, len(json.loads(document)))
227

228 229
    result, document2 = self.getInformation(key_2)
    self.assertEquals(1, len(json.loads(document2)))
230

231 232 233 234 235
    result, data = self.postInformation()
    self.assertEqual(result, httplib.CREATED)

    transaction.commit()
    self.tic()
236 237 238
    self.assertEquals(2, len(self.portal.data_set_module))
    self.assertEquals(3, len(self.portal.document_module))

239 240
    result, document3 = self.getInformation()
    self.assertEquals(2, len(json.loads(document3)))