-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reports): Add user create report of article functionality
- Modify article table - Create model for reports - Create view function for adding a report to an article - Add logic to prevent the same user reporting on an article more than once [Starts: #165273492]
- Loading branch information
1 parent
2a9c8c7
commit 838a13b
Showing
10 changed files
with
321 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. | ||
from .models import Article, Report | ||
|
||
admin.site.register(Article) | ||
admin.site.register(Report) |
44 changes: 44 additions & 0 deletions
44
authors/apps/articles/migrations/0004_auto_20190508_0526.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Generated by Django 2.1 on 2019-05-08 05:26 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('articles', '0003_auto_20190505_1829'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Report', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('reason', models.TextField()), | ||
('date_reported', models.DateTimeField(auto_now_add=True)), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name='article', | ||
name='report_count', | ||
field=models.IntegerField(default=0), | ||
), | ||
migrations.AddField( | ||
model_name='article', | ||
name='reported', | ||
field=models.BooleanField(default=False), | ||
), | ||
migrations.AddField( | ||
model_name='report', | ||
name='article', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='articles.Article'), | ||
), | ||
migrations.AddField( | ||
model_name='report', | ||
name='reporter', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, to_field='username'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
from django.urls import reverse | ||
|
||
from rest_framework import status | ||
|
||
from .base import BaseTestCase | ||
from authors.apps.authentication.models import User | ||
|
||
|
||
class ReportTest(BaseTestCase): | ||
""" | ||
Class Test Case for Testing reporting functionality | ||
""" | ||
|
||
def test_user_report_article_status(self): | ||
""" | ||
Method tests the status on report article response | ||
""" | ||
url = reverse('articles-list-create') | ||
response = self.client.post( | ||
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
article_id = response.data['article']['id'] | ||
url = '/api/articles/{}/report/'.format(article_id) | ||
response = self.client.post( | ||
url, self.report, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
||
def test_user_report_article_message(self): | ||
""" | ||
Method tests the message when a report is successfull | ||
""" | ||
url = reverse('articles-list-create') | ||
response = self.client.post( | ||
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
article_id = response.data['article']['id'] | ||
url = '/api/articles/{}/report/'.format(article_id) | ||
response = self.client.post( | ||
url, self.report, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
self.assertEqual(response.data['message'], | ||
"Report successfully created.") | ||
|
||
def test_user_report_article_twice_message(self): | ||
""" | ||
Method tests the message when the same user reports an article twice | ||
""" | ||
url = reverse('articles-list-create') | ||
response = self.client.post( | ||
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
article_id = response.data['article']['id'] | ||
url = '/api/articles/{}/report/'.format(article_id) | ||
response = self.client.post( | ||
url, self.report, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
response = self.client.post( | ||
url, self.report, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
self.assertEqual(response.data['error'], | ||
"You already reported this article") | ||
|
||
def test_user_report_article_blank_reason(self): | ||
""" | ||
Method tests the message when a reason is blank | ||
""" | ||
url = reverse('articles-list-create') | ||
response = self.client.post( | ||
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
article_id = response.data['article']['id'] | ||
url = '/api/articles/{}/report/'.format(article_id) | ||
response = self.client.post( | ||
url, self.report_with_blank_reason, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
def test_user_report_article_no_reason(self): | ||
""" | ||
Method tests the message when there is no reason in request body | ||
""" | ||
url = reverse('articles-list-create') | ||
response = self.client.post( | ||
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
article_id = response.data['article']['id'] | ||
url = '/api/articles/{}/report/'.format(article_id) | ||
response = self.client.post( | ||
url, self.report_with_no_reason, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
def test_user_without_permisions_view_report(self): | ||
""" | ||
Method tests whether a non admin user can view all reports endpoint | ||
""" | ||
url = '/api/articles/reports/' | ||
response = self.client.get( | ||
url, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
||
def test_user_without_permisions_view_report_message(self): | ||
""" | ||
Method tests message when a non admin user can view all reports endpoint | ||
""" | ||
url = '/api/articles/reports/' | ||
response = self.client.get( | ||
url, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
self.assertEqual( | ||
response.data['error'], "You do not have permission to view the reported articles") | ||
|
||
def test_user_with_permisions_view_report_status(self): | ||
""" | ||
Method tests status when a admin user can view all reports endpoint | ||
""" | ||
url = reverse('articles-list-create') | ||
response = self.client.post( | ||
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
article_id = response.data['article']['id'] | ||
url = '/api/articles/{}/report/'.format(article_id) | ||
response = self.client.post( | ||
url, self.report, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
url = '/api/articles/reports/' | ||
response = self.client.get( | ||
url, HTTP_AUTHORIZATION=self.super_auth_header, format="json") | ||
self.assertEqual( | ||
response.status_code, status.HTTP_200_OK) | ||
|
||
def test_user_with_permisions_view_report_message(self): | ||
""" | ||
Method tests status when a admin user can view all reports endpoint | ||
""" | ||
url = reverse('articles-list-create') | ||
response = self.client.post( | ||
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
article_id = response.data['article']['id'] | ||
url = '/api/articles/{}/report/'.format(article_id) | ||
response = self.client.post( | ||
url, self.report, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
url = '/api/articles/reports/' | ||
response = self.client.get( | ||
url, HTTP_AUTHORIZATION=self.super_auth_header, format="json") | ||
self.assertIn( | ||
"report", str(response.data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.