-
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
a2c1de7
commit a0708a1
Showing
13 changed files
with
454 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import json | ||
from rest_framework.renderers import JSONRenderer | ||
from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList | ||
|
||
|
||
class CommentJSONRenderer(JSONRenderer): | ||
charset = 'utf-8' | ||
|
||
def render(self, data, media_type=None, renderer_context=None): | ||
# If the view throws an error (such as the user can't be authenticated | ||
# or something similar), `data` will contain an `errors` key. We want | ||
# the default JSONRenderer to handle rendering errors, so we need to | ||
# check for this case. | ||
|
||
if type(data) != ReturnList: # try returning dict | ||
errors = data.get('errors', None) | ||
if errors is not None: | ||
# As mentioned about, we will let the default JSONRenderer handle | ||
# rendering errors. | ||
return super(CommentJSONRenderer, self).render(data) | ||
# Finally, we can render our data under the "comment" namespace. | ||
return json.dumps({'comment': data}) |
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,156 @@ | ||
""" | ||
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__()) | ||
|
||
# def test_update_a_comment(self): | ||
# """ | ||
# Test updating 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:single_comment", kwargs={"slug":slug, "pk":comment_id}) | ||
# response = self.client.put(fetch_url, data=update_comment, format="json") | ||
# data = response.data | ||
# self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
# self.assertIn(update_comment["body"], data["body"]) | ||
|
||
# def test_update_a_comment_same_data(self): | ||
# """ | ||
# Test update comment with the same data | ||
# """ | ||
# 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:single_comment", kwargs={"slug":slug, "pk":comment_id}) | ||
# response = self.client.put(fetch_url, data=comment, format="json") | ||
# data = response.data | ||
# self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
# self.assertIn("Please ensure the comment body has changed", | ||
# data["message"]) | ||
|
||
# def test_update_a_comment_not_yours(self): | ||
# """ | ||
# Test update a comment you did not create | ||
# """ | ||
# 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:single_comment", kwargs={"slug":slug, "pk":comment_id}) | ||
# self.user_access2() | ||
# response = self.client.put(fetch_url, data=update_comment, format="json") | ||
# data = response.data | ||
# self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
# self.assertIn("You can only edit a comment you created", | ||
# data["message"]) | ||
|
||
# def test_get_all_comments(self): | ||
# """ | ||
# test to get all comments | ||
# """ | ||
# self.user_access() | ||
# self.posting_article(post_article) | ||
# slug = self.article_slug() | ||
# url = reverse("comments:post_comment", kwargs={"slug":slug}) | ||
# self.client.post(url, data=comment, format="json") | ||
# response = self.client.get(url) | ||
# data = response.data | ||
# self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
# self.assertIn(comment["body"], data["comments"][0]["body"]) | ||
|
||
# def test_delete_comment(self): | ||
# """ | ||
# tests to delete 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:single_comment", kwargs={"slug":slug, "pk":comment_id}) | ||
# response = self.client.delete(fetch_url) | ||
# data = response.data | ||
# self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
# self.assertIn("Comment successfully deleted", | ||
# data["message"]) | ||
|
||
# def test_delete_comment_not_yours(self): | ||
# """ | ||
# Test delete a comment you did not create | ||
# """ | ||
# 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:single_comment", kwargs={"slug":slug, "pk":comment_id}) | ||
# self.user_access2() | ||
# response = self.client.delete(fetch_url) | ||
# data = response.data | ||
# self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
# self.assertIn("You can only delete a comment you created", | ||
# 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.