diff --git a/back/backend/migrations/0003_auto_20190308_1416.py b/back/backend/migrations/0003_auto_20190308_1416.py new file mode 100644 index 0000000..c72db8e --- /dev/null +++ b/back/backend/migrations/0003_auto_20190308_1416.py @@ -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'), + ), + ] diff --git a/back/backend/migrations/0004_auto_20190308_1423.py b/back/backend/migrations/0004_auto_20190308_1423.py new file mode 100644 index 0000000..2c4bf55 --- /dev/null +++ b/back/backend/migrations/0004_auto_20190308_1423.py @@ -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'), + ), + ] diff --git a/back/backend/models.py b/back/backend/models.py index dd47555..155e85e 100644 --- a/back/backend/models.py +++ b/back/backend/models.py @@ -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='') diff --git a/back/backend/test_report.py b/back/backend/test_report.py index c39e3d4..ff00e8f 100644 --- a/back/backend/test_report.py +++ b/back/backend/test_report.py @@ -116,3 +116,48 @@ class ReportTests(TestCase): 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 = create_report(request) + 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 = report_detail(review_request, 1) + self.assertEqual(response.status_code, 200) + reports = Report.objects.filter(user_id=user) + print("reports=",len(reports)) + 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 = report_detail(review_request, 1) + self.assertEqual(response.status_code, 409) diff --git a/back/backend/views.py b/back/backend/views.py index 28fdf16..f872c55 100644 --- a/back/backend/views.py +++ b/back/backend/views.py @@ -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() diff --git a/back/db.sqlite3 b/back/db.sqlite3 index 15729b1..74baa7c 100644 Binary files a/back/db.sqlite3 and b/back/db.sqlite3 differ