-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
940bc5d
commit a1b6c87
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from django.test import TestCase, override_settings | ||
from django.urls import reverse | ||
from django.contrib.auth.models import User | ||
from django.contrib.contenttypes.models import ContentType | ||
from website.models import Post, UserProfile | ||
from comments.models import Comment | ||
|
||
@override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage') | ||
class BlogCommentTests(TestCase): | ||
def setUp(self): | ||
self.user = User.objects.create_user(username='testuser', password='12345') | ||
self.user_profile, created = UserProfile.objects.get_or_create(user=self.user) | ||
self.post = Post.objects.create( | ||
title='Test Post', | ||
content='Test Content', | ||
author=self.user | ||
) | ||
self.client.login(username='testuser', password='12345') | ||
|
||
def test_add_comment(self): | ||
url = reverse('comment_on_content', args=[self.post.pk]) | ||
data = { | ||
'content_type': 'post', | ||
'comment': 'This is a test comment.' | ||
} | ||
response = self.client.post(url, data) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertTrue(Comment.objects.filter(content_type__model='post', object_id=self.post.pk).exists()) | ||
|
||
def test_update_comment(self): | ||
comment = Comment.objects.create( | ||
content_type=ContentType.objects.get_for_model(Post), | ||
object_id=self.post.pk, | ||
author=self.user.username, | ||
author_fk=self.user_profile, | ||
text='Original comment' | ||
) | ||
url = reverse('update_content_comment', args=[self.post.pk, comment.pk]) | ||
data = { | ||
'content_type': 'post', | ||
'comment': 'Updated comment' | ||
} | ||
response = self.client.post(url, data) | ||
self.assertEqual(response.status_code, 200) | ||
comment.refresh_from_db() | ||
self.assertEqual(comment.text, 'Updated comment') | ||
|
||
def test_delete_comment(self): | ||
comment = Comment.objects.create( | ||
content_type=ContentType.objects.get_for_model(Post), | ||
object_id=self.post.pk, | ||
author=self.user.username, | ||
author_fk=self.user_profile, | ||
text='Comment to be deleted' | ||
) | ||
url = reverse('delete_content_comment') | ||
data = { | ||
'content_type': 'post', | ||
'content_pk': self.post.pk, | ||
'comment_pk': comment.pk | ||
} | ||
response = self.client.post(url, data) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertFalse(Comment.objects.filter(pk=comment.pk).exists()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from django.test import TestCase,override_settings | ||
from django.urls import reverse | ||
from django.contrib.auth.models import User | ||
from django.contrib.contenttypes.models import ContentType | ||
from website.models import Issue, UserProfile | ||
from comments.models import Comment | ||
|
||
@override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage') | ||
class IssueCommentTests(TestCase): | ||
def setUp(self): | ||
self.user = User.objects.create_user(username='testuser', password='12345') | ||
self.user_profile, created = UserProfile.objects.get_or_create(user=self.user) | ||
self.issue = Issue.objects.create( | ||
url='http://example.com', | ||
description='Test Issue', | ||
user=self.user | ||
) | ||
self.client.login(username='testuser', password='12345') | ||
|
||
def test_add_comment(self): | ||
url = reverse('comment_on_content', args=[self.issue.pk]) | ||
data = { | ||
'content_type': 'issue', | ||
'comment': 'This is a test comment.' | ||
} | ||
response = self.client.post(url, data) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertTrue(Comment.objects.filter(content_type__model='issue', object_id=self.issue.pk).exists()) | ||
|
||
def test_update_comment(self): | ||
comment = Comment.objects.create( | ||
content_type=ContentType.objects.get_for_model(Issue), | ||
object_id=self.issue.pk, | ||
author=self.user.username, | ||
author_fk=self.user_profile, | ||
text='Original comment' | ||
) | ||
url = reverse('update_content_comment', args=[self.issue.pk, comment.pk]) | ||
data = { | ||
'content_type': 'issue', | ||
'comment': 'Updated comment' | ||
} | ||
response = self.client.post(url, data) | ||
self.assertEqual(response.status_code, 200) | ||
comment.refresh_from_db() | ||
self.assertEqual(comment.text, 'Updated comment') | ||
|
||
def test_delete_comment(self): | ||
comment = Comment.objects.create( | ||
content_type=ContentType.objects.get_for_model(Issue), | ||
object_id=self.issue.pk, | ||
author=self.user.username, | ||
author_fk=self.user_profile, | ||
text='Comment to be deleted' | ||
) | ||
url = reverse('delete_content_comment') | ||
data = { | ||
'content_type': 'issue', | ||
'content_pk': self.issue.pk, | ||
'comment_pk': comment.pk | ||
} | ||
response = self.client.post(url, data) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertFalse(Comment.objects.filter(pk=comment.pk).exists()) |