Tests fix
[snf-cloudcms] / cloudcms / tests / __init__.py
1 # Copyright 2012 GRNET S.A. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
5 # conditions are met:
6 #
7 #   1. Redistributions of source code must retain the above
8 #      copyright notice, this list of conditions and the following
9 #      disclaimer.
10 #
11 #   2. Redistributions in binary form must reproduce the above
12 #      copyright notice, this list of conditions and the following
13 #      disclaimer in the documentation and/or other materials
14 #      provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28 #
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
33
34
35 import os
36 import tempfile
37
38 from django.test import TransactionTestCase
39 from django.contrib.auth.models import User
40 from django.core.files.uploadedfile import SimpleUploadedFile
41
42 from feincms.module.medialibrary.models import MediaFile
43
44 from cloudcmsguide.models import UserGuideEntry
45 from cloudcmsfaq.models import Question
46 from cloudcms.forms import RstZipImportForm
47
48 from django.conf import settings
49
50
51 TESTS_BASE = os.path.abspath(os.path.dirname(__file__))
52
53 TEST_DIR1 = os.path.join(TESTS_BASE, 'userdocs1')
54 TEST_DIR2 = os.path.join(TESTS_BASE, 'userdocs2')
55
56 TEST_INVALID_ZIP = os.path.join(TESTS_BASE, 'invalidzip.zip')
57 TEST_VALID_ZIP = os.path.join(TESTS_BASE, 'okeanos-user-docs.zip')
58 TEST_VALID_ZIP = os.path.join(TESTS_BASE, 'userdocs1.zip')
59
60 settings.MEDIA_ROOT = tempfile.mkdtemp("cms-tests")
61
62
63 class TestSphinxImport(TransactionTestCase):
64
65     fixtures = ['cloudcms_media_categories', 'cloudcms_default_services']
66
67     def setUp(self):
68         self.user = User.objects.create(username="admin",
69                                         email="admin@admin.com")
70         self.user.is_superuser = True
71         self.user.set_password("test")
72         self.user.save()
73
74     def test_from_dir1(self):
75         zipfile = SimpleUploadedFile('file.zip', file(TEST_VALID_ZIP).read())
76         form = RstZipImportForm({
77             'import_categories': ['faqs', 'guide']
78         }, files={'import_file': zipfile})
79         form.is_valid()
80         success, log = form.save(self.user, TEST_DIR1)
81
82         self.assertEqual(success, True)
83         self.assertEqual(MediaFile.objects.all().count(), 3)
84         self.assertEqual(Question.objects.all().count(), 4)
85         self.assertEqual(UserGuideEntry.objects.all().count(), 3)
86
87     def test_form(self):
88         zipfile = SimpleUploadedFile('file.zip', file(TEST_INVALID_ZIP).read())
89         form = RstZipImportForm({
90             'import_categories': ['faqs', 'guide']},
91             files={'import_file': zipfile})
92         self.assertEqual(form.is_valid(), False)
93
94         zipfile = SimpleUploadedFile('file.zip', file(TEST_VALID_ZIP).read())
95         form = RstZipImportForm({
96             'import_categories': ['faqs', 'guide']},
97             files={'import_file': zipfile})
98         self.assertEqual(form.is_valid(), True)
99
100         form.save(self.user)
101         self.assertEqual(MediaFile.objects.all().count(), 3)
102         self.assertEqual(Question.objects.all().count(), 4)
103         self.assertEqual(UserGuideEntry.objects.all().count(), 3)