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 3882480 commit 3fd1448
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 20 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-21 09:14


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)
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
51 changes: 51 additions & 0 deletions authors/apps/comments/tests/test_comment_model.py
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__())
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 @@ -23,15 +23,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
105 changes: 105 additions & 0 deletions authors/apps/comments/tests/test_like_comment.py
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"])
8 changes: 7 additions & 1 deletion authors/apps/comments/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.urls import path
from .views import CommentCreateListView, CommentsAPIView
from .views import (CommentCreateListView, CommentsAPIView,
CommentLikeView
)

urlpatterns = [
path(
Expand All @@ -10,4 +12,8 @@
'<slug>/comments/<int:pk>',
CommentsAPIView.as_view(),
name='single_comment'),
path(
'<slug>/comments/<int:pk>/like/',
CommentLikeView.as_view(),
name='comment-like'),
]
Loading

0 comments on commit 3fd1448

Please sign in to comment.