~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to threadedcomments/tests/moderator_tests.py

  • Committer: franku
  • Date: 2018-04-03 05:18:03 UTC
  • mto: This revision was merged to the branch mainline in revision 491.
  • Revision ID: somal@arcor.de-20180403051803-2003zf49j3xd860i
url fixes; render_to_response() -> render(); disabled tracking

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from django.core import mail
2
 
from django.test import TestCase
3
 
 
4
 
from django.contrib.auth.models import User
5
 
 
6
 
from threadedcomments.moderation import moderator, CommentModerator
7
 
from threadedcomments.models import FreeThreadedComment, ThreadedComment, TestModel
8
 
from threadedcomments.models import MARKDOWN, TEXTILE, REST, PLAINTEXT
9
 
 
10
 
 
11
 
__all__ = ('ModeratorTestCase',)
12
 
 
13
 
 
14
 
class ModeratorTestCase(TestCase):
15
 
 
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')
22
 
 
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!',
26
 
        )
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!',
30
 
        )
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!',
34
 
        )
35
 
        comment4 = ThreadedComment.objects.create_for_object(
36
 
            topic, user=user, ip_address='127.0.0.1',
37
 
            comment='What are we talking about?',
38
 
        )
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!",
42
 
        )
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?',
46
 
        )
47
 
 
48
 
        class Moderator1(CommentModerator):
49
 
            enable_field = 'is_public'
50
 
            auto_close_field = 'date'
51
 
            close_after = 15
52
 
        moderator.register(TestModel, Moderator1)
53
 
 
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?',
57
 
        )
58
 
 
59
 
        topic.is_public = False
60
 
        topic.save()
61
 
 
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',
65
 
        )
66
 
 
67
 
        moderator.unregister(TestModel)
68
 
 
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',
72
 
        )
73
 
 
74
 
        self.assertEquals(len(mail.outbox), 0)
75
 
 
76
 
        ##################
77
 
 
78
 
        class Moderator2(CommentModerator):
79
 
            enable_field = 'is_public'
80
 
            auto_close_field = 'date'
81
 
            close_after = 15
82
 
            akismet = False
83
 
            email_notification = True
84
 
        moderator.register(TestModel, Moderator2)
85
 
 
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.',
89
 
        )
90
 
 
91
 
        topic.is_public = True
92
 
        topic.save()
93
 
 
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.',
97
 
        )
98
 
 
99
 
        self.assertEquals(len(mail.outbox), 1)
100
 
        mail.outbox = []
101
 
 
102
 
        topic.date = topic.date - datetime.timedelta(days=20)
103
 
        topic.save()
104
 
 
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.",
108
 
        )
109
 
 
110
 
        topic.date = topic.date + datetime.timedelta(days=20)
111
 
        topic.save()
112
 
 
113
 
        moderator.unregister(TestModel)
114
 
 
115
 
        class Moderator3(CommentModerator):
116
 
            max_comment_length = 10
117
 
        moderator.register(TestModel, Moderator3)
118
 
 
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.",
122
 
        )
123
 
 
124
 
        comment14 = ThreadedComment.objects.create_for_object(
125
 
            topic, user=user, ip_address='127.0.0.1', parent=comment7,
126
 
            comment='<10chars',
127
 
        )
128
 
 
129
 
        moderator.unregister(TestModel)
130
 
 
131
 
        class Moderator4(CommentModerator):
132
 
            allowed_markup = [REST, ]
133
 
        moderator.register(TestModel, Moderator4)
134
 
 
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
138
 
        )
139
 
 
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
143
 
        )
144
 
 
145
 
        moderator.unregister(TestModel)
146
 
 
147
 
        tree = ThreadedComment.public.get_tree(topic)
148
 
        output = []
149
 
        for comment in tree:
150
 
            output.append('%s %s' % ('    ' * comment.depth, comment.comment))
151
 
        self.assertEquals('\n'.join(output),
152
 
                          """
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!
158
 
        I'm a fanboy!
159
 
What are we talking about?
160
 
Post moderator addition.  Does it still work?
161
 
    <10chars
162
 
    VALID Markup.  Should show up.
163
 
This should appear again, due to unregistration
164
 
""".lstrip())
165
 
 
166
 
        tree = ThreadedComment.objects.get_tree(topic)
167
 
        output = []
168
 
        for comment in tree:
169
 
            output.append('%s %s' % ('    ' * comment.depth, comment.comment))
170
 
        self.assertEquals('\n'.join(output),
171
 
                          """
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!
177
 
        I'm a fanboy!
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.
181
 
    <10chars
182
 
    VALID Markup.  Should show up.
183
 
This should appear again, due to unregistration
184
 
""".lstrip())
185
 
 
186
 
        tree = ThreadedComment.objects.get_tree(topic, root=comment2)
187
 
        output = []
188
 
        for comment in tree:
189
 
            output.append('%s %s' % ('    ' * comment.depth, comment.comment))
