Skip to content

Commit

Permalink
Merge 85e3015 into d007f82
Browse files Browse the repository at this point in the history
  • Loading branch information
salma-nyagaka authored Dec 13, 2018
2 parents d007f82 + 85e3015 commit 4e55c3c
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 134 deletions.
5 changes: 4 additions & 1 deletion authors/apps/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class Article(models.Model):
# auto_now is updated with change
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

# add like and dislike field in the articles
like = models.ManyToManyField(User, blank=True, related_name='like')
dislike = models.ManyToManyField(User, blank=True, related_name='dislike')

def __str__(self):
"""
Return the article title.
Expand Down
19 changes: 19 additions & 0 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ class ArticleAuthorSerializer(serializers.ModelSerializer):
description = serializers.CharField()
body = serializers.CharField()
author = UserSerializer(read_only = True)

def likes(self, instance):
"""method to return a user who has liked an article"""
request = self.context.get('request')
liked = False
if request is not None and request.user.is_authenticated:
user_id = request.user.id
liked = instance.likes.all().filter(id=user_id).count() == 1
return {'likes': instance.likes.count(), 'User': liked}

def dislikes(self, instance):
"""method to return a user who has disliked an article"""
request = self.context.get('request')
disliked = False
if request is not None and request.user.is_authenticated:
user_id = request.user.id
disliked = instance.dislikes.all().filter(id=user_id).count() == 1
return {'dislikes': instance.dislikes.count(), 'User': disliked}

class Meta:
model = Article
fields = '__all__'
Expand Down
32 changes: 29 additions & 3 deletions authors/apps/articles/tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ def setUp(self):
""" Basic configurations for the tests. """

self.test_client = APIClient()
# urls
#urls
self.register_url = reverse("authentication:user-signup")
self.login_url = reverse("authentication:user-login")
self.articles_url = reverse("articles:articles")

# self.article_url = reverse("articles:get_article", kwargs={slug:'slug'})
self.comments_url = ''#reverse("articles:comments")
self.comment_url = ''#reverse("articles:comment")
self.likearticle_url = reverse("articles:like", kwargs={'slug':"salma123445"})
self.dislikearticle_url = reverse("articles:dislike", kwargs={'slug':"salma123445"})

self.register_data = {
"user":{
"username": "JohnDoe",
Expand Down Expand Up @@ -185,8 +190,29 @@ def create_article(self):
return article_url, saved_article, token

def create_article_user2(self):
"""Method to create articles for user 2"""
self.test_client.post(self.register_url ,self.register_data2, format='json')
login = self.test_client.post(self.login_url ,self.login_data2, format='json')
token = 'Token ' + login.data['token']
return token

def like_article(self):
self.user_signup()
token = 'Token ' + self.user_login()
saved_article = self.test_client.post(self.articles_url,
self.article_data, format='json',
HTTP_AUTHORIZATION=token)
slug = saved_article.data['slug']
like_url = reverse("articles:like", kwargs={'slug': slug})

return like_url

def dislike_article(self):
self.user_signup()
token = 'Token ' + self.user_login()
saved_article = self.test_client.post(self.articles_url,
self.article_data, format='json',
HTTP_AUTHORIZATION=token)
slug = saved_article.data['slug']
dislike_url = reverse("articles:dislike", kwargs={'slug': slug})

return dislike_url
147 changes: 147 additions & 0 deletions authors/apps/articles/tests/test_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import unittest
from rest_framework import status
from .base_test import BaseTestCase
import unittest


@unittest.skip("Skip this class")
@unittest.skip("Not implemented")
class TestComments(BaseTestCase):
""" Class for testing comments. """

# test post comment
def test_comment_creation(self):
""" Test comment posting. """
self.user_signup()
self.user_login()
self.post_article()
response = self.post_comment()
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_comment_creation_with_invalid_data(self):
""" Test creating a comment using invalid data. """
self.user_signup()
self.user_login()
self.post_article()
response = self.test_client.post(self.comment_url,
self.invalid_comment_data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_commenting_on_non_existing_article(self):
""" Test commenting on a missing article. """
self.user_signup()
self.user_login()
response = self.test_client.post(self.comment_url,
self.invalid_comment_data, format='json')
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

def test_commenting_by_a_non_user(self):
""" Test a non-user cannot comment. """
response = self.test_client.post(self.comment_url,
self.invalid_comment_data, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

# test getting comment
def test_getting_a_comment(self):
""" Test getting a single comment successfully. """
self.user_signup()
self.user_login()
self.post_article()
response = self.post_comment()
response2 = self.test_client.get(self.comment_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_getting_a_non_existing_comment(self):
""" Test getting a missing comment. """
self.user_signup()
self.user_login()
self.post_article()
response = self.test_client.get(self.comment_url)
self.assertEqual(response.status_code, status.HTTP_400_NOT_FOUND)

def test_getting_comment_from_a_missing_article(self):
""" Test getting comment from a non-existent article. """
self.user_signup()
self.user_login()
response2 = self.test_client.get(self.comment_url)
self.assertEqual(response2.status_code, status.HTTP_400_BAD_REQUEST)

def test_getting_all_comments(self):
""" Test getting all comments to an article. """
self.user_signup()
self.user_login()
self.post_article()
response = self.post_comment()
response2 = self.test_client.get(self.comments_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_getting_all_comments_from_a_missing_article(self):
""" Test getting all comments from a non-existent article. """
self.user_signup()
self.user_login()
response2 = self.test_client.get(self.comments_url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

# test updating comment
def test_updating_a_comment(self):
""" Test editing an existing comment. """
self.user_signup()
self.user_login()
self.post_article()
response = self.post_comment()
response2 = self.test_client.put(self.comment_url,
self.new_comment_data, format='json')
self.assertEqual(response2.status_code, status.HTTP_200_OK)

def test_updating_with_invalid_data(self):
""" Test updating comment using invalid data. """
self.user_signup()
self.user_login()
self.post_article()
response = self.post_comment()
response2 = self.test_client.put(self.comment_url,
self.invalid_comment_data, format='json')
self.assertEqual(response2.status_code, status.HTTP_400_BAD_REQUEST)

def test_updating_missing_comment(self):
""" Test updating a non-existent comment. """
self.user_signup()
self.user_login()
self.post_article()
response = self.test_client.put(self.comment_url,
self.new_comment_data, format='json')
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

def test_non_logged_in_user_cannot_update(self):
""" Test a user has to login before updating. """
self.user_signup()
self.post_article()
response = self.test_client.put(self.comment_url,
self.new_comment_data, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

# test deleting comment
def test_deleting_an_existing_comment(self):
""" Method for testing deleting an existing comment. """
self.user_signup()
self.user_login()
self.post_article()
response = self.post_comment()
response2 = self.test_client.delete(self.comment_url)
self.assertEqual(response2.status_code, status.HTTP_200_OK)

def test_deleting_a_non_existing_comment(self):
""" Method for testing deleting an existing comment. """
self.user_signup()
self.user_login()
self.post_article()
response = self.test_client.delete(self.comment_url)
self.assertEqual(response.status_code, status.HTTP_404_OK)

def test_non_logged_in_user_deletting_comment(self):
""" Test a user has to login before deleting. """
self.user_signup()
self.post_article()
response = self.post_comment()
response2 = self.test_client.delete(self.comment_url)
self.assertEqual(response2.status_code, status.HTTP_403_FORBIDDEN)
38 changes: 0 additions & 38 deletions authors/apps/articles/tests/test_delete.py

This file was deleted.

28 changes: 0 additions & 28 deletions authors/apps/articles/tests/test_get.py

This file was deleted.

59 changes: 59 additions & 0 deletions authors/apps/articles/tests/test_like_dislike.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from rest_framework import status
from .base_test import BaseTestCase
from django.urls import reverse


class TestArticles(BaseTestCase):
"class to test liking and disliking of articles"

def test_like(self):
"""test for liking an article"""
self.user_signup()
token = self.user_login()
response = self.client.put(self.like_article(), format='json', HTTP_AUTHORIZATION='Token ' + token)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, {'article': 'You have liked this article'})

def test_dislike(self):
"""test for liking an article"""
self.user_signup()
token = self.user_login()
response = self.client.put(self.dislike_article(), format='json', HTTP_AUTHORIZATION='Token ' + token)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, {'article': "You have disliked this article"})

def test_like_dislike(self):
"""test for liking then disliking an article"""
self.user_signup()
token = self.user_login()
response = self.client.put(self.like_article(), format='json', HTTP_AUTHORIZATION='Token ' + token)
self.assertEqual(response.data, {'article': 'You have liked this article'})
response = self.client.put(self.dislike_article(), format='json', HTTP_AUTHORIZATION='Token ' + token)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, {'article': "You have disliked this article"})

def test_dislike_like(self):
"""test for liking then disliking an article"""
self.user_signup()
token = self.user_login()
response = self.client.put(self.dislike_article(), format='json', HTTP_AUTHORIZATION='Token ' + token)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, {'article': "You have disliked this article"})
response = self.client.put(self.like_article(), format='json', HTTP_AUTHORIZATION='Token ' + token)
self.assertEqual(response.data, {'article': 'You have liked this article'})

def test_like_nonexisting_article(self):
"""test for liking an article"""
self.user_signup()
token = self.user_login()
response = self.client.put(self.likearticle_url, format='json', HTTP_AUTHORIZATION='Token ' + token)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(response.data, {'Error': "Article does not exist"})

def test_dislike_nonexisting_article(self):
"""test for liking an article"""
self.user_signup()
token = self.user_login()
response = self.client.put(self.dislikearticle_url, format='json', HTTP_AUTHORIZATION='Token ' + token)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(response.data, {'Error': "Article does not exist"})
24 changes: 0 additions & 24 deletions authors/apps/articles/tests/test_post.py

This file was deleted.

Loading

0 comments on commit 4e55c3c

Please sign in to comment.