Merge pull request #133 from danieldupriest/even-more-tests

Created tests for get_sections, get_fields, get_files, section, and fixed email confirmation bug
This commit is contained in:
Rupika Dikkala 2019-03-15 22:42:49 -07:00 committed by GitHub
commit 9a3a80b836
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 569 additions and 6 deletions

View file

@ -1,4 +1,4 @@
EMAIL_HOST_USER=gmail_account@gmail.com EMAIL_HOST_USER=accountemail@gmail.com
EMAIL_HOST_PASSWORD=gmail_password EMAIL_HOST_PASSWORD=password
SUBMIT_REPORT_DESTINATION_EMAIL=to_address@gmail.com SUBMIT_REPORT_DESTINATION_EMAIL=to_address@gmail.com
SUBMIT_REPORT_FROM_EMAIL=from_address@gmail.com SUBMIT_REPORT_FROM_EMAIL=from_address@gmail.com

View file

@ -1,10 +1,12 @@
from django.test import TestCase from django.test import TestCase
from rest_framework.test import APIRequestFactory, force_authenticate from rest_framework.test import APIRequestFactory, force_authenticate
from backend.models import Report from backend.models import Report, Field
from users.models import CustomUser from users.models import CustomUser
from unittest.mock import MagicMock, Mock, patch from unittest.mock import MagicMock, Mock, patch
from datetime import date from datetime import date, datetime, timezone
from backend.views import * from backend.views import *
from .policy import pol
from decimal import Decimal
import json import json
class BackendTests(TestCase): class BackendTests(TestCase):
@ -226,6 +228,69 @@ class BackendTests(TestCase):
reports = Report.objects.filter(user_id=user) reports = Report.objects.filter(user_id=user)
self.assertEqual(len(reports), 0) self.assertEqual(len(reports), 0)
def test_reports_user_with_two_reports(self):
"""
Test retrieving a list of reports for a user with two created.
"""
self.maxDiff = 5000
now = datetime(2019, 3, 1, 0, 0, 0, 0, tzinfo=timezone.utc)
# create two sample reports
report_1 = Report.objects.create(
user_id=self.test_user_1,
title="Report One",
date_created=now,
date_submitted=now,
reference_number="1234"
)
report_1.save()
report_2 = Report.objects.create(
user_id=self.test_user_1,
title="Report Two",
date_created=now,
date_submitted=now,
reference_number="1234"
)
report_2.save()
report_3 = Report.objects.create(
user_id=self.test_user_2,
title="Report Three",
date_created=now,
reference_number="1234"
)
report_3.save()
# get reports with user 1
factory = APIRequestFactory()
get_reports_request = factory.get('/api/v1/reports')
force_authenticate(get_reports_request, user=self.test_user_1)
response = reports(get_reports_request)
self.assertEqual(response.status_code, 200)
result = json.loads(response.content.decode("utf-8", "strict"))
formatted_date = '2019-03-01T00:00:00Z'
expected = {
"reports": [
{
"title": "Report One",
"submitted": False,
"report_pk": 1,
"date_created": formatted_date,
"date_submitted": formatted_date,
"user_id": 1,
"reference_number": "1234"
},
{
"title": "Report Two",
"submitted": False,
"report_pk": 2,
"date_created": formatted_date,
"date_submitted": formatted_date,
"user_id": 1,
"reference_number": "1234"
}
]
}
self.assertEqual(result, expected)
def test_user_owns_report_true(self): def test_user_owns_report_true(self):
""" """
Test when a user owns a report Test when a user owns a report
@ -249,6 +314,271 @@ class BackendTests(TestCase):
# Section-related Tests # Section-related Tests
####################### #######################
@patch('backend.views.get_fields', Mock(return_value={}))
def test_get_sections(self):
"""
Test gettings sections for a report.
"""
report = Report.objects.create(
user_id=self.test_user_1,
title="Report Title",
date_created=timezone.now(),
reference_number="1234"
)
report.save()
section_0 = Section.objects.create(
report_id=report,
auto_submit=False,
required=False,
completed=False,
title='Section Zero',
html_description='<p>Description zero</p>',
number=0
)
section_0.save()
section_1 = Section.objects.create(
report_id=report,
auto_submit=False,
required=False,
completed=False,
title='Section One',
html_description='<p>Description one</p>',
number=1
)
section_1.save()
expected = {
'sections': [
{
'completed': False,
'html_description': '<p>Description zero</p>',
'id': 1,
'rule_violations': [],
'title': 'Section Zero'
},
{
'completed': False,
'html_description': '<p>Description one</p>',
'id': 2,
'rule_violations': [],
'title': 'Section One'
}
]
}
result = get_sections(1)
self.assertEqual(expected, result)
def test_section_user_does_not_own_section(self):
"""
Test when a user attempts to access a section that they don't own.
"""
# create sample report with user 2
report = Report.objects.create(
user_id=self.test_user_2,
title="Report Title",
date_created=timezone.now(),
reference_number="1234"
)
report.save()
# create sample section
section_0 = Section.objects.create(
report_id=report,
auto_submit=False,
required=False,
completed=False,
title='Section Zero',
html_description='<p>Description zero</p>',
number=0
)
section_0.save()
# try to put with user 1
factory = APIRequestFactory()
put_section_request = factory.put('/api/v1/section/1', {'key':'value'})
force_authenticate(put_section_request, user=self.test_user_1)
result = section(put_section_request, 1)
self.assertEqual(result.status_code, 401)
def test_section_report_already_submitted(self):
"""
Test what happens when a report has already been submitted(finalized).
"""
# create sample report already submitted
report = Report.objects.create(
user_id=self.test_user_1,
title="Report Title",
date_created=timezone.now(),
reference_number="1234",
submitted=True
)
report.save()
# create sample section
section_0 = Section.objects.create(
report_id=report,
auto_submit=False,
required=False,
completed=False,
title='Section Zero',
html_description='<p>Description zero</p>',
number=0
)
section_0.save()
# try to put
factory = APIRequestFactory()
put_section_request = factory.put('/api/v1/section/1', {'key':'value'})
force_authenticate(put_section_request, user=self.test_user_1)
result = section(put_section_request, 1)
self.assertEqual(result.status_code, 409)
def test_section_correct_owner_unsubmitted(self):
"""
Test the happy path where the section's owner updates some field values.
"""
self.maxDiff = 5000
# create sample report
report = Report.objects.create(
user_id=self.test_user_1,
title="Report Title",
date_created=timezone.now(),
reference_number="1234"
)
report.save()
# create sample section
section_0 = Section.objects.create(
report_id=report,
auto_submit=False,
required=False,
completed=False,
title='Section Zero',
html_description='<p>Description zero</p>',
number=0,
approved=True
)
section_0.save()
# create sample fields
field_0 = Field.objects.create(
section_id=section_0,
field_name='boolean',
label='A boolean',
number=0,
field_type='boolean',
completed=True,
data_bool=True
)
field_0.save()
field_1 = Field.objects.create(
section_id=section_0,
field_name='decimal',
label='A decimal',
number=1,
field_type='decimal',
completed=True,
data_decimal=10.1
)
field_1.save()
field_2 = Field.objects.create(
section_id=section_0,
field_name='date',
label='A date',
number=2,
field_type='date',
completed=True,
data_date=date(2019,3,1)
)
field_2.save()
field_3 = Field.objects.create(
section_id=section_0,
field_name='file',
label='A file',
number=3,
field_type='file',
completed=True,
data_file='uploads/2019/03/01/file.jpg'
)
field_3.save()
field_4 = Field.objects.create(
section_id=section_0,
field_name='string',
label='A string',
number=4,
field_type='string',
completed=True,
data_string='string data'
)
field_4.save()
field_5 = Field.objects.create(
section_id=section_0,
field_name='integer',
label='An integer',
number=5,
field_type='integer',
completed=True,
data_integer=10
)
field_5.save()
factory = APIRequestFactory()
content = {
'boolean': True,
'decimal': '10.10',
'date': '2019-03-01',
'file': 'file.jpg',
'string': 'string data',
'integer': 10
}
put_section_request = factory.put('/api/v1/section/1', content)
force_authenticate(put_section_request, user=self.test_user_1)
response = section(put_section_request, 1)
self.assertEqual(response.status_code, 200)
expected = {
'fields': [
{
'field_name': 'boolean',
'field_type': 'boolean',
'label': 'A boolean',
'value': True
},
{
'field_name': 'decimal',
'field_type': 'decimal',
'label': 'A decimal',
'value': Decimal('10.10')
},
{
'field_name': 'date',
'field_type': 'date',
'label': 'A date',
'value': '{}'.format(date(2019,3,1))
},
{
'field_name': 'file',
'field_type': 'file',
'label': 'A file',
'value': 'file.jpg'
},
{
'field_name': 'string',
'field_type': 'string',
'label': 'A string',
'value': 'string data'
},
{
'field_name': 'integer',
'field_type': 'integer',
'label': 'An integer',
'value': 10
}
]
}
fields_result = get_fields(1)
self.assertEqual(expected, fields_result)
section_result = get_sections(1)
self.assertTrue(section_result['sections'][0]['completed'])
def test_user_owns_section_true(self): def test_user_owns_section_true(self):
""" """
Test when a user owns a section Test when a user owns a section
@ -304,6 +634,137 @@ class BackendTests(TestCase):
# Field-related Tests # Field-related Tests
##################### #####################
def test_get_fields(self):
"""
Test gettings fields for a section.
"""
self.maxDiff = 5000
# create sample report
report = Report.objects.create(
user_id=self.test_user_1,
title="Report Title",
date_created=timezone.now(),
reference_number="1234"
)
report.save()
# create sample section
section_0 = Section.objects.create(
report_id=report,
auto_submit=False,
required=False,
completed=False,
title='Section Zero',
html_description='<p>Description zero</p>',
number=0
)
section_0.save()
# create sample fields
field_0 = Field.objects.create(
section_id=section_0,
field_name='boolean',
label='A boolean',
number=0,
field_type='boolean',
completed=True,
data_bool=True
)
field_0.save()
field_1 = Field.objects.create(
section_id=section_0,
field_name='decimal',
label='A decimal',
number=1,
field_type='decimal',
completed=True,
data_decimal=10.1
)
field_1.save()
field_2 = Field.objects.create(
section_id=section_0,
field_name='date',
label='A date',
number=2,
field_type='date',
completed=True,
data_date=date(2019,3,1)
)
field_2.save()
field_3 = Field.objects.create(
section_id=section_0,
field_name='file',
label='A file',
number=3,
field_type='file',
completed=True,
data_file='uploads/2019/03/01/file.jpg'
)
field_3.save()
field_4 = Field.objects.create(
section_id=section_0,
field_name='string',
label='A string',
number=4,
field_type='string',
completed=True,
data_string='string data'
)
field_4.save()
field_5 = Field.objects.create(
section_id=section_0,
field_name='integer',
label='An integer',
number=5,
field_type='integer',
completed=True,
data_integer=10
)
field_5.save()
expected = {
'fields': [
{
'field_name': 'boolean',
'field_type': 'boolean',
'label': 'A boolean',
'value': True
},
{
'field_name': 'decimal',
'field_type': 'decimal',
'label': 'A decimal',
'value': Decimal('10.10')
},
{
'field_name': 'date',
'field_type': 'date',
'label': 'A date',
'value': '{}'.format(date(2019,3,1))
},
{
'field_name': 'file',
'field_type': 'file',
'label': 'A file',
'value': 'file.jpg'
},
{
'field_name': 'string',
'field_type': 'string',
'label': 'A string',
'value': 'string data'
},
{
'field_name': 'integer',
'field_type': 'integer',
'label': 'An integer',
'value': 10
}
]
}
result = get_fields(1)
self.assertEqual(expected, result)
def generate_test_fields(self): def generate_test_fields(self):
test_boolean = models.BooleanField(default=False) test_boolean = models.BooleanField(default=False)
test_boolean = True test_boolean = True
@ -341,3 +802,103 @@ class BackendTests(TestCase):
'string':'Some String', 'string':'Some String',
'integer':100 'integer':100
}) })
# Other tests
#############
def test_get_data(self):
"""
Tests the get_data function in models.py
:return: None
"""
test_obj = Mock()
test_obj.field_type = "boolean"
test_obj.data_bool = True
result_bool = Field.get_datatype(test_obj)
self.assertEqual(result_bool, True)
test_obj.field_type = "decimal"
test_obj.data_decimal = 1.0
result_dec = Field.get_datatype(test_obj)
self.assertEqual(result_dec, 1.0)
test_obj.field_type = "date"
test_obj.data_date = str(date(2018, 1, 1))
result_date = Field.get_datatype(test_obj)
self.assertEqual(result_date, str(date(2018, 1, 1)))
test_obj.field_type = "file"
test_obj.path_leaf = Mock(return_value="file.jpg")
result_file = Field.get_datatype(test_obj)
self.assertEqual(result_file, "file.jpg")
test_obj.field_type = "string"
test_obj.data_string = "hello"
result_str = Field.get_datatype(test_obj)
self.assertEqual(result_str, "hello")
test_obj.field_type = "integer"
test_obj.data_integer = 99
result_int = Field.get_datatype(test_obj)
self.assertEqual(result_int, 99)
def test_path_leaf(self):
"""
Tests the path_leaf function in models.py
:return: None
"""
result = Field.path_leaf(Mock(), "uploads/2018/01/01/file.jpg")
self.assertEqual(result, "file.jpg")
def test_get_files(self):
"""
Test getting files from a report.
"""
# create sample report
report = Report.objects.create(
user_id=self.test_user_1,
title="Report Title",
date_created=timezone.now(),
reference_number="1234"
)
report.save()
# create sample section
section_0 = Section.objects.create(
report_id=report,
auto_submit=False,
required=False,
completed=False,
title='Section Zero',
html_description='<p>Description zero</p>',
number=0
)
section_0.save()
# create sample fields
field_0 = Field.objects.create(
section_id=section_0,
field_name='file 0',
label='A file',
number=0,
field_type='file',
completed=True,
data_file='uploads/2019/03/01/file0.jpg'
)
field_0.save()
field_1 = Field.objects.create(
section_id=section_0,
field_name='file 1',
label='A file',
number=1,
field_type='file',
completed=True,
data_file='uploads/2019/03/01/file1.jpg'
)
field_1.save()
expected = ['uploads/2019/03/01/file0.jpg', 'uploads/2019/03/01/file1.jpg']
result = get_files(1)
self.assertEqual(result, expected)

