Skip to content

Commit f241c5e

Browse files
committed
test commentbox_props_response() and it's behavior with non-ID primary keys
1 parent ab98ab4 commit f241c5e

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Generated by Django 4.0.6 on 2022-07-08 11:28
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('tests', '0002_auto_20170523_1624'),
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='UUIDDiary',
16+
fields=[
17+
('diary_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, to='tests.diary')),
18+
('uuid', models.UUIDField(editable=False, primary_key=True, serialize=False, verbose_name='uuid')),
19+
],
20+
bases=('tests.diary',),
21+
),
22+
]

django_comments_xtd/tests/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class Meta:
5252
ordering = ('-publish',)
5353

5454

55+
class UUIDDiary(Diary):
56+
"""Diary, that accepts comments."""
57+
uuid = models.UUIDField("uuid", editable=False, primary_key=True)
58+
59+
5560
class DiaryCommentModerator(XtdCommentModerator):
5661
email_notification = True
5762
enable_field = 'allow_comments'
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import uuid
2+
3+
from django.contrib.auth.models import AnonymousUser, User
4+
from django.test import RequestFactory, TestCase
5+
6+
from django_comments_xtd.api.frontend import commentbox_props_response
7+
from django_comments_xtd.models import XtdComment
8+
from django_comments_xtd.tests.models import Diary, UUIDDiary
9+
10+
11+
class CommentBoxTestCase(TestCase):
12+
def test_commentbox_props_response(self):
13+
request_factory = RequestFactory()
14+
request = request_factory.get("/")
15+
user = User.objects.create_user("bob", "[email protected]", "pwd")
16+
diary = Diary.objects.create(
17+
body='Lorem ipsum',
18+
allow_comments=True
19+
)
20+
XtdComment.objects.create(
21+
content_object=diary,
22+
site_id=1,
23+
)
24+
response = commentbox_props_response(diary, user, request)
25+
d = response.data
26+
self.assertEqual(d['comment_count'], 1)
27+
self.assertEqual(d['form']['object_pk'], str(diary.id))
28+
self.assertEqual(d['count_url'], '/comments/api/tests-diary/1/count/')
29+
self.assertEqual(d['list_url'], '/comments/api/tests-diary/1/')
30+
self.assertEqual(d['current_user'], "1:bob")
31+
32+
def test_commentbox_props_response_anonymous(self):
33+
request_factory = RequestFactory()
34+
request = request_factory.get("/")
35+
user = AnonymousUser()
36+
diary = Diary.objects.create(
37+
body='Lorem ipsum',
38+
allow_comments=True
39+
)
40+
XtdComment.objects.create(
41+
content_object=diary,
42+
site_id=1,
43+
)
44+
response = commentbox_props_response(diary, user, request)
45+
d = response.data
46+
self.assertEqual(d['comment_count'], 1)
47+
self.assertEqual(d['form']['object_pk'], str(diary.id))
48+
self.assertEqual(d['count_url'], '/comments/api/tests-diary/1/count/')
49+
self.assertEqual(d['list_url'], '/comments/api/tests-diary/1/')
50+
self.assertEqual(d['current_user'], "0:Anonymous")
51+
52+
def test_commentbox_props_response_uuid(self):
53+
request_factory = RequestFactory()
54+
request = request_factory.get("/")
55+
user = AnonymousUser()
56+
diary = UUIDDiary.objects.create(
57+
uuid=uuid.uuid4(),
58+
body='Lorem ipsum',
59+
allow_comments=True
60+
)
61+
XtdComment.objects.create(
62+
content_object=diary,
63+
site_id=1,
64+
)
65+
response = commentbox_props_response(diary, user, request)
66+
d = response.data
67+
self.assertEqual(d['comment_count'], 1)
68+
self.assertEqual(d['form']['object_pk'], str(diary.uuid))
69+
self.assertEqual(d['count_url'], f'/comments/api/tests-uuiddiary/{diary.uuid}/count/')
70+
self.assertEqual(d['list_url'], f'/comments/api/tests-uuiddiary/{diary.uuid}/')

0 commit comments

Comments
 (0)