Changeset View
Changeset View
Standalone View
Standalone View
blog/tests/test_blog_posts.py
| from django.contrib.auth.models import User | from django.contrib.auth.models import User | ||||
| from django.test import TestCase | from django.test import TestCase | ||||
| from django.urls import reverse | from django.urls import reverse | ||||
| from actstream.models import Action | |||||
| from blog.models import Post, Revision | from blog.models import Post, Revision | ||||
| from comments.models import Comment | |||||
| from common.tests.factories.blog import RevisionFactory, PostFactory | from common.tests.factories.blog import RevisionFactory, PostFactory | ||||
| from common.tests.factories.comments import CommentUnderPostFactory | |||||
| from common.tests.factories.films import FilmFactory | from common.tests.factories.films import FilmFactory | ||||
| from common.tests.factories.helpers import create_test_image | from common.tests.factories.helpers import create_test_image | ||||
| from common.tests.factories.users import UserFactory | |||||
| class TestPostAndRevisionCreation(TestCase): | class TestPostAndRevisionCreation(TestCase): | ||||
| @classmethod | @classmethod | ||||
| def setUpTestData(cls) -> None: | def setUpTestData(cls) -> None: | ||||
| cls.admin = User.objects.create_superuser( | cls.admin = User.objects.create_superuser( | ||||
| username='superuser', password='Blender123!', email='admin@example.com' | username='superuser', password='Blender123!', email='admin@example.com' | ||||
| ) | ) | ||||
| Show All 38 Lines | def test_updating_post_creates_new_revision(self): | ||||
| } | } | ||||
| response = self.client.post(post_change_url, change_data, follow=True) | response = self.client.post(post_change_url, change_data, follow=True) | ||||
| self.assertEqual(response.status_code, 200) | self.assertEqual(response.status_code, 200) | ||||
| self.assertEqual(Post.objects.count(), initial_post_count) | self.assertEqual(Post.objects.count(), initial_post_count) | ||||
| self.assertEqual(Revision.objects.count(), initial_revision_count + 1) | self.assertEqual(Revision.objects.count(), initial_revision_count + 1) | ||||
| latest_revision = post.revisions.latest('date_created') | latest_revision = post.revisions.latest('date_created') | ||||
| self.assertEqual(latest_revision.content, change_data['content']) | self.assertEqual(latest_revision.content, change_data['content']) | ||||
| class TestPostComments(TestCase): | |||||
| def setUp(self): | |||||
| self.user = UserFactory() | |||||
| self.other_user = UserFactory() | |||||
| self.post = RevisionFactory().post | |||||
| self.post_url = reverse('api-post-comment', kwargs={'post_pk': self.post.pk}) | |||||
| self.post_comment = CommentUnderPostFactory(comment_post__post=self.post) | |||||
| def test_reply_to_comment_creates_notifications(self): | |||||
| # No activity yet | |||||
| self.assertEqual(Action.objects.count(), 0) | |||||
| self.client.force_login(self.user) | |||||
| data = {'message': 'Comment message', 'reply_to': self.post_comment.pk} | |||||
| response = self.client.post(self.post_url, data, content_type='application/json') | |||||
| self.assertEqual(response.status_code, 200) | |||||
| self.assertEqual(Action.objects.count(), 2) | |||||
| # No notifications for the user who replied to the comment | |||||
| self.assertEqual(list(Action.objects.notifications(self.user)), [], self.user) | |||||
| # A notification for the author of the comment they replied to | |||||
| self.assertEqual( | |||||
| [str(_) for _ in Action.objects.notifications(self.post_comment.user)], | |||||
| [f'{self.user} replied to {self.post_comment} on {self.post} 0 minutes ago'], | |||||
| self.post_comment.user, | |||||
| ) | |||||
| # A notification for the author of the blog post | |||||
| comment = Comment.objects.get(pk=response.json()['id']) | |||||
| self.assertEqual( | |||||
| [str(_) for _ in Action.objects.notifications(self.post.author)], | |||||
| [f'{self.user} added {comment} on {self.post} 0 minutes ago'], | |||||
| ) | |||||
| def test_reply_to_your_own_comment_does_not_create_notification(self): | |||||
| # No activity yet | |||||
| self.assertEqual(Action.objects.count(), 0) | |||||
| self.client.force_login(self.post_comment.user) | |||||
| data = {'message': 'Comment message', 'reply_to': self.post_comment.pk} | |||||
| response = self.client.post(self.post_url, data, content_type='application/json') | |||||
| self.assertEqual(response.status_code, 200) | |||||
| self.assertEqual(Action.objects.count(), 1) | |||||
| comment = Comment.objects.get(pk=response.json()['id']) | |||||
| # No notifications for the user who replied to the comment | |||||
| self.assertEqual(list(Action.objects.notifications(self.post_comment.user)), []) | |||||
| # A notification for the author of the blog post | |||||
| self.assertEqual( | |||||
| [str(_) for _ in Action.objects.notifications(self.post.author)], | |||||
| [f'{self.post_comment.user} added {comment} on {self.post} 0 minutes ago'], | |||||
| ) | |||||
| def test_liking_post_comment_creates_notification_for_comments_author(self): | |||||
| # No activity yet | |||||
| self.assertEqual(Action.objects.count(), 0) | |||||
| self.client.force_login(self.user) | |||||
| response = self.client.post(self.post_comment.like_url, {'like': True}, content_type='application/json') | |||||
| self.assertEqual(response.status_code, 200) | |||||
| self.assertEqual(Action.objects.count(), 1) | |||||
| action = Action.objects.first() | |||||
| self.assertEqual(action.action_object, self.post_comment) | |||||
| self.assertEqual(action.actor, self.user) | |||||
| self.assertEqual(action.target, self.post) | |||||
| self.assertFalse(action.public) | |||||
| self.assertNotEqual(self.post.author, self.post_comment.user) | |||||
| # Comment's author should be notified about the like on their comment | |||||
| self.assertEqual( | |||||
| [str(_) for _ in Action.objects.notifications(self.post_comment.user)], | |||||
| [f'{self.user} liked {self.post_comment} on {self.post} 0 minutes ago'], | |||||
| ) | |||||
| # but blog post's author should not be notified | |||||
| self.assertEqual(list(Action.objects.notifications(self.post.author)), [], self.post.author) | |||||
| def test_commenting_on_post_creates_notification_for_posts_author(self): | |||||
| # No activity yet | |||||
| self.assertEqual(Action.objects.count(), 0) | |||||
| self.client.force_login(self.user) | |||||
| data = {'message': 'Comment message'} | |||||
| response = self.client.post(self.post_url, data, content_type='application/json') | |||||
| self.assertEqual(response.status_code, 200) | |||||
| self.assertEqual(Action.objects.count(), 1) | |||||
| action = Action.objects.first() | |||||
| comment = Comment.objects.get(pk=response.json()['id']) | |||||
| self.assertEqual(action.actor, self.user) | |||||
| self.assertEqual(action.target, self.post) | |||||
| self.assertEqual(action.action_object, comment) | |||||
| self.assertEqual( | |||||
| str(action), | |||||
| f'{self.user} added {comment} on {self.post} 0 minutes ago', | |||||
| str(action), | |||||
| ) | |||||
| self.assertNotEqual(self.post.author, self.post_comment.user) | |||||
| # Blog post's author should be notified about the comment on their post | |||||
| self.assertEqual(list(Action.objects.notifications(self.post.author)), [action]) | |||||
| self.assertEqual(list(Action.objects.notifications(self.post_comment.user)), []) | |||||