3
from xml.dom.minidom import parseString
5
from django.core import mail
6
from django.core.urlresolvers import reverse
7
from django.template import Context, Template
8
from django.test import TestCase
9
from django.utils.simplejson import loads
11
from django.contrib.auth.models import User
12
from django.contrib.contenttypes.models import ContentType
14
from threadedcomments.models import FreeThreadedComment, ThreadedComment, TestModel
15
from threadedcomments.models import MARKDOWN, TEXTILE, REST, PLAINTEXT
16
from threadedcomments.templatetags import threadedcommentstags as tags
19
__all__ = ('ViewsTestCase',)
22
class ViewsTestCase(TestCase):
23
urls = 'threadedcomments.tests.threadedcomments_urls'
25
def test_freecomment_create(self):
27
topic = TestModel.objects.create(name='Test2')
28
content_type = ContentType.objects.get_for_model(topic)
30
url = reverse('tc_free_comment', kwargs={
31
'content_type': content_type.id,
34
response = self.client.post(url, {
37
'website': 'http://www.eflorenzano.com/',
38
'email': 'floguy@gmail.com',
41
o = FreeThreadedComment.objects.latest(
42
'date_submitted').get_base_data(show_dates=False)
43
self.assertEquals(o, {
44
'website': u'http://www.eflorenzano.com/',
48
'markup': u'plaintext',
49
'content_object': topic,
51
'ip_address': u'127.0.0.1',
52
'email': u'floguy@gmail.com',
56
def test_freecomment_preview(self):
58
topic = TestModel.objects.create(name='Test2')
59
content_type = ContentType.objects.get_for_model(topic)
61
url = reverse('tc_free_comment', kwargs={
62
'content_type': content_type.id,
66
response = self.client.post(url, {
69
'website': 'http://www.eflorenzano.com/',
70
'email': 'floguy@gmail.com',
74
self.assertEquals(len(response.content) > 0, True)
76
def test_freecomment_edit(self):
78
topic = TestModel.objects.create(name='Test2')
80
comment = FreeThreadedComment.objects.create_for_object(topic,
81
ip_address='127.0.0.1',
82
comment='My test free comment!',
85
url = reverse('tc_free_comment_edit', kwargs={
89
response = self.client.post(url, {
90
'comment': 'test1_edited',
92
'website': 'http://www.eflorenzano.com/',
93
'email': 'floguy@gmail.com',
96
o = FreeThreadedComment.objects.latest(
97
'date_submitted').get_base_data(show_dates=False)
98
self.assertEquals(o, {
99
'website': u'http://www.eflorenzano.com/',
100
'comment': u'test1_edited',
103
'markup': u'plaintext',
104
'content_object': topic,
106
'ip_address': u'127.0.0.1',
107
'email': u'floguy@gmail.com',
111
def test_freecomment_edit_with_preview(self):
113
topic = TestModel.objects.create(name='Test2')
115
comment = FreeThreadedComment.objects.create_for_object(topic,
116
website='http://oebfare.com/',
117
comment='My test free comment!',
118
ip_address='127.0.0.1',
121
url = reverse('tc_free_comment_edit', kwargs={
122
'edit_id': comment.pk
125
response = self.client.post(url, {
126
'comment': 'test1_edited',
128
'website': 'http://www.eflorenzano.com/',
129
'email': 'floguy@gmail.com',
133
o = FreeThreadedComment.objects.latest(
134
'date_submitted').get_base_data(show_dates=False)
135
self.assertEquals(o, {
136
'website': u'http://oebfare.com/',
137
'comment': u'My test free comment!',
140
'markup': u'plaintext',
141
'content_object': topic,
143
'ip_address': u'127.0.0.1',
147
self.assertEquals(len(response.content) > 0, True)
149
def test_freecomment_json_create(self):
151
topic = TestModel.objects.create(name='Test2')
152
content_type = ContentType.objects.get_for_model(topic)
154
url = reverse('tc_free_comment_ajax', kwargs={
155
'content_type': content_type.id,
156
'object_id': topic.id,
160
response = self.client.post(url, {
163
'website': 'http://www.eflorenzano.com/',
164
'email': 'floguy@gmail.com'
166
tmp = loads(response.content)
167
o = FreeThreadedComment.objects.latest(
168
'date_submitted').get_base_data(show_dates=False)
169
self.assertEquals(o, {
170
'website': u'http://www.eflorenzano.com/',
174
'markup': u'plaintext',
175
'content_object': topic,
177
'ip_address': u'127.0.0.1',
178
'email': u'floguy@gmail.com',
182
def test_freecomment_json_edit(self):
184
topic = TestModel.objects.create(name='Test2')
186
comment = FreeThreadedComment.objects.create_for_object(topic,
187
ip_address='127.0.0.1',
188
comment='My test free comment!',
191
url = reverse('tc_free_comment_edit_ajax', kwargs={
192
'edit_id': comment.pk,
196
response = self.client.post(url, {
197
'comment': 'test2_edited',
199
'website': 'http://www.eflorenzano.com/',
200
'email': 'floguy@gmail.com'
202
tmp = loads(response.content)
203
o = FreeThreadedComment.objects.latest(
204
'date_submitted').get_base_data(show_dates=False)
205
self.assertEquals(o, {
206
'website': u'http://www.eflorenzano.com/',
207
'comment': u'test2_edited',
210
'markup': u'plaintext',
211
'content_object': topic,
213
'ip_address': u'127.0.0.1',
214
'email': u'floguy@gmail.com',
218
def test_freecomment_xml_create(self):
220
topic = TestModel.objects.create(name='Test2')
221
content_type = ContentType.objects.get_for_model(topic)
223
url = reverse('tc_free_comment_ajax', kwargs={
224
'content_type': content_type.id,
225
'object_id': topic.id,
229
response = self.client.post(url, {'comment': 'test3', 'name': 'eric',
230
'website': 'http://www.eflorenzano.com/', 'email': 'floguy@gmail.com', 'next': '/'})
231
tmp = parseString(response.content)
232
o = FreeThreadedComment.objects.latest(
233
'date_submitted').get_base_data(show_dates=False)
234
self.assertEquals(o, {
235
'website': u'http://www.eflorenzano.com/',
239
'markup': u'plaintext',
240
'content_object': topic,
242
'ip_address': u'127.0.0.1',
243
'email': u'floguy@gmail.com',
247
def test_freecomment_xml_edit(self):
249
topic = TestModel.objects.create(name='Test2')
251
comment = FreeThreadedComment.objects.create_for_object(topic,
252
ip_address='127.0.0.1',
253
comment='My test free comment!',
256
url = reverse('tc_free_comment_edit_ajax', kwargs={
257
'edit_id': comment.pk,
261
response = self.client.post(url, {
262
'comment': 'test2_edited',
264
'website': 'http://www.eflorenzano.com/',
265
'email': 'floguy@gmail.com'
267
tmp = parseString(response.content)
268
o = FreeThreadedComment.objects.latest(
269
'date_submitted').get_base_data(show_dates=False)
270
self.assertEquals(o, {
271
'website': u'http://www.eflorenzano.com/',
272
'comment': u'test2_edited',
275
'markup': u'plaintext',
276
'content_object': topic,
278
'ip_address': u'127.0.0.1',
279
'email': u'floguy@gmail.com',
283
def test_freecomment_child_create(self):
285
topic = TestModel.objects.create(name='Test2')
286
content_type = ContentType.objects.get_for_model(topic)
288
parent = FreeThreadedComment.objects.create_for_object(topic,
289
ip_address='127.0.0.1',
290
comment='My test free comment!',
293
url = reverse('tc_free_comment_parent', kwargs={
294
'content_type': content_type.id,
295
'object_id': topic.id,
296
'parent_id': parent.id
298
response = self.client.post(url, {
301
'website': 'http://www.eflorenzano.com/',
302
'email': 'floguy@gmail.com',
305
o = FreeThreadedComment.objects.latest(
306
'date_submitted').get_base_data(show_dates=False)
307
self.assertEquals(o, {
308
'website': u'http://www.eflorenzano.com/',
312
'markup': u'plaintext',
313
'content_object': topic,
315
'ip_address': u'127.0.0.1',
316
'email': u'floguy@gmail.com',
320
def test_freecomment_child_json_create(self):
322
topic = TestModel.objects.create(name='Test2')
323
content_type = ContentType.objects.get_for_model(topic)
325
parent = FreeThreadedComment.objects.create_for_object(topic,
326
ip_address='127.0.0.1',
327
comment='My test free comment!',
330
url = reverse('tc_free_comment_parent_ajax', kwargs={
331
'content_type': content_type.id,
332
'object_id': topic.id,
333
'parent_id': parent.id,
337
response = self.client.post(url, {
340
'website': 'http://www.eflorenzano.com/',
341
'email': 'floguy@gmail.com'
343
tmp = loads(response.content)
344
o = FreeThreadedComment.objects.latest(
345
'date_submitted').get_base_data(show_dates=False)
346
self.assertEquals(o, {
347
'website': u'http://www.eflorenzano.com/',
351
'markup': u'plaintext',
352
'content_object': topic,
354
'ip_address': u'127.0.0.1',
355
'email': u'floguy@gmail.com',
359
def test_freecomment_child_xml_create(self):
361
topic = TestModel.objects.create(name='Test2')
362
content_type = ContentType.objects.get_for_model(topic)
364
parent = FreeThreadedComment.objects.create_for_object(topic,
365
ip_address='127.0.0.1',
366
comment='My test free comment!',
369
url = reverse('tc_free_comment_parent_ajax', kwargs={
370
'content_type': content_type.id,
371
'object_id': topic.id,
372
'parent_id': parent.id,
376
response = self.client.post(url, {
377
'comment': 'test6', 'name': 'eric',
378
'website': 'http://www.eflorenzano.com/',
379
'email': 'floguy@gmail.com'
381
tmp = parseString(response.content)
382
o = FreeThreadedComment.objects.latest(
383
'date_submitted').get_base_data(show_dates=False)
384
self.assertEquals(o, {
385
'website': u'http://www.eflorenzano.com/',
389
'markup': u'plaintext',
390
'content_object': topic,
392
'ip_address': u'127.0.0.1',
393
'email': u'floguy@gmail.com',
397
def create_user_and_login(self):
398
user = User.objects.create_user(
400
'testuser@gmail.com',
403
user.is_active = True
405
self.client.login(username='testuser', password='password')
408
def test_comment_create(self):
410
user = self.create_user_and_login()
412
topic = TestModel.objects.create(name='Test2')
413
content_type = ContentType.objects.get_for_model(topic)
415
url = reverse('tc_comment', kwargs={
416
'content_type': content_type.id,
417
'object_id': topic.id
420
response = self.client.post(url, {
424
o = ThreadedComment.objects.latest(
425
'date_submitted').get_base_data(show_dates=False)
426
self.assertEquals(o, {
428
'is_approved': False,
430
'markup': u'plaintext',
431
'content_object': topic,
434
'ip_address': u'127.0.0.1',
437
def test_comment_preview(self):
439
user = self.create_user_and_login()
441
topic = TestModel.objects.create(name='Test2')
442
content_type = ContentType.objects.get_for_model(topic)
444
url = reverse('tc_comment', kwargs={
445
'content_type': content_type.id,
446
'object_id': topic.id
449
response = self.client.post(url, {
454
self.assertEquals(len(response.content) > 0, True)
456
def test_comment_edit(self):
458
user = self.create_user_and_login()
460
topic = TestModel.objects.create(name='Test2')
461
comment = ThreadedComment.objects.create_for_object(topic,
463
ip_address=u'127.0.0.1',
464
comment='My test comment!',
467
url = reverse('tc_comment_edit', kwargs={
468
'edit_id': comment.pk,
471
response = self.client.post(url, {
472
'comment': 'test7_edited',
475
o = ThreadedComment.objects.latest(
476
'date_submitted').get_base_data(show_dates=False)
477
self.assertEquals(o, {
478
'comment': u'test7_edited',
479
'is_approved': False,
481
'markup': u'plaintext',
482
'content_object': topic,
485
'ip_address': u'127.0.0.1',
488
def test_comment_edit_with_preview(self):
490
user = self.create_user_and_login()
492
topic = TestModel.objects.create(name='Test2')
493
comment = ThreadedComment.objects.create_for_object(topic,
495
ip_address=u'127.0.0.1',
496
comment='My test comment!',
499
url = reverse('tc_comment_edit', kwargs={
500
'edit_id': comment.pk,
503
response = self.client.post(url, {
504
'comment': 'test7_edited',
509
self.assertEquals(len(response.content) > 0, True)
510
o = ThreadedComment.objects.latest(
511
'date_submitted').get_base_data(show_dates=False)
512
self.assertEquals(o, {
513
'comment': u'My test comment!',
514
'is_approved': False,
516
'markup': u'plaintext',
517
'content_object': topic,
520
'ip_address': u'127.0.0.1',
523
def test_comment_json_create(self):
525
user = self.create_user_and_login()
527
topic = TestModel.objects.create(name='Test2')
528
content_type = ContentType.objects.get_for_model(topic)
530
url = reverse('tc_comment_ajax', kwargs={
531
'content_type': content_type.id,
532
'object_id': topic.id,
536
response = self.client.post(url, {
539
tmp = loads(response.content)
540
o = ThreadedComment.objects.latest(
541
'date_submitted').get_base_data(show_dates=False)
542
self.assertEquals(o, {
544
'is_approved': False,
546
'markup': u'plaintext',
547
'content_object': topic,
550
'ip_address': u'127.0.0.1',
553
def test_comment_json_edit(self):
555
user = self.create_user_and_login()
557
topic = TestModel.objects.create(name='Test2')
558
comment = ThreadedComment.objects.create_for_object(topic,
560
ip_address=u'127.0.0.1',
561
comment='My test comment!',
564
url = reverse('tc_comment_edit_ajax', kwargs={
565
'edit_id': comment.pk,
569
response = self.client.post(url, {
570
'comment': 'test8_edited'
572
tmp = loads(response.content)
573
o = ThreadedComment.objects.latest(
574
'date_submitted').get_base_data(show_dates=False)
575
self.assertEquals(o, {
576
'comment': u'test8_edited',
577
'is_approved': False,
579
'markup': u'plaintext',
580
'content_object': topic,
583
'ip_address': u'127.0.0.1',
586
def test_comment_xml_create(self):
588
user = self.create_user_and_login()
590
topic = TestModel.objects.create(name='Test2')
591
content_type = ContentType.objects.get_for_model(topic)
593
url = reverse('tc_comment_ajax', kwargs={
594
'content_type': content_type.id,
595
'object_id': topic.id,
599
response = self.client.post(url, {
602
tmp = parseString(response.content)
603
o = ThreadedComment.objects.latest(
604
'date_submitted').get_base_data(show_dates=False)
605
self.assertEquals(o, {
607
'is_approved': False,
609
'markup': u'plaintext',
610
'content_object': topic,
613
'ip_address': u'127.0.0.1',
616
def test_comment_xml_edit(self):
618
user = self.create_user_and_login()
620
topic = TestModel.objects.create(name='Test2')
621
comment = ThreadedComment.objects.create_for_object(topic,
623
ip_address=u'127.0.0.1',
624
comment='My test comment!',
627
url = reverse('tc_comment_edit_ajax', kwargs={
628
'edit_id': comment.pk,
632
response = self.client.post(url, {
633
'comment': 'test8_edited'
635
tmp = parseString(response.content)
636
o = ThreadedComment.objects.latest(
637
'date_submitted').get_base_data(show_dates=False)
638
self.assertEquals(o, {
639
'comment': u'test8_edited',
640
'is_approved': False,
642
'markup': u'plaintext',
643
'content_object': topic,
646
'ip_address': u'127.0.0.1',
649
def test_comment_child_create(self):
651
user = self.create_user_and_login()
653
topic = TestModel.objects.create(name='Test2')
654
content_type = ContentType.objects.get_for_model(topic)
656
parent = ThreadedComment.objects.create_for_object(topic,
658
ip_address=u'127.0.0.1',
659
comment='My test comment!',
662
url = reverse('tc_comment_parent', kwargs={
663
'content_type': content_type.id,
664
'object_id': topic.id,
665
'parent_id': parent.id
668
response = self.client.post(url, {
672
o = ThreadedComment.objects.latest(
673
'date_submitted').get_base_data(show_dates=False)
674
self.assertEquals(o, {
675
'comment': u'test10',
676
'is_approved': False,
678
'markup': u'plaintext',
679
'content_object': topic,
682
'ip_address': u'127.0.0.1',
685
def test_comment_child_json_create(self):
687
user = self.create_user_and_login()
689
topic = TestModel.objects.create(name='Test2')
690
content_type = ContentType.objects.get_for_model(topic)
692
parent = ThreadedComment.objects.create_for_object(topic,
694
ip_address=u'127.0.0.1',
695
comment='My test comment!',
698
url = reverse('tc_comment_parent_ajax', kwargs={
699
'content_type': content_type.id,
700
'object_id': topic.id,
701
'parent_id': parent.id,
705
response = self.client.post(url, {
708
tmp = loads(response.content)
709
o = ThreadedComment.objects.latest(
710
'date_submitted').get_base_data(show_dates=False)
711
self.assertEquals(o, {
712
'comment': u'test11',
713
'is_approved': False,
715
'markup': u'plaintext',
716
'content_object': topic,
719
'ip_address': u'127.0.0.1',
722
def test_comment_child_xml_create(self):
724
user = self.create_user_and_login()
726
topic = TestModel.objects.create(name='Test2')
727
content_type = ContentType.objects.get_for_model(topic)
729
parent = ThreadedComment.objects.create_for_object(topic,
731
ip_address=u'127.0.0.1',
732
comment='My test comment!',
735
url = reverse('tc_comment_parent_ajax', kwargs={
736
'content_type': content_type.id,
737
'object_id': topic.id,
738
'parent_id': parent.id,
742
response = self.client.post(url, {
745
tmp = parseString(response.content)
746
o = ThreadedComment.objects.latest(
747
'date_submitted').get_base_data(show_dates=False)
748
self.assertEquals(o, {
749
'comment': u'test12',
750
'is_approved': False,
752
'markup': u'plaintext',
753
'content_object': topic,
756
'ip_address': u'127.0.0.1',
759
def test_freecomment_delete(self):
761
user = User.objects.create_user(
763
'testuser@gmail.com',
766
user.is_active = True
768
self.client.login(username='testuser', password='password')
770
topic = TestModel.objects.create(name='Test2')
772
comment = FreeThreadedComment.objects.create_for_object(topic,
773
ip_address=u'127.0.0.1',
774
comment='My test comment!',
776
deleted_id = comment.id
778
url = reverse('tc_free_comment_delete', kwargs={
779
'object_id': comment.id,
782
response = self.client.post(url, {'next': '/'})
783
o = response['Location'].split(
784
'?')[-1] == 'next=/freecomment/%d/delete/' % deleted_id
785
self.assertEquals(o, True)
787
# become super user and try deleting comment
788
user.is_superuser = True
791
response = self.client.post(url, {'next': '/'})
792
self.assertEquals(response['Location'], 'http://testserver/')
794
FreeThreadedComment.DoesNotExist,
795
lambda: FreeThreadedComment.objects.get(id=deleted_id)
801
response = self.client.get(url, {'next': '/'})
802
self.assertEquals(len(response.content) > 0, True)
804
o = FreeThreadedComment.objects.get(id=deleted_id) != None
805
self.assertEquals(o, True)
807
def test_comment_delete(self):
809
some_other_guy = User.objects.create_user(
811
'somewhere@overthemoon.com',
814
user = User.objects.create_user(
816
'testuser@gmail.com',
819
user.is_active = True
821
self.client.login(username='testuser', password='password')
823
topic = TestModel.objects.create(name='Test2')
825
comment = ThreadedComment.objects.create_for_object(topic,
827
ip_address=u'127.0.0.1',
828
comment='My test comment!',
830
deleted_id = comment.id
832
url = reverse('tc_comment_delete', kwargs={
833
'object_id': comment.id,
835
response = self.client.post(url, {'next': '/'})
836
self.assertEquals(response['Location'].split(
837
'?')[-1], 'next=/comment/%s/delete/' % deleted_id)
839
user.is_superuser = True
842
response = self.client.post(url, {'next': '/'})
843
self.assertEquals(response['Location'], 'http://testserver/')
845
ThreadedComment.DoesNotExist,
846
lambda: ThreadedComment.objects.get(id=deleted_id)
852
response = self.client.get(url, {'next': '/'})
853
self.assertEquals(len(response.content) > 0, True)
855
o = ThreadedComment.objects.get(id=deleted_id) != None
856
self.assertEquals(o, True)