Skip to content

Commit a376962

Browse files
committed
add test for Add request.FILES to post_comment form
1 parent 2d197ac commit a376962

File tree

8 files changed

+82
-5
lines changed

8 files changed

+82
-5
lines changed

tests/custom_comments/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.urls import reverse
22

33
from . import views
4-
from .forms import CustomCommentForm
4+
55

66

77
def get_model():
@@ -10,6 +10,7 @@ def get_model():
1010

1111

1212
def get_form():
13+
from .forms import CustomCommentForm
1314
return CustomCommentForm
1415

1516

tests/custom_comments/forms.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
from django import forms
22

3+
from django_comments.forms import CommentForm
34

4-
class CustomCommentForm(forms.Form):
5-
pass
5+
6+
class CustomCommentForm(CommentForm):
7+
file = forms.FileField()
8+
9+
def get_comment_create_data(self, site_id=None):
10+
data = super().get_comment_create_data(site_id=site_id)
11+
data["file"] = self.cleaned_data["file"]
12+
return data
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Generated by Django 4.2.1 on 2023-05-17 22:32
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
initial = True
9+
10+
dependencies = [
11+
("django_comments", "0004_add_object_pk_is_removed_index"),
12+
]
13+
14+
operations = [
15+
migrations.CreateModel(
16+
name="CustomComment",
17+
fields=[
18+
(
19+
"comment_ptr",
20+
models.OneToOneField(
21+
auto_created=True,
22+
on_delete=django.db.models.deletion.CASCADE,
23+
parent_link=True,
24+
primary_key=True,
25+
serialize=False,
26+
to="django_comments.comment",
27+
),
28+
),
29+
("file", models.FileField(upload_to="")),
30+
],
31+
options={
32+
"verbose_name": "comment",
33+
"verbose_name_plural": "comments",
34+
"ordering": ("submit_date",),
35+
"permissions": [("can_moderate", "Can moderate comments")],
36+
"abstract": False,
37+
},
38+
bases=("django_comments.comment",),
39+
),
40+
]

tests/custom_comments/migrations/__init__.py

Whitespace-only changes.

tests/custom_comments/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from django.db import models
22

3+
from django_comments.models import Comment
34

4-
class CustomComment(models.Model):
5-
pass
5+
6+
class CustomComment(Comment):
7+
file = models.FileField()

tests/runtests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import sys
99
import django
10+
from django.core.management import call_command
1011

1112
here = os.path.dirname(os.path.abspath(__file__))
1213
parent = os.path.dirname(here)
@@ -27,6 +28,7 @@
2728
"testapp",
2829
"custom_comments",
2930
],
31+
MEDIA_ROOT=os.path.join(here, 'media'),
3032
MIDDLEWARE=(
3133
'django.contrib.sessions.middleware.SessionMiddleware',
3234
'django.contrib.auth.middleware.AuthenticationMiddleware',

tests/testapp/tests/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import shutil
2+
3+
from django.conf import settings
14
from django.contrib.auth.models import User
25
from django.contrib.contenttypes.models import ContentType
36
from django.contrib.sites.models import Site
@@ -23,6 +26,10 @@ class CommentTestCase(TestCase):
2326
"""
2427
fixtures = ["comment_tests"]
2528

29+
def tearDown(self):
30+
super().tearDown()
31+
shutil.rmtree(settings.MEDIA_ROOT, ignore_errors=True)
32+
2633
def createSomeComments(self):
2734
# Two anonymous comments on two different objects
2835
c1 = Comment.objects.create(

tests/testapp/tests/test_comment_views.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from django.conf import settings
2+
23
from django.contrib.auth.models import User
4+
from django.core.files.uploadedfile import SimpleUploadedFile
5+
from django.test.utils import override_settings
36

7+
import django_comments
48
from django_comments import signals
59
from django_comments.abstracts import COMMENT_MAX_LENGTH
610
from django_comments.models import Comment
@@ -85,6 +89,20 @@ def testPostTooLongComment(self):
8589
response, "Ensure this value has at most %d characters" % COMMENT_MAX_LENGTH
8690
)
8791

92+
@override_settings(
93+
COMMENTS_APP='custom_comments',
94+
)
95+
def testPostCommentWithFile(self):
96+
a = Article.objects.get(pk=1)
97+
data = self.getValidData(a)
98+
test_file = SimpleUploadedFile("test_file.txt", b"file_content")
99+
data["file"] = test_file
100+
response = self.client.post("/post/", data)
101+
self.assertEqual(response.status_code, 302)
102+
custom_comment = django_comments.get_model().objects.first()
103+
self.assertTrue(custom_comment.file)
104+
self.assertEqual(custom_comment.file.read(), b"file_content")
105+
88106
def testCommentPreview(self):
89107
a = Article.objects.get(pk=1)
90108
data = self.getValidData(a)

0 commit comments

Comments
 (0)