Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add late change check to Excel file uploads #2013

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backend/audit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,9 @@ class ExcelFile(models.Model):
date_created = models.DateTimeField(auto_now_add=True)

def save(self, *args, **kwargs):
if self.sac.submission_status != SingleAuditChecklist.STATUS.IN_PROGRESS:
raise LateChangeError("Attemtped Excel file upload")

self.filename = f"{self.sac.report_id}--{self.form_section}.xlsx"

event_user = kwargs.pop("event_user", None)
Expand Down
77 changes: 75 additions & 2 deletions backend/audit/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@


# Mocking the user login and file scan functions
def _mock_login_and_scan(client, mock_scan_file):
def _mock_login_and_scan(client, mock_scan_file, **kwargs):
"""Helper function to mock the login and file scan functions"""
user, sac = _make_user_and_sac()
user, sac = _make_user_and_sac(**kwargs)

baker.make(Access, user=user, sac=sac)

Expand Down Expand Up @@ -940,6 +940,79 @@ def test_valid_file_upload_for_secondary_auditors(self, mock_scan_file):
SubmissionEvent.EventType.SECONDARY_AUDITORS_UPDATED,
)

@patch("audit.validators._scan_file")
def test_late_file_upload(self, mock_scan_file):
"""When a valid Excel file is uploaded after the submission has been locked, the upload should be rejected"""

test_cases = [
(
FEDERAL_AWARDS_ENTRY_FIXTURES,
FEDERAL_AWARDS_TEMPLATE,
FORM_SECTIONS.FEDERAL_AWARDS_EXPENDED,
),
(
CORRECTIVE_ACTION_PLAN_ENTRY_FIXTURES,
CORRECTIVE_ACTION_PLAN_TEMPLATE,
FORM_SECTIONS.CORRECTIVE_ACTION_PLAN,
),
(
FINDINGS_UNIFORM_GUIDANCE_ENTRY_FIXTURES,
FINDINGS_UNIFORM_GUIDANCE_TEMPLATE,
FORM_SECTIONS.FINDINGS_UNIFORM_GUIDANCE,
),
(
FINDINGS_TEXT_ENTRY_FIXTURES,
FINDINGS_TEXT_TEMPLATE,
FORM_SECTIONS.FINDINGS_TEXT,
),
(
SECONDARY_AUDITORS_ENTRY_FIXTURES,
SECONDARY_AUDITORS_TEMPLATE,
FORM_SECTIONS.SECONDARY_AUDITORS,
),
]

for test_case in test_cases:
with self.subTest():
fixtures, template, section = test_case

sac = _mock_login_and_scan(
self.client,
mock_scan_file,
submission_status=SingleAuditChecklist.STATUS.READY_FOR_CERTIFICATION,
)

test_data = json.loads(fixtures.read_text(encoding="utf-8"))

# add valid data to the workbook
workbook = load_workbook(template, data_only=True)
_set_by_name(
workbook, "auditee_uei", ExcelFileHandlerViewTests.GOOD_UEI
)
_set_by_name(workbook, "section_name", section)
_add_entry(workbook, 0, test_data[0])

with NamedTemporaryFile(suffix=".xlsx") as tmp:
workbook.save(tmp.name)
tmp.seek(0)

with open(tmp.name, "rb") as excel_file:
response = self.client.post(
reverse(
f"audit:{section}",
kwargs={
"report_id": sac.report_id,
"form_section": section,
},
),
data={"FILES": excel_file},
)

self.assertEqual(response.status_code, 400)
self.assertIn(
"no_late_changes", response.content.decode("utf-8")
)


class SingleAuditReportFileHandlerViewTests(TestCase):
def test_login_required(self):
Expand Down