190
 
        self.assertEquals('\n'.join(output),
191
 
                          """
192
 
This is stupid!  I hate it!
193
 
    I agree, the first comment was wrong and you are right!
194
 
        I'm a fanboy!
195
 
""".lstrip())
196
 
 
197
 
        tree = ThreadedComment.objects.get_tree(topic, root=comment2.id)
198
 
        for comment in tree:
199
 
            output.append('%s %s' % ('    ' * comment.depth, comment.comment))
200
 
        self.assertEquals('\n'.join(output),
201
 
                          """
202
 
This is stupid!  I hate it!
203
 
    I agree, the first comment was wrong and you are right!
204
 
        I'm a fanboy!
205
 
""".lstrip())
206
 
 
207
 
    def test_freethreadedcomment(self):
208
 
 
209
 
        ###########################
210
 
        ### FreeThreadedComment ###
211
 
        ###########################
212
 
 
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!',
216
 
        )
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!',
220
 
        )
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!',
224
 
        )
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?',
229
 
        )
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!",
233
 
        )
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?',
237
 
        )
238
 
 
239
 
        moderator.register(TestModel, Moderator1)
240
 
 
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?',
244
 
        )
245
 
 
246
 
        topic.is_public = False
247
 
        topic.save()
248
 
 
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',
252
 
        )
253
 
 
254
 
        moderator.unregister(TestModel)
255
 
 
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',
259
 
        )
260
 
 
261
 
        self.assertEquals(len(mail.outbox), 0)
262
 
 
263
 
        moderator.register(TestModel, Moderator2)
264
 
 
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.',
268
 
        )
269
 
 
270
 
        topic.is_public = True
271
 
        topic.save()
272
 
 
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.',
276
 
        )
277
 
 
278
 
        self.assertEquals(len(mail.outbox), 1)
279
 
 
280
 
        mail.outbox = []
281
 
 
282
 
        topic.date = topic.date - datetime.timedelta(days=20)
283
 
        topic.save()
284
 
 
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.",
288
 
        )
289
 
 
290
 
        topic.date = topic.date + datetime.timedelta(days=20)
291
 
        topic.save()
292
 
 
293
 
        moderator.unregister(TestModel)
294
 
        moderator.register(TestModel, Moderator3)
295
 
 
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.",
299
 
        )
300
 
 
301
 
        fcomment14 = FreeThreadedComment.objects.create_for_object(
302
 
            topic, name='Eric', ip_address='127.0.0.1', parent=fcomment7,
303
 
            comment='<10chars',
304
 
        )
305
 
 
306
 
        moderator.unregister(TestModel)
307
 
 
308
 
        class Moderator5(CommentModerator):
309
 
            allowed_markup = [REST, ]
310
 
            max_depth = 3
311
 
        moderator.register(TestModel, Moderator5)
312
 
 
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
316
 
        )
317
 
 
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
321
 
        )
322
 
 
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
326
 
        )
327
 
 
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
331
 
        )
332
 
 
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
336
 
        )
337
 
 
338
 
        moderator.unregister(TestModel)
339
 
 
340
 
        tree = FreeThreadedComment.public.get_tree(topic)
341
 
        output = []
342
 
        for comment in tree:
343
 
            output.append('%s %s' % ('    ' * comment.depth, comment.comment))
344
 
        self.assertEquals('\n'.join(output),
345
 
                          """
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!
351
 
        I'm a fanboy!
352
 
What are we talking about?
353
 
Post moderator addition.  Does it still work?
354
 
    <10chars
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.
359
 
""".lstrip())
360
 
 
361
 
        tree = FreeThreadedComment.objects.get_tree(topic)
362
 
        output = []
363
 
        for comment in tree:
364
 
            output.append('%s %s' % ('    ' * comment.depth, comment.comment))
365
 
        self.assertEquals('\n'.join(output),
366
 
                          """
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!
372
 
        I'm a fanboy!
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.
376
 
    <10chars
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.
381
 
""".lstrip())
382
 
 
383
 
        tree = FreeThreadedComment.objects.get_tree(topic, root=comment2)
384
 
        output = []
385
 
        for comment in tree:
386
 
            output.append('%s %s' % ('    ' * comment.depth, comment.comment))
387
 
        self.assertEquals('\n'.join(output),
388
 
                          """
389
 
This is stupid!  I hate it!
390
 
    I agree, the first comment was wrong and you are right!
391
 
        I'm a fanboy!
392
 
""".lstrip())
393
 
 
394
 
        tree = FreeThreadedComment.objects.get_tree(topic, root=comment2.id)
395
 
        output = []
396
 
        for comment in tree:
397
 
            output.append('%s %s' % ('    ' * comment.depth, comment.comment))
398
 
        self.assertEquals('\n'.join(output),
399
 
                          """
400
 
This is stupid!  I hate it!
401
 
    I agree, the first comment was wrong and you are right!
402
 
        I'm a fanboy!
403
 
""".lstrip())