Merge branch 'master' into user_check_final
This commit is contained in:
commit
cd18146e9c
6 changed files with 217 additions and 4 deletions
25
back/backend/migrations/0003_auto_20190308_1416.py
Normal file
25
back/backend/migrations/0003_auto_20190308_1416.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
# Generated by Django 2.1.7 on 2019-03-08 22:16
|
||||
|
||||
import datetime
|
||||
from django.db import migrations, models
|
||||
from django.utils.timezone import utc
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('backend', '0002_auto_20190303_0106'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='report',
|
||||
name='date_created',
|
||||
field=models.DateTimeField(default=datetime.datetime(2019, 3, 8, 22, 16, 32, 697634, tzinfo=utc), verbose_name='date created'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='report',
|
||||
name='date_submitted',
|
||||
field=models.DateTimeField(default=datetime.datetime(2019, 3, 8, 22, 16, 32, 697634, tzinfo=utc), verbose_name='date submitted'),
|
||||
),
|
||||
]
|
24
back/backend/migrations/0004_auto_20190308_1423.py
Normal file
24
back/backend/migrations/0004_auto_20190308_1423.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
# Generated by Django 2.1.7 on 2019-03-08 22:23
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.utils.timezone
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('backend', '0003_auto_20190308_1416'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='report',
|
||||
name='date_created',
|
||||
field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='date created'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='report',
|
||||
name='date_submitted',
|
||||
field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='date submitted'),
|
||||
),
|
||||
]
|
|
@ -1,6 +1,6 @@
|
|||
from django.db import models
|
||||
from django.conf import settings
|
||||
import datetime
|
||||
from django.utils import timezone
|
||||
import ntpath
|
||||
|
||||
class Report(models.Model):
|
||||
|
@ -10,8 +10,8 @@ class Report(models.Model):
|
|||
"""
|
||||
user_id = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
title = models.CharField(max_length=128)
|
||||
date_created = models.DateTimeField('date created', default=datetime.date.today)
|
||||
date_submitted = models.DateTimeField('date submitted', default=datetime.date.today)
|
||||
date_created = models.DateTimeField('date created', default=timezone.now)
|
||||
date_submitted = models.DateTimeField('date submitted', default=timezone.now)
|
||||
submitted = models.BooleanField(default=False)
|
||||
reference_number = models.CharField(max_length=32, default='')
|
||||
|
||||
|
|
|
@ -5,14 +5,21 @@ from users.models import CustomUser
|
|||
from unittest.mock import Mock, patch
|
||||
from datetime import date
|
||||
from backend.views import *
|
||||
import json
|
||||
|
||||
class ReportTests(TestCase):
|
||||
|
||||
def create_test_user(self, email, first, last, password):
|
||||
"""
|
||||
Create a test user and return it.
|
||||
"""
|
||||
user = CustomUser.objects.create_user(username=email, email=email, first_name=first, last_name=last, password=password)
|
||||
return user
|
||||
|
||||
def mock_report():
|
||||
"""
|
||||
Generates a mock object with the attributes of a report.
|
||||
"""
|
||||
r = Mock()
|
||||
r.report_pk = 1
|
||||
r.title = 'Report Title'
|
||||
|
@ -23,10 +30,18 @@ class ReportTests(TestCase):
|
|||
return r
|
||||
|
||||
def setUp(self):
|
||||
"""
|
||||
Create a couple test users and save them in the database.
|
||||
"""
|
||||
self.test_user_1 = self.create_test_user('one@one.com', 'One', 'Mr. One', '1password')
|
||||
self.test_user_1.save()
|
||||
self.test_user_2 = self.create_test_user('two@two.com', 'Two', 'Mr. Two', '1password')
|
||||
self.test_user_2.save()
|
||||
|
||||
def test_create_report_logged_in(self):
|
||||
"""
|
||||
Test when an authenticated user tries to submit a report.
|
||||
"""
|
||||
factory = APIRequestFactory()
|
||||
request = factory.post('/api/v1/report', {'title':'Test Report', 'reference':'12345'})
|
||||
user = CustomUser.objects.get(email='one@one.com')
|
||||
|
@ -37,6 +52,9 @@ class ReportTests(TestCase):
|
|||
self.assertEqual(report.title, 'Test Report')
|
||||
|
||||
def test_create_report_logged_out(self):
|
||||
"""
|
||||
Test when an unauthenticated user tries to create a report.
|
||||
"""
|
||||
factory = APIRequestFactory()
|
||||
request = factory.post('/api/v1/report', {'title':'Test Report', 'reference':'12345'})
|
||||
response = create_report(request)
|
||||
|
@ -57,3 +75,147 @@ class ReportTests(TestCase):
|
|||
'submitted':False
|
||||
}
|
||||
)
|
||||
|
||||
def test_report_submit_for_review_logged_out(self):
|
||||
"""
|
||||
Test for when an unauthenticated user tries to submit a report for review.
|
||||
"""
|
||||
factory = APIRequestFactory()
|
||||
request = factory.put('/api/v1/report/1')
|
||||
response = report_detail(request)
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_report_submit_for_review_logged_in_not_finalized(self):
|
||||
"""
|
||||
Test for when an authenticated user tries to submit for review a report
|
||||
that has not been finalized yet.
|
||||
"""
|
||||
factory = APIRequestFactory()
|
||||
add_report_request = factory.post('/api/v1/report', {'title':'Test Report', 'reference':'12345'})
|
||||
user = CustomUser.objects.get(email='one@one.com')
|
||||
force_authenticate(add_report_request, user=user)
|
||||
create_report(add_report_request)
|
||||
review_request = factory.put('/api/v1/report/1')
|
||||
force_authenticate(review_request, user=user)
|
||||
response = report_detail(review_request, 1)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
report = Report.objects.get(user_id=user)
|
||||
self.assertFalse(report.submitted)
|
||||
|
||||
def test_report_submit_for_review_logged_in_already_finalized(self):
|
||||
"""
|
||||
Test for when an authenticated user tries to submit for review a report
|
||||
that has already been finalized.
|
||||
"""
|
||||
factory = APIRequestFactory()
|
||||
add_report_request = factory.post('/api/v1/report', {'title':'Test Report', 'reference':'12345'})
|
||||
user = CustomUser.objects.get(email='one@one.com')
|
||||
force_authenticate(add_report_request, user=user)
|
||||
create_report(add_report_request)
|
||||
report = Report.objects.get(user_id=user)
|
||||
report.submitted = True
|
||||
report.save()
|
||||
review_request = factory.put('/api/v1/report/1')
|
||||
force_authenticate(review_request, user=user)
|
||||
response = report_detail(review_request, 1)
|
||||
self.assertEqual(response.status_code, 409)
|
||||
|
||||
def test_report_finalize_logged_out(self):
|
||||
"""
|
||||
Test for when an unauthenticated user tries to finalize a report.
|
||||
"""
|
||||
factory = APIRequestFactory()
|
||||
request = factory.put('/api/v1/report/1/final')
|
||||
response = finalize_report(request, 1)
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_report_finalize_wrong_owner(self):
|
||||
"""
|
||||
Test for when an authenticated user tries to finalize someone else's report.
|
||||
"""
|
||||
factory = APIRequestFactory()
|
||||
|
||||
# Create a report for user One
|
||||
add_report_1_request = factory.post('/api/v1/report', {'title':'One\'s Report', 'reference':'12345'})
|
||||
force_authenticate(add_report_1_request, user=self.test_user_1)
|
||||
create_report(add_report_1_request)
|
||||
|
||||
# Create a report for user Two
|
||||
add_report_2_request = factory.post('/api/v1/report', {'title':'Two\'s Report', 'reference':'12345'})
|
||||
force_authenticate(add_report_2_request, user=self.test_user_2)
|
||||
create_report(add_report_2_request)
|
||||
|
||||
# Try finalizing user Two's report with user One
|
||||
request = factory.put('/api/v1/report/2/final')
|
||||
force_authenticate(request, user=self.test_user_1)
|
||||
response = finalize_report(request, 2)
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_report_finalize_logged_in_not_finalized(self):
|
||||
"""
|
||||
Test for when an authenticated user tries to finalize a report
|
||||
that has not been finalized yet.
|
||||
"""
|
||||
factory = APIRequestFactory()
|
||||
add_report_request = factory.post('/api/v1/report', {'title':'Test Report', 'reference':'12345'})
|
||||
user = CustomUser.objects.get(email='one@one.com')
|
||||
force_authenticate(add_report_request, user=user)
|
||||
create_report(add_report_request)
|
||||
review_request = factory.put('/api/v1/report/1/final')
|
||||
force_authenticate(review_request, user=user)
|
||||
response = finalize_report(review_request, 1)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
report = Report.objects.get(user_id=user)
|
||||
self.assertTrue(report.submitted)
|
||||
|
||||
def test_report_finalize_logged_in_already_finalized(self):
|
||||
"""
|
||||
Test for when an authenticated user tries to finalize a report
|
||||
that has already been finalized.
|
||||
"""
|
||||
factory = APIRequestFactory()
|
||||
add_report_request = factory.post('/api/v1/report', {'title':'Test Report', 'reference':'12345'})
|
||||
user = CustomUser.objects.get(email='one@one.com')
|
||||
force_authenticate(add_report_request, user=user)
|
||||
create_report(add_report_request)
|
||||
report = Report.objects.get(user_id=user)
|
||||
report.submitted = True
|
||||
report.save()
|
||||
review_request = factory.put('/api/v1/report/1/final')
|
||||
force_authenticate(review_request, user=user)
|
||||
response = finalize_report(review_request, 1)
|
||||
self.assertEqual(response.status_code, 409)
|
||||
|
||||
def test_report_get_report_logged_in(self):
|
||||
"""
|
||||
Test for when an authenticated user tries to view a report.
|
||||
"""
|
||||
factory = APIRequestFactory()
|
||||
add_report_request = factory.post('/api/v1/report', {'title':'Test Report', 'reference':'12345'})
|
||||
user = CustomUser.objects.get(email='one@one.com')
|
||||
force_authenticate(add_report_request, user=user)
|
||||
create_report(add_report_request)
|
||||
get_request = factory.get('/api/v1/report/1')
|
||||
force_authenticate(get_request, user=user)
|
||||
response = report_detail(get_request, 1)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
report = Report.objects.get(user_id=user)
|
||||
# Check that the json response contains the title of the report we want
|
||||
j = json.loads(response.content.decode("utf-8", "strict"))
|
||||
self.assertEqual(report.title, j['title'])
|
||||
|
||||
def test_report_delete_report_logged_in(self):
|
||||
"""
|
||||
Test for when an authenticated user tries to delete a report.
|
||||
"""
|
||||
factory = APIRequestFactory()
|
||||
add_report_request = factory.post('/api/v1/report', {'title':'Test Report', 'reference':'12345'})
|
||||
user = CustomUser.objects.get(email='one@one.com')
|
||||
force_authenticate(add_report_request, user=user)
|
||||
create_report(add_report_request)
|
||||
delete_request = factory.delete('/api/v1/report/1')
|
||||
force_authenticate(delete_request, user=user)
|
||||
response = report_detail(delete_request, 1)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
reports = Report.objects.filter(user_id=user)
|
||||
self.assertEqual(len(reports), 0)
|
||||
|
|
|
@ -6,6 +6,7 @@ import os
|
|||
from django.core.mail import EmailMultiAlternatives
|
||||
from django.template.loader import render_to_string
|
||||
from decouple import config
|
||||
from django.utils import timezone
|
||||
|
||||
def get_report(report_pk):
|
||||
"""
|
||||
|
@ -125,7 +126,7 @@ def create_report(request):
|
|||
report = Report.objects.create(
|
||||
user_id=request.user,
|
||||
title=request.data['title'],
|
||||
date_created=datetime.date.today(),
|
||||
date_created=timezone.now(),
|
||||
reference_number=request.data['reference']
|
||||
)
|
||||
report.save()
|
||||
|
@ -249,6 +250,7 @@ def finalize_report(request, report_pk):
|
|||
if r.submitted:
|
||||
return JsonResponse({"message": "Cannot submit a report that has already been submitted."}, status=409)
|
||||
r.submitted = True
|
||||
r.date_submitted = timezone.now()
|
||||
r.save()
|
||||
# Send email
|
||||
send_report_to_admin(request, report_pk, status="FINAL")
|
||||
|
|
BIN
back/db.sqlite3
BIN
back/db.sqlite3
Binary file not shown.
Loading…
Reference in a new issue