From a1b6c870eed4a3f5eb6f4de013252d81b51d59bd Mon Sep 17 00:00:00 2001 From: Krrish Sehgal <133865424+krrish-sehgal@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:20:49 +0530 Subject: [PATCH] tests added --- website/test_blog.py | 64 ++++++++++++++++++++++++++++++++++++++++++ website/test_issues.py | 64 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 website/test_blog.py create mode 100644 website/test_issues.py diff --git a/website/test_blog.py b/website/test_blog.py new file mode 100644 index 000000000..58b723dbb --- /dev/null +++ b/website/test_blog.py @@ -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()) diff --git a/website/test_issues.py b/website/test_issues.py new file mode 100644 index 000000000..22a867dd9 --- /dev/null +++ b/website/test_issues.py @@ -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())