Skip to content

Commit

Permalink
chore(refactor-codebase): Refactor comments app
Browse files Browse the repository at this point in the history
- Refactor comments views
- Refactor likes views
- Refactor unit tests

[Starts #166027332]
  • Loading branch information
mwinel committed May 16, 2019
1 parent c9e1fba commit f8edffb
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 153 deletions.
5 changes: 2 additions & 3 deletions authors/apps/comments/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,12 @@ def setUp(self):
self.comment_text4 = {
"comment": {
"body": "This is sample text.",
"first_index": 200,
"last_index": 100,
"first_index": 20,
"last_index": 10,
"highlighted_text": "This is sample text."
}
}


self.user2 = User.objects.create_user(
username='test2', email='test2@example.com', password='12345678'
)
Expand Down
18 changes: 1 addition & 17 deletions authors/apps/comments/tests/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,42 +51,35 @@ def test_post_a_comment_article_not_found(self):

def test_get_a_comment(self):
"""Test get a single comment."""
# post an article
url = reverse('comment_list', kwargs={'article_id': 1})
self.client.post(url, self.comment,
HTTP_AUTHORIZATION=self.auth_header, format="json")
# get a single article
url = reverse('comment_detail', kwargs={'article_id': 1, 'pk': 1})
response = self.client.get(url, HTTP_AUTHORIZATION=self.auth_header)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_get_a_comment_on_article_not_found(self):
"""Test get a single comment on a non existinng article."""
# post an article
url = reverse('comment_list', kwargs={'article_id': 1})
self.client.post(url, self.comment,
HTTP_AUTHORIZATION=self.auth_header, format="json")
# get a single article
url = reverse('comment_detail', kwargs={'article_id': 1000, 'pk': 1})
response = self.client.get(url, HTTP_AUTHORIZATION=self.auth_header)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(response.json()['error'], 'Article not found.')

def test_get_a_comment_not_found(self):
"""Test get a single comment on a non existinng article."""
# post an article
post_url = reverse('comment_list', kwargs={'article_id': 1})
self.client.post(post_url, self.comment,
HTTP_AUTHORIZATION=self.auth_header, format="json")
# get a single comment
url = reverse('comment_detail', kwargs={'article_id': 1, 'pk': 1000})
response = self.client.get(url, HTTP_AUTHORIZATION=self.auth_header)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(response.json()['error'], 'Comment not found.')

def test_update_a_comment(self):
"""Test update a single comment."""
# post an article
post_url = reverse('comment_list', kwargs={'article_id': 1})
response = self.client.post(post_url, self.comment,
HTTP_AUTHORIZATION=self.auth_header,
Expand Down Expand Up @@ -246,7 +239,7 @@ def test_if_last_or_first_index_is_greater_than_the_body(self):
response = self.client.post(url, self.comment_text3,
HTTP_AUTHORIZATION=self.auth_header,
format="json")
self.assertIn('You should only highlight within the article body.',
self.assertIn('You should only highlight within the article body.',
str(response.data))

def test_if_first_index_is_greater_last_index(self):
Expand All @@ -256,12 +249,3 @@ def test_if_first_index_is_greater_last_index(self):
HTTP_AUTHORIZATION=self.auth_header,
format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_iff_first_index_is_greater_last_index(self):
"""Test whether first index is greater than last index."""
url = reverse('comment_list', kwargs={'article_id': 1})
response = self.client.post(url, self.comment_text4,
HTTP_AUTHORIZATION=self.auth_header,
format="json")
self.assertIn('You should only highlight within the article body.',
str(response.data))
111 changes: 45 additions & 66 deletions authors/apps/comments/tests/test_likes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import json
from rest_framework.test import APIClient, APITestCase
from authors.apps.authentication.models import User
from authors.apps.comments.tests.base import BaseTestCase
from django.urls import reverse
from rest_framework import status


class TestLikeCommment(BaseTestCase):
"""Class to test like and unlike a comment."""
Expand All @@ -15,10 +12,10 @@ def test_user_to_like_own_comment(self):
HTTP_AUTHORIZATION=self.auth_header,
format="json")
comment_id = response.data['comment']['id']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
url1 = reverse('like_comment', kwargs={'article_id':1, 'comment_id':comment_id})
response1 = self.client.post(url1,
HTTP_AUTHORIZATION=self.auth_header, format="json")
url1 = reverse('like_comment',
kwargs={'article_id': 1, 'pk': comment_id})
response1 = self.client.post(url1, HTTP_AUTHORIZATION=self.auth_header,
format="json")
self.assertEqual(response1.status_code, 403)

def test_user_liking_your_own_comment(self):
Expand All @@ -28,11 +25,12 @@ def test_user_liking_your_own_comment(self):
HTTP_AUTHORIZATION=self.auth_header,
format="json")
comment_id = response.data['comment']['id']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
url1 = reverse('like_comment', kwargs={'article_id':1, 'comment_id':comment_id})
response1 = self.client.post(url1,
HTTP_AUTHORIZATION=self.auth_header, format="json")
self.assertEqual(response1.data['message'], "You can not like your own comment.")
url1 = reverse('like_comment',
kwargs={'article_id': 1, 'pk': comment_id})
response1 = self.client.post(url1, HTTP_AUTHORIZATION=self.auth_header,
format="json")
self.assertEqual(response1.data['message'],
"You can not like your own comment.")

def test_user_to_liking_another_comment(self):
"""Test liking your own comment."""
Expand All @@ -41,10 +39,11 @@ def test_user_to_liking_another_comment(self):
HTTP_AUTHORIZATION=self.auth_header,
format="json")
comment_id = response.data['comment']['id']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
url1 = reverse('like_comment', kwargs={'article_id':1, 'comment_id':comment_id})
url1 = reverse('like_comment',
kwargs={'article_id': 1, 'pk': comment_id})
response1 = self.client.post(url1,
HTTP_AUTHORIZATION=self.auth_header2, format="json")
HTTP_AUTHORIZATION=self.auth_header2,
format="json")
self.assertEqual(response1.status_code, 200)

