summaryrefslogtreecommitdiff
path: root/gstudio/tests/nodetype.py
blob: 2d09336b9cec589a631632667dd27f4f413a3473 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# Copyright (c) 2011,  2012 Free Software Foundation

#     This program is free software: you can redistribute it and/or modify
#     it under the terms of the GNU Affero General Public License as
#     published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.

#     You should have received a copy of the GNU Affero General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.


# This project incorporates work covered by the following copyright and permission notice:  

#    Copyright (c) 2009, Julien Fache
#    All rights reserved.

#    Redistribution and use in source and binary forms, with or without
#    modification, are permitted provided that the following conditions
#    are met:

#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in
#      the documentation and/or other materials provided with the
#      distribution.
#    * Neither the name of the author nor the names of other
#      contributors may be used to endorse or promote products derived
#      from this software without specific prior written permission.

#    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
#    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
#    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
#    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
#    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
#    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
#    OF THE POSSIBILITY OF SUCH DAMAGE.

# Copyright (c) 2011,  2012 Free Software Foundation

#     This program is free software: you can redistribute it and/or modify
#     it under the terms of the GNU Affero General Public License as
#     published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.

#     You should have received a copy of the GNU Affero General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.


"""Test cases for Gstudio's Nodetype"""
from __future__ import with_statement
import warnings
from datetime import datetime
from datetime import timedelta

from django.test import TestCase
from django.conf import settings
from django.contrib import comments
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.contrib.comments.models import CommentFlag

from gstudio import models
from gstudio.models import Nodetype
from gstudio.managers import PUBLISHED
from gstudio.models import get_base_model
from gstudio.models import Nodetype
from gstudio import models as models_settings
from gstudio import url_shortener as shortener_settings


