Create tests for section().
This commit is contained in:
parent
1fe1149cf8
commit
ab721e6dcf
1 changed files with 212 additions and 0 deletions
|
@ -304,6 +304,218 @@ class BackendTests(TestCase):
|
|||
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):
|
||||
"""
|
||||
Test when a user owns a section
|
||||
|
|
Loading…
Reference in a new issue