summaryrefslogtreecommitdiff
path: root/objectapp/tests/gbobject.py
blob: c5746bd6f733dec12ec536eb37dbffb9e25d5e10 (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
# 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.
"""Test cases for Objectapp's Gbobject"""
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 objectapp import models
from objectapp.models import Gbobject
from objectapp.managers import PUBLISHED
from objectapp.models import get_base_model
from objectapp.models import GbobjectAbstractClass
from objectapp import models as models_settings
from objectapp import url_shortener as shortener_settings


class GbobjectTestCase(TestCase):

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

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

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

        comments.get_model().objects.create(comment='My Comment 2',
                                            content_object=self.gbobject,
                                            site=site, is_public=False)
        self.assertEquals(self.gbobject.discussions.count(), 1)
        self.assertEquals(self.gbobject.comments.count(), 1)
        self.assertEquals(self.gbobject.pingbacks.count(), 0)
        self.assertEquals(self.gbobject.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.gbobject,
            site=Site.objects.create(domain='http://toto.com',
                                     name='Toto.com'))
        comment.flags.create(user=author, flag=CommentFlag.MODERATOR_APPROVAL)
        self.assertEquals(self.gbobject.discussions.count(), 2)
        self.assertEquals(self.gbobject.comments.count(), 2)
        self.assertEquals(self.gbobject.pingbacks.count(), 0)
        self.assertEquals(self.gbobject.trackbacks.count(), 0)

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

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

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

    def test_word_count(self):
        self.assertEquals(self.gbobject.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.gbobject.comments_are_open, True)
        models.AUTO_CLOSE_COMMENTS_AFTER = 5
        self.gbobject.start_publication = datetime.now() - timedelta(days=7)
        self.gbobject.save()
        self.assertEquals(self.gbobject.comments_are_open, False)

        models.AUTO_CLOSE_COMMENTS_AFTER = original_auto_close

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

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

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

    def test_previous_gbobject(self):
        site = Site.objects.get_current()
        self.assertFalse(self.gbobject.previous_gbobject)
        params = {'title': 'My second gbobject',
                  'content': 'My second content',
                  'slug': 'my-second-gbobject',
                  'creation_date': datetime(2000, 1, 1),
                  'status': PUBLISHED}
        self.second_gbobject = Gbobject.objects.create(**params)
        self.second_gbobject.sites.add(site)
        self.assertEquals(self.gbobject.previous_gbobject, self.second_gbobject)
        params = {'title': 'My third gbobject',
                  'content': 'My third content',
                  'slug': 'my-third-gbobject',
                  'creation_date': datetime(2001, 1, 1),
                  'status': PUBLISHED}
        self.third_gbobject = Gbobject.objects.create(**params)
        self.third_gbobject.sites.add(site)
        self.assertEquals(self.gbobject.previous_gbobject, self.third_gbobject)
        self.assertEquals(self.third_gbobject.previous_gbobject, self.second_gbobject)

    def test_next_gbobject(self):
        site = Site.objects.get_current()
        self.assertFalse(self.gbobject.next_gbobject)
        params = {'title': 'My second gbobject',
                  'content': 'My second content',
                  'slug': 'my-second-gbobject',
                  'creation_date': datetime(2100, 1, 1),
                  'status': PUBLISHED}
        self.second_gbobject = Gbobject.objects.create(**params)
        self.second_gbobject.sites.add(site)
        self.assertEquals(self.gbobject.next_gbobject, self.second_gbobject)
        params = {'title': 'My third gbobject',
                  'content': 'My third content',
                  'slug': 'my-third-gbobject',
                  'creation_date': datetime(2050, 1, 1),
                  'status': PUBLISHED}
        self.third_gbobject = Gbobject.objects.create(**params)
        self.third_gbobject.sites.add(site)
        self.assertEquals(self.gbobject.next_gbobject, self.third_gbobject)
        self.assertEquals(self.third_gbobject.next_gbobject, self.second_gbobject)

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

        self.second_gbobject.sites.add(site)
        self.assertEquals(len(self.gbobject.related_published), 1)
        self.assertEquals(len(self.second_gbobject.related_published), 0)

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


class GbobjectHtmlContentTestCase(TestCase):

    def setUp(self):
        params = {'title': 'My gbobject',
                  'content': 'My content',
                  'slug': 'my-gbobject'}
        self.gbobject = Gbobject(**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.gbobject.html_content, '<p>My content</p>')

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

    def test_html_content_textitle(self):
        models_settings.MARKUP_LANGUAGE = 'textile'
        self.gbobject.content = 'Hello world !\n\n' \
                             'this is my content :\n\n' \
                             '* Item 1\n* Item 2'
        html_content = self.gbobject.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.gbobject.content)

    def test_html_content_markdown(self):
        models_settings.MARKUP_LANGUAGE = 'markdown'
        self.gbobject.content = 'Hello world !\n\n' \
                             'this is my content :\n\n' \
                             '* Item 1\n* Item 2'
        html_content = self.gbobject.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.gbobject.content)

    def test_html_content_restructuredtext(self):
        models_settings.MARKUP_LANGUAGE = 'restructuredtext'
        self.gbobject.content = 'Hello world !\n\n' \
                             'this is my content :\n\n' \
                             '* Item 1\n* Item 2'
        html_content = self.gbobject.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.gbobject.content)


class GbobjectGetBaseModelTestCase(TestCase):

    def setUp(self):
        self.original_gbobject_base_model = models_settings.GBOBJECT_BASE_MODEL

    def tearDown(self):
        models_settings.GBOBJECT_BASE_MODEL = self.original_gbobject_base_model

    def test_get_base_model(self):
        models_settings.GBOBJECT_BASE_MODEL = ''
        self.assertEquals(get_base_model(), GbobjectAbstractClass)

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

        models_settings.GBOBJECT_BASE_MODEL = 'objectapp.models.GbobjectAbstractClass'
        self.assertEquals(get_base_model(), GbobjectAbstractClass)