View file

@ -452,6 +452,5 @@ def get_files(report_pk):
fields = Field.objects.filter(section_id=section.id, completed=True) fields = Field.objects.filter(section_id=section.id, completed=True)
for field in fields: for field in fields:
if field.field_type == "file": if field.field_type == "file":
print("appending {}".format(field.data_file.name))
files.append(field.data_file.name) files.append(field.data_file.name)
return files return files

View file

@ -166,6 +166,9 @@ SITE_ID = 1
# Registration # Registration
LOGIN_URL = 'https://192.168.99.100:8443'
LOGIN_REDIRECT_URL = 'https://192.168.99.100:8443'
#ACCOUNT_USER_MODEL_USERNAME_FIELD = 'email' #ACCOUNT_USER_MODEL_USERNAME_FIELD = 'email'
ACCOUNT_EMAIL_REQUIRED = True ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False ACCOUNT_USERNAME_REQUIRED = False

View file

@ -14,6 +14,6 @@ urlpatterns = [
path('api/v1/', include("backend.urls")), path('api/v1/', include("backend.urls")),
path('api/v1/account/', include('rest_auth.urls')), path('api/v1/account/', include('rest_auth.urls')),
path('api/v1/account/register/', include('rest_auth.registration.urls')), path('api/v1/account/register/', include('rest_auth.registration.urls')),
# path('api/v1/account/register/', NameRegistrationView.as_view()), path('api/v1/accounts/', include('allauth.urls')),
path('api-auth/', include('rest_framework.urls')), path('api-auth/', include('rest_framework.urls')),
] ]