def test_user_to_like_another_comment(self):
Expand All @@ -54,11 +53,13 @@ def test_user_to_like_another_comment(self):
HTTP_AUTHORIZATION=self.auth_header,
format="json")
comment_id = response.data['comment']['id']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
url1 = reverse('like_comment', kwargs={'article_id':1, 'comment_id':comment_id})
url1 = reverse('like_comment',
kwargs={'article_id': 1, 'pk': comment_id})
response1 = self.client.post(url1,
HTTP_AUTHORIZATION=self.auth_header2, format="json")
self.assertEqual(response1.data['success'], "You have successfully liked this comment.")
HTTP_AUTHORIZATION=self.auth_header2,
format="json")
self.assertEqual(response1.data['success'],
"You have successfully liked this comment.")
self.assertEqual(response1.status_code, 200)

def test_user_to_like_twice_a_comment(self):
Expand All @@ -68,70 +69,48 @@ def test_user_to_like_twice_a_comment(self):
HTTP_AUTHORIZATION=self.auth_header,
format="json")
comment_id = response.data['comment']['id']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
url1 = reverse('like_comment', kwargs={'article_id':1, 'comment_id':comment_id})
url1 = reverse('like_comment',
kwargs={'article_id': 1, 'pk': comment_id})
response1 = self.client.post(url1,
HTTP_AUTHORIZATION=self.auth_header2, format="json")
HTTP_AUTHORIZATION=self.auth_header2,
format="json")
response1 = self.client.post(url1,
HTTP_AUTHORIZATION=self.auth_header2, format="json")
self.assertEqual(response1.data['message'], "Your like has been cancelled")
HTTP_AUTHORIZATION=self.auth_header2,
format="json")
self.assertEqual(response1.data['message'],
"Your like has been cancelled")
self.assertEqual(response1.status_code, 200)

