Skip to content

Commit

Permalink
Merge 075a4e9 into a6cfa89
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMonstro committed Mar 15, 2019
2 parents a6cfa89 + 075a4e9 commit f2337b6
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 7 deletions.
8 changes: 7 additions & 1 deletion authors/apps/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ class Meta:
ordering = ["-created_at", "-updated_at"]



class Like(models.Model):
user_id = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
related_name='likes')
article_id = models.ForeignKey(
Article, on_delete=models.CASCADE, related_name='likes')
is_like = models.BooleanField()
11 changes: 10 additions & 1 deletion authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework import serializers
from .models import Article
from .models import Article, Like


class TheArticleSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -41,3 +41,12 @@ def update(self, instance, validated_data):
instance.save()
return instance


class LikesSerializer(serializers.ModelSerializer):
"""
Serializers for likes
"""
class Meta():
model = Like
fields = ('id', 'user_id', 'article_id', 'is_like')
read_only_fields = ['id']
46 changes: 46 additions & 0 deletions authors/apps/articles/tests/article_tests_base_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from rest_framework.test import APITestCase, APIClient
from django.urls import reverse
from .test_data import *
from authors.apps.authentication.models import User
from authors.apps.articles.models import Article


class ArticlesBaseTest(APITestCase):
"""Sets up tests for features related to articles"""

def setUp(self):
client = APIClient()
self.user1 = User.objects.create_user(
username='Moracha', email="jratcher@gmail.com", password='password')
self.user1.is_verified = True
self.user1.save()
self.user2 = User.objects.create_user(
username='Josh', email='joshmoracha@gmail.com', password='password')
self.user2.is_verified = True
self.user2.save()
self.user1_credentials = client.post(reverse('authentication:login'),
user1, format='json')
self.user2_credentials = client.post(reverse('authentication:login'),
user2, format='json')
user1_token = self.user1_credentials.data.get('token')
user2_token = self.user2_credentials.data.get('token')
self.header_user1 = {
'HTTP_AUTHORIZATION': f'Bearer {user1_token}'
}
self.header_user2 = {
'HTTP_AUTHORIZATION': f'Bearer {user2_token}'
}
response = client.post(reverse('articles:create_article'),
article, **self.header_user1, format='json')

self.slug = response.data.get('slug')
#Publish article
retrieved_article = Article.objects.filter(slug=self.slug).first()
retrieved_article.published = True
retrieved_article.save()
self.like_article_url = '/api/articles/{}/like/'.format(self.slug)





29 changes: 29 additions & 0 deletions authors/apps/articles/tests/test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
user2 = {
"user": {
"username": "Josh",
"email": "joshmoracha@gmail.com",
"password": "password",
'callback_url': 'http://www.youtube.com'
}
}

user1 = {
"user": {
"username": "Moracha",
"email": "jratcher@gmail.com",
"password": "password",
'callback_url': 'http://www.youtube.com'
}
}
article = {
"article": {
"title": "How to train your dragon",
"description": "Ever wonder how?",
"body": "You have to believe",
"tagList": ["reactjs", "angularjs", "dragons"]
}
}

like_data = {
"is_like": True
}
107 changes: 107 additions & 0 deletions authors/apps/articles/tests/test_like_dislike_article.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import os
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase, APIClient
from .article_tests_base_class import ArticlesBaseTest
from .test_data import *
from authors.apps.articles.views import *


class LikeDislikeArticleTestCase(ArticlesBaseTest):
"""Test like and dislike of articles"""

def test_like_article(self):
"""Test like"""
response = self.client.post(self.like_article_url,
like_data, **self.header_user1, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

# Test invalid slug
response = self.client.post('/api/articles/invalid-slug/like/',
like_data, **self.header_user1, format='json')
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
# Test already liked article
response = self.client.post(self.like_article_url,
like_data, **self.header_user1, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_get_like(self):
"""Test like"""
response = self.client.get(self.like_article_url,
**self.header_user1)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

self.client.post(self.like_article_url,
like_data, **self.header_user1, format='json')
response1 = self.client.get(
self.like_article_url, **self.header_user1)
self.assertEqual(response1.status_code, status.HTTP_200_OK)
# Test invalid slug
response = self.client.get('/api/articles/invalid-slug/like/',
**self.header_user1)
detail = "This article has not been found."
self.assertEqual(response.data.get('detail'), detail)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

def test_update_like(self):
"""Test update like to dislike or vice versa"""
# test updating like that does not exist
response1 = self.client.patch(self.like_article_url + str(3) + '/',
like_data, **self.header_user1, format='json')
self.assertEqual(response1.status_code, status.HTTP_404_NOT_FOUND)
# Create like
response = self.client.post(self.like_article_url,
like_data, **self.header_user1, format='json')
like_id = response.data.get('id')
response1 = self.client.patch(self.like_article_url + str(like_id) + '/',
like_data, **self.header_user1, format='json')
self.assertContains(response1, 'is_like', status_code=200)
# Trying to update while not an owner
response = self.client.patch(self.like_article_url + str(like_id) + '/',
like_data, **self.header_user2, format='json')
detail = "This user does not own this like"
self.assertEqual(response.data.get('detail'), detail)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_delete_like(self):
"""Test update like to dislike or vice versa"""
# test updating like that does not exist
response1 = self.client.delete(self.like_article_url + str(3) + '/',
like_data, **self.header_user1, format='json')
self.assertEqual(response1.status_code, status.HTTP_404_NOT_FOUND)

response = self.client.post(self.like_article_url,
like_data, **self.header_user1, format='json')
like_id = response.data.get('id')

# Trying to update while not an owner
response = self.client.delete(self.like_article_url + str(like_id) + '/',
like_data, **self.header_user2, format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

response1 = self.client.delete(self.like_article_url + str(like_id) + '/',
like_data, **self.header_user1, format='json')
self.assertEqual(response1.status_code, status.HTTP_204_NO_CONTENT)

def test_method_not_allowed(self):
"""Test module not found class"""
response = self.client.put(self.like_article_url,
like_data, **self.header_user1, format='json')
self.assertEqual(response.status_code,
status.HTTP_405_METHOD_NOT_ALLOWED)

def test_get_likes(self):
"""Test get likes count endpoint"""
self.client.post(self.like_article_url,
like_data, **self.header_user1, format='json')

response = self.client.get('/api/articles/' + self.slug + '/likes/',
**self.header_user1)
self.assertEqual(response.data.get('likes'), 1)
self.assertEqual(response.status_code, status.HTTP_200_OK)
# Test invalid slug
response = self.client.get('/api/articles/invalid-slug/likes/',
**self.header_user1)
detail = "This article has not been found."
self.assertEqual(response.data.get('detail'), detail)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
15 changes: 13 additions & 2 deletions authors/apps/articles/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.urls import path
from .views import (
CreateArticleView, GetArticlesView,
GetAnArticleView, UpdateAnArticleView
GetAnArticleView, UpdateAnArticleView,
CreateRetrieveLikeView, UpdateDeleteLikeView,
GetArticleLikesView
)
app_name = 'articles'

Expand All @@ -18,5 +20,14 @@
path(
'articles/<slug:slug>/edit', UpdateAnArticleView.as_view(),
name="update_an_article"
)
),

path('articles/<slug:slug>/like/',
CreateRetrieveLikeView.as_view(), name="create_like"),

path('articles/<slug:slug>/like/<int:pk>/',
UpdateDeleteLikeView.as_view(), name="update_like"),

path('articles/<slug:slug>/likes/',
GetArticleLikesView.as_view(), name="get_likes")
]
Loading

0 comments on commit f2337b6

Please sign in to comment.