-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Like): user should like a comment
- Ensure that authenticated users can like a comment on an article - Ensure that a user can view all likes on a comment - Ensure that a user can unlike a comment on an article [Starts #164069241]
- Loading branch information
wasibani roy
committed
Mar 21, 2019
1 parent
3882480
commit 3fd1448
Showing
11 changed files
with
263 additions
and
20 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
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
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
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
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
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,51 @@ | ||
""" | ||
Test comments app | ||
""" | ||
from django.urls import reverse | ||
from rest_framework import status | ||
|
||
from ..tests.test_data import (post_article, comment, | ||
update_comment | ||
) | ||
|
||
from authors.apps.comments.tests.base import BaseTestCase | ||
from ..models import Comment, CommentLike | ||
from authors.apps.authentication.models import User | ||
from authors.apps.profiles.models import Profile | ||
from authors.apps.articles.models import Article | ||
|
||
class TestComment(BaseTestCase): | ||
""" | ||
test class to contain functions to handle test for the commenting on an article | ||
""" | ||
def setUp(self): | ||
super().setUp() | ||
self.verified_user = User.objects.create_user( | ||
username='testuser1', | ||
email='testemail1@test.com', | ||
password='testpassworD12') | ||
self.profile=Profile.objects.get(user=self.verified_user) | ||
self.created_article=Article.objects.create( | ||
body=' hello world',description='a description', | ||
title='a title',author=self.profile | ||
) | ||
self.testcomment=Comment.objects.create(body='a test comment body', | ||
commented_by=self.profile, | ||
article=self.created_article) | ||
|
||
|
||
def test_comment_str_return(self): | ||
""" | ||
Test create comment | ||
""" | ||
self.comment = Comment.objects.create( | ||
body='A comment body', commented_by=self.profile, article=self.created_article) | ||
self.assertEqual(self.comment.__str__(), 'A comment body') | ||
|
||
def test_commentlike_str_return(self): | ||
""" | ||
Test retriving a single comment | ||
""" | ||
self.commentLike = CommentLike.objects.create( | ||
liked_by=self.profile, comment=self.testcomment, like_status=True) | ||
self.assertEqual('testuser1', self.commentLike.__str__()) |
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
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,105 @@ | ||
""" | ||
Test Liking a comment | ||
""" | ||
from django.urls import reverse | ||
from rest_framework import status | ||
|
||
from ..tests.test_data import (post_article, comment, | ||
update_comment | ||
) | ||
|
||
from authors.apps.comments.tests.base import BaseTestCase | ||
|
||
|
||
class TestComment(BaseTestCase): | ||
""" | ||
test class to contain functions to handle test for the commenting on an article | ||
""" | ||
|
||
def test_like_a_comment(self): | ||
""" | ||
Test liking a comment | ||
""" | ||
self.user_access() | ||
self.posting_article(post_article) | ||
slug = self.article_slug() | ||
url = reverse("comments:post_comment", kwargs={'slug': slug}) | ||
res = self.client.post(url, data=comment, format="json") | ||
data = res.data | ||
comment_id = data["comment"]["id"] | ||
fetch_url = reverse("comments:comment-like", kwargs={"slug":slug, "pk":comment_id}) | ||
response = self.client.put(fetch_url) | ||
data = response.data | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertIn("comment liked successfully", data["message"]) | ||
|
||
def test_like_a_comment_twice(self): | ||
""" | ||
Test liking a comment twice | ||
""" | ||
self.user_access() | ||
self.posting_article(post_article) | ||
slug = self.article_slug() | ||
url = reverse("comments:post_comment", kwargs={'slug': slug}) | ||
res = self.client.post(url, data=comment, format="json") | ||
data = res.data | ||
comment_id = data["comment"]["id"] | ||
fetch_url = reverse("comments:comment-like", kwargs={"slug":slug, "pk":comment_id}) | ||
self.client.put(fetch_url) | ||
response = self.client.put(fetch_url) | ||
data = response.data | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn("you already liked this comment", data["message"]) | ||
|
||
def test_getting_likes_on_a_comment(self): | ||
""" | ||
Test getting likes on a comment | ||
""" | ||
self.user_access() | ||
self.posting_article(post_article) | ||
slug = self.article_slug() | ||
url = reverse("comments:post_comment", kwargs={'slug': slug}) | ||
res = self.client.post(url, data=comment, format="json") | ||
data = res.data | ||
comment_id = data["comment"]["id"] | ||
fetch_url = reverse("comments:comment-like", kwargs={"slug":slug, "pk":comment_id}) | ||
self.client.put(fetch_url) | ||
response = self.client.get(fetch_url) | ||
data = response.data | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertIn("1", str(data["likesCount"])) | ||
|
||
def test_unlike_comment(self): | ||
""" | ||
tests to unlike a comment | ||
""" | ||
self.user_access() | ||
self.posting_article(post_article) | ||
slug = self.article_slug() | ||
url = reverse("comments:post_comment", kwargs={'slug': slug}) | ||
res = self.client.post(url, data=comment, format="json") | ||
data = res.data | ||
comment_id = data["comment"]["id"] | ||
fetch_url = reverse("comments:comment-like", kwargs={"slug":slug, "pk":comment_id}) | ||
self.client.put(fetch_url) | ||
response = self.client.delete(fetch_url) | ||
data = response.data | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertIn("unliked comment successfully", data["message"]) | ||
|
||
def test_unlike_comment_not_liked(self): | ||
""" | ||
Test unlike a comment you had not liked | ||
""" | ||
self.user_access() | ||
self.posting_article(post_article) | ||
slug = self.article_slug() | ||
url = reverse("comments:post_comment", kwargs={'slug': slug}) | ||
res = self.client.post(url, data=comment, format="json") | ||
data = res.data | ||
comment_id = data["comment"]["id"] | ||
fetch_url = reverse("comments:comment-like", kwargs={"slug":slug, "pk":comment_id}) | ||
response = self.client.delete(fetch_url) | ||
data = response.data | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn("you have not yet liked this comment", data["message"]) |
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
Oops, something went wrong.