summaryrefslogtreecommitdiff
path: root/build/lib.linux-i686-2.6/objectapp/tests/feeds.py
blob: 3223fe9420294ff9e8d8c6a05a3dd847476f24b7 (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
# 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 feeds"""
from datetime import datetime

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.utils.translation import ugettext as _
from django.utils.feedgenerator import Atom1Feed
from django.utils.feedgenerator import DefaultFeed
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.contenttypes.models import ContentType

from tagging.models import Tag

from objectapp.models import Gbobject
from objectapp.models import Objecttype
from objectapp.managers import PUBLISHED
from objectapp import feeds
from objectapp.feeds import GbobjectFeed
from objectapp.feeds import LatestGbobjects
from objectapp.feeds import ObjecttypeGbobjects
from objectapp.feeds import AuthorGbobjects
from objectapp.feeds import TagGbobjects
from objectapp.feeds import SearchGbobjects
from objectapp.feeds import GbobjectDiscussions
from objectapp.feeds import GbobjectComments
from objectapp.feeds import GbobjectPingbacks
from objectapp.feeds import GbobjectTrackbacks


class ObjectappFeedsTestCase(TestCase):
    """Test cases for the Feed classes provided"""
    urls = 'objectapp.tests.urls'

    def setUp(self):
        self.site = Site.objects.get_current()
        self.author = User.objects.create(username='admin',
                                          email='admin@example.com')
        self.Objecttype = Objecttype.objects.create(title='Tests', slug='tests')
        self.gbobject_ct_id = ContentType.objects.get_for_model(Gbobject).pk

    def create_published_gbobject(self):
        params = {'title': 'My test gbobject',
                  'content': 'My test content with image '
                  '<img src="/image.jpg" />',
                  'slug': 'my-test-gbobject',
                  'tags': 'tests',
                  'creation_date': datetime(2010, 1, 1),
                  'status': PUBLISHED}
        gbobject = Gbobject.objects.create(**params)
        gbobject.sites.add(self.site)
        gbobject.objecttypes.add(self.Objecttype)
        gbobject.authors.add(self.author)
        return gbobject

    def create_discussions(self, gbobject):
        comment = comments.get_model().objects.create(comment='My Comment',
                                                      user=self.author,
                                                      content_object=gbobject,
                                                      site=self.site)
        pingback = comments.get_model().objects.create(comment='My Pingback',
                                                       user=self.author,
                                                       content_object=gbobject,
                                                       site=self.site)
        pingback.flags.create(user=self.author, flag='pingback')
        trackback = comments.get_model().objects.create(comment='My Trackback',
                                                        user=self.author,
                                                        content_object=gbobject,
                                                        site=self.site)
        trackback.flags.create(user=self.author, flag='trackback')
        return [comment, pingback, trackback]

    def test_gbobject_feed(self):
        original_feeds_format = feeds.FEEDS_FORMAT
        feeds.FEEDS_FORMAT = ''
        gbobject = self.create_published_gbobject()
        feed = GbobjectFeed()
        self.assertEquals(feed.item_pubdate(gbobject), gbobject.creation_date)
        self.assertEquals(feed.item_objecttypes(gbobject), [self.Objecttype.title])
        self.assertEquals(feed.item_author_name(gbobject), self.author.username)
        self.assertEquals(feed.item_author_email(gbobject), self.author.email)
        self.assertEquals(
            feed.item_author_link(gbobject),
            'http://example.com/authors/%s/' % self.author.username)
        # Test a NoReverseMatch for item_author_link
        self.author.username = '[]'
        self.author.save()
        feed.item_author_name(gbobject)
        self.assertEquals(feed.item_author_link(gbobject), 'http://example.com')
        feeds.FEEDS_FORMAT = original_feeds_format

    def test_gbobject_feed_enclosure(self):
        original_feeds_format = feeds.FEEDS_FORMAT
        feeds.FEEDS_FORMAT = ''
        gbobject = self.create_published_gbobject()
        feed = GbobjectFeed()
        self.assertEquals(
            feed.item_enclosure_url(gbobject), 'http://example.com/image.jpg')
        gbobject.content = 'My test content with image <img src="image.jpg" />',
        gbobject.save()
        self.assertEquals(
            feed.item_enclosure_url(gbobject), 'http://example.com/image.jpg')
        gbobject.content = 'My test content with image ' \
                        '<img src="http://test.com/image.jpg" />'
        gbobject.save()
        self.assertEquals(
            feed.item_enclosure_url(gbobject), 'http://test.com/image.jpg')
        gbobject.image = 'image_field.jpg'
        gbobject.save()
        self.assertEquals(feed.item_enclosure_url(gbobject),
                          '%simage_field.jpg' % settings.MEDIA_URL)
        self.assertEquals(feed.item_enclosure_length(gbobject), '100000')
        self.assertEquals(feed.item_enclosure_mime_type(gbobject), 'image/jpeg')
        feeds.FEEDS_FORMAT = original_feeds_format

    def test_latest_gbobjects(self):
        self.create_published_gbobject()
        feed = LatestGbobjects()
        self.assertEquals(feed.link(), '/')
        self.assertEquals(len(feed.items()), 1)
        self.assertEquals(feed.title(),
                          'example.com - %s' % _('Latest gbobjects'))
        self.assertEquals(
            feed.description(),
            _('The latest gbobjects for the site %s') % 'example.com')

    def test_Objecttype_gbobjects(self):
        self.create_published_gbobject()
        feed = ObjecttypeGbobjects()
        self.assertEquals(feed.get_object('request', '/tests/'), self.Objecttype)
        self.assertEquals(len(feed.items(self.Objecttype)), 1)
        self.assertEquals(feed.link(self.Objecttype), '/objecttypes/tests/')
        self.assertEquals(
            feed.title(self.Objecttype),
            _('Gbobjects for the Objecttype %s') % self.Objecttype.title)
        self.assertEquals(
            feed.description(self.Objecttype),
            _('The latest gbobjects for the Objecttype %s') % self.Objecttype.title)

    def test_author_gbobjects(self):
        self.create_published_gbobject()
        feed = AuthorGbobjects()
        self.assertEquals(feed.get_object('request', 'admin'), self.author)
        self.assertEquals(len(feed.items(self.author)), 1)
        self.assertEquals(feed.link(self.author), '/authors/admin/')
        self.assertEquals(feed.title(self.author),
                          _('Gbobjects for author %s') % self.author.username)
        self.assertEquals(feed.description(self.author),
                          _('The latest gbobjects by %s') % self.author.username)

    def test_tag_gbobjects(self):
        self.create_published_gbobject()
        feed = TagGbobjects()
        tag = Tag(name='tests')
        self.assertEquals(feed.get_object('request', 'tests').name, 'tests')
        self.assertEquals(len(feed.items('tests')), 1)
        self.assertEquals(feed.link(tag), '/tags/tests/')
        self.assertEquals(feed.title(tag),
                          _('Gbobjects for the tag %s') % tag.name)
        self.assertEquals(feed.description(tag),
                          _('The latest gbobjects for the tag %s') % tag.name)

    def test_search_gbobjects(self):
        class FakeRequest:
            def __init__(self, val):
                self.GET = {'pattern': val}
        self.create_published_gbobject()
        feed = SearchGbobjects()
        self.assertRaises(ObjectDoesNotExist,
                          feed.get_object, FakeRequest('te'))
        self.assertEquals(feed.get_object(FakeRequest('test')), 'test')
        self.assertEquals(len(feed.items('test')), 1)
        self.assertEquals(feed.link('test'), '/search/?pattern=test')
        self.assertEquals(feed.title('test'),
                          _("Results of the search for '%s'") % 'test')
        self.assertEquals(
            feed.description('test'),
            _("The gbobjects containing the pattern '%s'") % 'test')

    def test_gbobject_discussions(self):
        gbobject = self.create_published_gbobject()
        comments = self.create_discussions(gbobject)
        feed = GbobjectDiscussions()
        self.assertEquals(feed.get_object(
            'request', 2010, 1, 1, gbobject.slug), gbobject)
        self.assertEquals(feed.link(gbobject), '/2010/01/01/my-test-gbobject/')
        self.assertEquals(len(feed.items(gbobject)), 3)
        self.assertEquals(feed.item_pubdate(comments[0]),
                          comments[0].submit_date)
        self.assertEquals(feed.item_link(comments[0]),
                          '/comments/cr/%i/1/#c1' % self.gbobject_ct_id)
        self.assertEquals(feed.item_author_name(comments[0]), 'admin')
        self.assertEquals(feed.item_author_email(comments[0]),
                          'admin@example.com')
        self.assertEquals(feed.item_author_link(comments[0]), '')
        self.assertEquals(feed.title(gbobject),
                          _('Discussions on %s') % gbobject.title)
        self.assertEquals(
            feed.description(gbobject),
            _('The latest discussions for the gbobject %s') % gbobject.title)

    def test_gbobject_comments(self):
        gbobject = self.create_published_gbobject()
        comments = self.create_discussions(gbobject)
        feed = GbobjectComments()
        self.assertEquals(list(feed.items(gbobject)), [comments[0]])
        self.assertEquals(feed.item_link(comments[0]),
                          '/comments/cr/%i/1/#comment_1' % self.gbobject_ct_id)
        self.assertEquals(feed.title(gbobject),
                          _('Comments on %s') % gbobject.title)
        self.assertEquals(
            feed.description(gbobject),
            _('The latest comments for the gbobject %s') % gbobject.title)
        self.assertEquals(
            feed.item_enclosure_url(comments[0]),
            'http://www.gravatar.com/avatar/e64c7d89f26b'
            'd1972efa854d13d7dd61.jpg?s=80&amp;r=g')
        self.assertEquals(feed.item_enclosure_length(gbobject), '100000')
        self.assertEquals(feed.item_enclosure_mime_type(gbobject), 'image/jpeg')

    def test_gbobject_pingbacks(self):
        gbobject = self.create_published_gbobject()
        comments = self.create_discussions(gbobject)
        feed = GbobjectPingbacks()
        self.assertEquals(list(feed.items(gbobject)), [comments[1]])
        self.assertEquals(feed.item_link(comments[1]),
                          '/comments/cr/%i/1/#pingback_2' % self.gbobject_ct_id)
        self.assertEquals(feed.title(gbobject),
                          _('Pingbacks on %s') % gbobject.title)
        self.assertEquals(
            feed.description(gbobject),
            _('The latest pingbacks for the gbobject %s') % gbobject.title)

    def test_gbobject_trackbacks(self):
        gbobject = self.create_published_gbobject()
        comments = self.create_discussions(gbobject)
        feed = GbobjectTrackbacks()
        self.assertEquals(list(feed.items(gbobject)), [comments[2]])
        self.assertEquals(feed.item_link(comments[2]),
                          '/comments/cr/%i/1/#trackback_3' % self.gbobject_ct_id)
        self.assertEquals(feed.title(gbobject),
                          _('Trackbacks on %s') % gbobject.title)
        self.assertEquals(
            feed.description(gbobject),
            _('The latest trackbacks for the gbobject %s') % gbobject.title)

    def test_gbobject_feed_no_authors(self):
        original_feeds_format = feeds.FEEDS_FORMAT
        feeds.FEEDS_FORMAT = ''
        gbobject = self.create_published_gbobject()
        gbobject.authors.clear()
        feed = GbobjectFeed()
        self.assertEquals(feed.item_author_name(gbobject), None)
        feeds.FEEDS_FORMAT = original_feeds_format

    def test_gbobject_feed_rss_or_atom(self):
        original_feeds_format = feeds.FEEDS_FORMAT
        feeds.FEEDS_FORMAT = ''
        feed = LatestGbobjects()
        self.assertEquals(feed.feed_type, DefaultFeed)
        feeds.FEEDS_FORMAT = 'atom'
        feed = LatestGbobjects()
        self.assertEquals(feed.feed_type, Atom1Feed)
        self.assertEquals(feed.subtitle, feed.description)
        feeds.FEEDS_FORMAT = original_feeds_format

    def test_discussion_feed_with_same_slugs(self):
        """
        https://github.com/Fantomas42/django-blog-objectapp/issues/104

        OK, Here I will reproduce the original case: getting a discussion
        type feed, with a same slug.

        The correction of this case, will need some changes in the
        get_object method.
        """
        gbobject = self.create_published_gbobject()

        feed = GbobjectDiscussions()
        self.assertEquals(feed.get_object(
            'request', 2010, 1, 1, gbobject.slug), gbobject)

        params = {'title': 'My test gbobject, part II',
                  'content': 'My content ',
                  'slug': 'my-test-gbobject',
                  'tags': 'tests',
                  'creation_date': datetime(2010, 2, 1),
                  'status': PUBLISHED}
        gbobject_same_slug = Gbobject.objects.create(**params)
        gbobject_same_slug.sites.add(self.site)
        gbobject_same_slug.authors.add(self.author)

        self.assertEquals(feed.get_object(
            'request', 2010, 2, 1, gbobject_same_slug.slug), gbobject_same_slug)