class NodetypeTestCase(TestCase):

    def setUp(self):
        params = {'title': 'My nodetype',
                  'content': 'My content',
                  'slug': 'my-nodetype'}
        self.nodetype = Nodetype.objects.create(**params)

    def test_discussions(self):
        site = Site.objects.get_current()
        self.assertEquals(self.nodetype.discussions.count(), 0)
        self.assertEquals(self.nodetype.comments.count(), 0)
        self.assertEquals(self.nodetype.pingbacks.count(), 0)
        self.assertEquals(self.nodetype.trackbacks.count(), 0)

        comments.get_model().objects.create(comment='My Comment 1',
                                            content_object=self.nodetype,
                                            site=site)
        self.assertEquals(self.nodetype.discussions.count(), 1)
        self.assertEquals(self.nodetype.comments.count(), 1)
        self.assertEquals(self.nodetype.pingbacks.count(), 0)
        self.assertEquals(self.nodetype.trackbacks.count(), 0)

        comments.get_model().objects.create(comment='My Comment 2',
                                            content_object=self.nodetype,
                                            site=site, is_public=False)
        self.assertEquals(self.nodetype.discussions.count(), 1)
        self.assertEquals(self.nodetype.comments.count(), 1)
        self.assertEquals(self.nodetype.pingbacks.count(), 0)
        self.assertEquals(self.nodetype.trackbacks.count(), 0)

        author = User.objects.create_user(username='webmaster',
                                          email='webmaster@example.com')

        comment = comments.get_model().objects.create(
            comment='My Comment 3',
            content_object=self.nodetype,
            site=Site.objects.create(domain='http://toto.com',
                                     name='Toto.com'))
        comment.flags.create(user=author, flag=CommentFlag.MODERATOR_APPROVAL)
        self.assertEquals(self.nodetype.discussions.count(), 2)
        self.assertEquals(self.nodetype.comments.count(), 2)
        self.assertEquals(self.nodetype.pingbacks.count(), 0)
        self.assertEquals(self.nodetype.trackbacks.count(), 0)

        comment = comments.get_model().objects.create(
            comment='My Pingback 1', content_object=self.nodetype, site=site)
        comment.flags.create(user=author, flag='pingback')
        self.assertEquals(self.nodetype.discussions.count(), 3)
        self.assertEquals(self.nodetype.comments.count(), 2)
        self.assertEquals(self.nodetype.pingbacks.count(), 1)
        self.assertEquals(self.nodetype.trackbacks.count(), 0)

        comment = comments.get_model().objects.create(
            comment='My Trackback 1', content_object=self.nodetype, site=site)
        comment.flags.create(user=author, flag='trackback')
        self.assertEquals(self.nodetype.discussions.count(), 4)
        self.assertEquals(self.nodetype.comments.count(), 2)
        self.assertEquals(self.nodetype.pingbacks.count(), 1)
        self.assertEquals(self.nodetype.trackbacks.count(), 1)

    def test_str(self):
        self.assertEquals(str(self.nodetype), 'My nodetype: draft')

    def test_word_count(self):
        self.assertEquals(self.nodetype.word_count, 2)

    def test_comments_are_open(self):
        original_auto_close = models.AUTO_CLOSE_COMMENTS_AFTER
        models.AUTO_CLOSE_COMMENTS_AFTER = None
        self.assertEquals(self.nodetype.comments_are_open, True)
        models.AUTO_CLOSE_COMMENTS_AFTER = 5
        self.nodetype.start_publication = datetime.now() - timedelta(days=7)
        self.nodetype.save()
        self.assertEquals(self.nodetype.comments_are_open, False)

        models.AUTO_CLOSE_COMMENTS_AFTER = original_auto_close

    def test_is_actual(self):
        self.assertTrue(self.nodetype.is_actual)
        self.nodetype.start_publication = datetime(2020, 3, 15)
        self.assertFalse(self.nodetype.is_actual)
        self.nodetype.start_publication = datetime.now()
        self.assertTrue(self.nodetype.is_actual)
        self.nodetype.end_publication = datetime(2000, 3, 15)
        self.assertFalse(self.nodetype.is_actual)

    def test_is_visible(self):
        self.assertFalse(self.nodetype.is_visible)
        self.nodetype.status = PUBLISHED
        self.assertTrue(self.nodetype.is_visible)
        self.nodetype.start_publication = datetime(2020, 3, 15)
        self.assertFalse(self.nodetype.is_visible)

    def test_short_url(self):
        original_shortener = shortener_settings.URL_SHORTENER_BACKEND
        shortener_settings.URL_SHORTENER_BACKEND = 'gstudio.url_shortener.'\
                                                   'backends.default'
        self.assertEquals(self.nodetype.short_url,
                          'http://example.com' +
                          reverse('gstudio_nodetype_shortlink',
                                  args=[self.nodetype.pk]))
        shortener_settings.URL_SHORTENER_BACKEND = original_shortener

    def test_previous_nodetype(self):
        site = Site.objects.get_current()
        self.assertFalse(self.nodetype.previous_nodetype)
        params = {'title': 'My second nodetype',
                  'content': 'My second content',
                  'slug': 'my-second-nodetype',
                  'creation_date': datetime(2000, 1, 1),
                  'status': PUBLISHED}
        self.second_nodetype = Nodetype.objects.create(**params)
        self.second_nodetype.sites.add(site)
        self.assertEquals(self.nodetype.previous_nodetype, self.second_nodetype)
        params = {'title': 'My third nodetype',
                  'content': 'My third content',
                  'slug': 'my-third-nodetype',
                  'creation_date': datetime(2001, 1, 1),
                  'status': PUBLISHED}
        self.third_nodetype = Nodetype.objects.create(**params)
        self.third_nodetype.sites.add(site)
        self.assertEquals(self.nodetype.previous_nodetype, self.third_nodetype)
        self.assertEquals(self.third_nodetype.previous_nodetype, self.second_nodetype)

    def test_next_nodetype(self):
        site = Site.objects.get_current()
        self.assertFalse(self.nodetype.next_nodetype)
        params = {'title': 'My second nodetype',
                  'content': 'My second content',
                  'slug': 'my-second-nodetype',
                  'creation_date': datetime(2100, 1, 1),
                  'status': PUBLISHED}
        self.second_nodetype = Nodetype.objects.create(**params)
        self.second_nodetype.sites.add(site)
        self.assertEquals(self.nodetype.next_nodetype, self.second_nodetype)
        params = {'title': 'My third nodetype',
                  'content': 'My third content',
                  'slug': 'my-third-nodetype',
                  'creation_date': datetime(2050, 1, 1),
                  'status': PUBLISHED}
        self.third_nodetype = Nodetype.objects.create(**params)
        self.third_nodetype.sites.add(site)
        self.assertEquals(self.nodetype.next_nodetype, self.third_nodetype)
        self.assertEquals(self.third_nodetype.next_nodetype, self.second_nodetype)

    def test_related_published(self):
        site = Site.objects.get_current()
        self.assertFalse(self.nodetype.related_published)
        params = {'title': 'My second nodetype',
                  'content': 'My second content',
                  'slug': 'my-second-nodetype',
                  'status': PUBLISHED}
        self.second_nodetype = Nodetype.objects.create(**params)
        self.second_nodetype.related.add(self.nodetype)
        self.assertEquals(len(self.nodetype.related_published), 0)

        self.second_nodetype.sites.add(site)
        self.assertEquals(len(self.nodetype.related_published), 1)
        self.assertEquals(len(self.second_nodetype.related_published), 0)

        self.nodetype.status = PUBLISHED
        self.nodetype.save()
        self.nodetype.sites.add(site)
        self.assertEquals(len(self.nodetype.related_published), 1)
        self.assertEquals(len(self.second_nodetype.related_published), 1)