def test_user_has_not_liked_a_comment(self):
def test_user_has_liked_a_comment(self):
"""Test a user has ever liked a comment."""
url = reverse('comment_list', kwargs={'article_id': 1})
response = self.client.post(url, self.comment,
HTTP_AUTHORIZATION=self.auth_header,
format="json")
comment_id = response.data['comment']['id']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
url1 = reverse('get_likes', kwargs={'article_id':1, 'comment_id':comment_id})
response1 = self.client.get(url1,
HTTP_AUTHORIZATION=self.auth_header2, format="json")
self.assertIn('False', str(response1.data))

def test_user_has_not_liked_comment(self):
"""Test a user has never liked a comment using status code."""
url = reverse('comment_list', kwargs={'article_id': 1})
response = self.client.post(url, self.comment,
HTTP_AUTHORIZATION=self.auth_header,
format="json")
comment_id = response.data['comment']['id']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
url1 = reverse('get_likes', kwargs={'article_id':1, 'comment_id':comment_id})
url1 = reverse('like_comment',
kwargs={'article_id': 1, 'pk': comment_id})
self.client.post(url1, HTTP_AUTHORIZATION=self.auth_header2,
format="json")
url1 = reverse('get_likes',
kwargs={'article_id': 1, 'pk': comment_id})
response1 = self.client.get(url1,
HTTP_AUTHORIZATION=self.auth_header2, format="json")
self.assertEqual(response1.status_code, 200)

def test_user_has_liked_comment(self):
"""Test a user has ever liked a comment using status code."""
url = reverse('comment_list', kwargs={'article_id': 1})
response = self.client.post(url, self.comment,
HTTP_AUTHORIZATION=self.auth_header,
HTTP_AUTHORIZATION=self.auth_header2,
format="json")
comment_id = response.data['comment']['id']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
url0 = reverse('like_comment', kwargs={'article_id':1, 'comment_id':comment_id})
url1 = reverse('get_likes', kwargs={'article_id':1, 'comment_id':comment_id})
response2 = response.client.post(url0,
HTTP_AUTHORIZATION=self.auth_header2, format="json")
response1 = self.client.get(url1,
HTTP_AUTHORIZATION=self.auth_header2, format="json")
self.assertEqual(response1.status_code, 200)
self.assertIn('True', str(response1.data))

def test_user_has_likes_comment(self):
"""Test a user has ever liked a comment using message."""
def test_user_has_not_liked_a_comment(self):
"""Test a user has never liked a comment."""
url = reverse('comment_list', kwargs={'article_id': 1})
response = self.client.post(url, self.comment,
HTTP_AUTHORIZATION=self.auth_header,
format="json")
comment_id = response.data['comment']['id']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
url0 = reverse('like_comment', kwargs={'article_id':1, 'comment_id':comment_id})
url1 = reverse('get_likes', kwargs={'article_id':1, 'comment_id':comment_id})
response2 = response.client.post(url0,
HTTP_AUTHORIZATION=self.auth_header2, format="json")
url1 = reverse('get_likes',
kwargs={'article_id': 1, 'pk': comment_id})
response1 = self.client.get(url1,
HTTP_AUTHORIZATION=self.auth_header2, format="json")
self.assertIn('True', str(response1.data))
HTTP_AUTHORIZATION=self.auth_header2,
format="json")
self.assertEqual(response1.status_code, 200)
self.assertIn('False', str(response1.data))
4 changes: 2 additions & 2 deletions authors/apps/comments/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
name="comment_list"),
path('articles/<article_id>/comments/<int:pk>/',
RetrieveUpdateDeleteComment.as_view(), name="comment_detail"),
path('articles/<article_id>/comments/<comment_id>/like/',
path('articles/<article_id>/comments/<int:pk>/like/',
Like.as_view(), name="like_comment"),
path('articles/<article_id>/comment/<comment_id>/',
path('articles/<article_id>/comments/<int:pk>/likes/',
Like.as_view(), name="get_likes"),
path('articles/<article_id>/comments/<int:pk>/update_history/',
CommentEditHistoryAPIView.as_view(), name="update_history")
Expand Down
Loading

0 comments on commit f8edffb

Please sign in to comment.