Skip to content

Commit 3a4be5d

Browse files
committed
Raise exception on parse of unittest from yml. Do this rather than trying to convert it into an integrationtest or manualtest.
1 parent 5488ca6 commit 3a4be5d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

contentctl/objects/abstract_security_content_objects/detection_abstract.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def adjust_tests_and_groups(self) -> None:
167167
the model from the list of unit tests. Also, preemptively skips all manual tests, as well as
168168
tests for experimental/deprecated detections and Correlation type detections.
169169
"""
170+
170171
# Since ManualTest and UnitTest are not differentiable without looking at the manual_test
171172
# tag, Pydantic builds all tests as UnitTest objects. If we see the manual_test flag, we
172173
# convert these to ManualTest
@@ -789,6 +790,45 @@ def search_observables_exist_validate(self):
789790
# Found everything
790791
return self
791792

793+
@field_validator("tests", mode="before")
794+
def ensure_yml_test_is_unittest(cls, v:list[dict]):
795+
"""The typing for the tests field allows it to be one of
796+
a number of different types of tests. However, ONLY
797+
UnitTest should be allowed to be defined in the YML
798+
file. If part of the UnitTest defined in the YML
799+
is incorrect, such as the attack_data file, then
800+
it will FAIL to be instantiated as a UnitTest and
801+
may instead be instantiated as a different type of
802+
test, such as IntegrationTest (since that requires
803+
less fields) which is incorrect. Ensure that any
804+
raw data read from the YML can actually construct
805+
a valid UnitTest and, if not, return errors right
806+
away instead of letting Pydantic try to construct
807+
it into a different type of test
808+
809+
Args:
810+
v (list[dict]): list of dicts read from the yml.
811+
Each one SHOULD be a valid UnitTest. If we cannot
812+
construct a valid unitTest from it, a ValueError should be raised
813+
814+
Returns:
815+
_type_: The input of the function, assuming no
816+
ValueError is raised.
817+
"""
818+
valueErrors:list[ValueError] = []
819+
for unitTest in v:
820+
#This raises a ValueError on a failed UnitTest.
821+
try:
822+
UnitTest.model_validate(unitTest)
823+
except ValueError as e:
824+
valueErrors.append(e)
825+
if len(valueErrors):
826+
raise ValueError(valueErrors)
827+
# All of these can be constructred as UnitTests with no
828+
# Exceptions, so let the normal flow continue
829+
return v
830+
831+
792832
@field_validator("tests")
793833
def tests_validate(
794834
cls,

0 commit comments

Comments
 (0)