diff --git a/backend/audit/intakelib/checks/check_finding_reference_year.py b/backend/audit/intakelib/checks/check_finding_reference_year.py index 5a4dbd9a39..0bfb02a5ff 100644 --- a/backend/audit/intakelib/checks/check_finding_reference_year.py +++ b/backend/audit/intakelib/checks/check_finding_reference_year.py @@ -23,9 +23,25 @@ def finding_reference_year(ir, is_gsa_migration=False): references = get_range_by_name(ir, "reference_number") range_start = int(get_range_start_row(references)) errors = [] - if is_gsa_migration: - return errors sac = get_sac_from_context() + + if is_gsa_migration or sac and sac.general_information is None: + # In real use cases, no report can be created if auditee_uei is missing, as it is a required field. + # The condition sac.general_information is None can occur only in test cases + # where general_information has been ignored purposefully (like in test_workbooks_should_pass.py). + return + elif sac is None: + raise ValidationError( + ( + "(O_o)", + "", + "Workbook Validation Failed", + { + "text": "The workbook cannot be validated at the moment. Please contact the helpdesk for assistance.", + "link": "Intake checks: no link defined", + }, + ) + ) audit_date = sac.general_information["auditee_fiscal_period_end"] audit_year = int(audit_date.split("-")[0]) for index, reference in enumerate(references["values"]): diff --git a/backend/audit/test_check_finding_reference_year.py b/backend/audit/test_check_finding_reference_year.py index 57a1152645..6add1049f6 100644 --- a/backend/audit/test_check_finding_reference_year.py +++ b/backend/audit/test_check_finding_reference_year.py @@ -26,7 +26,10 @@ def test_success(self): """ Test case where all finding reference years match the audit year. """ - self.mock_sac.general_information = {"auditee_fiscal_period_end": "2022-12-31"} + self.mock_sac.general_information = { + "auditee_fiscal_period_end": "2022-12-31", + "auditee_uei": "UEI", + } with set_sac_to_context(self.mock_sac): errors = finding_reference_year(self.ir) @@ -37,7 +40,10 @@ def test_mismatched_years(self): """ Test case where finding reference years do not match the audit year. """ - self.mock_sac.general_information = {"auditee_fiscal_period_end": "2023-12-31"} + self.mock_sac.general_information = { + "auditee_fiscal_period_end": "2023-12-31", + "auditee_uei": "UEI", + } with set_sac_to_context(self.mock_sac): with self.assertRaises(ValidationError) as context: finding_reference_year(self.ir) @@ -54,4 +60,31 @@ def test_gsa_migration(self): """ errors = finding_reference_year(self.ir, is_gsa_migration=True) - self.assertEqual(errors, []) + self.assertEqual(errors, None) + + def test_auditee_uei_missing(self): + """ + Test case where auditee_uei is None and the function returns without validation. + """ + self.mock_sac.general_information = { + "auditee_fiscal_period_end": "2022-12-31", + "auditee_uei": None, + } + with set_sac_to_context(self.mock_sac): + errors = finding_reference_year(self.ir) + self.assertEqual(errors, None) + + def test_sac_is_none(self): + """ + Test case where sac is None and a ValidationError is raised. + """ + with set_sac_to_context(None): + with self.assertRaises(ValidationError) as context: + finding_reference_year(self.ir) + + error = context.exception.args[0] + self.assertEqual(error[2], "Workbook Validation Failed") + self.assertEqual( + error[3]["text"], + "The workbook cannot be validated at the moment. Please contact the helpdesk for assistance.", + ) diff --git a/backend/audit/test_workbooks_should_pass.py b/backend/audit/test_workbooks_should_pass.py index be8c605c10..715755dd24 100644 --- a/backend/audit/test_workbooks_should_pass.py +++ b/backend/audit/test_workbooks_should_pass.py @@ -1,9 +1,10 @@ -from django.test import SimpleTestCase +from django.test import TestCase import os from functools import reduce import re - - +from audit.context import set_sac_to_context +from model_bakery import baker +from .models import SingleAuditChecklist from audit.intakelib import ( extract_additional_eins, extract_additional_ueis, @@ -76,12 +77,14 @@ def process_workbook_set(workbook_set_path, is_gsa_migration=True): raise Exception(msg) -class PassingWorkbooks(SimpleTestCase): +class PassingWorkbooks(TestCase): def test_passing_workbooks(self): workbook_sets = reduce( os.path.join, ["audit", "fixtures", "workbooks", "should_pass"] ) - for dirpath, dirnames, _ in os.walk(workbook_sets): - for workbook_set in dirnames: - print("Walking ", workbook_set) - process_workbook_set(os.path.join(dirpath, workbook_set)) + sac = baker.make(SingleAuditChecklist) + with set_sac_to_context(sac): + for dirpath, dirnames, _ in os.walk(workbook_sets): + for workbook_set in dirnames: + print("Walking ", workbook_set) + process_workbook_set(os.path.join(dirpath, workbook_set))