|
| 1 | +package lib_test |
| 2 | + |
| 3 | +import rego.v1 |
| 4 | + |
| 5 | +import data.lib |
| 6 | + |
| 7 | +test_rule_annotations_with_annotations if { |
| 8 | + rule_annotations := {"custom": { |
| 9 | + "short_name": "TestRule", |
| 10 | + "failure_msg": "Test failure message", |
| 11 | + "pipeline_intention": ["build", "test"], |
| 12 | + }} |
| 13 | + |
| 14 | + chain := [ |
| 15 | + {"annotations": rule_annotations, "path": ["data", "test", "deny"]}, |
| 16 | + {"annotations": {}, "path": ["ignored", "path"]}, |
| 17 | + ] |
| 18 | + |
| 19 | + lib.assert_equal(rule_annotations, lib._rule_annotations(chain)) |
| 20 | +} |
| 21 | + |
| 22 | +test_rule_annotations_empty_annotations if { |
| 23 | + empty_annotations := {} |
| 24 | + |
| 25 | + chain := [ |
| 26 | + {"annotations": empty_annotations, "path": ["data", "test", "deny"]}, |
| 27 | + {"annotations": {"some": "other"}, "path": ["ignored", "path"]}, |
| 28 | + ] |
| 29 | + |
| 30 | + lib.assert_equal(empty_annotations, lib._rule_annotations(chain)) |
| 31 | +} |
| 32 | + |
| 33 | +test_rule_annotations_only_first_entry if { |
| 34 | + first_rule_annotations := {"custom": {"short_name": "FirstRule"}} |
| 35 | + second_rule_annotations := {"custom": {"short_name": "SecondRule"}} |
| 36 | + |
| 37 | + chain := [ |
| 38 | + {"annotations": first_rule_annotations, "path": ["data", "test", "deny"]}, |
| 39 | + {"annotations": second_rule_annotations, "path": ["other", "path"]}, |
| 40 | + ] |
| 41 | + |
| 42 | + # Should only return annotations from the first entry |
| 43 | + lib.assert_equal(first_rule_annotations, lib._rule_annotations(chain)) |
| 44 | +} |
| 45 | + |
| 46 | +test_rule_annotations_single_entry_chain if { |
| 47 | + rule_annotations := {"custom": {"short_name": "SingleRule"}} |
| 48 | + |
| 49 | + chain := [{"annotations": rule_annotations, "path": ["data", "single", "deny"]}] |
| 50 | + |
| 51 | + lib.assert_equal(rule_annotations, lib._rule_annotations(chain)) |
| 52 | +} |
| 53 | + |
| 54 | +test_pipeline_intention_match_with_matching_intention if { |
| 55 | + rule_annotations := {"custom": { |
| 56 | + "short_name": "TestRule", |
| 57 | + "pipeline_intention": ["build", "release", "test"], |
| 58 | + }} |
| 59 | + |
| 60 | + chain := [{"annotations": rule_annotations, "path": ["data", "test", "deny"]}] |
| 61 | + |
| 62 | + # When rule_data("pipeline_intention") matches one of the pipeline_intention values |
| 63 | + lib.assert_equal(true, lib.pipeline_intention_match(chain)) with data.rule_data.pipeline_intention as "release" |
| 64 | +} |
| 65 | + |
| 66 | +test_pipeline_intention_match_with_non_matching_intention if { |
| 67 | + rule_annotations := {"custom": { |
| 68 | + "short_name": "TestRule", |
| 69 | + "pipeline_intention": ["build", "test"], |
| 70 | + }} |
| 71 | + |
| 72 | + chain := [{"annotations": rule_annotations, "path": ["data", "test", "deny"]}] |
| 73 | + |
| 74 | + # When rule_data("pipeline_intention") doesn't match any of the pipeline_intention values |
| 75 | + lib.assert_equal(false, lib.pipeline_intention_match(chain)) with data.rule_data.pipeline_intention as "release" |
| 76 | +} |
| 77 | + |
| 78 | +test_pipeline_intention_match_with_empty_pipeline_intention if { |
| 79 | + rule_annotations := {"custom": { |
| 80 | + "short_name": "TestRule", |
| 81 | + "pipeline_intention": [], |
| 82 | + }} |
| 83 | + |
| 84 | + chain := [{"annotations": rule_annotations, "path": ["data", "test", "deny"]}] |
| 85 | + |
| 86 | + # When pipeline_intention is an empty list, should return false |
| 87 | + lib.assert_equal(false, lib.pipeline_intention_match(chain)) with data.rule_data.pipeline_intention as "release" |
| 88 | +} |
| 89 | + |
| 90 | +test_pipeline_intention_match_without_pipeline_intention_field if { |
| 91 | + rule_annotations := {"custom": { |
| 92 | + "short_name": "TestRule", |
| 93 | + "failure_msg": "Some failure message", |
| 94 | + }} |
| 95 | + |
| 96 | + chain := [{"annotations": rule_annotations, "path": ["data", "test", "deny"]}] |
| 97 | + |
| 98 | + # When pipeline_intention field is missing, should return false |
| 99 | + lib.assert_equal(false, lib.pipeline_intention_match(chain)) with data.rule_data.pipeline_intention as "release" |
| 100 | +} |
| 101 | + |
| 102 | +test_pipeline_intention_match_without_custom_field if { |
| 103 | + rule_annotations := {"other": {"some_field": "value"}} |
| 104 | + |
| 105 | + chain := [{"annotations": rule_annotations, "path": ["data", "test", "deny"]}] |
| 106 | + |
| 107 | + # When custom field is missing, should return false |
| 108 | + lib.assert_equal(false, lib.pipeline_intention_match(chain)) with data.rule_data.pipeline_intention as "release" |
| 109 | +} |
| 110 | + |
| 111 | +test_pipeline_intention_match_with_null_rule_data if { |
| 112 | + rule_annotations := {"custom": { |
| 113 | + "short_name": "TestRule", |
| 114 | + "pipeline_intention": ["build", "release", "test"], |
| 115 | + }} |
| 116 | + |
| 117 | + chain := [{"annotations": rule_annotations, "path": ["data", "test", "deny"]}] |
| 118 | + |
| 119 | + # When rule_data("pipeline_intention") is null, should return false |
| 120 | + lib.assert_equal(false, lib.pipeline_intention_match(chain)) with data.rule_data.pipeline_intention as null |
| 121 | +} |
| 122 | + |
| 123 | +test_pipeline_intention_match_with_multiple_matching_intentions if { |
| 124 | + rule_annotations := {"custom": { |
| 125 | + "short_name": "TestRule", |
| 126 | + "pipeline_intention": ["build", "release", "production", "test"], |
| 127 | + }} |
| 128 | + |
| 129 | + chain := [{"annotations": rule_annotations, "path": ["data", "test", "deny"]}] |
| 130 | + |
| 131 | + # When rule_data("pipeline_intention") matches one of multiple pipeline_intention values |
| 132 | + lib.assert_equal(true, lib.pipeline_intention_match(chain)) with data.rule_data.pipeline_intention as "production" |
| 133 | +} |
| 134 | + |
| 135 | +test_pipeline_intention_match_case_sensitivity if { |
| 136 | + rule_annotations := {"custom": { |
| 137 | + "short_name": "TestRule", |
| 138 | + "pipeline_intention": ["Build", "Release"], |
| 139 | + }} |
| 140 | + |
| 141 | + chain := [{"annotations": rule_annotations, "path": ["data", "test", "deny"]}] |
| 142 | + |
| 143 | + # Case sensitivity should be preserved |
| 144 | + lib.assert_equal(false, lib.pipeline_intention_match(chain)) with data.rule_data.pipeline_intention as "release" |
| 145 | + lib.assert_equal(true, lib.pipeline_intention_match(chain)) with data.rule_data.pipeline_intention as "Release" |
| 146 | +} |
| 147 | + |
| 148 | +test_result_helper if { |
| 149 | + expected_result := { |
| 150 | + "code": "oh.Hey", |
| 151 | + "effective_on": "2022-01-01T00:00:00Z", |
| 152 | + "msg": "Bad thing foo", |
| 153 | + } |
| 154 | + |
| 155 | + rule_annotations := {"custom": { |
| 156 | + "short_name": "Hey", |
| 157 | + "failure_msg": "Bad thing %s", |
| 158 | + }} |
| 159 | + |
| 160 | + chain := [ |
| 161 | + {"annotations": rule_annotations, "path": ["data", "oh", "deny"]}, |
| 162 | + {"annotations": {}, "path": ["ignored", "ignored"]}, # Actually not needed any more |
| 163 | + ] |
| 164 | + |
| 165 | + lib.assert_equal(expected_result, lib.result_helper(chain, ["foo"])) |
| 166 | +} |
| 167 | + |
| 168 | +test_result_helper_without_package_annotation if { |
| 169 | + expected_result := { |
| 170 | + "code": "package_name.Hey", # Fixme |
| 171 | + "effective_on": "2022-01-01T00:00:00Z", |
| 172 | + "msg": "Bad thing foo", |
| 173 | + } |
| 174 | + |
| 175 | + rule_annotations := {"custom": { |
| 176 | + "short_name": "Hey", |
| 177 | + "failure_msg": "Bad thing %s", |
| 178 | + }} |
| 179 | + |
| 180 | + chain := [{"annotations": rule_annotations, "path": ["package_name", "deny"]}] |
| 181 | + |
| 182 | + lib.assert_equal(expected_result, lib.result_helper(chain, ["foo"])) |
| 183 | +} |
| 184 | + |
| 185 | +test_result_helper_with_collections if { |
| 186 | + expected := { |
| 187 | + "code": "some.path.oh.Hey", |
| 188 | + "collections": ["spam"], |
| 189 | + "effective_on": "2022-01-01T00:00:00Z", |
| 190 | + "msg": "Bad thing foo", |
| 191 | + } |
| 192 | + |
| 193 | + rule_annotations := {"custom": { |
| 194 | + "collections": ["spam"], |
| 195 | + "short_name": "Hey", |
| 196 | + "failure_msg": "Bad thing %s", |
| 197 | + }} |
| 198 | + |
| 199 | + chain := [ |
| 200 | + {"annotations": rule_annotations, "path": ["some", "path", "oh", "deny"]}, |
| 201 | + {"annotations": {}, "path": ["ignored", "ignored"]}, # Actually not needed any more |
| 202 | + ] |
| 203 | + |
| 204 | + lib.assert_equal(expected, lib.result_helper(chain, ["foo"])) |
| 205 | +} |
| 206 | + |
| 207 | +test_result_helper_with_term if { |
| 208 | + expected := { |
| 209 | + "code": "path.oh.Hey", |
| 210 | + "term": "ola", |
| 211 | + "effective_on": "2022-01-01T00:00:00Z", |
| 212 | + "msg": "Bad thing foo", |
| 213 | + } |
| 214 | + |
| 215 | + rule_annotations := {"custom": { |
| 216 | + "short_name": "Hey", |
| 217 | + "failure_msg": "Bad thing %s", |
| 218 | + }} |
| 219 | + |
| 220 | + chain := [ |
| 221 | + {"annotations": rule_annotations, "path": ["data", "path", "oh", "deny"]}, |
| 222 | + {"annotations": {}, "path": ["ignored", "also_ignored"]}, |
| 223 | + ] |
| 224 | + |
| 225 | + lib.assert_equal(expected, lib.result_helper_with_term(chain, ["foo"], "ola")) |
| 226 | +} |
| 227 | + |
| 228 | +test_result_helper_pkg_name if { |
| 229 | + # "Normal" for policy repo |
| 230 | + lib.assert_equal("foo", lib._pkg_name(["data", "foo", "deny"])) |
| 231 | + lib.assert_equal("foo", lib._pkg_name(["data", "foo", "warn"])) |
| 232 | + |
| 233 | + # Long package paths are retained |
| 234 | + lib.assert_equal("another.foo.bar", lib._pkg_name(["data", "another", "foo", "bar", "deny"])) |
| 235 | + lib.assert_equal("another.foo.bar", lib._pkg_name(["data", "another", "foo", "bar", "warn"])) |
| 236 | + |
| 237 | + # Unlikely edge case: No deny or warn |
| 238 | + lib.assert_equal("foo", lib._pkg_name(["data", "foo"])) |
| 239 | + lib.assert_equal("foo.bar", lib._pkg_name(["data", "foo", "bar"])) |
| 240 | + |
| 241 | + # Unlikely edge case: No data |
| 242 | + lib.assert_equal("foo", lib._pkg_name(["foo", "deny"])) |
| 243 | + lib.assert_equal("foo.bar", lib._pkg_name(["foo", "bar", "warn"])) |
| 244 | + |
| 245 | + # Very unlikely edge case: Just to illustrate how deny/warn/data are stripped once |
| 246 | + lib.assert_equal("foo", lib._pkg_name(["data", "foo", "warn", "deny"])) |
| 247 | + lib.assert_equal("foo.deny", lib._pkg_name(["data", "foo", "deny", "warn"])) |
| 248 | + lib.assert_equal("foo.warn", lib._pkg_name(["data", "foo", "warn", "warn"])) |
| 249 | + lib.assert_equal("data.foo.warn.deny", lib._pkg_name(["data", "data", "foo", "warn", "deny", "warn"])) |
| 250 | +} |
0 commit comments