Skip to content

Commit

Permalink
feat(Like): user should like a comment
Browse files Browse the repository at this point in the history
- 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
Show file tree
Hide file tree
Showing 13 changed files with 454 additions and 84 deletions.
10 changes: 10 additions & 0 deletions authors/apps/comments/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by Django 2.1.5 on 2019-03-20 08:46


from django.db import migrations, models
import django.db.models.deletion

Expand All @@ -25,4 +26,13 @@ class Migration(migrations.Migration):
('commented_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='profiles.Profile')),
],
),
migrations.CreateModel(
name='CommentLike',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('like_status', models.BooleanField()),
('comment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='comments.Comment')),
('liked_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='profiles.Profile')),
],
),
]
8 changes: 8 additions & 0 deletions authors/apps/comments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ class Comment(models.Model):

def __str__(self):
return str(self.body)

class CommentLike(models.Model):
comment=models.ForeignKey(Comment, on_delete=models.CASCADE)
like_status=models.BooleanField()
liked_by=models.ForeignKey(Profile, on_delete=models.CASCADE)

def __str__(self):
return str(self.liked_by)
22 changes: 22 additions & 0 deletions authors/apps/comments/renderers.py
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})
11 changes: 9 additions & 2 deletions authors/apps/comments/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework import serializers
from .models import Comment
from .models import Comment, CommentLike
from ..profiles.serializers import ProfileSerializer


Expand All @@ -15,7 +15,7 @@ class Meta:
"body",
"author",
"article",
"id"
"id",
)
read_only_fields = (
"article",
Expand All @@ -24,3 +24,10 @@ class Meta:
"updated_at",
"id"
)

class CommentLikeSerializer(serializers.ModelSerializer):
liked_by = ProfileSerializer(read_only=True)
class Meta:
model = CommentLike
fields = ('liked_by',)
read_only_fields = ('like_status', 'liked_by')
1 change: 1 addition & 0 deletions authors/apps/comments/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
post_article, new_user2,
user_login2
)
from authors.apps.authentication.models import User
from authors.apps.articles.models import Article


Expand Down
9 changes: 4 additions & 5 deletions authors/apps/comments/tests/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_create_a_comment(self):
response = self.client.post(url, data=comment, format="json")
data = response.data
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertIn(comment["comment"]["body"], data["comment"]["body"])
self.assertIn(comment["body"], data["comment"]["body"])

def test_get_single_comment(self):
"""
Expand Down Expand Up @@ -61,7 +61,7 @@ def test_update_a_comment(self):
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["comment"]["body"], data["body"])
self.assertIn(update_comment["body"], data["body"])

def test_update_a_comment_same_data(self):
"""
Expand Down Expand Up @@ -102,8 +102,7 @@ def test_update_a_comment_not_yours(self):

def test_get_all_comments(self):
"""
test to get all comments
test to get all comments
"""
self.user_access()
self.posting_article(post_article)
Expand All @@ -113,7 +112,7 @@ def test_get_all_comments(self):
response = self.client.get(url)
data = response.data
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn(comment["comment"]["body"], data["comments"][0]["body"])
self.assertIn(comment["body"], data["comments"][0]["body"])

def test_delete_comment(self):
"""
Expand Down
156 changes: 156 additions & 0 deletions authors/apps/comments/tests/test_comment_model.py
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"])
7 changes: 2 additions & 5 deletions authors/apps/comments/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@
}

comment = {
"comment": {
"body": "This is a comment",
}
"body": "This is a comment",

}

update_comment = {
"comment": {
"body": "this is a test"
}
}

user_login = {
Expand Down
Loading

0 comments on commit a0708a1

Please sign in to comment.