1
from django.core import mail
2
from django.test import TestCase
4
from django.contrib.auth.models import User
6
from threadedcomments.moderation import moderator, CommentModerator
7
from threadedcomments.models import FreeThreadedComment, ThreadedComment, TestModel
8
from threadedcomments.models import MARKDOWN, TEXTILE, REST, PLAINTEXT
11
__all__ = ('ModeratorTestCase',)
14
class ModeratorTestCase(TestCase):
16
def test_threadedcomment(self):
17
topic = TestModel.objects.create(name='Test')
18
user = User.objects.create_user(
19
'user', 'floguy@gmail.com', password='password')
20
user2 = User.objects.create_user(
21
'user2', 'floguy@gmail.com', password='password')
23
comment1 = ThreadedComment.objects.create_for_object(
24
topic, user=user, ip_address='127.0.0.1',
25
comment='This is fun! This is very fun!',
27
comment2 = ThreadedComment.objects.create_for_object(
28
topic, user=user, ip_address='127.0.0.1',
29
comment='This is stupid! I hate it!',
31
comment3 = ThreadedComment.objects.create_for_object(
32
topic, user=user, ip_address='127.0.0.1', parent=comment2,
33
comment='I agree, the first comment was wrong and you are right!',
35
comment4 = ThreadedComment.objects.create_for_object(
36
topic, user=user, ip_address='127.0.0.1',
37
comment='What are we talking about?',
39
comment5 = ThreadedComment.objects.create_for_object(
40
topic, user=user, ip_address='127.0.0.1', parent=comment3,
41
comment="I'm a fanboy!",
43
comment6 = ThreadedComment.objects.create_for_object(
44
topic, user=user, ip_address='127.0.0.1', parent=comment1,
45
comment='What are you talking about?',
48
class Moderator1(CommentModerator):
49
enable_field = 'is_public'
50
auto_close_field = 'date'
52
moderator.register(TestModel, Moderator1)
54
comment7 = ThreadedComment.objects.create_for_object(
55
topic, user=user, ip_address='127.0.0.1',
56
comment='Post moderator addition. Does it still work?',
59
topic.is_public = False
62
comment8 = ThreadedComment.objects.create_for_object(
63
topic, user=user, ip_address='127.0.0.1', parent=comment7,
64
comment='This should not appear, due to enable_field',
67
moderator.unregister(TestModel)
69
comment9 = ThreadedComment.objects.create_for_object(
70
topic, user=user, ip_address='127.0.0.1',
71
comment='This should appear again, due to unregistration',
74
self.assertEquals(len(mail.outbox), 0)
78
class Moderator2(CommentModerator):
79
enable_field = 'is_public'
80
auto_close_field = 'date'
83
email_notification = True
84
moderator.register(TestModel, Moderator2)
86
comment10 = ThreadedComment.objects.create_for_object(
87
topic, user=user, ip_address='127.0.0.1',
88
comment='This should not appear again, due to registration with a new manager.',
91
topic.is_public = True
94
comment11 = ThreadedComment.objects.create_for_object(
95
topic, user=user, ip_address='127.0.0.1', parent=comment1,
96
comment='This should appear again.',
99
self.assertEquals(len(mail.outbox), 1)
102
topic.date = topic.date - datetime.timedelta(days=20)
105
comment12 = ThreadedComment.objects.create_for_object(
106
topic, user=user, ip_address='127.0.0.1', parent=comment7,
107
comment="This shouldn't appear, due to close_after=15.",
110
topic.date = topic.date + datetime.timedelta(days=20)
113
moderator.unregister(TestModel)
115
class Moderator3(CommentModerator):
116
max_comment_length = 10
117
moderator.register(TestModel, Moderator3)
119
comment13 = ThreadedComment.objects.create_for_object(
120
topic, user=user, ip_address='127.0.0.1', parent=comment7,
121
comment="This shouldn't appear because it has more than 10 chars.",
124
comment14 = ThreadedComment.objects.create_for_object(
125
topic, user=user, ip_address='127.0.0.1', parent=comment7,
129
moderator.unregister(TestModel)
131
class Moderator4(CommentModerator):
132
allowed_markup = [REST, ]
133
moderator.register(TestModel, Moderator4)
135
comment15 = ThreadedComment.objects.create_for_object(
136
topic, user=user, ip_address='127.0.0.1', parent=comment7,
137
comment='INVALID Markup. Should not show up.', markup=TEXTILE
140
comment16 = ThreadedComment.objects.create_for_object(
141
topic, user=user, ip_address='127.0.0.1', parent=comment7,
142
comment='VALID Markup. Should show up.', markup=REST
145
moderator.unregister(TestModel)
147
tree = ThreadedComment.public.get_tree(topic)
150
output.append('%s %s' % (' ' * comment.depth, comment.comment))
151
self.assertEquals('\n'.join(output),
153
This is fun! This is very fun!
154
What are you talking about?
155
This should appear again.
156
This is stupid! I hate it!
157
I agree, the first comment was wrong and you are right!
159
What are we talking about?
160
Post moderator addition. Does it still work?
162
VALID Markup. Should show up.
163
This should appear again, due to unregistration
166
tree = ThreadedComment.objects.get_tree(topic)
169
output.append('%s %s' % (' ' * comment.depth, comment.comment))
170
self.assertEquals('\n'.join(output),
172
This is fun! This is very fun!
173
What are you talking about?
174
This should appear again.
175
This is stupid! I hate it!
176
I agree, the first comment was wrong and you are right!
178
What are we talking about?
179
Post moderator addition. Does it still work?
180
This shouldn't appear because it has more than 10 chars.
182
VALID Markup. Should show up.
183
This should appear again, due to unregistration
186
tree = ThreadedComment.objects.get_tree(topic, root=comment2)
189
output.append('%s %s' % (' ' * comment.depth, comment.comment))
190
self.assertEquals('\n'.join(output),
192
This is stupid! I hate it!
193
I agree, the first comment was wrong and you are right!
197
tree = ThreadedComment.objects.get_tree(topic, root=comment2.id)
199
output.append('%s %s' % (' ' * comment.depth, comment.comment))
200
self.assertEquals('\n'.join(output),
202
This is stupid! I hate it!
203
I agree, the first comment was wrong and you are right!
207
def test_freethreadedcomment(self):
209
###########################
210
### FreeThreadedComment ###
211
###########################
213
fcomment1 = FreeThreadedComment.objects.create_for_object(
214
topic, name='Eric', ip_address='127.0.0.1',
215
comment='This is fun! This is very fun!',
217
fcomment2 = FreeThreadedComment.objects.create_for_object(
218
topic, name='Eric', ip_address='127.0.0.1',
219
comment='This is stupid! I hate it!',
221
fcomment3 = FreeThreadedComment.objects.create_for_object(
222
topic, name='Eric', ip_address='127.0.0.1', parent=fcomment2,
223
comment='I agree, the first comment was wrong and you are right!',
225
fcomment4 = FreeThreadedComment.objects.create_for_object(
226
topic, name='Eric', ip_address='127.0.0.1',
227
website='http://www.eflorenzano.com/', email='floguy@gmail.com',
228
comment='What are we talking about?',
230
fcomment5 = FreeThreadedComment.objects.create_for_object(
231
topic, name='Eric', ip_address='127.0.0.1', parent=fcomment3,
232
comment="I'm a fanboy!",
234
fcomment6 = FreeThreadedComment.objects.create_for_object(
235
topic, name='Eric', ip_address='127.0.0.1', parent=fcomment1,
236
comment='What are you talking about?',
239
moderator.register(TestModel, Moderator1)
241
fcomment7 = FreeThreadedComment.objects.create_for_object(
242
topic, name='Eric', ip_address='127.0.0.1',
243
comment='Post moderator addition. Does it still work?',
246
topic.is_public = False
249
fcomment8 = FreeThreadedComment.objects.create_for_object(
250
topic, name='Eric', ip_address='127.0.0.1', parent=fcomment7,
251
comment='This should not appear, due to enable_field',
254
moderator.unregister(TestModel)
256
fcomment9 = FreeThreadedComment.objects.create_for_object(
257
topic, name='Eric', ip_address='127.0.0.1',
258
comment='This should appear again, due to unregistration',
261
self.assertEquals(len(mail.outbox), 0)
263
moderator.register(TestModel, Moderator2)
265
fcomment10 = FreeThreadedComment.objects.create_for_object(
266
topic, name='Eric', ip_address='127.0.0.1',
267
comment='This should not appear again, due to registration with a new manager.',
270
topic.is_public = True
273
fcomment11 = FreeThreadedComment.objects.create_for_object(
274
topic, name='Eric', ip_address='127.0.0.1', parent=fcomment1,
275
comment='This should appear again.',
278
self.assertEquals(len(mail.outbox), 1)
282
topic.date = topic.date - datetime.timedelta(days=20)
285
fcomment12 = FreeThreadedComment.objects.create_for_object(
286
topic, name='Eric', ip_address='127.0.0.1', parent=fcomment7,
287
comment="This shouldn't appear, due to close_after=15.",
290
topic.date = topic.date + datetime.timedelta(days=20)
293
moderator.unregister(TestModel)
294
moderator.register(TestModel, Moderator3)
296
fcomment13 = FreeThreadedComment.objects.create_for_object(
297
topic, name='Eric', ip_address='127.0.0.1', parent=fcomment7,
298
comment="This shouldn't appear because it has more than 10 chars.",
301
fcomment14 = FreeThreadedComment.objects.create_for_object(
302
topic, name='Eric', ip_address='127.0.0.1', parent=fcomment7,
306
moderator.unregister(TestModel)
308
class Moderator5(CommentModerator):
309
allowed_markup = [REST, ]
311
moderator.register(TestModel, Moderator5)
313
fcomment15 = FreeThreadedComment.objects.create_for_object(
314
topic, name='Eric', ip_address='127.0.0.1', parent=fcomment7,
315
comment='INVALID Markup. Should not show up.', markup=TEXTILE
318
fcomment16 = FreeThreadedComment.objects.create_for_object(
319
topic, name='Eric', ip_address='127.0.0.1', parent=None,
320
comment='VALID Markup. Should show up.', markup=REST
323
fcomment17 = FreeThreadedComment.objects.create_for_object(
324
topic, name='Eric', ip_address='127.0.0.1', parent=fcomment16,
325
comment='Building Depth...Should Show Up.', markup=REST
328
fcomment18 = FreeThreadedComment.objects.create_for_object(
329
topic, name='Eric', ip_address='127.0.0.1', parent=fcomment17,
330
comment='More Depth...Should Show Up.', markup=REST
333
fcomment19 = FreeThreadedComment.objects.create_for_object(
334
topic, name='Eric', ip_address='127.0.0.1', parent=fcomment18,
335
comment='Too Deep..Should NOT Show UP', markup=REST
338
moderator.unregister(TestModel)
340
tree = FreeThreadedComment.public.get_tree(topic)
343
output.append('%s %s' % (' ' * comment.depth, comment.comment))
344
self.assertEquals('\n'.join(output),
346
This is fun! This is very fun!
347
What are you talking about?
348
This should appear again.
349
This is stupid! I hate it!
350
I agree, the first comment was wrong and you are right!
352
What are we talking about?
353
Post moderator addition. Does it still work?
355
This should appear again, due to unregistration
356
VALID Markup. Should show up.
357
Building Depth...Should Show Up.
358
More Depth...Should Show Up.
361
tree = FreeThreadedComment.objects.get_tree(topic)
364
output.append('%s %s' % (' ' * comment.depth, comment.comment))
365
self.assertEquals('\n'.join(output),
367
This is fun! This is very fun!
368
What are you talking about?
369
This should appear again.
370
This is stupid! I hate it!
371
I agree, the first comment was wrong and you are right!
373
What are we talking about?
374
Post moderator addition. Does it still work?
375
This shouldn't appear because it has more than 10 chars.
377
This should appear again, due to unregistration
378
VALID Markup. Should show up.
379
Building Depth...Should Show Up.
380
More Depth...Should Show Up.
383
tree = FreeThreadedComment.objects.get_tree(topic, root=comment2)
386
output.append('%s %s' % (' ' * comment.depth, comment.comment))
387
self.assertEquals('\n'.join(output),
389
This is stupid! I hate it!
390
I agree, the first comment was wrong and you are right!
394
tree = FreeThreadedComment.objects.get_tree(topic, root=comment2.id)
397
output.append('%s %s' % (' ' * comment.depth, comment.comment))
398
self.assertEquals('\n'.join(output),
400
This is stupid! I hate it!
401
I agree, the first comment was wrong and you are right!