Skip to content

Commit

Permalink
Merge bf3f411 into ce008fe
Browse files Browse the repository at this point in the history
  • Loading branch information
barema4 authored Dec 13, 2018
2 parents ce008fe + bf3f411 commit 52dcb2d
Show file tree
Hide file tree
Showing 11 changed files with 495 additions and 0 deletions.
Empty file.
Empty file.
56 changes: 56 additions & 0 deletions authors/apps/comments/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from django.db import models
from django.utils import timezone
from authors.apps.articles.models import Article
from authors.apps.authentication.models import User

class Comments(models.Model):
"""
create model for comments
"""
article = models.ForeignKey(
Article, related_name='comments', on_delete=models.CASCADE, blank=True, null=True)

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

comment_body = models.TextField(null=False, blank=False, error_messages={
"required": "Please input the comment"})

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

def __str__(self):
"""
It returns a string
"""
return self.comment_body

class Meta:

ordering = ['created_at']


class Replies(models.Model):
"""
create a model for replies to a comment
"""
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)

reply_message = models.TextField(null=False, blank=False,
error_messages={"required": "please input your reply"})

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

def __str__(self):
"""
It returns a string
"""
return self.reply_message

class Meta:

ordering = ['created_at']
45 changes: 45 additions & 0 deletions authors/apps/comments/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from rest_framework import serializers
from authors.apps.articles.models import Article
from authors.apps.comments.models import Comments, Replies
from authors.apps.authentication.models import User
from authors.apps.profiles.models import UserProfile
from authors.apps.profiles.serializers import GetUserProfileSerializer

class RepliesSerializer(serializers.ModelSerializer):

def to_representation(self, instance):
"""
formats serializer display response
:param instance:
"""
response = super().to_representation(instance)
profile = UserProfile.objects.get(user=instance.author)

response['author'] = profile.user.username
return response

class Meta:
model = Replies
fields = ('id', 'reply_message', 'created_at', 'comment', 'author')


class CommentSerializer(serializers.ModelSerializer):

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

def to_representation(self, instance):
"""
formats serializer display response
:param instance:
"""
response = super().to_representation(instance)
profile = UserProfile.objects.get(user=instance.author)

response['article'] = instance.article.slug
response['author'] = profile.user.username
return response

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

Empty file.
95 changes: 95 additions & 0 deletions authors/apps/comments/tests/test_comment_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from django.urls import path, reverse
from rest_framework import status
from rest_framework.test import APITestCase, APIRequestFactory, APIClient
from authors.apps.authentication.views import RegistrationAPIView, AccountVerified
from authors.apps.authentication.models import User
from authors.apps.authentication.backends import JWTAuthentication


class CommentTest(APITestCase):

def setUp(self):
self.slug = 0
self.user_data = {
"user": {
"username": "derict",
"email": "sekayasin@yahoo.com",
"password": "Bet/2015"}}
self.url = reverse("registration")
self.client = APIClient()
self.client.post(self.url, self.user_data, format='json')
self.request = APIRequestFactory().post(
reverse("registration")
)

self.new_article = {
"article": {
"title": "How to fight dragons 8",
"description": "Ever wonder jckvlahvhow?",
"body": "You have kenglto believe"
}
}

self.post_comment = {"comment":{
"comment_body": "nice article and well done"
}}

self.update_comment = {
"comment": {
"body": "that is good"
}
}

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

self.reply_comment = {"reply":{
"reply_message": "that is not a good comment"
}}

self.upate_reply = {"reply":{
"reply_message": "wawoo that is good"
}}

self.reply_missing_body = {"reply":{
"reply_message": ""
}}

user = User.objects.get()
request = self.request
token, uid = RegistrationAPIView.generate_token(user, request)
response = self.account_verification(token, uid)
self.assertEqual(response.status_code, status.HTTP_200_OK)
user = User.objects.get()
self.assertTrue(user.is_verified)

