2019-03-06 23:10:18 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from rest_framework.test import APIRequestFactory, force_authenticate
|
|
|
|
from backend.models import Report
|
|
|
|
from users.models import CustomUser
|
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
from datetime import date
|
|
|
|
from backend.views import *
|
|
|
|
|
|
|
|
class ReportTests(TestCase):
|
|
|
|
|
|
|
|
def create_test_user(self, email, first, last, password):
|
2019-03-08 22:14:22 +00:00
|
|
|
"""
|
|
|
|
Create a test user and return it.
|
|
|
|
"""
|
2019-03-06 23:10:18 +00:00
|
|
|
user = CustomUser.objects.create_user(username=email, email=email, first_name=first, last_name=last, password=password)
|
|
|
|
return user
|
|
|
|
|
|
|
|
def mock_report():
|
2019-03-08 22:14:22 +00:00
|
|
|
"""
|
|
|
|
Generates a mock object with the attributes of a report.
|
|
|
|
"""
|
2019-03-06 23:10:18 +00:00
|
|
|
r = Mock()
|
|
|
|
r.report_pk = 1
|
|
|
|
r.title = 'Report Title'
|
|
|
|
r.date_created = '2019-03-01'
|
|
|
|
r.date_submitted = '2019-03-01'
|
|
|
|
r.submitted = False
|
|
|
|
r.reference_number = '12345'
|
|
|
|
return r
|
|
|
|
|
|
|
|
def setUp(self):
|
2019-03-08 22:14:22 +00:00
|
|
|
"""
|
|
|
|
Create a test user and save it in the database.
|
|
|
|
"""
|
2019-03-06 23:36:47 +00:00
|
|
|
self.test_user_1 = self.create_test_user('one@one.com', 'One', 'Mr. One', '1password')
|
|
|
|
self.test_user_1.save()
|
2019-03-06 23:10:18 +00:00
|
|
|
|
|
|
|
def test_create_report_logged_in(self):
|
2019-03-08 22:14:22 +00:00
|
|
|
"""
|
|
|
|
Test when an authenticated user tries to submit a report.
|
|
|
|
"""
|
2019-03-06 23:10:18 +00:00
|
|
|
factory = APIRequestFactory()
|
|
|
|
request = factory.post('/api/v1/report', {'title':'Test Report', 'reference':'12345'})
|
|
|
|
user = CustomUser.objects.get(email='one@one.com')
|
|
|
|
force_authenticate(request, user=user)
|
|
|
|
response = create_report(request)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
report = Report.objects.get(user_id=user)
|
|
|
|
self.assertEqual(report.title, 'Test Report')
|
|
|
|
|
|
|
|
def test_create_report_logged_out(self):
|
2019-03-08 22:14:22 +00:00
|
|
|
"""
|
|
|
|
Test when an unauthenticated user tries to create a report.
|
|
|
|
"""
|
2019-03-06 23:10:18 +00:00
|
|
|
factory = APIRequestFactory()
|
|
|
|
request = factory.post('/api/v1/report', {'title':'Test Report', 'reference':'12345'})
|
|
|
|
response = create_report(request)
|
|
|
|
self.assertEqual(response.status_code, 401)
|
|
|
|
|
|
|
|
@patch('backend.models.Report.objects.filter', Mock(return_value=[mock_report()]))
|
|
|
|
@patch('backend.views.get_sections', Mock(return_value={}))
|
|
|
|
def test_get_report(self):
|
|
|
|
result = get_report(1)
|
|
|
|
self.assertEqual(
|
|
|
|
result,
|
|
|
|
{
|
|
|
|
'date_created':'2019-03-01',
|
|
|
|
'reference_number':'12345',
|
|
|
|
'report_pk':1,
|
|
|
|
'title':'Report Title',
|
|
|
|
'date_submitted':'2019-03-01',
|
|
|
|
'submitted':False
|
|
|
|
}
|
|
|
|
)
|
2019-03-08 22:14:22 +00:00
|
|
|
|
|
|
|
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')
|
2019-03-08 22:36:28 +00:00
|
|
|
response = report_detail(request)
|
2019-03-08 22:14:22 +00:00
|
|
|
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)
|
2019-03-08 22:31:29 +00:00
|
|
|
|
|
|
|
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')
|
2019-03-08 22:36:28 +00:00
|
|
|
response = finalize_report(request)
|
2019-03-08 22:31:29 +00:00
|
|
|
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)
|
2019-03-08 22:34:54 +00:00
|
|
|
response = finalize_report(review_request, 1)
|
2019-03-08 22:31:29 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2019-03-08 22:34:54 +00:00
|
|
|
report = Report.objects.get(user_id=user)
|
2019-03-08 22:31:29 +00:00
|
|
|
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)
|
2019-03-08 22:34:54 +00:00
|
|
|
response = finalize_report(review_request, 1)
|
2019-03-08 22:31:29 +00:00
|
|
|
self.assertEqual(response.status_code, 409)
|