Skip to content

Commit

Permalink
[feature #164047079]Create report article endpoint]
Browse files Browse the repository at this point in the history
-create report endpoint
-write test
- remove duplicate lines of code
- change the email message
[Finishes#164047079]
  • Loading branch information
Joan Ngatia authored and winniekariuki committed Mar 22, 2019
2 parents 8d81926 + 99b94a3 commit ff25ceb
Show file tree
Hide file tree
Showing 9 changed files with 365 additions and 39 deletions.
20 changes: 18 additions & 2 deletions authors/apps/article/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ class Meta:
def __str__(self):
return self.body[:20]


class LikeComment(models.Model):
"""This class creates a model for comments likes and dislikes"""
user_like = models.ForeignKey(
Expand Down Expand Up @@ -239,4 +238,21 @@ class ArticleFavourite(models.Model):
on_delete=models.CASCADE
)
favourited = models.BooleanField(default=False)


class ReportArticle(models.Model):
"""
Report model schema
"""
article = models.ForeignKey(
Article,
to_field='slug',
on_delete=models.CASCADE
)
report_message = models.CharField(
max_length=225,null=False
)
reader = models.ForeignKey(
User,
to_field='email',
on_delete=models.CASCADE
)
22 changes: 19 additions & 3 deletions authors/apps/article/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
from rest_framework import response

from authors.apps.authentication.serializers import UserSerializer
from .models import Article, ArticleLikes, Rate, ArticleComment, ArticleFavourite
from .models import Article, ArticleLikes, Rate, ArticleComment, ArticleFavourite,ReportArticle
from ..profiles.serializers import ProfileSerializer


class LikesSerializer(serializers.ModelSerializer):
"""
This class serializes data from ArticleLikes model
Expand Down Expand Up @@ -283,7 +282,6 @@ class Meta:
model = ArticleComment
fields = ['is_active']


class GetArticleSerializer(serializers.ModelSerializer):
tag_list = serializers.SerializerMethodField()

Expand All @@ -293,3 +291,21 @@ def get_tag_list(self, article):
class Meta:
model = Article
fields = '__all__'

class ReportSerializer(serializers.ModelSerializer):
"""
Report model serializers
"""
def get_article(self, obj):
"""
Gets article slug
"""
return obj.article.slug

class Meta:
model = ReportArticle
fields = ['article','report_message','reader']

class Meta:
model = ReportArticle
fields = ['article','report_message','reader']
167 changes: 167 additions & 0 deletions authors/apps/article/tests/test_report_article.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import json

from rest_framework.test import APITestCase, APIClient
from rest_framework.views import status


class TestReportArticle(APITestCase):
"""
Class tests for rating article.
"""
client = APIClient()

def setUp(self):
""" Creates user and article for testing"""
self.article = {
"title": "epic",
"description": "sdsd",
"body": "dsd",
"images": ""
}
self.user = {
"user": {
"email": "winniekariuki07@gmail.com",
"username": "Winnie",
"password": "Winnie07"
}
}
self.user_1 = {
"user": {
"email": "julietkariuki07@gmail.com",
"username": "Juliet",
"password": "Juliet07"
}
}
self.admin = {
"user": {
"email": "andelaolympians@andela.com",
"username": "andela",
"password": "Kariuki07"
}
}

self.report = {
"report_message":"plagerism"
}


create_user = self.client.post(
'/api/users/', self.user, format='json')

create_user_1 = self.client.post(
'/api/users/', self.user_1, format='json')
create_admin = self.client.post(
'/api/users/', self.admin, format='json')


self.request_tkn = self.client.post(
'/api/users/login/', self.user, format='json')
token_request = json.loads(self.request_tkn.content)
self.token = token_request["user"]["token"]

self.request_tkn_1= self.client.post(
'/api/users/login/', self.user_1, format='json')
token_request_1 = json.loads(self.request_tkn_1.content)
self.token_1 = token_request_1["user"]["token"]

self.request_tkn_admin= self.client.post(
'/api/users/login/', self.admin, format='json')
token_request_admin= json.loads(self.request_tkn_admin.content)
self.token_admin = token_request_admin["user"]["token"]

create_article = self.client.post('/api/articles/', self.article,
HTTP_AUTHORIZATION='Token ' + self.token,
format='json')
def test_successful_report(self):
"""Test reporting of an article
"""
from rest_framework.test import APIClient
client = APIClient()
response = client.post('/api/report/epic/', self.report,
HTTP_AUTHORIZATION='Token ' + self.token_1,
format='json')
result = json.loads(response.content)

self.assertEqual(result["message"], "Your report has been sent successfully to the admin ")
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_report_of_article_does_not_exist(self):
"""Test reporting of an article that does not exist
"""
from rest_framework.test import APIClient
client = APIClient()
response = client.post('/api/report/spoon/', self.report,
HTTP_AUTHORIZATION='Token ' + self.token_1,
format='json')
result = json.loads(response.content)

self.assertEqual(result["error_message"], "The article you are reporting does not exist")
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_reporting_your_own_article(self):
"""Test reporting your own article
"""

response = self.client.post('/api/articles/', self.article,
HTTP_AUTHORIZATION='Token ' + self.token,
format='json')
result = json.loads(response.content)

response = self.client.post('/api/report/epic/', self.report,
HTTP_AUTHORIZATION='Token ' + self.token,
format='json')
result = json.loads(response.content)

self.assertEqual(result["errors"], "You cannot report your own article")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_report_article_more_than_once(self):
"""Test reporting of an article
"""
from rest_framework.test import APIClient
client = APIClient()

response = client.post('/api/report/epic/', self.report,
HTTP_AUTHORIZATION='Token ' + self.token_1,
format='json')
result = json.loads(response.content)

response = client.post('/api/report/epic/', self.report,
HTTP_AUTHORIZATION='Token ' + self.token_1,
format='json')
result = json.loads(response.content)

self.assertEqual(result['errors'],'You can only report an article once')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_get_all_reports(self):
"""
Test getting of all reports
"""
from rest_framework.test import APIClient
client = APIClient()

response = self.client.get('/api/reports/',
HTTP_AUTHORIZATION='Token ' + self.token_admin,
format='json')
result = json.loads(response.content)

self.assertEqual(result["message"], "You have no permissions")
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_get_single_report(self):
"""
Test getting of single report
"""
from rest_framework.test import APIClient
client = APIClient()

response = self.client.get('/api/reports/epic/',
HTTP_AUTHORIZATION='Token ' + self.token_admin,
format='json')
result = json.loads(response.content)

self.assertEqual(result["message"], "You have no permissions")
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
7 changes: 6 additions & 1 deletion authors/apps/article/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.urls import path


from .views import (ArticlesAPIView, RetrieveArticleAPIView, LikeAPIView, DislikeAPIView, RateAPIView, FavouriteAPIView,
CommentsAPIView, RetrieveCommentsAPIView, SubCommentAPIView,LikeUnlikeAPIView, CommentDislikeAPIView)
CommentsAPIView, RetrieveCommentsAPIView, SubCommentAPIView,LikeUnlikeAPIView, CommentDislikeAPIView,ReportArticlesView,GetSingleReportView,GetAllReportsViews)


app_name = "articles"

Expand All @@ -17,4 +19,7 @@
path('articles/<slug>/like_comment/<pk>', LikeUnlikeAPIView.as_view()),
path('articles/<slug>/dislike_comment/<pk>', CommentDislikeAPIView.as_view()),
path('articles/<slug>/favorite', FavouriteAPIView.as_view()),
path('report/<slug>/',ReportArticlesView.as_view()),
path('reports/<slug>/',GetSingleReportView.as_view()),
path('reports/',GetAllReportsViews.as_view()),
]
28 changes: 8 additions & 20 deletions authors/apps/article/utils.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
from rest_framework import serializers

from django.utils.text import slugify

from .models import Tag


class TagField(serializers.ModelSerializer):
"""
to cater for Tag field
"""
def get_queryset(self):
res = Tag.objects.all()
return res

def to_internal_value(self,data):
tag, created = Tag.objects.get_or_create(
tag=data, slug=slugify(data.lower())
)

"""
the message function
"""
def email_message(slug,report_message):
message = " This article with the following slug " + slug + " has been reported because of\n " \
+ report_message

return message
Loading

0 comments on commit ff25ceb

Please sign in to comment.