response = self.client.post(
'/api/users/login/',
self.user_data,
format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.login_token = response.data['token']
self.client.credentials(
HTTP_AUTHORIZATION='Bearer ' +
self.login_token)

def account_verification(self, token, uid):
request = APIRequestFactory().get(
reverse("verify_account", kwargs={"token": token, "uid": uid})
)
verify = AccountVerified.as_view()
response = verify(request, token=token, uid=uid)
return response

def get_slug(self, response=[]):
self.client.post(
'/api/articles/',
data=self.new_article,
format='json')
response = self.client.get('/api/articles/', format='json')
for i in response.data:
self.slug = i['slug']
return self.slug
161 changes: 161 additions & 0 deletions authors/apps/comments/tests/test_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
from rest_framework.test import APIClient
from .test_comment_data import CommentTest
# from authors.apps.articles.tests.base_test import BaseTest
from rest_framework import status


class TestComment(CommentTest):

def test_create_comment(self):
"""
create a comment on an article
"""
article = self.client.post('/api/articles/',data=self.new_article, format='json')
slug = self.get_slug(article)
reponse = self.client.post(
'/api/{}/comments/'.format(slug),
data=self.post_comment,
format='json')
self.assertEqual(reponse.status_code, status.HTTP_201_CREATED)

def test_commenting_without_body(self):
"""
tests for commenting on an article without comment body
"""
article = self.client.post('/api/articles/',data=self.new_article, format='json')
slug = self.get_slug(article)
reponse = self.client.post(
'/api/{}/comments/'.format(slug),
data=self.post_missing_comment,
format='json')
self.assertEqual(reponse.status_code, status.HTTP_400_BAD_REQUEST)

def test_get_all_comment_and_replies(self):
"""
tests for getting all comments and replies on an article
"""

article = self.client.post('/api/articles/',data=self.new_article, format='json')
slug = self.get_slug(article)
self.client.post(
'/api/{}/comments/'.format(slug),
data=self.post_comment,
format='json')
reponse = self.client.get('/api/{}/comments/'.format(slug), format='json')
self.assertEqual(reponse.status_code, status.HTTP_200_OK)

def test_post__comment_non_existing_article(self):
"""
tests for commenting a non existing article
"""
article = self.client.post('/api/articles/',data=self.new_article, format='json')
slug = self.get_slug(article)
self.client.post(
'/api/{}/comments/'.format(slug),
data=self.post_comment,
format='json')
reponse = self.client.get(
'/api/ghfdghbbjb/comments/',
format='json')
self.assertEqual(reponse.status_code, status.HTTP_404_NOT_FOUND)

def test_update_comment(self):
"""
tests for updating a comment
"""
article = self.client.post('/api/articles/',data=self.new_article, format='json')
slug = self.get_slug(article)

created_comment = self.client.post(
'/api/{}/comments/'.format(slug),
data=self.post_comment,
format='json')
slug = self.get_slug(created_comment)

comment_id = created_comment.data['id']

update_comment = {"comment":{
"comment_body": "woooow!"
}}
response = self.client.put(
'/api/comment/{}/'.format(comment_id ),
data=update_comment,
format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_reply_to_comment(self):
"""
tests for replying to a comment
"""
article = self.client.post('/api/articles/',data=self.new_article, format='json')
slug = self.get_slug(article)
comment = self.client.post(
'/api/{}/comments/'.format(slug),
data=self.post_comment,
format='json')
slug = self.get_slug(comment)

comment_id = comment.data['id']

reponse = self.client.post(
'/api/comment/{}/replies/'.format(comment_id),
data=self.reply_comment,
format='json')

self.assertEqual(reponse.status_code, status.HTTP_201_CREATED)

def test_reply_without_reply_message(self):
"""
reply to comment without reply message
"""
article = self.client.post('/api/articles/',data=self.new_article, format='json')
slug = self.get_slug(article)
comment = self.client.post(
'/api/{}/comments/'.format(slug),
data=self.post_comment,
format='json')
slug = self.get_slug(comment)
comment_id = comment.data['id']

reponse = self.client.post(
'/api/comment/{}/replies/'.format(comment_id),
data=self.reply_missing_body,
format='json')
self.assertEqual(reponse.status_code, status.HTTP_400_BAD_REQUEST)


def test_update_reply(self):
"""
update reply
"""

article = self.client.post('/api/articles/',data=self.new_article, format='json')
slug = self.get_slug(article)

created_comment = self.client.post(
'/api/{}/comments/'.format(slug),
data=self.post_comment,
format='json')
slug = self.get_slug(created_comment)
comment_id = created_comment.data['id']
created_reply = self.client.post(
'/api/comment/{}/replies/'.format(comment_id),
data=self.reply_comment,
format='json')

slug = self.get_slug(created_reply)
reply_id = created_reply.data['id']

upate_reply = {"reply":{
"reply_message": "hehehh that is nice"
}}

response = self.client.put(
'/api/comment/replies/{}/'.format(reply_id ),
data=upate_reply,
format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)




19 changes: 19 additions & 0 deletions authors/apps/comments/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Defines urls used in comments package
"""
from django.urls import path
from rest_framework.routers import DefaultRouter
from authors.apps.comments.views import CommentsView, RepliesView

urlpatterns = [

path('<slug>/comments/', CommentsView.as_view()),
path('comment/<Id>/', CommentsView.as_view()),
path('comment/<commentID>/replies/', RepliesView.as_view()),
path('comment/replies/<Id>/', RepliesView.as_view()),

]

router = DefaultRouter()

urlpatterns += router.urls
Loading

0 comments on commit 52dcb2d

Please sign in to comment.