Skip to content

Commit 0a3ce7c

Browse files
committed
add tests to the interchangable keys
1 parent b13361c commit 0a3ce7c

File tree

5 files changed

+119
-1
lines changed

5 files changed

+119
-1
lines changed

tests/testapp/fixtures/comment_tests.json

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"pk" : 1,
2828
"fields" : {
2929
"author" : 1,
30+
"uuid" : "336384ea-b04f-4a3a-a06a-1f25a8048f8f",
3031
"headline" : "Man Bites Dog"
3132
}
3233
},
@@ -35,6 +36,7 @@
3536
"pk" : 2,
3637
"fields" : {
3738
"author" : 2,
39+
"uuid" : "d77c5d7d-1b0d-467b-814b-96697bd9a686",
3840
"headline" : "Dog Bites Man"
3941
}
4042
},

tests/testapp/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def __str__(self):
1515

1616

1717
class Article(models.Model):
18+
uuid = models.UUIDField(editable=False, null=True)
1819
author = models.ForeignKey(Author, on_delete=models.CASCADE)
1920
headline = models.CharField(max_length=100)
2021

tests/testapp/tests/test_comment_form.py

+92-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import time
2+
from datetime import datetime
23

34
from django.conf import settings
5+
from django.contrib.contenttypes.models import ContentType
46
from django.contrib.sites.models import Site
7+
from django.test.utils import override_settings
8+
from freezegun import freeze_time
9+
from testapp.models import Article
510

611
from django_comments.forms import CommentForm
712
from django_comments.models import Comment
813

914
from . import CommentTestCase
10-
from testapp.models import Article
1115

16+
CT = ContentType.objects.get_for_model
1217

1318
class CommentFormTests(CommentTestCase):
1419

@@ -75,6 +80,92 @@ def testGetCommentObject(self):
7580
c = f.get_comment_object(site_id=self.site_2.id)
7681
self.assertEqual(c.site_id, self.site_2.id)
7782

83+
@freeze_time("2012-01-14 13:21:34")
84+
def test_get_comment_create_data_uuid(self):
85+
"""
86+
The get_comment_create_data() method returns
87+
uuid field as object_pk if overriden by settings
88+
"""
89+
a = Article.objects.get(pk=1)
90+
d = self.getValidData(a)
91+
d["comment"] = "testGetCommentObject with a site"
92+
f = CommentForm(Article.objects.get(pk=1), data=d)
93+
self.assertTrue(f.is_valid())
94+
with override_settings(
95+
COMMENTS_ID_OVERRIDES={
96+
"testapp.Article": "uuid",
97+
}
98+
):
99+
c = f.get_comment_create_data(site_id=self.site_2.id)
100+
self.assertDictEqual(
101+
c,
102+
{
103+
"comment": "testGetCommentObject with a site",
104+
"content_type": CT(Article),
105+
"is_public": True,
106+
"is_removed": False,
107+
"object_pk": "336384ea-b04f-4a3a-a06a-1f25a8048f8f", # uuid is returned
108+
"site_id": 2,
109+
"submit_date": datetime(2012, 1, 14, 13, 21, 34),
110+
"user_email": "[email protected]",
111+
"user_name": "Jim Bob",
112+
"user_url": "",
113+
},
114+
)
115+
c = f.get_comment_create_data(site_id=self.site_2.id)
116+
self.assertDictEqual(
117+
c,
118+
{
119+
"comment": "testGetCommentObject with a site",
120+
"content_type": CT(Article),
121+
"is_public": True,
122+
"is_removed": False,
123+
"object_pk": "1", # pk is returned as object_pk
124+
"site_id": 2,
125+
"submit_date": datetime(2012, 1, 14, 13, 21, 34),
126+
"user_email": "[email protected]",
127+
"user_name": "Jim Bob",
128+
"user_url": "",
129+
},
130+
)
131+
132+
@freeze_time("2012-01-14 13:21:34")
133+
def test_generate_security_data_uuid(self):
134+
"""
135+
The generate_security_data() method returns
136+
uuid field as object_pk if overriden by settings
137+
"""
138+
a = Article.objects.get(pk=1)
139+
d = self.getValidData(a)
140+
d["comment"] = "testGetCommentObject with a site"
141+
f = CommentForm(Article.objects.get(pk=1), data=d)
142+
self.assertTrue(f.is_valid())
143+
with override_settings(
144+
COMMENTS_ID_OVERRIDES={
145+
"testapp.Article": "uuid",
146+
}
147+
):
148+
c = f.generate_security_data()
149+
self.assertDictEqual(
150+
c,
151+
{
152+
"content_type": "testapp.article",
153+
"object_pk": "336384ea-b04f-4a3a-a06a-1f25a8048f8f",
154+
"security_hash": "b89ebc7c1c6ed757991fa06027405aecdf8d51f1",
155+
"timestamp": "1326547294",
156+
},
157+
)
158+
c = f.generate_security_data()
159+
self.assertDictEqual(
160+
c,
161+
{
162+
"content_type": "testapp.article",
163+
"object_pk": "1",
164+
"security_hash": "2f4a55f47e58b22791d4d26f3b1a2e594302fb61",
165+
"timestamp": "1326547294",
166+
},
167+
)
168+
78169
def testProfanities(self):
79170
"""Test COMMENTS_ALLOW_PROFANITIES and PROFANITIES_LIST settings"""
80171
a = Article.objects.get(pk=1)

tests/testapp/tests/test_comment_views.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.conf import settings
22
from django.contrib.auth.models import User
3+
from django.test.utils import override_settings
34

45
from django_comments import signals
56
from django_comments.abstracts import COMMENT_MAX_LENGTH
@@ -333,6 +334,28 @@ def testCommentPostRedirectWithInvalidIntegerPK(self):
333334
response = self.client.get(broken_location)
334335
self.assertEqual(response.status_code, 200)
335336

337+
@override_settings(
338+
COMMENTS_ID_OVERRIDES={
339+
"testapp.Article": "uuid",
340+
}
341+
)
342+
def testCommentPostWithUUID(self):
343+
"""
344+
Tests that attempting to retrieve the location specified in the
345+
post redirect, after adding some invalid data to the expected
346+
querystring it ends with, doesn't cause a server error.
347+
"""
348+
a = Article.objects.get(pk=1)
349+
data = self.getValidData(a)
350+
data["comment"] = "This is another comment"
351+
self.assertEqual(data["object_pk"], "336384ea-b04f-4a3a-a06a-1f25a8048f8f")
352+
response = self.client.post("/post/", data)
353+
self.assertEqual(response.status_code, 302)
354+
self.assertEqual(Comment.objects.count(), 1)
355+
self.assertEqual(
356+
Comment.objects.first().object_pk, "336384ea-b04f-4a3a-a06a-1f25a8048f8f"
357+
)
358+
336359
def testCommentNextWithQueryStringAndAnchor(self):
337360
"""
338361
The `next` key needs to handle already having an anchor. Refs #13411.

tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ deps=
2727
django-40: Django>=4.0a1,<4.1
2828
django-41: Django>=4.1a1,<4.2
2929
django-main: https://github.com/django/django/archive/main.tar.gz
30+
freezegun

0 commit comments

Comments
 (0)