|
7 | 7 | ContractExpectations, |
8 | 8 | Expectation, |
9 | 9 | ExpectationType, |
| 10 | + SecurityPlatformType, |
10 | 11 | ) |
11 | 12 |
|
12 | 13 |
|
13 | | -def _expectation(expectation_type, name, is_predefined): |
| 14 | +def _expectation(expectation_type, name, is_predefined, expected_platforms=None): |
| 15 | + # Only pass the field when declared so the default (None) path exercises |
| 16 | + # the dataclass default_factory instead of an explicit empty list. |
| 17 | + kwargs = {} |
| 18 | + if expected_platforms is not None: |
| 19 | + kwargs["expectation_expected_security_platform_types"] = expected_platforms |
14 | 20 | return Expectation( |
15 | 21 | expectation_type=expectation_type, |
16 | 22 | expectation_name=name, |
17 | 23 | expectation_description="", |
18 | 24 | expectation_score=100, |
19 | 25 | expectation_expectation_group=False, |
20 | 26 | expectation_is_predefined=is_predefined, |
| 27 | + **kwargs, |
21 | 28 | ) |
22 | 29 |
|
23 | 30 |
|
@@ -104,6 +111,59 @@ def test_no_flag_yields_no_predefined(self): |
104 | 111 |
|
105 | 112 | self.assertEqual([], data["predefinedExpectations"]) |
106 | 113 |
|
| 114 | + def test_expected_security_platform_types_default_empty(self): |
| 115 | + """An expectation without declared platforms serializes an empty list.""" |
| 116 | + field = ContractExpectations( |
| 117 | + key="expectations", |
| 118 | + label="Expectations", |
| 119 | + availableExpectations=[ |
| 120 | + _expectation(ExpectationType.detection, "Detection", True), |
| 121 | + ], |
| 122 | + ) |
| 123 | + |
| 124 | + data = _serialize(field) |
| 125 | + |
| 126 | + self.assertEqual( |
| 127 | + [], |
| 128 | + data["availableExpectations"][0][ |
| 129 | + "expectation_expected_security_platform_types" |
| 130 | + ], |
| 131 | + ) |
| 132 | + |
| 133 | + def test_expected_security_platform_types_serialize(self): |
| 134 | + """Declared platform types serialize as their string values on both |
| 135 | + the available and predefined arrays.""" |
| 136 | + field = ContractExpectations( |
| 137 | + key="expectations", |
| 138 | + label="Expectations", |
| 139 | + availableExpectations=[ |
| 140 | + _expectation( |
| 141 | + ExpectationType.detection, |
| 142 | + "Detection", |
| 143 | + True, |
| 144 | + expected_platforms=[ |
| 145 | + SecurityPlatformType.EDR, |
| 146 | + SecurityPlatformType.XDR, |
| 147 | + ], |
| 148 | + ), |
| 149 | + ], |
| 150 | + ) |
| 151 | + |
| 152 | + data = _serialize(field) |
| 153 | + |
| 154 | + self.assertEqual( |
| 155 | + ["EDR", "XDR"], |
| 156 | + data["availableExpectations"][0][ |
| 157 | + "expectation_expected_security_platform_types" |
| 158 | + ], |
| 159 | + ) |
| 160 | + self.assertEqual( |
| 161 | + ["EDR", "XDR"], |
| 162 | + data["predefinedExpectations"][0][ |
| 163 | + "expectation_expected_security_platform_types" |
| 164 | + ], |
| 165 | + ) |
| 166 | + |
107 | 167 |
|
108 | 168 | if __name__ == "__main__": |
109 | 169 | unittest.main() |
0 commit comments