class NodetypeHtmlContentTestCase(TestCase):

    def setUp(self):
        params = {'title': 'My nodetype',
                  'content': 'My content',
                  'slug': 'my-nodetype'}
        self.nodetype = Nodetype(**params)
        self.original_debug = settings.DEBUG
        self.original_rendering = models_settings.MARKUP_LANGUAGE
        settings.DEBUG = False

    def tearDown(self):
        settings.DEBUG = self.original_debug
        models_settings.MARKUP_LANGUAGE = self.original_rendering

    def test_html_content_default(self):
        models_settings.MARKUP_LANGUAGE = None
        self.assertEquals(self.nodetype.html_content, '<p>My content</p>')

        self.nodetype.content = 'Hello world !\n' \
                             ' this is my content'
        self.assertEquals(self.nodetype.html_content,
                          '<p>Hello world !<br /> this is my content</p>')

    def test_html_content_textitle(self):
        models_settings.MARKUP_LANGUAGE = 'textile'
        self.nodetype.content = 'Hello world !\n\n' \
                             'this is my content :\n\n' \
                             '* Item 1\n* Item 2'
        html_content = self.nodetype.html_content
        try:
            self.assertEquals(html_content,
                              '\t<p>Hello world !</p>\n\n\t' \
                              '<p>this is my content :</p>\n\n\t' \
                              '<ul>\n\t\t<li>Item 1</li>\n\t\t' \
                              '<li>Item 2</li>\n\t</ul>')
        except AssertionError:
            self.assertEquals(html_content, self.nodetype.content)

    def test_html_content_markdown(self):
        models_settings.MARKUP_LANGUAGE = 'markdown'
        self.nodetype.content = 'Hello world !\n\n' \
                             'this is my content :\n\n' \
                             '* Item 1\n* Item 2'
        html_content = self.nodetype.html_content
        try:
            self.assertEquals(html_content,
                              '<p>Hello world !</p>\n' \
                              '<p>this is my content :</p>'\
                              '\n<ul>\n<li>Item 1</li>\n' \
                              '<li>Item 2</li>\n</ul>')
        except AssertionError:
            self.assertEquals(html_content, self.nodetype.content)

    def test_html_content_restructuredtext(self):
        models_settings.MARKUP_LANGUAGE = 'restructuredtext'
        self.nodetype.content = 'Hello world !\n\n' \
                             'this is my content :\n\n' \
                             '* Item 1\n* Item 2'
        html_content = self.nodetype.html_content
        try:
            self.assertEquals(html_content,
                              '<p>Hello world !</p>\n' \
                              '<p>this is my content :</p>'\
                              '\n<ul class="simple">\n<li>Item 1</li>\n' \
                              '<li>Item 2</li>\n</ul>\n')
        except AssertionError:
            self.assertEquals(html_content, self.nodetype.content)

# this class can be removed since the base abstract class is no longer present.
class NodetypeGetBaseModelTestCase(TestCase):

    def setUp(self):
        self.original_nodetype_base_model = models_settings.NODETYPE_BASE_MODEL

    def tearDown(self):
        models_settings.NODETYPE_BASE_MODEL = self.original_nodetype_base_model

    def test_get_base_model(self):
        models_settings.NODETYPE_BASE_MODEL = ''
        self.assertEquals(get_base_model(), Nodetype)

        models_settings.NODETYPE_BASE_MODEL = 'mymodule.myclass'
        try:
            with warnings.catch_warnings(record=True) as w:
                self.assertEquals(get_base_model(), Nodetype)
                self.assertTrue(issubclass(w[-1].metatype, RuntimeWarning))
        except AttributeError:
            # Fail under Python2.5, because of'warnings.catch_warnings'
            pass

        models_settings.NODETYPE_BASE_MODEL = 'gstudio.models.Nodetype'
        self.assertEquals(get_base_model(), Nodetype)