Skip to content

Commit

Permalink
Merge 96a6d94 into 01667cc
Browse files Browse the repository at this point in the history
  • Loading branch information
huxaiphaer committed Sep 24, 2018
2 parents 01667cc + 96a6d94 commit ef9654d
Show file tree
Hide file tree
Showing 9 changed files with 765 additions and 14 deletions.
46 changes: 45 additions & 1 deletion authors/apps/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def save(self, *args, **kwargs):
:param kwargs:
"""
self.slug = generate_slug(Article, self)

super(Article, self).save(*args, **kwargs)

@property
Expand Down Expand Up @@ -91,3 +91,47 @@ class Rating(models.Model):

class Meta:
ordering = ('-score',)


class Comments(models.Model):

article = models.ForeignKey(Article, related_name='comments', on_delete=models.CASCADE, blank=True, null=True)

author = models.ForeignKey(User, on_delete=models.CASCADE)

body = models.TextField(null=False, blank=False, error_messages={"required": "You cannot submit without a comment."})

created_at = models.DateTimeField(auto_created=True, auto_now=False, default=timezone.now)

def __str__(self):
"""
:return: string
"""
return self.body

class Meta:
get_latest_by = 'created_at'
ordering = ['-created_at']


class Replies(models.Model):

comment = models.ForeignKey(Comments, related_name='replies', on_delete=models.CASCADE, blank=True, null=True)

author = models.ForeignKey(User, related_name='replies', on_delete=models.CASCADE, blank=True , null=True)

content = models.TextField(null=False, blank=False,
error_messages={"required": "You cannot submit without a reply."})

created_at = models.DateTimeField(auto_created=True, auto_now=False, default=timezone.now)

def __str__(self):
"""
:return: string
"""
return self.content

class Meta:
get_latest_by = 'created_at'
ordering = ['-created_at']

28 changes: 21 additions & 7 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
"""
from rest_framework import serializers
from rest_framework.pagination import PageNumberPagination

from authors.apps import articles
from authors.apps.articles.exceptions import NotFoundException
from authors.apps.articles.models import Article, Tag, Rating
from authors.apps.articles.models import Article, Tag, Rating, Comments, Replies
from authors.apps.articles.utils import get_date
from authors.apps.authentication.models import User
from authors.apps.profiles.models import UserProfile
Expand All @@ -18,7 +16,6 @@ class TagRelatedField(serializers.RelatedField):
Implements a custom relational field by overriding RelatedFied.
returns a list of tag names.
"""

def to_representation(self, value):

return value.tag_name
Expand All @@ -32,6 +29,22 @@ class Meta:
fields = "__all__"


class RepliesSerializer(serializers.ModelSerializer):

class Meta:
model = Replies
fields = '__all__'


class CommentSerializer(serializers.ModelSerializer):

replies = RepliesSerializer(many=True, read_only=True)

class Meta:
model = Comments
fields = ('id', 'body', 'article', 'author', 'replies')


class ArticleSerializer(serializers.ModelSerializer):
"""
Define action logic for an article
Expand All @@ -41,12 +54,12 @@ class ArticleSerializer(serializers.ModelSerializer):
source='author.average_rating', required=False)
tagList = TagRelatedField(
many=True, required=False, source='tags', queryset=Tag.objects.all())

author = serializers.PrimaryKeyRelatedField(
queryset=User.objects.all(), required=False)
slug = serializers.CharField(read_only=True)
favorites_count = serializers.SerializerMethodField()
tags = []
comments = CommentSerializer(many=True, read_only=True)

def create(self, validated_data):
"""
Expand Down Expand Up @@ -128,9 +141,9 @@ class Meta:
class behaviours
"""
model = Article
# noinspection SpellCheckingInspection

fields = ('slug', 'title', 'description', 'body', 'created_at', 'average_rating', 'user_rating',
'updated_at', 'favorites_count', 'photo_url', 'author', 'tagList')
'updated_at', 'favorited', 'favorites_count', 'photo_url', 'author', 'tagList', 'comments')

