-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(like and dislike a comment): implement feature to allow likin…
…g and disliking articles - add feature to allow liking article comments - add feature to allow disliking article comments. [Delivers #161966629]
- Loading branch information
vincent Onyango
committed
Dec 19, 2018
1 parent
67e5f0a
commit 15e220a
Showing
8 changed files
with
206 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
from rest_framework.exceptions import NotFound | ||
|
||
class ArticleNotFound(NotFound): | ||
default_detail = 'Article with this slug not found' | ||
default_detail = 'Article with this slug not found' | ||
|
||
class CommentNotFound(NotFound): | ||
""" | ||
comment id not found exception | ||
""" | ||
|
||
default_detail = 'Comment ID not found.' |
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
25 changes: 25 additions & 0 deletions
25
authors/apps/articles/migrations/0004_auto_20181219_0847.py
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,25 @@ | ||
# Generated by Django 2.1 on 2018-12-19 08:47 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('articles', '0003_auto_20181218_2105'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='comment', | ||
name='dislike', | ||
field=models.ManyToManyField(blank=True, related_name='dislikecomment', to=settings.AUTH_USER_MODEL), | ||
), | ||
migrations.AddField( | ||
model_name='comment', | ||
name='like', | ||
field=models.ManyToManyField(blank=True, related_name='likecomment', to=settings.AUTH_USER_MODEL), | ||
), | ||
] |
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
57 changes: 57 additions & 0 deletions
57
authors/apps/articles/tests/test_liking_and_disliking_comments.py
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,57 @@ | ||
from rest_framework import status | ||
from .base_test import BaseTestCase | ||
from django.urls import reverse | ||
|
||
|
||
class TestLikingComments(BaseTestCase): | ||
"class to test liking and disliking of articles" | ||
|
||
def test_like_comment(self): | ||
"""test for liking a comment""" | ||
comment_id, token = self.get_comment_id() | ||
response = self.client.put(self.like_comment_url(comment_id), format='json', HTTP_AUTHORIZATION=token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data, {'message': 'You have liked this comment'}) | ||
|
||
def test_dislike_comment(self): | ||
"""test for liking a comment""" | ||
comment_id, token = self.get_comment_id() | ||
response = self.client.put(self.dislike_comment_url(comment_id) , format='json', HTTP_AUTHORIZATION=token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data, {'message': "You have disliked this comment"}) | ||
|
||
def test_like_dislike_comment(self): | ||
"""test for liking then disliking a comment""" | ||
comment_id, token = self.get_comment_id() | ||
response = self.client.put(self.like_comment_url(comment_id), format='json', HTTP_AUTHORIZATION=token) | ||
self.assertEqual(response.data, {'message': 'You have liked this comment'}) | ||
response = self.client.put(self.dislike_comment_url(comment_id), format='json', HTTP_AUTHORIZATION=token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data, {'message': "You have disliked this comment"}) | ||
|
||
def test_dislike_like_comment(self): | ||
"""test for liking then unliking a comment""" | ||
comment_id, token = self.get_comment_id() | ||
response = self.client.put(self.dislike_comment_url(comment_id), format='json', HTTP_AUTHORIZATION=token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data, {'message': "You have disliked this comment"}) | ||
response = self.client.put(self.like_comment_url(comment_id), format='json', HTTP_AUTHORIZATION=token) | ||
self.assertEqual(response.data, {'message': 'You have unliked this comment'}) | ||
|
||
def test_like_nonexisting_comment(self): | ||
"""test for liking an comment""" | ||
self.user_signup() | ||
token = self.user_login() | ||
non_existing_id = 5 # not existing comment ID | ||
response = self.client.put(self.like_comment_url(non_existing_id), format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
self.assertEqual(response.data, {'detail': "Comment ID not found."}) | ||
|
||
def test_dislike_nonexisting_comment(self): | ||
"""test for liking an comment""" | ||
self.user_signup() | ||
token = self.user_login() | ||
non_existing_id = 5 # not existing comment ID | ||
response = self.client.put(self.like_comment_url(non_existing_id), format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
self.assertEqual(response.data, {'detail': "Comment ID not found."}) |
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