Statistics
| Branch: | Tag: | Revision:

root / cloudcms / tests / __init__.py @ 2cd5eeec

History | View | Annotate | Download (4.1 kB)

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 TestCase, 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 Category as MediaCategory, \
43
        MediaFile
44

    
45
from cloudcmsguide.models import UserGuideEntry
46
from cloudcmsfaq.models import Category, Question
47
from cloudcms.rstutils import generate_rst_contents_from_dir
48
from cloudcms.models import Service, ServiceTranslation
49
from cloudcms.forms import RstZipImportForm
50

    
51
from synnefo.lib.log import initialize_logging
52

    
53
from django.conf import settings
54

    
55
initialize_logging()
56

    
57
TESTS_BASE = os.path.abspath(os.path.dirname(__file__))
58

    
59
TEST_DIR1 = os.path.join(TESTS_BASE, 'userdocs1')
60
TEST_DIR2 = os.path.join(TESTS_BASE, 'userdocs2')
61

    
62
TEST_INVALID_ZIP = os.path.join(TESTS_BASE, 'invalidzip.zip')
63
TEST_VALID_ZIP = os.path.join(TESTS_BASE, 'okeanos-user-docs.zip')
64
TEST_VALID_ZIP = os.path.join(TESTS_BASE, 'userdocs1.zip')
65

    
66
settings.MEDIA_ROOT = tempfile.mkdtemp("cms-tests")
67

    
68
class TestSphinxImport(TransactionTestCase):
69

    
70
    fixtures = ['cloudcms_media_categories', 'cloudcms_default_services']
71

    
72
    def setUp(self):
73
        c = Category.objects.create()
74
        self.user = User.objects.create(username="admin", email="admin@admin.com")
75
        self.user.is_superuser = True
76
        self.user.set_password("test")
77
        self.user.save()
78

    
79
    def test_from_dir1(self):
80
        zipfile = SimpleUploadedFile('file.zip', file(TEST_VALID_ZIP).read())
81
        form = RstZipImportForm({'import_categories': ['faqs', 'guide']}, files={'import_file': zipfile})
82
        form.is_valid()
83
        success, log = form.save(self.user, TEST_DIR1)
84

    
85
        self.assertEqual(success, True)
86
        self.assertEqual(MediaFile.objects.all().count(), 3)
87
        self.assertEqual(Question.objects.all().count(), 4)
88
        self.assertEqual(UserGuideEntry.objects.all().count(), 3)
89

    
90
    def test_form(self):
91
        zipfile = SimpleUploadedFile('file.zip', file(TEST_INVALID_ZIP).read())
92
        form = RstZipImportForm({'import_categories': ['faqs', 'guide']}, files={'import_file': zipfile})
93
        self.assertEqual(form.is_valid(), False)
94

    
95
        zipfile = SimpleUploadedFile('file.zip', file(TEST_VALID_ZIP).read())
96
        form = RstZipImportForm({'import_categories': ['faqs', 'guide']}, files={'import_file': zipfile})
97
        self.assertEqual(form.is_valid(), True)
98

    
99
        form.save(self.user)
100
        self.assertEqual(MediaFile.objects.all().count(), 5)
101
        self.assertEqual(Question.objects.all().count(), 4)
102
        self.assertEqual(UserGuideEntry.objects.all().count(), 3)
103