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('date_submitted').get_base_data(show_dates=False)
42
self.assertEquals(o, {
43
'website': u'http://www.eflorenzano.com/',
47
'markup': u'plaintext',
48
'content_object': topic,
50
'ip_address': u'127.0.0.1',
51
'email': u'floguy@gmail.com',
55
def test_freecomment_preview(self):
57
topic = TestModel.objects.create(name="Test2")
58
content_type = ContentType.objects.get_for_model(topic)
60
url = reverse('tc_free_comment', kwargs={
61
'content_type': content_type.id,
65
response = self.client.post(url, {
68
'website': 'http://www.eflorenzano.com/',
69
'email': 'floguy@gmail.com',
73
self.assertEquals(len(response.content) > 0, True)
75
def test_freecomment_edit(self):
77
topic = TestModel.objects.create(name="Test2")
79
comment = FreeThreadedComment.objects.create_for_object(topic,
80
ip_address = '127.0.0.1',
81
comment = "My test free comment!",
84
url = reverse('tc_free_comment_edit', kwargs={
88
response = self.client.post(url, {
89
'comment' : 'test1_edited',
91
'website' : 'http://www.eflorenzano.com/',
92
'email' : 'floguy@gmail.com',
95
o = FreeThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
96
self.assertEquals(o, {
97
'website': u'http://www.eflorenzano.com/',
98
'comment': u'test1_edited',
101
'markup': u'plaintext',
102
'content_object': topic,
104
'ip_address': u'127.0.0.1',
105
'email': u'floguy@gmail.com',
109
def test_freecomment_edit_with_preview(self):
111
topic = TestModel.objects.create(name="Test2")
113
comment = FreeThreadedComment.objects.create_for_object(topic,
114
website = "http://oebfare.com/",
115
comment = "My test free comment!",
116
ip_address = '127.0.0.1',
119
url = reverse('tc_free_comment_edit', kwargs={
120
'edit_id': comment.pk
123
response = self.client.post(url, {
124
'comment': 'test1_edited',
126
'website': 'http://www.eflorenzano.com/',
127
'email': 'floguy@gmail.com',
131
o = FreeThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
132
self.assertEquals(o, {
133
'website': u'http://oebfare.com/',
134
'comment': u'My test free comment!',
137
'markup': u'plaintext',
138
'content_object': topic,
140
'ip_address': u'127.0.0.1',
144
self.assertEquals(len(response.content) > 0, True)
146
def test_freecomment_json_create(self):
148
topic = TestModel.objects.create(name="Test2")
149
content_type = ContentType.objects.get_for_model(topic)
151
url = reverse('tc_free_comment_ajax', kwargs={
152
'content_type': content_type.id,
153
'object_id': topic.id,
157
response = self.client.post(url, {
160
'website': 'http://www.eflorenzano.com/',
161
'email': 'floguy@gmail.com'
163
tmp = loads(response.content)
164
o = FreeThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
165
self.assertEquals(o, {
166
'website': u'http://www.eflorenzano.com/',
170
'markup': u'plaintext',
171
'content_object': topic,
173
'ip_address': u'127.0.0.1',
174
'email': u'floguy@gmail.com',
178
def test_freecomment_json_edit(self):
180
topic = TestModel.objects.create(name="Test2")
182
comment = FreeThreadedComment.objects.create_for_object(topic,
183
ip_address = '127.0.0.1',
184
comment = "My test free comment!",
187
url = reverse('tc_free_comment_edit_ajax',kwargs={
188
'edit_id': comment.pk,
192
response = self.client.post(url, {
193
'comment': 'test2_edited',
195
'website': 'http://www.eflorenzano.com/',
196
'email': 'floguy@gmail.com'
198
tmp = loads(response.content)
199
o = FreeThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
200
self.assertEquals(o, {
201
'website': u'http://www.eflorenzano.com/',
202
'comment': u'test2_edited',
205
'markup': u'plaintext',
206
'content_object': topic,
208
'ip_address': u'127.0.0.1',
209
'email': u'floguy@gmail.com',
213
def test_freecomment_xml_create(self):
215
topic = TestModel.objects.create(name="Test2")
216
content_type = ContentType.objects.get_for_model(topic)
218
url = reverse('tc_free_comment_ajax', kwargs={
219
'content_type': content_type.id,
220
'object_id': topic.id,
224
response = self.client.post(url, {'comment' : 'test3', 'name' : 'eric', 'website' : 'http://www.eflorenzano.com/', 'email' : 'floguy@gmail.com', 'next' : '/'})
225
tmp = parseString(response.content)
226
o = FreeThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
227
self.assertEquals(o, {
228
'website': u'http://www.eflorenzano.com/',
232
'markup': u'plaintext',
233
'content_object': topic,
235
'ip_address': u'127.0.0.1',
236
'email': u'floguy@gmail.com',
240
def test_freecomment_xml_edit(self):
242
topic = TestModel.objects.create(name="Test2")
244
comment = FreeThreadedComment.objects.create_for_object(topic,
245
ip_address = '127.0.0.1',
246
comment = "My test free comment!",
249
url = reverse('tc_free_comment_edit_ajax', kwargs={
250
'edit_id': comment.pk,
254
response = self.client.post(url, {
255
'comment': 'test2_edited',
257
'website': 'http://www.eflorenzano.com/',
258
'email': 'floguy@gmail.com'
260
tmp = parseString(response.content)
261
o = FreeThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
262
self.assertEquals(o, {
263
'website': u'http://www.eflorenzano.com/',
264
'comment': u'test2_edited',
267
'markup': u'plaintext',
268
'content_object': topic,
270
'ip_address': u'127.0.0.1',
271
'email': u'floguy@gmail.com',
275
def test_freecomment_child_create(self):
277
topic = TestModel.objects.create(name="Test2")
278
content_type = ContentType.objects.get_for_model(topic)
280
parent = FreeThreadedComment.objects.create_for_object(topic,
281
ip_address = '127.0.0.1',
282
comment = "My test free comment!",
285
url = reverse('tc_free_comment_parent', kwargs={
286
'content_type': content_type.id,
287
'object_id': topic.id,
288
'parent_id': parent.id
290
response = self.client.post(url, {
293
'website': 'http://www.eflorenzano.com/',
294
'email': 'floguy@gmail.com',
297
o = FreeThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
298
self.assertEquals(o, {
299
'website': u'http://www.eflorenzano.com/',
303
'markup': u'plaintext',
304
'content_object': topic,
306
'ip_address': u'127.0.0.1',
307
'email': u'floguy@gmail.com',
311
def test_freecomment_child_json_create(self):
313
topic = TestModel.objects.create(name="Test2")
314
content_type = ContentType.objects.get_for_model(topic)
316
parent = FreeThreadedComment.objects.create_for_object(topic,
317
ip_address = '127.0.0.1',
318
comment = "My test free comment!",
321
url = reverse('tc_free_comment_parent_ajax', kwargs={
322
'content_type': content_type.id,
323
'object_id': topic.id,
324
'parent_id': parent.id,
328
response = self.client.post(url, {
331
'website': 'http://www.eflorenzano.com/',
332
'email': 'floguy@gmail.com'
334
tmp = loads(response.content)
335
o = FreeThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
336
self.assertEquals(o, {
337
'website': u'http://www.eflorenzano.com/',
341
'markup': u'plaintext',
342
'content_object': topic,
344
'ip_address': u'127.0.0.1',
345
'email': u'floguy@gmail.com',
349
def test_freecomment_child_xml_create(self):
351
topic = TestModel.objects.create(name="Test2")
352
content_type = ContentType.objects.get_for_model(topic)
354
parent = FreeThreadedComment.objects.create_for_object(topic,
355
ip_address = '127.0.0.1',
356
comment = "My test free comment!",
359
url = reverse('tc_free_comment_parent_ajax', kwargs={
360
'content_type': content_type.id,
361
'object_id': topic.id,
362
'parent_id': parent.id,
366
response = self.client.post(url, {
367
'comment': 'test6', 'name': 'eric',
368
'website': 'http://www.eflorenzano.com/',
369
'email': 'floguy@gmail.com'
371
tmp = parseString(response.content)
372
o = FreeThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
373
self.assertEquals(o, {
374
'website': u'http://www.eflorenzano.com/',
378
'markup': u'plaintext',
379
'content_object': topic,
381
'ip_address': u'127.0.0.1',
382
'email': u'floguy@gmail.com',
386
def create_user_and_login(self):
387
user = User.objects.create_user(
389
'testuser@gmail.com',
392
user.is_active = True
394
self.client.login(username='testuser', password='password')
397
def test_comment_create(self):
399
user = self.create_user_and_login()
401
topic = TestModel.objects.create(name="Test2")
402
content_type = ContentType.objects.get_for_model(topic)
404
url = reverse('tc_comment', kwargs={
405
'content_type': content_type.id,
406
'object_id': topic.id
409
response = self.client.post(url, {
413
o = ThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
414
self.assertEquals(o, {
416
'is_approved': False,
418
'markup': u'plaintext',
419
'content_object': topic,
422
'ip_address': u'127.0.0.1',
425
def test_comment_preview(self):
427
user = self.create_user_and_login()
429
topic = TestModel.objects.create(name="Test2")
430
content_type = ContentType.objects.get_for_model(topic)
432
url = reverse('tc_comment', kwargs={
433
'content_type': content_type.id,
434
'object_id': topic.id
437
response = self.client.post(url, {
442
self.assertEquals(len(response.content) > 0, True)
444
def test_comment_edit(self):
446
user = self.create_user_and_login()
448
topic = TestModel.objects.create(name="Test2")
449
comment = ThreadedComment.objects.create_for_object(topic,
451
ip_address = u'127.0.0.1',
452
comment = "My test comment!",
455
url = reverse('tc_comment_edit', kwargs={
456
'edit_id': comment.pk,
459
response = self.client.post(url, {
460
'comment': 'test7_edited',
463
o = ThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
464
self.assertEquals(o, {
465
'comment': u'test7_edited',
466
'is_approved': False,
468
'markup': u'plaintext',
469
'content_object': topic,
472
'ip_address': u'127.0.0.1',
475
def test_comment_edit_with_preview(self):
477
user = self.create_user_and_login()
479
topic = TestModel.objects.create(name="Test2")
480
comment = ThreadedComment.objects.create_for_object(topic,
482
ip_address = u'127.0.0.1',
483
comment = "My test comment!",
486
url = reverse('tc_comment_edit', kwargs={
487
'edit_id': comment.pk,
490
response = self.client.post(url, {
491
'comment': 'test7_edited',
496
self.assertEquals(len(response.content) > 0, True)
497
o = ThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
498
self.assertEquals(o, {
499
'comment': u'My test comment!',
500
'is_approved': False,
502
'markup': u'plaintext',
503
'content_object': topic,
506
'ip_address': u'127.0.0.1',
509
def test_comment_json_create(self):
511
user = self.create_user_and_login()
513
topic = TestModel.objects.create(name="Test2")
514
content_type = ContentType.objects.get_for_model(topic)
516
url = reverse('tc_comment_ajax', kwargs={
517
'content_type': content_type.id,
518
'object_id': topic.id,
522
response = self.client.post(url, {
525
tmp = loads(response.content)
526
o = ThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
527
self.assertEquals(o, {
529
'is_approved': False,
531
'markup': u'plaintext',
532
'content_object': topic,
535
'ip_address': u'127.0.0.1',
538
def test_comment_json_edit(self):
540
user = self.create_user_and_login()
542
topic = TestModel.objects.create(name="Test2")
543
comment = ThreadedComment.objects.create_for_object(topic,
545
ip_address = u'127.0.0.1',
546
comment = "My test comment!",
549
url = reverse('tc_comment_edit_ajax', kwargs={
550
'edit_id': comment.pk,
554
response = self.client.post(url, {
555
'comment': 'test8_edited'
557
tmp = loads(response.content)
558
o = ThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
559
self.assertEquals(o, {
560
'comment': u'test8_edited',
561
'is_approved': False,
563
'markup': u'plaintext',
564
'content_object': topic,
567
'ip_address': u'127.0.0.1',
570
def test_comment_xml_create(self):
572
user = self.create_user_and_login()
574
topic = TestModel.objects.create(name="Test2")
575
content_type = ContentType.objects.get_for_model(topic)
577
url = reverse('tc_comment_ajax', kwargs={
578
'content_type': content_type.id,
579
'object_id': topic.id,
583
response = self.client.post(url, {
586
tmp = parseString(response.content)
587
o = ThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
588
self.assertEquals(o, {
590
'is_approved': False,
592
'markup': u'plaintext',
593
'content_object': topic,
596
'ip_address': u'127.0.0.1',
599
def test_comment_xml_edit(self):
601
user = self.create_user_and_login()
603
topic = TestModel.objects.create(name="Test2")
604
comment = ThreadedComment.objects.create_for_object(topic,
606
ip_address = u'127.0.0.1',
607
comment = "My test comment!",
610
url = reverse('tc_comment_edit_ajax', kwargs={
611
'edit_id': comment.pk,
615
response = self.client.post(url, {
616
'comment': 'test8_edited'
618
tmp = parseString(response.content)
619
o = ThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
620
self.assertEquals(o, {
621
'comment': u'test8_edited',
622
'is_approved': False,
624
'markup': u'plaintext',
625
'content_object': topic,
628
'ip_address': u'127.0.0.1',
631
def test_comment_child_create(self):
633
user = self.create_user_and_login()
635
topic = TestModel.objects.create(name="Test2")
636
content_type = ContentType.objects.get_for_model(topic)
638
parent = ThreadedComment.objects.create_for_object(topic,
640
ip_address = u'127.0.0.1',
641
comment = "My test comment!",
644
url = reverse('tc_comment_parent', kwargs={
645
'content_type': content_type.id,
646
'object_id': topic.id,
647
'parent_id': parent.id
650
response = self.client.post(url, {
654
o = ThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
655
self.assertEquals(o, {
656
'comment': u'test10',
657
'is_approved': False,
659
'markup': u'plaintext',
660
'content_object': topic,
663
'ip_address': u'127.0.0.1',
666
def test_comment_child_json_create(self):
668
user = self.create_user_and_login()
670
topic = TestModel.objects.create(name="Test2")
671
content_type = ContentType.objects.get_for_model(topic)
673
parent = ThreadedComment.objects.create_for_object(topic,
675
ip_address = u'127.0.0.1',
676
comment = "My test comment!",
679
url = reverse('tc_comment_parent_ajax', kwargs={
680
'content_type': content_type.id,
681
'object_id': topic.id,
682
'parent_id': parent.id,
686
response = self.client.post(url, {
689
tmp = loads(response.content)
690
o = ThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
691
self.assertEquals(o, {
692
'comment': u'test11',
693
'is_approved': False,
695
'markup': u'plaintext',
696
'content_object': topic,
699
'ip_address': u'127.0.0.1',
702
def test_comment_child_xml_create(self):
704
user = self.create_user_and_login()
706
topic = TestModel.objects.create(name="Test2")
707
content_type = ContentType.objects.get_for_model(topic)
709
parent = ThreadedComment.objects.create_for_object(topic,
711
ip_address = u'127.0.0.1',
712
comment = "My test comment!",
715
url = reverse('tc_comment_parent_ajax', kwargs={
716
'content_type': content_type.id,
717
'object_id': topic.id,
718
'parent_id': parent.id,
722
response = self.client.post(url, {
725
tmp = parseString(response.content)
726
o = ThreadedComment.objects.latest('date_submitted').get_base_data(show_dates=False)
727
self.assertEquals(o, {
728
'comment': u'test12',
729
'is_approved': False,
731
'markup': u'plaintext',
732
'content_object': topic,
735
'ip_address': u'127.0.0.1',
738
def test_freecomment_delete(self):
740
user = User.objects.create_user(
742
'testuser@gmail.com',
745
user.is_active = True
747
self.client.login(username='testuser', password='password')
749
topic = TestModel.objects.create(name="Test2")
751
comment = FreeThreadedComment.objects.create_for_object(topic,
752
ip_address = u'127.0.0.1',
753
comment = "My test comment!",
755
deleted_id = comment.id
757
url = reverse('tc_free_comment_delete', kwargs={
758
'object_id': comment.id,
761
response = self.client.post(url, {'next': '/'})
762
o = response['Location'].split('?')[-1] == 'next=/freecomment/%d/delete/' % deleted_id
763
self.assertEquals(o, True)
765
# become super user and try deleting comment
766
user.is_superuser = True
769
response = self.client.post(url, {'next': '/'})
770
self.assertEquals(response['Location'], 'http://testserver/')
772
FreeThreadedComment.DoesNotExist,
773
lambda: FreeThreadedComment.objects.get(id=deleted_id)
779
response = self.client.get(url, {'next' : '/'})
780
self.assertEquals(len(response.content) > 0, True)
782
o = FreeThreadedComment.objects.get(id=deleted_id) != None
783
self.assertEquals(o, True)
785
def test_comment_delete(self):
787
some_other_guy = User.objects.create_user(
789
'somewhere@overthemoon.com',
792
user = User.objects.create_user(
794
'testuser@gmail.com',
797
user.is_active = True
799
self.client.login(username='testuser', password='password')
801
topic = TestModel.objects.create(name="Test2")
803
comment = ThreadedComment.objects.create_for_object(topic,
804
user = some_other_guy,
805
ip_address = u'127.0.0.1',
806
comment = "My test comment!",
808
deleted_id = comment.id
810
url = reverse('tc_comment_delete', kwargs={
811
'object_id': comment.id,
813
response = self.client.post(url, {'next' : '/'})
814
self.assertEquals(response['Location'].split('?')[-1], 'next=/comment/%s/delete/' % deleted_id)
816
user.is_superuser = True
819
response = self.client.post(url, {'next' : '/'})
820
self.assertEquals(response['Location'], 'http://testserver/')
822
ThreadedComment.DoesNotExist,
823
lambda: ThreadedComment.objects.get(id=deleted_id)
829
response = self.client.get(url, {'next' : '/'})
830
self.assertEquals(len(response.content) > 0, True)
832
o = ThreadedComment.objects.get(id=deleted_id) != None
833
self.assertEquals(o, True)
b'\\ No newline at end of file'