Changeset View
Changeset View
Standalone View
Standalone View
comments/tests/test_comments_api.py
| from unittest.mock import ANY | from unittest.mock import ANY | ||||
| import json | import json | ||||
| import logging | import logging | ||||
| from actstream.models import Action | |||||
| from django.test import TestCase | from django.test import TestCase | ||||
| from django.urls import reverse | from django.urls import reverse | ||||
| from comments.models import Comment | from comments.models import Comment | ||||
| from common.tests.factories.comments import CommentFactory | from common.tests.factories.comments import CommentFactory | ||||
| from common.tests.factories.users import UserFactory | from common.tests.factories.users import UserFactory | ||||
| ▲ Show 20 Lines • Show All 349 Lines • ▼ Show 20 Lines | def test_like_comment_increases_number_of_likes_by_one(self): | ||||
| ) | ) | ||||
| self.assertEqual(response.status_code, 200) | self.assertEqual(response.status_code, 200) | ||||
| self.assertEqual( | self.assertEqual( | ||||
| json.loads(response.content), | json.loads(response.content), | ||||
| {'like': False, 'number_of_likes': 1}, | {'like': False, 'number_of_likes': 1}, | ||||
| ) | ) | ||||
| self.assertEqual(self.comment.likes.count(), 1) | self.assertEqual(self.comment.likes.count(), 1) | ||||
| def test_like_comment_does_not_create_a_notification_for_the_same_user(self): | |||||
| # No activity yet | |||||
| self.assertEqual(Action.objects.count(), 0) | |||||
| response = self.client.post( | |||||
| self.comment.like_url, {'like': True}, content_type='application/json', | |||||
| ) | |||||
| self.assertEqual(response.status_code, 200) | |||||
| # No activity still | |||||
| self.assertEqual(Action.objects.count(), 0) | |||||
| def test_like_comment_creates_a_notification(self): | |||||
| # No activity yet | |||||
| self.assertEqual(Action.objects.count(), 0) | |||||
| # Login as a new user | |||||
| user = UserFactory() | |||||
| self.client.force_login(user) | |||||
| response = self.client.post( | |||||
| self.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() | |||||
| # Comment's author should be notified about the like | |||||
| self.assertEqual( | |||||
| [str(_) for _ in Action.objects.notifications(self.comment.user)], | |||||
| [f'{user} liked {self.comment} 0 minutes ago'], | |||||
| ) | |||||
| # TODO(anna): check notification endpoint too | |||||
| self.assertEqual(action.action_object, self.comment) | |||||
| self.assertEqual(action.actor, user) | |||||
| self.assertFalse(action.public) | |||||