def get_favorites_count(self, instance):
return instance.favorited_by.count()
Expand Down Expand Up @@ -236,3 +249,4 @@ class behaviours
"""
model = Rating
fields = ("score", "rated_by", "rated_at", "article")

42 changes: 42 additions & 0 deletions authors/apps/articles/tests/test_comment_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

class TestDataComments:

post_comment = {
"comment": {
"body": "Yeah you are good. "
}
}

post_missing_comment= {
"comment":{
"body":""
}
}

post_article = {
"article": {
"title": "Yet another Sand Blog",
"description": "Sand is m testing",
"body": "another that am doin test"
}
}

post_update_comment = {
"comment": {
"body" : "it all over now."
}
}

post_update_no_body = {
"comment": {
"body": ""
}
}



user_name = "iroq"
user_email = "iroq@sims.andela"
password = "teamiroq1"


215 changes: 215 additions & 0 deletions authors/apps/articles/tests/test_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
from django.test import TestCase
import json
from authors.apps.articles.models import Comments
from authors.apps.authentication.models import User
from rest_framework.test import APIClient
from rest_framework import status
from .test_comment_data import TestDataComments


class Tests(TestCase, TestDataComments):

def setUp(self):
"""
setup tests
"""
User.objects.all().delete()
Comments.objects.all().delete()

self.login_data = {"user": {"email": self.user_email, "password": self.password,
}
}
self.user_data = {"user": {"username": self.user_name, "email": self.user_email,
"password": self.password,
}
}
self.client = APIClient()

self.response = self.client.post(
"/api/users/",
self.user_data,
format="json")
self.user = User.objects.get(email=self.user_email)
self.user.is_active = True
self.user.is_email_verified = True
self.user.save()
self.response = self.client.post(
"/api/users/login/",
self.login_data,
format="json")
self.assertEqual(status.HTTP_200_OK, self.response.status_code)
self.assertIn('token', self.response.data)
token = self.response.data.get("token", None)
self.client.credentials(HTTP_AUTHORIZATION="Token {0}".format(token))

def test_post_new_comment_on_article(self):
"""first post an article """
response = self.client.post(
"/api/articles/", data=json.dumps(
self.post_article), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertIn('article', response.json())
self.assertIsInstance(response.json().get("article"), dict)

response = self.client.get(
"/api/articles/", content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertIn('article', response.json())
self.assertIsInstance(response.json().get("article"), dict)

slug = response.json().get("article").get("results").get("slug")

response = self.client.post(
"/api/articles/{}/comment/".format(slug), data=json.dumps(
self.post_comment), content_type='application/json')
self.assertEqual(201, response.status_code)
self.assertIsInstance(response.json(), dict)

def test_post_empty_comment(self):

response = self.client.post(
"/api/articles/", data=json.dumps(
self.post_article), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertIn('article', response.json())
self.assertIsInstance(response.json().get("article"), dict)

response = self.client.get(
"/api/articles/", content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertIn('article', response.json())
self.assertIsInstance(response.json().get("article"), dict)

slug = response.json().get("article").get("results").get("slug")

response = self.client.post(
"/api/articles/{}/comment/".format(slug), data=json.dumps(
self.post_missing_comment), content_type='application/json')
self.assertEqual(400, response.status_code)

def test_post_wrong_slug(self):
"""test wrong slug."""
slug = "heh83838"
response = self.client.post(
"/api/articles/{}/comment/".format(slug), data=json.dumps(
self.post_missing_comment), content_type='application/json')
self.assertEqual(404, response.status_code)

def test_update_comment(self):
"""test Update comment"""
response = self.client.post(
"/api/articles/", data=json.dumps(
self.post_article), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertIn('article', response.json())
self.assertIsInstance(response.json().get("article"), dict)

response = self.client.get(
"/api/articles/", content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertIn('article', response.json())
self.assertIsInstance(response.json().get("article"), dict)

slug = response.json().get("article").get("results").get("slug")

response = self.client.post(
"/api/articles/{}/comment/".format(slug), data=json.dumps(
self.post_comment), content_type='application/json')
self.assertEqual(201, response.status_code)
self.assertIsInstance(response.json(), dict)

id = response.json().get('id')

response = self.client.put(
"/api/articles/comment/{}/".format(id), data=json.dumps(
self.post_comment), content_type='application/json')
self.assertEqual(200, response.status_code)
self.assertIsInstance(response.json(), dict)

def test_update_with_no_body(self):
"""test Update comment"""
response = self.client.post(
"/api/articles/", data=json.dumps(
self.post_article), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertIn('article', response.json())
self.assertIsInstance(response.json().get("article"), dict)

response = self.client.get(
"/api/articles/", content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertIn('article', response.json())
self.assertIsInstance(response.json().get("article"), dict)

slug = response.json().get("article").get("results").get("slug")

response = self.client.post(
"/api/articles/{}/comment/".format(slug), data=json.dumps(
self.post_comment), content_type='application/json')
self.assertEqual(201, response.status_code)
self.assertIsInstance(response.json(), dict)

id = response.json().get('id')

response = self.client.put(
"/api/articles/comment/{}/".format(id), data=json.dumps(
self.post_update_no_body), content_type='application/json')
self.assertEqual(400, response.status_code)
self.assertIsInstance(response.json(), dict)

def test_update_comment_with_non_existing_id(self):
"""update with non existing ID"""
id = 1000
response = self.client.put(
"/api/articles/comment/{}/".format(id), data=json.dumps(
self.post_comment), content_type='application/json')
self.assertEqual(404, response.status_code)
self.assertIsInstance(response.json(), dict)

def test_delete_comment(self):
"""test Delete comment"""
response = self.client.post(
"/api/articles/", data=json.dumps(
self.post_article), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertIn('article', response.json())
self.assertIsInstance(response.json().get("article"), dict)

response = self.client.get(
"/api/articles/", content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertIn('article', response.json())
self.assertIsInstance(response.json().get("article"), dict)

slug = response.json().get("article").get("results").get("slug")

response = self.client.post(
"/api/articles/{}/comment/".format(slug), data=json.dumps(
self.post_comment), content_type='application/json')
self.assertEqual(201, response.status_code)
self.assertIsInstance(response.json(), dict)

id = response.json().get('id')

response = self.client.delete(
"/api/articles/comment/{}/".format(id), content_type='application/json')
self.assertEqual(204, response.status_code)

def test_delete_with_non_existing_id(self):
"""delete with a non existing ID"""

id = 1000
response = self.client.delete(
"/api/articles/comment/{}/".format(id), content_type='application/json')
self.assertEqual(404, response.status_code)
self.assertIsInstance(response.json(), dict)










Loading

0 comments on commit ef9654d

Please sign in to comment.